All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Aaron Tomlin" <atomlin@atomlin.com>
Cc: linux-scsi@vger.kernel.org, virtualization@lists.linux.dev,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Eugenio Perez" <eperezma@redhat.com>
Subject: Re: [PATCH v17 05/10] blk-mq: use hk cpus only when isolcpus=managed_irq is enabled
Date: Sun, 29 Nov 2026 07:58:48 +0000	[thread overview]
Message-ID: <sashiko-outbox-239409@kernel.org> (raw)
In-Reply-To: <20261129045436.255364-6-atomlin@atomlin.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] blk-mq: offline housekeeping queues marked active in
  blk_mq_map_queues()
- [Low] blk-mq: commit message examples contradict blk_mq_map_queues()
  behavior
--

commit b027119b22c4d4a60567ed849ca88fa491e6c4b3
Author: Daniel Wagner <wagi@kernel.org>

blk-mq: use hk cpus only when isolcpus=managed_irq is enabled

This commit restricts blk-mq queue counting and CPU-to-hctx mapping to
housekeeping CPUs when isolcpus=managed_irq is enabled, and distributes
remaining isolated CPUs across active hardware queues.

[ ... ]

>     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=managed_irq,2-3,6-7,12-13
>         virtio_blk.num_request_queues=2
>     
>             Queue mapping:
>                     hctx0: default 0 1 2 3 4 5 6 7 8 12 13
>                     hctx1: default 9 10 11 14 15

[ ... ]

>         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.

[ ... ]

>     Noteworthy is that for the normal/default configuration (without
>     isolcpus=) 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

[Severity: Low]
Do these two examples in the commit message match the actual mapping code?

In the first quoted example, both blk_mq_map_queues() and
blk_mq_map_hw_queues() mark hctx0 and hctx1 as active in active_hctx
because both receive possible housekeeping CPUs from constraint, and the
unassigned-CPU loop then spreads isolated CPUs across both queues via
find_next_bit_wrap() (mapping CPUs 2, 6, 12 to hctx0 and CPUs 3, 7, 13 to
hctx1) rather than placing all isolated CPUs on hctx0.

In the second quoted example, blk_mq_map_queues() already used
group_cpus_evenly() before this patch; when HK_TYPE_MANAGED_IRQ is disabled,
constraint is cpu_possible_mask and group_mask_cpus_evenly() produces the
same mapping as the baseline code.

> diff --git a/block/blk-mq-cpumap.c b/block/blk-mq-cpumap.c
> index 705da074ad6c7..d63c298010540 100644
> --- a/block/blk-mq-cpumap.c
> +++ b/block/blk-mq-cpumap.c

[ ... ]

