Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=34.202.193.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795928177; b=cRFyH4ddLYkEI7vYVlyxsZBkGL7N4TrzKtUfHaWvmgVjQvz1e0zmdros6VVUYvXdnAku 22G7wD8OpyYgxLuSZEVhh5J0QEre6Ws5ienqDQESSzRk8KzS0z7WgLbyK54N2FMRmkKhA 4M4DDaO9vhWK+0NFW2ctty8NeQJre5SOFczKxXMXm/ad0XSLjF1NJboWyJC+slby0pZ6K vwumSWP1L9SFVQXDV0ic5GTROKMFKCP8pVijvpQFLM3fetEZ3VWSDApRvTk+t5ErKDapo AZTVpVMQzlLPightinuNAGtbfA/hvQhPlpiRK6PNS1EeIuzQYMiLUVkzgD/cT/T2vSw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795928177; h=DMARC-Filter:DKIM-Signature:DKIM-Signature:Feedback-ID: X-Pm-Original-To:Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding: X-MIME-Autoconverted:Content-Type; bh=/et/Lv+XPuWwF2Jig4KIxGiHvqq/1Pc9AqN79ILeGDw=; b=HhO9NjVC5OAMe9pxQSjbQ4O4bVFnyDkkoW4zwIGK/hcqGUbr47ENnvCOPDl8NahF9qP+ zKfjD8ctwNK/I6w49e941j0ujOjR9XTZrkWqHI85PJDdp0VMikOleTHaU6LU3Pa2ZeY7p UIwjAbcrMaw0J/7juWeDkUNneOjt7jCB78i///KJGFVANNLwkeIsJHpc/MLEAuG81LdG9 ZVfTm7RF8zTd00ayqP2CBRA7kwKEmPjfkWncy9FsUsaxTpwATDawx+iDz/A8Hlef2aznE 0c61SzB9+88tg+5DkEnNuxLFuIUjD3Mm4JuunIHnErdAFjw5MmDN8p1xq66Beqprgaw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=atomlin.com; spf=pass smtp.mailfrom=atomlin.com; dkim=pass header.d=atomlin.com header.i=@atomlin.com header.a=rsa-sha256 header.s=purelymail3 header.b=L7aYneox; dkim=pass header.d=purelymail.com header.i=@purelymail.com header.a=rsa-sha256 header.s=purelymail3 header.b=PCWe8DDU; arc=none smtp.remote-ip=34.202.193.197 Received: from sendmail.purelymail.com (sendmail.purelymail.com [34.202.193.197]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A01C606A325 for ; Sun, 29 Nov 2026 04:56:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5A01C606A325 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=atomlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=atomlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=atomlin.com header.i=@atomlin.com header.a=rsa-sha256 header.s=purelymail3 header.b=L7aYneox; dkim=pass (2048-bit key, unprotected) header.d=purelymail.com header.i=@purelymail.com header.a=rsa-sha256 header.s=purelymail3 header.b=PCWe8DDU DKIM-Signature: a=rsa-sha256; b=L7aYneoxufJU+dZoGAGhh6Er4y7ML1Ul5ZBJYw9clW4O92MKwSG3aGRi5SKfk6STBpGS9Ul5lFMYwM6Xrsuq4Mk8OQZeXs2w+Yrx3OV7/9mlgMpL7pR08pjj03L12YiaqfGaityyW8zbtiyMgqz+Bh/5/3tsmdTeayk16LSbv2TaJLPGCmjivKjIk+iLC45BBC34xZhXYwZI03KUX/A0xHl7FTnYECZjmPsF4IX4uURQsos2E1ddqjYx5eNIF5hP3rdqsw0IRWgGvP8PjXiWz3q5RzigltI2Sz+TSseFsqwSmf8w738fzjLvm+Ow4tgkYGJ4cF//zgT2DLcNd5s7uA==; s=purelymail3; d=atomlin.com; v=1; bh=szf95xRCqeQFAPoRHMQ5ykcDnjwjK69Arsgqtl0oZYM=; h=Received:From:To:Subject:Date:Message-ID; DKIM-Signature: a=rsa-sha256; b=PCWe8DDU0OiXbq6sud4yOkQTe5AA8mCkReAEYiJ2UefYZnxZ+CRNFBawOq5RVfP7/1DAA/iTkRPl4FplNt58XkJKH91/21suTG+EAY0fqATXD8ULVJRj3dhPfF8WsfNxVVMgc8gK3zd7f0YKhx8vE5zy0rj7bQQJawao32FMja3SFofKxPh0qwCMDBhhmqJqJmkx7jkKNY0MnDIbe0mkH+qjkbo02aU6iBENcsdOVPB3AuxrfEY0EwSD9YEES2M/d4/ZMVaZQBya8XCCabGMTuIc6kIk3oL8KzQauSvAne4UUsML0rbAWsYa9vty+UdONufWNR1tCkZvAuQ04dKqCQ==; s=purelymail3; d=purelymail.com; v=1; bh=szf95xRCqeQFAPoRHMQ5ykcDnjwjK69Arsgqtl0oZYM=; h=Feedback-ID:Received:From:To:Subject:Date:Message-ID; Feedback-ID: 1802931:67223:null:purelymail X-Pm-Original-To: linux-doc@vger.kernel.org Authentication-Results: purelymail.com; auth=pass Received: by smtp.purelymail.com (Purelymail SMTP) with ESMTPSA id 782039210; (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384); Sun, 29 Nov 2026 04:54:41 +0000 (UTC) From: Aaron Tomlin To: axboe@kernel.dk, tglx@kernel.org, aacraid@microsemi.com, James.Bottomley@HansenPartnership.com, mkp@kernel.org, frederic@kernel.org, bigeasy@linutronix.de, gmaglione@redhat.com, vgoyal@redhat.com, stefanha@redhat.com, miklos@szeredi.hu Cc: atomlin@atomlin.com, dwagner@suse.de, eperezma@redhat.com, tom.leiming@gmail.com, ionut.nechita@windriver.com, corbet@lwn.net, vincent.guittot@linaro.org, mingo@redhat.com, peterz@infradead.org, radu@rendec.net, akpm@linux-foundation.org, steve@abita.co, sean@ashe.io, chjohnst@gmail.com, neelx@suse.com, mproche@gmail.com, nick.lange@gmail.com, marco.crivellari@suse.com, rishil1999@outlook.com, linux-doc@vger.kernel.org, linux-block@vger.kernel.org, linux-scsi@vger.kernel.org, virtualization@lists.linux.dev, linux-fsdevel@vger.kernel.org, fuse-devel@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v17 05/10] blk-mq: use hk cpus only when isolcpus=managed_irq is enabled Date: Sat, 28 Nov 2026 23:54:29 -0500 Message-ID: <20261129045436.255364-6-atomlin@atomlin.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261129045436.255364-1-atomlin@atomlin.com> References: <20261129045436.255364-1-atomlin@atomlin.com> X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-MIME-Autoconverted: from 8bit to quoted-printable by Purelymail Content-Type: text/plain; charset=UTF-8 From: Daniel Wagner Extend the capabilities of the generic CPU to hardware queue (hctx) mapping code, so it maps housekeeping CPUs and isolated CPUs to the hardware queues evenly. Example mapping result: 16 online CPUs isolcpus=3Dmanaged_irq,2-3,6-7,12-13 Queue mapping: hctx0: default 0 2 hctx1: default 1 3 hctx2: default 4 6 hctx3: default 5 7 hctx4: default 8 12 hctx5: default 9 13 hctx6: default 10 hctx7: default 11 hctx8: default 14 hctx9: default 15 IRQ mapping: irq 42 affinity 0 effective 0 nvme0q0 irq 43 affinity 0 effective 0 nvme0q1 irq 44 affinity 1 effective 1 nvme0q2 irq 45 affinity 4 effective 4 nvme0q3 irq 46 affinity 5 effective 5 nvme0q4 irq 47 affinity 8 effective 8 nvme0q5 irq 48 affinity 9 effective 9 nvme0q6 irq 49 affinity 10 effective 10 nvme0q7 irq 50 affinity 11 effective 11 nvme0q8 irq 51 affinity 14 effective 14 nvme0q9 irq 52 affinity 15 effective 15 nvme0q10 In this scenario, the system has 16 online CPUs with 6 isolated cores (2, 3, 6, 7, 12, and 13) and 10 housekeeping cores (0, 1, 4, 5, 8, 9, 10, 11, 14, and 15): 1. Queue allocation and ownership Rather than allocating 16 hardware queues, the block layer allocates only 10 hardware contexts (hctx0 to hctx9), corresponding strictly to the 10 housekeeping CPUs. The 6 isolated CPUs do not own dedicated hardware queues; instead, they are mapped across the existing active housekeeping queues (e.g. isolated CPU 2 shares hctx0 with CPU 0). This ensures tasks running on isolated CPUs can still issue I/O without restriction. 2. Interrupt routing All device interrupts, including the NVMe admin queue (irq 42) and the 10 I/O completion queues (irq 43 to 52), target housekeeping CPUs exclusively. When a task on isolated CPU 2 issues I/O via hctx0, the resulting completion interrupt (irq 43) fires on housekeeping CPU 0. Consequently, isolated CPUs are never interrupted by device hardware, guaranteeing zero latency disturbance for isolated workloads. A corner case is when the number of online CPUs and present CPUs differ and the driver asks for less queues than online CPUs, e.g. 8 online CPUs, 16 possible CPUs isolcpus=3Dmanaged_irq,2-3,6-7,12-13 virtio_blk.num_request_queues=3D2 Queue mapping: hctx0: default 0 1 2 3 4 5 6 7 8 12 13 hctx1: default 9 10 11 14 15 IRQ mapping irq 27 affinity 0 effective 0 virtio0-config irq 28 affinity 0-1,4-5,8 effective 5 virtio0-req.0 irq 29 affinity 9-11,14-15 effective 0 virtio0-req.1 This corner case demonstrates behaviour when hardware queue counts are constrained (only 2 request queues) on a system with CPU hotplug (8 CPUs online out of 16 possible): 1. Coarse queue grouping Because the driver requests only 2 queues, the 10 possible housekeeping CPUs are partitioned into two groups: hctx0 receives CPUs 0-1, 4-5, and 8, while hctx1 receives CPUs 9-11 and 14-15. All isolated CPUs (both online cores 2-3, 6-7 and offline cores 12-13) are mapped to hctx0 to share submission capacity without allocating excess queues. 2. Isolation and hotplug protection in interrupt affinity Although hctx0 serves both housekeeping and isolated CPUs, the resulting interrupt affinity mask for irq 28 (affinity 0-1,4-5,8) strictly includes only the housekeeping cores, completely excluding isolated cores 2-3, 6-7, and 12-13. Furthermore, for hctx1 (irq 29), whose assigned housekeeping CPUs (9-11, 14-15) are currently offline, the kernel routes the effective interrupt to an available online housekeeping core (CPU 0), guaranteeing that interrupts never spill onto isolated cores under any hotplug state. Noteworthy is that for the normal/default configuration (without isolcpus=3D) the mapping will change for systems which have non hyperthreading CPUs. The main assignment loop will completely rely that group_mask_cpus_evenly to do the right thing. The old code would distribute the CPUs linearly over the hardware context: queue mapping for /dev/nvme0n1 hctx0: default 0 8 hctx1: default 1 9 hctx2: default 2 10 hctx3: default 3 11 hctx4: default 4 12 hctx5: default 5 13 hctx6: default 6 14 hctx7: default 7 15 The assign each hardware context the map generated by the group_mask_cpus_evenly function: queue mapping for /dev/nvme0n1 hctx0: default 0 1 hctx1: default 2 3 hctx2: default 4 5 hctx3: default 6 7 hctx4: default 8 9 hctx5: default 10 11 hctx6: default 12 13 hctx7: default 14 15 In case of hyperthreading CPUs, the resulting map stays the same. Signed-off-by: Daniel Wagner Tested-by: Ionut Nechita Co-developed-by: Aaron Tomlin Signed-off-by: Aaron Tomlin --- block/blk-mq-cpumap.c | 184 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 166 insertions(+), 18 deletions(-) diff --git a/block/blk-mq-cpumap.c b/block/blk-mq-cpumap.c index 705da074ad6c..d63c29801054 100644 --- a/block/blk-mq-cpumap.c +++ b/block/blk-mq-cpumap.c @@ -17,13 +17,32 @@ #include "blk.h" #include "blk-mq.h" =20 +/** + * blk_mq_num_queues - Calculate number of queues for multiqueue devices + * @mask:=09CPUs to consider for the queue count + * @max_queues:=09The maximum number of queues the hardware/driver + *=09=09supports. If max_queues is 0, the argument is + *=09=09ignored. + * + * Calculates the number of queues to allocate from @mask, restricting the + * count to housekeeping CPUs when isolcpus=3Dmanaged_irq is enabled. + * + * Return: Number of queues to allocate (at least 1). + */ static unsigned int blk_mq_num_queues(const struct cpumask *mask, =09=09=09=09 unsigned int max_queues) { =09unsigned int num; =20 -=09num =3D cpumask_weight(mask); -=09return min_not_zero(num, max_queues); +=09if (housekeeping_enabled(HK_TYPE_MANAGED_IRQ)) +=09=09num =3D cpumask_weight_and(mask, housekeeping_cpumask(HK_TYPE_MANAGE= D_IRQ)); +=09else +=09=09num =3D cpumask_weight(mask); +=09/* +=09 * Ensure that a count of zero does not inadvertently result in +=09 * allocating the maximum number of queues. +=09 */ +=09return min_not_zero(num ?: 1U, max_queues); } =20 /** @@ -33,7 +52,8 @@ static unsigned int blk_mq_num_queues(const struct cpumas= k *mask, *=09=09ignored. * * Calculates the number of queues to be used for a multiqueue - * device based on the number of possible CPUs. + * device based on the number of possible CPUs. This helper + * takes isolcpus settings into account. */ unsigned int blk_mq_num_possible_queues(unsigned int max_queues) { @@ -48,7 +68,8 @@ EXPORT_SYMBOL_GPL(blk_mq_num_possible_queues); *=09=09ignored. * * Calculates the number of queues to be used for a multiqueue - * device based on the number of online CPUs. + * device based on the number of online CPUs. This helper + * takes isolcpus settings into account. */ unsigned int blk_mq_num_online_queues(unsigned int max_queues) { @@ -56,23 +77,92 @@ unsigned int blk_mq_num_online_queues(unsigned int max_= queues) } EXPORT_SYMBOL_GPL(blk_mq_num_online_queues); =20 +/** + * blk_mq_map_fallback - Map all possible CPUs to the first queue of @qmap + * @qmap:=09CPU to hardware queue map + * + * Map all possible CPUs to the first hardware context of @qmap, respectin= g + * @qmap->queue_offset so secondary maps do not route into the default map= . + */ +static void blk_mq_map_fallback(struct blk_mq_queue_map *qmap) +{ +=09unsigned int cpu; + +=09for_each_possible_cpu(cpu) +=09=09qmap->mq_map[cpu] =3D qmap->queue_offset; +} + +/** + * blk_mq_map_queues - Create default CPU to hardware queue mapping + * @qmap:=09CPU to hardware queue map + * + * Distribute housekeeping CPUs across @qmap->nr_queues according to NUMA + * and CPU locality, and spread any remaining isolated CPUs evenly across + * the active hardware queues. + */ void blk_mq_map_queues(struct blk_mq_queue_map *qmap) { -=09const struct cpumask *masks; +=09struct cpumask *masks; +=09const struct cpumask *constraint; =09unsigned int queue, cpu, nr_masks; +=09unsigned long *active_hctx; =20 -=09masks =3D group_cpus_evenly(qmap->nr_queues, &nr_masks); -=09if (!masks) { -=09=09for_each_possible_cpu(cpu) -=09=09=09qmap->mq_map[cpu] =3D qmap->queue_offset; -=09=09return; -=09} +=09active_hctx =3D bitmap_zalloc(qmap->nr_queues, GFP_KERNEL); +=09if (!active_hctx) +=09=09goto fallback; =20 -=09for (queue =3D 0; queue < qmap->nr_queues; queue++) { -=09=09for_each_cpu(cpu, &masks[queue % nr_masks]) +=09if (housekeeping_enabled(HK_TYPE_MANAGED_IRQ)) +=09=09constraint =3D housekeeping_cpumask(HK_TYPE_MANAGED_IRQ); +=09else +=09=09constraint =3D cpu_possible_mask; + +=09/* Map CPUs to the hardware contexts (hctx) */ +=09masks =3D group_mask_cpus_evenly(qmap->nr_queues, constraint, &nr_masks= ); +=09if (!masks) +=09=09goto free_fallback_hctx; + +=09/* +=09 * Iterate directly over the generated CPU masks. +=09 * Calculate the final, highest hardware queue index that maps to this +=09 * mask. This skips all intermediate overwrites and safely evaluates +=09 * active_hctx only for queues that survive the mapping. +=09 */ +=09for (unsigned int idx =3D 0; idx < nr_masks; idx++) { +=09=09queue =3D qmap->nr_queues - 1 - +=09=09=09((qmap->nr_queues - 1 - idx) % nr_masks); + +=09=09for_each_cpu(cpu, &masks[idx]) =09=09=09qmap->mq_map[cpu] =3D qmap->queue_offset + queue; + +=09=09__set_bit(queue, active_hctx); =09} + +=09/* +=09 * If the active_hctx bitmap is empty, attempting to route unassigned +=09 * CPUs will map them out-of-bounds. Fall back instead. +=09 */ +=09if (bitmap_empty(active_hctx, qmap->nr_queues)) +=09=09goto free_fallback; + +=09/* Map any unassigned CPU evenly to the hardware contexts (hctx) */ +=09queue =3D find_first_bit(active_hctx, qmap->nr_queues); +=09for_each_cpu_andnot(cpu, cpu_possible_mask, constraint) { +=09=09qmap->mq_map[cpu] =3D qmap->queue_offset + queue; +=09=09queue =3D find_next_bit_wrap(active_hctx, qmap->nr_queues, queue + 1= ); +=09} + =09kfree(masks); +=09bitmap_free(active_hctx); + +=09return; + +free_fallback: +=09kfree(masks); +free_fallback_hctx: +=09bitmap_free(active_hctx); + +fallback: +=09blk_mq_map_fallback(qmap); } EXPORT_SYMBOL_GPL(blk_mq_map_queues); =20 @@ -109,24 +199,82 @@ void blk_mq_map_hw_queues(struct blk_mq_queue_map *qm= ap, =09=09=09 struct device *dev, unsigned int offset) =20 { -=09const struct cpumask *mask; +=09cpumask_var_t mask; +=09const struct cpumask *constraint; +=09unsigned long *active_hctx; =09unsigned int queue, cpu; =20 =09if (!dev->bus->irq_get_affinity) +=09=09goto map_software; + +=09active_hctx =3D bitmap_zalloc(qmap->nr_queues, GFP_KERNEL); +=09if (!active_hctx) =09=09goto fallback; =20 +=09if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) { +=09=09bitmap_free(active_hctx); +=09=09goto fallback; +=09} + +=09if (housekeeping_enabled(HK_TYPE_MANAGED_IRQ)) +=09=09constraint =3D housekeeping_cpumask(HK_TYPE_MANAGED_IRQ); +=09else +=09=09constraint =3D cpu_possible_mask; + +=09/* Map CPUs to the hardware contexts (hctx) */ =09for (queue =3D 0; queue < qmap->nr_queues; queue++) { -=09=09mask =3D dev->bus->irq_get_affinity(dev, queue + offset); -=09=09if (!mask) -=09=09=09goto fallback; +=09=09const struct cpumask *affinity_mask; + +=09=09affinity_mask =3D dev->bus->irq_get_affinity(dev, offset + queue); +=09=09if (!affinity_mask) +=09=09=09goto free_map_software; =20 -=09=09for_each_cpu(cpu, mask) +=09=09for_each_cpu_and(cpu, affinity_mask, constraint) { =09=09=09qmap->mq_map[cpu] =3D qmap->queue_offset + queue; +=09=09=09cpumask_set_cpu(cpu, mask); +=09=09} +=09} + +=09/* +=09 * Evaluate active_hctx after mapping to handle overlapping masks. +=09 * This ensures queues that were overwritten do not falsely pass valida= tion. +=09 */ +=09for_each_cpu(cpu, mask) { +=09=09queue =3D qmap->mq_map[cpu] - qmap->queue_offset; +=09=09__set_bit(queue, active_hctx); +=09} + +=09/* +=09 * If no assigned CPU matches the constraint, the active_hctx +=09 * bitmap will be empty. Fall back instead of routing out of bounds. +=09 */ +=09if (bitmap_empty(active_hctx, qmap->nr_queues)) +=09=09goto free_fallback; + +=09/* Map any unassigned CPU evenly to the hardware contexts (hctx) */ +=09queue =3D find_first_bit(active_hctx, qmap->nr_queues); +=09for_each_cpu_andnot(cpu, cpu_possible_mask, mask) { +=09=09qmap->mq_map[cpu] =3D qmap->queue_offset + queue; +=09=09queue =3D find_next_bit_wrap(active_hctx, qmap->nr_queues, queue + 1= ); =09} =20 +=09bitmap_free(active_hctx); +=09free_cpumask_var(mask); + =09return; =20 +free_fallback: +=09bitmap_free(active_hctx); +=09free_cpumask_var(mask); + fallback: +=09blk_mq_map_fallback(qmap); +=09return; + +free_map_software: +=09free_cpumask_var(mask); +=09bitmap_free(active_hctx); +map_software: =09blk_mq_map_queues(qmap); } EXPORT_SYMBOL_GPL(blk_mq_map_hw_queues); --=20 2.55.0