> @@ -56,23 +77,92 @@ unsigned int blk_mq_num_online_queues(unsigned int max_queues)
>  void blk_mq_map_queues(struct blk_mq_queue_map *qmap)
>  {
> -	const struct cpumask *masks;
> +	struct cpumask *masks;
> +	const struct cpumask *constraint;
>  	unsigned int queue, cpu, nr_masks;
> +	unsigned long *active_hctx;
>  
> -	masks = group_cpus_evenly(qmap->nr_queues, &nr_masks);
> -	if (!masks) {
> -		for_each_possible_cpu(cpu)
> -			qmap->mq_map[cpu] = qmap->queue_offset;
> -		return;
> -	}
> +	active_hctx = bitmap_zalloc(qmap->nr_queues, GFP_KERNEL);
> +	if (!active_hctx)
> +		goto fallback;
>  
> -	for (queue = 0; queue < qmap->nr_queues; queue++) {
> -		for_each_cpu(cpu, &masks[queue % nr_masks])
> +	if (housekeeping_enabled(HK_TYPE_MANAGED_IRQ))
> +		constraint = housekeeping_cpumask(HK_TYPE_MANAGED_IRQ);
> +	else
> +		constraint = cpu_possible_mask;
> +
> +	/* Map CPUs to the hardware contexts (hctx) */
> +	masks = group_mask_cpus_evenly(qmap->nr_queues, constraint, &nr_masks);
> +	if (!masks)
> +		goto free_fallback_hctx;
> +
> +	/*
> +	 * Iterate directly over the generated CPU masks.
> +	 * Calculate the final, highest hardware queue index that maps to this
> +	 * mask. This skips all intermediate overwrites and safely evaluates
> +	 * active_hctx only for queues that survive the mapping.
> +	 */
> +	for (unsigned int idx = 0; idx < nr_masks; idx++) {
> +		queue = qmap->nr_queues - 1 -
> +			((qmap->nr_queues - 1 - idx) % nr_masks);
> +
> +		for_each_cpu(cpu, &masks[idx])
>  			qmap->mq_map[cpu] = qmap->queue_offset + queue;
> +
> +		__set_bit(queue, active_hctx);
>  	}
> +
> +	/*
> +	 * If the active_hctx bitmap is empty, attempting to route unassigned
> +	 * CPUs will map them out-of-bounds. Fall back instead.
> +	 */
> +	if (bitmap_empty(active_hctx, qmap->nr_queues))
> +		goto free_fallback;
> +
> +	/* Map any unassigned CPU evenly to the hardware contexts (hctx) */
> +	queue = find_first_bit(active_hctx, qmap->nr_queues);
> +	for_each_cpu_andnot(cpu, cpu_possible_mask, constraint) {
> +		qmap->mq_map[cpu] = qmap->queue_offset + queue;
> +		queue = find_next_bit_wrap(active_hctx, qmap->nr_queues, queue + 1);
> +	}

[ ... ]

> @@ -109,24 +199,82 @@ void blk_mq_map_hw_queues(struct blk_mq_queue_map *qmap,
> +	/* Map CPUs to the hardware contexts (hctx) */
>  	for (queue = 0; queue < qmap->nr_queues; queue++) {
> -		mask = dev->bus->irq_get_affinity(dev, queue + offset);
> -		if (!mask)
> -			goto fallback;
> +		const struct cpumask *affinity_mask;
> +
> +		affinity_mask = dev->bus->irq_get_affinity(dev, offset + queue);
> +		if (!affinity_mask)
> +			goto free_map_software;
>  
> -		for_each_cpu(cpu, mask)
> +		for_each_cpu_and(cpu, affinity_mask, constraint) {
>  			qmap->mq_map[cpu] = qmap->queue_offset + queue;
> +			cpumask_set_cpu(cpu, mask);
> +		}
> +	}
> +
> +	/*
> +	 * Evaluate active_hctx after mapping to handle overlapping masks.
> +	 * This ensures queues that were overwritten do not falsely pass validation.
> +	 */
> +	for_each_cpu(cpu, mask) {
> +		queue = qmap->mq_map[cpu] - qmap->queue_offset;
> +		__set_bit(queue, active_hctx);
> +	}
> +
> +	/*
> +	 * If no assigned CPU matches the constraint, the active_hctx
> +	 * bitmap will be empty. Fall back instead of routing out of bounds.
> +	 */
> +	if (bitmap_empty(active_hctx, qmap->nr_queues))
> +		goto free_fallback;
> +
> +	/* Map any unassigned CPU evenly to the hardware contexts (hctx) */
> +	queue = find_first_bit(active_hctx, qmap->nr_queues);
> +	for_each_cpu_andnot(cpu, cpu_possible_mask, mask) {
> +		qmap->mq_map[cpu] = qmap->queue_offset + queue;
> +		queue = find_next_bit_wrap(active_hctx, qmap->nr_queues, queue + 1);
>  	}

[Severity: High]
Can blk_mq_map_queues() and blk_mq_map_hw_queues() map online isolated CPUs
to a hardware queue whose assigned housekeeping CPUs are all offline?

Because housekeeping_cpumask(HK_TYPE_MANAGED_IRQ) includes offline possible
housekeeping CPUs, both functions set bits in active_hctx for queues that
only have offline housekeeping CPUs (such as hctx1 with offline CPUs 9-11
and 14-15 in the second example of the commit message).  The unassigned-CPU
loop then routes online isolated CPUs (such as CPUs 3 and 7) to that queue.

When all housekeeping CPUs in a queue's managed IRQ affinity mask are
offline, __irq_startup_managed() aborts startup and leaves the interrupt in
IRQD_MANAGED_SHUTDOWN, while blk_mq_init_hctx() leaves BLK_MQ_S_INACTIVE
clear.  I/O submitted from the online isolated CPUs mapped to that hctx is
then dispatched to a queue with no active completion interrupt, causing I/O
hangs.  The follow-up commit "blk-mq: prevent offlining hk CPUs with
associated online isolated CPUs" only checks runtime CPU hot-unplug in
blk_mq_hctx_notify_offline() and does not prevent mapping online isolated
CPUs onto queues whose housekeeping CPUs are already offline when queues are
mapped.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261129045436.255364-1-atomlin@atomlin.com?part=5

  reply	other threads:[~2026-11-29  7:58 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29  4:54 [PATCH v17 00/10] blk: honor isolcpus configuration Aaron Tomlin
2026-11-29  4:54 ` [PATCH v17 01/10] scsi: aacraid: use block layer helpers to calculate num of queues Aaron Tomlin
2026-11-29  4:54 ` [PATCH v17 02/10] lib/group_cpus: remove dead !SMP code Aaron Tomlin
2026-11-29  4:54 ` [PATCH v17 03/10] lib/group_cpus: Add group_mask_cpus_evenly() Aaron Tomlin
2026-11-29  7:33   ` sashiko-bot
2026-11-29 17:32     ` Aaron Tomlin
2026-11-29  4:54 ` [PATCH v17 04/10] sched/isolation: Prevent out-of-bounds read in isolcpus= boot parameter parser Aaron Tomlin
2026-11-29 21:45   ` Frederic Weisbecker
2026-11-30  2:36     ` Aaron Tomlin
2026-11-29  4:54 ` [PATCH v17 05/10] blk-mq: use hk cpus only when isolcpus=managed_irq is enabled Aaron Tomlin
2026-11-29  7:58   ` sashiko-bot [this message]
2026-11-29 20:45     ` Aaron Tomlin
2026-11-29  7:26 ` [PATCH v17 06/10] blk-mq: prevent offlining hk CPUs with associated online isolated CPUs Aaron Tomlin
2026-11-29  7:46   ` sashiko-bot
2026-11-30  1:11     ` Aaron Tomlin
2026-11-29  7:26 ` [PATCH v17 07/10] virtio-fs: honour isolcpus=managed_irq in queue mapping Aaron Tomlin
2026-11-29  7:26 ` [PATCH v17 08/10] genirq/affinity: Restrict managed IRQ affinity to housekeeping CPUs Aaron Tomlin
2026-11-29  7:48   ` sashiko-bot
2026-11-30  1:42     ` Aaron Tomlin
2026-11-29  7:26 ` [PATCH v17 09/10] docs: update isolcpus=managed_irq documentation Aaron Tomlin
2026-11-29  7:26 ` [PATCH v17 10/10] genirq/affinity: Confine pre and post vectors to housekeeping CPUs Aaron Tomlin
2026-11-29  7:41   ` sashiko-bot
2026-11-30  2:33     ` Aaron Tomlin
2026-11-29  7:58 ` [PATCH v17 00/10] blk: honor isolcpus configuration sashiko-bot
2026-11-29 22:15 ` Frederic Weisbecker
2026-11-30  2:52   ` Aaron Tomlin
2026-11-30  9:37     ` Frederic Weisbecker
2026-11-30  9:53 ` Marco Crivellari
2026-11-30 17:00   ` Aaron Tomlin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-239409@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=atomlin@atomlin.com \
    --cc=eperezma@redhat.com \
    --cc=linux-scsi@vger.kernel.org \
    --cc=mst@redhat.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=virtualization@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.