blk: honor isolcpus configuration
Subsystems (11)
v17 cover blk: honor isolcpus configuration
Hi Thomas, Jens, Sebastian, Frederic,
This is version 17 of the series enabling multiqueue devices and managed interrupts to adhere to CPU isolation constraints. The series has been rebased on v7.4-rc1-3893-g946d9ed1aa02.
Background and Problem
======================
Modern multiqueue storage controllers (e.g. NVMe devices) typically allocate one hardware queue and interrupt vector per possible CPU. When CPU isolation is requested via "isolcpus=managed_irq", vector spreading in irq_create_affinity_masks() still distributes vectors across all possible CPUs, relying on irq_do_set_affinity() to strip isolated CPUs afterwards. However, that filtering only works when a vector's affinity mask contains both housekeeping and isolated CPUs. When per-CPU queues are allocated, isolated CPUs receive dedicated vectors whose masks contain no housekeeping CPUs; irq_do_set_affinity() then falls back to the unfiltered mask, leaving completion interrupts for I/O submitted on isolated CPUs targeted directly at those isolated cores.
Proposed solution and architecture
==================================
Rather than introducing a separate "isolcpus=" sub-parameter as explored in earlier iterations, this series fixes "isolcpus=managed_irq" (HK_TYPE_MANAGED_IRQ) directly in place across the CPU grouping library, generic IRQ affinity allocator, block layer, and virtio-fs:
1. Bounded topological CPU grouping
lib/group_cpus introduces group_mask_cpus_evenly(), a bounded counterpart to group_cpus_evenly() that confines NUMA and CPU locality grouping strictly to a caller-supplied cpumask while preserving two-stage (present-first, non-present second) spreading for CPU hotplug safety.
2. Managed and reserved IRQ affinity confinement
The genirq/affinity subsystem queries housekeeping_cpumask(HK_TYPE_MANAGED_IRQ) directly and passes it to group_mask_cpus_evenly(), restricting managed interrupt vector calculation and distribution exclusively to housekeeping CPUs. Unmanaged pre and post vectors (e.g. NVMe admin or device configuration queues) are also confined to housekeeping CPUs by default via irq_set_reserved_mask(), while remaining unmanaged so user space can adjust their affinity after probe.
3. Block layer and virtio-fs queue mapping
Both blk-mq and virtio-fs bound their queue counts to the HK_TYPE_MANAGED_IRQ housekeeping CPU mask and map housekeeping CPUs to hardware/request queues first. Any remaining possible CPUs (including isolated CPUs) are then distributed evenly across the active housekeeping-backed queues, allowing tasks on isolated CPUs to submit I/O while completion interrupts fire exclusively on housekeeping cores.
4. CPU hotplug protection
To prevent I/O stalls, the block layer rejects hotplug requests that attempt to offline the last online housekeeping CPU serving a hardware context (hctx) while any isolated CPU mapped to that hctx remains online (with system suspend and hibernation exempted via cpuhp_tasks_frozen).
Please let me know your thoughts.
Changes since v16:
- Dropped the separate "isolcpus=managed_irq_strict" (HK_TYPE_MANAGED_IRQ_STRICT) parameter and upgraded "isolcpus=managed_irq" (HK_TYPE_MANAGED_IRQ) directly in place across genirq/affinity, blk-mq, and documentation (Daniel Wagner, Sebastian Andrzej Siewior)
- Added patch 7/10 ("virtio-fs: honour isolcpus=managed_irq in queue mapping") to cap fs->num_request_queues to the HK_TYPE_MANAGED_IRQ housekeeping CPU weight and spread unmapped isolated CPUs across active VQ_REQUEST queues instead of leaving fs->mq_map[cpu] zero-initialised to VQ_HIPRIO
- Added patch 10/10 ("genirq/affinity: Confine pre and post vectors to housekeeping CPUs") introducing irq_set_reserved_mask() so unmanaged pre_vectors and post_vectors default to housekeeping CPUs when isolcpus=managed_irq is enabled (Ionut Nechita)
- Filtered the initial hardware affinity mapping loop in blk_mq_map_hw_queues() against the housekeeping constraint mask so isolated CPUs present in an affinity mask are not prematurely marked as mapped to an inactive hctx
- Added kernel-doc comments for blk_mq_num_queues(), blk_mq_map_fallback(), blk_mq_map_queues(), and blk_mq_hctx_can_offline_hk_cpu()
- Collected Tested-by tags from Ionut Nechita
- Rebased onto v7.4-rc1-3893-g946d9ed1aa02
- Link to v16: https://lore.kernel.org/all/[email protected]/
Changes since v15:
- Migrated feature naming from "isolcpus=io_queue" (HK_TYPE_IO_QUEUE) to "isolcpus=managed_irq_strict" (HK_TYPE_MANAGED_IRQ_STRICT), establishing it as the strict, pre-allocation counterpart to "managed_irq"
- Specifying "managed_irq_strict" automatically enables HK_FLAG_MANAGED_IRQ so that dependent subsystems continue to function
- Cleaned up commit bodies across the series, stripping informal scratchpads and adding Co-developed-by attribution where appropriate
- Added new patch 4/9 ("sched/isolation: Prevent out-of-bounds read in isolcpus= boot parameter parser"). Incorporated prerequisite bugfix resolving an out-of-bounds read in housekeeping_isolcpus_setup() when sub-parameters lack trailing commas
- Defined HK_TYPE_MANAGED_IRQ_STRICT and HK_FLAG_MANAGED_IRQ_STRICT. Ensured HK_FLAG_MANAGED_IRQ is set when managed_irq_strict is enabled
- Decoupled queue mapping from dynamic cpu_online_mask; eliminated the blk_mq_validate() check and online cpumask snapshotting to avoid TOCTOU races during concurrent CPU hotplug events
- Added cpuhp_tasks_frozen check to ensure suspend, hibernation, and resume transitions are not aborted
- Added blk_mq_map_queue_type() check so CPU offline validation only evaluates hardware contexts actively mapped to the CPU being offlined
- Enhanced warning message to report the offending CPU ID and queue number
- Prioritised affd->calc_sets in irq_calc_affinity_vectors() so multi-set drivers scale vector allocations with requested set sizes to prevent cross-queue contention, while vector spreading remains strictly constrained to housekeeping cores
- Documented "managed_irq_strict" in kernel-parameters.txt, detailing its role in blk-mq queue distribution and genirq affinity masking, as well as its superset relationship with legacy "managed_irq"
- Link to v15: https://lore.kernel.org/lkml/[email protected]/
Changes since v14:
- Fixed a division-by-zero by ensuring group_mask_cpus_evenly() safely frees its allocations and returns NULL instead of an empty array if the provided mask yields zero groups.
- Fixed a device probe -ENOSPC regression in blk_mq_num_queues(). If the housekeeping mask intersection evaluated to 0 (e.g., against a localised NUMA node), min_not_zero() would erroneously return the absolute maximum hardware queues. The result is now safely clamped to a minimum of 1.
- Added a mapping verification check to prevent unrelated housekeeping CPUs from aborting the global hotplug offline sequence.
- Aligned the pr_warn format specifier with the unsigned int declaration of hctx->queue_num in blk_mq_hctx_can_offline_hk_cpu().
- Link to v14: https://lore.kernel.org/lkml/[email protected]/
Changes since v13:
- Removed ineffective data_race() annotations around mask and cpu_present_mask pointers. Wrapping the pointers failed to suppress KCSAN warnings for the underlying inline bitmap memory accesses.
- Fixed a silent validation bypass in blk_mq_map_hw_queues() caused by overlapping IRQ affinity masks by removing the short-circuiting optimisation and evaluating the active_hctx bitmap in a secondary pass.
- Restored topology-aware multi-queue fallback by correctly routing missing IRQ affinity masks to the map_software path instead of the naive map-all fallback.
- Dropped hctx->queue->disk->disk_name from warning to avoid a UAF.
- Fixed an isolation leak where excess allocated hardware queues were improperly padded with irq_default_affinity. Because these queues are marked as managed, they bypassed user-space IRQ balancing; they are now safely padded with the housekeeping mask.
- Enforced the housekeeping vector cap prior to evaluating driver-provided calc_sets() callbacks, preventing modern multi-queue drivers from bypassing the cap and wasting memory on dead queues.
- Introduced a safety net to the vector calculation to prevent fatal -ENOSPC device probe aborts on heavily isolated systems where the housekeeping CPU count is lower than the device's structural minimum.
- Removed an inaccurate claim stating that the io_queue isolation flag takes precedence over managed_irq. Both flags are parsed, evaluated, and enforced entirely independently by their respective subsystems.
- Link to v13: https://lore.kernel.org/lkml/[email protected]/
Changes since v12:
- Resolved TOCTOU race conditions against CPU hotplug events in blk_mq_map_queues() and group_mask_cpus_evenly() by taking lockless snapshots of the online CPU mask prior to algorithmic evaluation.
- Migrated the active_hctx tracking to a dynamically sized bitmap (bitmap_zalloc), resolving a critical out-of-bounds memory write that occurred when hardware queues exceeded the system CPU count.
- Wrapped the disk pointer fetch in blk_mq_hctx_can_offline_hk_cpu() with READ_ONCE() to prevent a TOCTOU NULL pointer dereference against concurrent device teardowns.
- Introduced bitmap_empty() checks to prevent the mapping logic from routing unassigned CPUs into unallocated memory when all mapped CPUs are offline, safely forcing a fallback mapping instead.
- Implemented a native two-stage distribution logic in group_mask_cpus_evenly() that first prioritises physically present CPUs to prevent I/O starvation before distributing remaining vectors to non-present CPUs for hotplug safety.
- Restricted the maximum number of allocated vectors in irq_calc_affinity_vectors() to the weight of the housekeeping mask, preventing drivers from wasting memory on dead hardware queues that physically cannot be routed.
- Added padding logic using irq_default_affinity for sets where isolation constraints yield fewer masks than requested vectors, preserving the 1:1 hardware queue mapping sequence for subsequent sets.
- Fixed a logic flaw that prematurely rejected valid offline requests by manually iterating over cpu_online_mask and reverse-mapping to accurately detect isolated CPUs, properly permitting the offlining of non-housekeeping CPUs.
- Corrected an absolute versus relative queue index calculation bug in blk_mq_map_queues() that was overwriting loop iterations, by iterating directly over the generated masks.
- Replaced scoped __free cleanups with traditional goto unwinding in the block layer to align with subsystem styling guidelines.
- Refined the io_queue kernel command-line parameter documentation for better clarity and precision.
- Link to v12: https://lore.kernel.org/lkml/[email protected]/
Changes since v11:
- Removed duplicate paragraph from the commit message in patch 11 (Marco Crivellari)
- Ensure ZERO_SIZE_PTR is not returned by group_mask_cpus_evenly() (Marco Crivellari)
- Link to v11: https://lore.kernel.org/lkml/[email protected]/
Changes since v10:
- Completely rewrote the isolcpus=io_queue documentation in Documentation/admin-guide/kernel-parameters.txt to clarify its exclusive application to managed IRQs, queue allocation limits, vector exhaustion prevention, and hardware interrupt routing (Ming Lei)
- Fixed a stack frame bloat issue by avoiding the on-stack declaration of struct cpumask (Waiman Long)
- Link to v10: https://lore.kernel.org/linux-nvme/[email protected]/
Changes since v9:
- Fixed a page fault regression encountered when initialising secondary queue maps (e.g. NVMe poll queues). Restored the qmap->queue_offset to the mq_map assignment to ensure CPUs are strictly mapped to absolute hardware indices (Keith Busch)
- Corrected the active_hctx tracker to utilise relative queue indices, preventing out-of-bounds mask assignments
- Fixed the blk_mq_validate() sanity check to properly evaluate absolute queue indices against the offset-adjusted loop index
- Corrected typographical errors within block/blk-mq-cpumap.c (Keith Busch)
- Clarified the commit message regarding the removal of the !SMP fallback code, explicitly noting that the core scheduler now mandates SMP unconditionally (Sebastian Andrzej Siewior)
- Added missing "Signed-off-by:" tags to properly record the patch series chain of custody
- Link to v9: https://lore.kernel.org/lkml/[email protected]/
Changes since v8:
- Added "Reviewed-by:" tags
- Introduced irq_spread_hk_filter() to safely restrict managed IRQ affinity to housekeeping CPUs (Thomas Gleixner)
- Removed the unsafe global static variable blk_hk_online_mask from blk-mq-cpumap.c and blk-mq.c. blk_mq_online_queue_affinity() now returns a stable pointer, delegating safe intersection to the callers to prevent concurrent modification races (Thomas Gleixner, Hannes Reinecke)
- Resolved BUG: kernel NULL pointer dereference in __blk_mq_all_tag_iter reported by the kernel test robot during cpuhotplug rcutorture stress testing
- Link to v8: https://lore.kernel.org/lkml/[email protected]/
Changes since v7:
- Added commit 524f5eea4bbe ("lib/group_cpus: remove !SMP code")
- Merged the new mapping logic directly into the existing function to avoid special casing
- Refined the group_mask_cpus_evenly() implementation with the following updates:
- Corrected the function name typo (changed group_masks_cpus_evenly to group_mask_cpus_evenly)
- Updated the documentation comment to accurately reflect the function's behavior
- Renamed the cpu_mask argument to mask for consistency
- Added a new patch for aacraid to include the missing number of queues calculation
- Restricted updates to only affect SCSI drivers that support PCI_IRQ_AFFINITY and do not utilise nvme-fabrics
- Removed the __free cleanup attribute usage for cpumask_var_t allocations due to compatibility issues
- Updated the documentation to explicitly highlight the limitations surrounding CPU offlining
- Collected accumulated Reviewed-by and Acked-by tags
- Link to v7: https://patch.msgid.link/[email protected]
Changes since v6:
- Sent out the first part of the series independently:
https://lore.kernel.org/all/[email protected]/
- Added comprehensive kernel command-line documentation
- Added validation logic to ensure the resulting CPU-to-queue mapping is fully operational
- Rewrote the isolcpus mapping code to properly account for active hardware contexts (hctx)
- Introduced blk_mq_map_hk_irq_queues, which utilizes the mask retrieved from irq_get_affinity()
- Refactored blk_mq_map_hk_queues to require the caller to explicitly test for HK_TYPE_MANAGED_IRQ
- Link to v6: https://patch.msgid.link/[email protected]
Changes since v5:
- Reintroduced the io_queue type for the isolcpus kernel parameter
- Prevented the offlining of a housekeeping CPU if an isolated CPU is still present, upgrading this behavior from a simple warning to a hard restriction
- Link to v5: https://lore.kernel.org/r/[email protected]
Changes since v4:
- Rebased the series onto the latest for-6.14/block branch.
- Updated the documentation regarding the managed_irq parameters
- Reworded the commit message for "blk-mq: issue warning when offlining hctx with online isolcpus" for better clarity
- Split the input and output parameters in the patch "lib/group_cpus: let group_cpu_evenly return number of groups"
- Dropped the patch "sched/isolation: document HK_TYPE housekeeping option"
- Link to v4: https://lore.kernel.org/r/[email protected]
Changes since v3:
- Added the patch "blk-mq: issue warning when offlining hctx with online isolcpus"
- Fixed the check in group_cpus_evenly(); the condition now properly uses housekeeping_enabled() instead of cpumask_weight(), as the latter always returns a valid mask
- Dropped the Fixes: tag from "lib/group_cpus.c: honor housekeeping config when grouping CPUs"
- Fixed an overlong line warning in the patch "scsi: use block layer helpers to calculate num of queues"
- Dropped the patch "sched/isolation: Add io_queue housekeeping option" in favor of simply documenting the housekeeping hk_type enum
- Added the patch "lib/group_cpus: let group_cpu_evenly return number of groups"
- Collected accumulated Reviewed-by and Acked-by tags
- Split the patchset by moving foundational changes into a separate preparation series:
https://lore.kernel.org/linux-nvme/20241202-refactor-blk-affinity-helpers-v6-0-27211e9c2cd5@kernel.org/
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes since v2:
- Integrated patches from Ming Lei
(https://lore.kernel.org/all/[email protected]/):
"virtio: add APIs for retrieving vq affinity" and "blk-mq: introduce blk_mq_dev_map_queues"
- Replaced all instances of blk_mq_pci_map_queues and blk_mq_virtio_map_queues with the new unified blk_mq_dev_map_queues
- Updated and expanded the helper functions used for calculating the number of queues
- Added the CPU-to-hctx mapping function specifically to support the isolcpus=io_queue parameter
- Documented the hk_type enum and the newly introduced isolcpus=io_queue parameter
- Added the patch "scsi: pm8001: do not overwrite PCI queue mapping"
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes since v1:
- Updated the feature documentation for clarity and completeness
- Split the blk/nvme-pci patch into smaller, logical commits
- Dropped the HK_TYPE_IO_QUEUE macro in favor of reusing HK_TYPE_MANAGED_IRQ
- Link to v1: https://lore.kernel.org/r/[email protected]
Aaron Tomlin (3):
sched/isolation: Prevent out-of-bounds read in isolcpus= boot
parameter parser
virtio-fs: honour isolcpus=managed_irq in queue mapping genirq/affinity: Restrict managed IRQ affinity to housekeeping CPUs
Daniel Wagner (6):
scsi: aacraid: use block layer helpers to calculate num of queues lib/group_cpus: remove dead !SMP code
lib/group_cpus: Add group_mask_cpus_evenly()
blk-mq: use hk cpus only when isolcpus=managed_irq is enabled
blk-mq: prevent offlining hk CPUs with associated online isolated CPUs
docs: update isolcpus=managed_irq documentation
Ionut Nechita (1):
genirq/affinity: Confine pre and post vectors to housekeeping CPUs
Documentation/admin-guide/cpu-isolation.rst | 3 +- .../admin-guide/kernel-parameters.txt | 55 +++--- Documentation/core-api/housekeeping.rst | 5 +- Documentation/core-api/irq/managed_irq.rst | 59 +++--- block/blk-mq-cpumap.c | 184 ++++++++++++++++-- block/blk-mq.c | 75 +++++++ drivers/scsi/aacraid/comminit.c | 3 +- fs/fuse/virtio_fs.c | 98 +++++++--- include/linux/group_cpus.h | 3 + kernel/irq/affinity.c | 57 +++++- kernel/sched/isolation.c | 2 + lib/group_cpus.c | 112 +++++++++-- 12 files changed, 539 insertions(+), 117 deletions(-)
Signature · 1 line
2.55.0
PATCH v17 1/10 scsi: aacraid: use block layer helpers to calculate num of queues
From: Daniel Wagner <[email protected]>
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.
This patch brings aacraid in line with the API migration initiated for other SCSI drivers in commit 94970cfb5f10 ("scsi: use block layer helpers to calculate num of queues").
--- drivers/scsi/aacraid/comminit.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/scsi/aacraid/comminit.c +1 -2
@@ -469,8 +469,7 @@ void aac_define_int_mode(struct aac_dev *dev) }/* Don't bother allocating more MSI-X vectors than cpus */ - msi_count =min (dev->max_msix, - (unsigned int )num_online_cpus ()); + msi_count =blk_mq_num_online_queues (dev->max_msix); dev->max_msix = msi_count;
Signature · 1 line
2.55.0
PATCH v17 2/10 lib/group_cpus: remove dead !SMP code
In reply to Aaron TomlinFrom: Daniel Wagner <[email protected]>
The core scheduler recently transitioned to compiling SMP data structures unconditionally to reduce code complexity - see commit cac5cefbade9 ("sched/smp: Make SMP unconditional").
In alignment with this philosophy of reducing dual-path maintenance, this patch removes the #ifdef CONFIG_SMP guards and the dedicated !SMP fallback logic here.
While the !SMP path provided a slightly simpler execution flow for uniprocessor kernels (avoiding SMP-specific overhead), maintaining these separate code paths adds unnecessary complexity and testing burden. Removing these guards simplifies the codebase by standardizing entirely on the SMP logic, which safely resolves to single-CPU operations on UP configurations.
--- lib/group_cpus.c | 20 -------------------- 1 file changed, 20 deletions(-)
lib/group_cpus.c +0 -20
@@ -9,8 +9,6 @@#include <linux/sort.h> #include <linux/group_cpus.h> -#ifdef CONFIG_SMP -static void grp_spread_init_one (struct cpumask *irqmsk,struct cpumask *nmsk,unsigned int cpus_per_grp) {
@@ -564,22 +562,4 @@ struct cpumask *group_cpus_evenly(unsigned int numgrps, unsigned int *nummasks) *nummasks =min (nr_present + nr_others, numgrps);return masks; } -#else /* CONFIG_SMP */ -struct cpumask *group_cpus_evenly (unsigned int numgrps,unsigned int *nummasks) -{ -struct cpumask *masks; - -if (numgrps ==0 ) -return NULL ; - - masks =kzalloc_objs (*masks, numgrps); -if (!masks) -return NULL ; - -/* assign all CPUs(cpu 0) to the 1st group only */ -cpumask_copy (&masks[0 ], cpu_possible_mask); - *nummasks =1 ; -return masks; -} -#endif /* CONFIG_SMP */ EXPORT_SYMBOL_GPL (group_cpus_evenly);
Signature · 1 line
2.55.0
PATCH v17 3/10 lib/group_cpus: Add group_mask_cpus_evenly()
In reply to Aaron TomlinFrom: Daniel Wagner <[email protected]>
This commit introduces group_mask_cpus_evenly(), which allows callers to distribute a specific CPU mask evenly across groups. It serves as a bounded version of group_cpus_evenly().
While group_cpus_evenly() operates on the global cpu_possible_mask, group_mask_cpus_evenly() confines the distribution strictly within the boundaries of the caller-provided mask. It preserves the kernel's native two-stage spreading logic, first prioritising CPUs that are physically present (cpu_present_mask) to prevent I/O starvation, and then distributing any remaining vectors to non-present CPUs to maintain hotplug safety.
--- include/linux/group_cpus.h | 3 + lib/group_cpus.c | 110 +++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+)
include/linux/group_cpus.h +3 -0
@@ -10,5 +10,8 @@#include <linux/cpu.h> struct cpumask *group_cpus_evenly (unsigned int numgrps,unsigned int *nummasks); +struct cpumask *group_mask_cpus_evenly (unsigned int numgrps, +const struct cpumask *mask, +unsigned int *nummasks);#endif
lib/group_cpus.c +110 -0
113 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v17 03/10] lib/group_cpus: Add group_mask_cpus_evenly()
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] lib/group_cpus: group_mask_cpus_evenly() duplicates group_cpus_evenly() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] lib/group_cpus: group_mask_cpus_evenly() duplicates group_cpus_evenly()
--
commit ea8c98b46f36bc22b6c75e4130661116efe0e69c
Author: Daniel Wagner <[email protected]>
lib/group_cpus: Add group_mask_cpus_evenly()
This commit introduces group_mask_cpus_evenly() to distribute a specific caller-provided CPU mask evenly across groups using a two-stage present and non-present spreading pass.
Quoted 11 lines
lib/group_cpus.c +3
-0@@ -563,3 +563,113 @@ struct cpumask *group_cpus_evenly(unsigned int numgrps, unsigned int *nummasks)return masks; }EXPORT_SYMBOL_GPL (group_cpus_evenly); + +/** +* group_mask_cpus_evenly - Group all CPUs evenly per NUMA/CPU locality
[ ... ]
Quoted 9 lines
lib/group_cpus.c +9
-0+struct cpumask *group_mask_cpus_evenly (unsigned int numgrps, +const struct cpumask *mask, +unsigned int *nummasks) +{ +unsigned int curgrp =0 , nr_present =0 , nr_others =0 ; +cpumask_var_t *node_to_cpumask; +cpumask_var_t nmsk, local_mask, npresmsk; +int ret = -ENOMEM ; +struct cpumask *masks =NULL ;
[ ... ]
Quoted 19 lines
lib/group_cpus.c +19
-0+cpumask_copy (local_mask, mask); + +/* +* Grouping present CPUs first. We intersect the provided mask with +* cpu_present_mask to ensure that we prioritise physically +* available CPUs for the initial distribution. +*/ +cpumask_and (npresmsk, local_mask, cpu_present_mask); + ret =__group_cpus_evenly (curgrp, numgrps, node_to_cpumask, + npresmsk, nmsk, masks); +if (ret <0 ) + goto fail_node_to_cpumask; + nr_present = ret; + +/* +* Allocate non-present CPUs starting from the next group to be +* handled. If the grouping of present CPUs already exhausted the +* group space, assign the non-present CPUs to the already +* allocated out groups.
lib/group_cpus.c struct cpumask *group_cpus_evenly(unsigned int numgrps, unsigned int *nummasks) · line 651
Quoted 10 lines
lib/group_cpus.c +10
-0+*/ +if (nr_present >= numgrps) + curgrp =0 ; +else + curgrp = nr_present; +cpumask_andnot (npresmsk, local_mask, npresmsk); + ret =__group_cpus_evenly (curgrp, numgrps, node_to_cpumask, + npresmsk, nmsk, masks); +if (ret >=0 ) + nr_others = ret;
[Severity: Low]
Could group_cpus_evenly() simply call group_mask_cpus_evenly(numgrps, cpu_possible_mask, nummasks) instead of duplicating the allocation, two-stage spreading, and cleanup logic? Since cpu_present_mask is a subset of cpu_possible_mask, passing cpu_possible_mask as mask produces the same two-stage distribution.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v17 4/10 sched/isolation: Prevent out-of-bounds read in isolcpus= boot parameter parser
In reply to Aaron TomlinThe "isolcpus=" boot parameter parser in housekeeping_isolcpus_setup() contains an out-of-bounds memory read bug when handling unterminated flags.
When parsing the boot parameter string, the logic expects flags to be comma-separated. If a user passes an unrecognised or legitimate flag at the very end of the string without a trailing comma (e.g., "isolcpus=unknown"), the strict strncmp() checks will fail.
The execution then falls through to a fallback for loop designed to skip the unknown sub-parameter. This inner loop consumes characters until it encounters either a comma or the NULL terminator ('\0'). When the loop terminates due to hitting the end of the string, the str pointer rests exactly on the NULL terminator.
However, immediately following this inner loop, the code unconditionally executes str++. This advances the pointer past the end of the string and into uninitialised memory. The outer while (isalpha(*str)) loop subsequently evaluates this out-of-bounds memory. If the adjacent byte happens to be alphabetical, the parser will continue reading garbage data, potentially leading to undefined behavior or boot anomalies.
Fix this by adding a bounds check immediately before the pointer increment. This ensures the parsing loop cleanly terminates when reaching the end of the boot parameter string.
--- kernel/sched/isolation.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/sched/isolation.c +2 -0
@@ -380,6 +380,8 @@ static int __init housekeeping_isolcpus_setup(char *str) }pr_info ("isolcpus: Skipped unknown flag %.*s\n" , len, par); +if (!*str) +break ; str++; }
Signature · 1 line
2.55.0
PATCH v17 5/10 blk-mq: use hk cpus only when isolcpus=managed_irq is enabled
In reply to Aaron TomlinFrom: Daniel Wagner <[email protected]>
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=managed_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=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
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.1This 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=) 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 15The 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.
--- block/blk-mq-cpumap.c | 184 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 166 insertions(+), 18 deletions(-)
block/blk-mq-cpumap.c +166 -18
@@ -17,13 +17,32 @@#include "blk.h" #include "blk-mq.h" +/** +* blk_mq_num_queues - Calculate number of queues for multiqueue devices +* @mask: CPUs to consider for the queue count +* @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 allocate from @mask, restricting the +* count to housekeeping CPUs when isolcpus=managed_irq is enabled. +* +* Return: Number of queues to allocate (at least 1). +*/ static unsigned int blk_mq_num_queues (const struct cpumask *mask,unsigned int max_queues) {unsigned int num; - num =cpumask_weight (mask); -return min_not_zero (num, max_queues); +if (housekeeping_enabled (HK_TYPE_MANAGED_IRQ )) + num =cpumask_weight_and (mask,housekeeping_cpumask (HK_TYPE_MANAGED_IRQ )); +else + num =cpumask_weight (mask); +/* +* Ensure that a count of zero does not inadvertently result in +* allocating the maximum number of queues. +*/ +return min_not_zero (num ?:1U , max_queues); }/**
@@ -33,7 +52,8 @@ static unsigned int blk_mq_num_queues(const struct cpumask *mask,* ignored. * * 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);* ignored. * * 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); +/** +* blk_mq_map_fallback - Map all possible CPUs to the first queue of @qmap +* @qmap: CPU to hardware queue map +* +* Map all possible CPUs to the first hardware context of @qmap, respecting +* @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) +{ +unsigned int cpu; + +for_each_possible_cpu (cpu) + qmap->mq_map[cpu] = qmap->queue_offset; +} + +/** +* blk_mq_map_queues - Create default CPU to hardware queue mapping +* @qmap: CPU 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) { -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 ); + } +kfree (masks); +bitmap_free (active_hctx); + +return ; + +free_fallback: +kfree (masks); +free_fallback_hctx: +bitmap_free (active_hctx); + +fallback: +blk_mq_map_fallback (qmap); }EXPORT_SYMBOL_GPL (blk_mq_map_queues);
@@ -109,24 +199,82 @@ void blk_mq_map_hw_queues(struct blk_mq_queue_map *qmap, struct device *dev, unsigned int offset) { -const struct cpumask *mask; +cpumask_var_t mask; +const struct cpumask *constraint; +unsigned long *active_hctx;unsigned int queue, cpu;if (!dev->bus->irq_get_affinity) + goto map_software; + + active_hctx =bitmap_zalloc (qmap->nr_queues,GFP_KERNEL ); +if (!active_hctx) goto fallback; +if (!zalloc_cpumask_var (&mask,GFP_KERNEL )) { +bitmap_free (active_hctx); + goto fallback; + } + +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) */ 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 ); } +bitmap_free (active_hctx); +free_cpumask_var (mask); +return ; +free_fallback: +bitmap_free (active_hctx); +free_cpumask_var (mask); + fallback: +blk_mq_map_fallback (qmap); +return ; + +free_map_software: +free_cpumask_var (mask); +bitmap_free (active_hctx); +map_software:blk_mq_map_queues (qmap); }EXPORT_SYMBOL_GPL (blk_mq_map_hw_queues);
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v17 05/10] blk-mq: use hk cpus only when isolcpus=managed_irq is enabled
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…
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 <[email protected]>
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.
[ ... ]
Quoted 11 lines
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=2Queue mapping:
hctx0: default 0 1 2 3 4 5 6 7 8 12 13
hctx1: default 9 10 11 14 15
[ ... ]
Quoted 8 lines
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.
[ ... ]
Quoted 19 lines
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_evenlyto 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 15The assign each hardware context the map generated by the
group_mask_cpus_evenlyfunction:
Quoted 9 lines
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.
Quoted 4 lines
block/blk-mq-cpumap.c +0
-0
[ ... ]
Quoted 60 lines
block/blk-mq-cpumap.c +43
-9@@ -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 ); + }
[ ... ]
Quoted 31 lines
block/blk-mq-cpumap.c +23
-4@@ -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.
block/blk-mq-cpumap.c void blk_mq_map_hw_queues(struct blk_mq_queue_map *qmap, · line 234
Quoted 10 lines
block/blk-mq-cpumap.c +9
-0+*/ +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.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
PATCH v17 6/10 blk-mq: prevent offlining hk CPUs with associated online isolated CPUs
In reply to Aaron TomlinFrom: Daniel Wagner <[email protected]>
When isolcpus=managed_irq is enabled and the last housekeeping CPU for a given hctx goes offline, no CPU would be left to handle I/O. To prevent I/O stalls, disallow offlining housekeeping CPUs that are still serving isolated CPUs.
--- block/blk-mq.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+)
block/blk-mq.c +75 -0
@@ -3726,6 +3726,76 @@ static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx)return data.has_rq; } +/** +* blk_mq_hctx_can_offline_hk_cpu - Check if a housekeeping CPU may go offline +* @hctx: Hardware queue context being evaluated +* @this_cpu: CPU going offline +* +* Prevent offlining the last online housekeeping CPU mapped to @hctx while +* any isolated CPU mapped to @hctx remains online, avoiding stranded I/O +* submissions. System suspend and hibernation are exempted via +* cpuhp_tasks_frozen. +* +* Return: true if @this_cpu may go offline, false otherwise. +*/ +static bool blk_mq_hctx_can_offline_hk_cpu (struct blk_mq_hw_ctx *hctx, +unsigned int this_cpu) +{ +const struct cpumask *hk_mask =housekeeping_cpumask (HK_TYPE_MANAGED_IRQ ); +int cpu, fallback_isolated_cpu =-1 ; + +/* +* During system suspend or hibernation, tasks are frozen and all +* CPUs are being taken offline. Allow offlining to proceed so +* suspend is not aborted. +*/ +if (cpuhp_tasks_frozen) +return true; + +/* +* If the CPU being offlined is not a housekeeping CPU, +* offlining it will not strand isolated CPUs. Allow it. +*/ +if (!cpumask_test_cpu (this_cpu, hk_mask)) +return true; +/* +* If this CPU is not mapped to this specific hardware context, +* offlining it will not affect the context's I/O routing. Allow it. +*/ +if (blk_mq_map_queue_type (hctx->queue, hctx->type, this_cpu) != hctx) +return true; +/* +* Iterate over all online CPUs and manually check their mapping. +* We cannot use hctx->cpumask here because blk_mq_map_swqueue() +* intentionally strips isolated CPUs from it to prevent kworker +* routing. +*/ +for_each_online_cpu (cpu) { +struct blk_mq_hw_ctx *h; + +if (cpu == this_cpu) +continue ; + + h =blk_mq_map_queue_type (hctx->queue, hctx->type, cpu); +if (h != hctx) +continue ; + +if (cpumask_test_cpu (cpu, hk_mask)) +return true; + +if (fallback_isolated_cpu ==-1 ) + fallback_isolated_cpu = cpu; + } + +if (fallback_isolated_cpu !=-1 ) { +pr_warn ("blk-mq: cannot offline CPU %u: online isolated CPU %d is still mapped to hctx%u\n" , + this_cpu, fallback_isolated_cpu, hctx->queue_num); +return false; + } + +return true; +} +static bool blk_mq_hctx_has_online_cpu (struct blk_mq_hw_ctx *hctx,unsigned int this_cpu) {
@@ -3758,6 +3828,11 @@ static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node) struct blk_mq_hw_ctx, cpuhp_online);int ret =0 ; +if (housekeeping_enabled (HK_TYPE_MANAGED_IRQ )) { +if (!blk_mq_hctx_can_offline_hk_cpu (hctx, cpu)) +return -EINVAL ; + } +if (!hctx->nr_ctx ||blk_mq_hctx_has_online_cpu (hctx, cpu))return 0 ;
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v17 06/10] blk-mq: prevent offlining hk CPUs with associated online isolated CPUs
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] blk-mq: missing kexec shutdown exemption in blk_mq_hctx_can_offline_hk_cpu() - [Medium] blk-mq…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] blk-mq: missing kexec shutdown exemption in blk_mq_hctx_can_offline_hk_cpu()
- [Medium] blk-mq: blk_mq_hctx_notify_online() activates hctx without online hk CPU
--
commit b727ebd4323e3c57fb0bd576c67e6c3d028b7b3e
Author: Daniel Wagner <[email protected]>
blk-mq: prevent offlining hk CPUs with associated online isolated CPUs
This commit adds blk_mq_hctx_can_offline_hk_cpu() to prevent offlining the last online housekeeping CPU mapped to an hctx while any isolated CPU mapped to that hctx remains online.
Quoted 23 lines
block/blk-mq.c +15
-0@@ -3718,6 +3718,76 @@ static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx)return data.has_rq; } +/** +* blk_mq_hctx_can_offline_hk_cpu - Check if a housekeeping CPU may go offline +* @hctx: Hardware queue context being evaluated +* @this_cpu: CPU going offline +* +* Prevent offlining the last online housekeeping CPU mapped to @hctx while +* any isolated CPU mapped to @hctx remains online, avoiding stranded I/O +* submissions. System suspend and hibernation are exempted via +* cpuhp_tasks_frozen. +* +* Return: true if @this_cpu may go offline, false otherwise. +*/ +static bool blk_mq_hctx_can_offline_hk_cpu (struct blk_mq_hw_ctx *hctx, +unsigned int this_cpu) +{
block/blk-mq.c static bool blk_mq_hctx_has_requests(struct blk_mq_hw_ctx *hctx) · line 3745
Quoted 10 lines
block/blk-mq.c +10
-0+const struct cpumask *hk_mask =housekeeping_cpumask (HK_TYPE_MANAGED_IRQ ); +int cpu, fallback_isolated_cpu =-1 ; + +/* +* During system suspend or hibernation, tasks are frozen and all +* CPUs are being taken offline. Allow offlining to proceed so +* suspend is not aborted. +*/ +if (cpuhp_tasks_frozen) +return true;
[Severity: High]
Can this trigger BUG_ON(num_online_cpus() > 1) in smp_shutdown_nonboot_cpus() during a kexec reboot?
During kexec or machine_shutdown(), smp_shutdown_nonboot_cpus() offlines all non-boot CPUs in ascending order via cpu_down_maps_locked(), which calls _cpu_down(cpu, 0, CPUHP_OFFLINE) with cpuhp_tasks_frozen == false:
kernel/cpu.c:smp_shutdown_nonboot_cpus() {
...
for_each_online_cpu(cpu) {
if (cpu == primary_cpu)
continue;
error = cpu_down_maps_locked(cpu, CPUHP_OFFLINE);
if (error) {
pr_err("Failed to offline CPU%d - error=%d",
cpu, error);
break;
}
}
/*
* Ensure all but the reboot CPU are offline.
*/
BUG_ON(num_online_cpus() > 1);
...
}If a non-boot housekeeping CPU has a lower CPU ID than an online isolated CPU mapped to the same hctx, blk_mq_hctx_can_offline_hk_cpu() returns false, blk_mq_hctx_notify_offline() fails with -EINVAL, and smp_shutdown_nonboot_cpus() aborts the loop and hits the BUG_ON().
[ ... ]
block/blk-mq.c static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node) · line 3829
Quoted 11 lines
block/blk-mq.c +5
-0@@ -3750,6 +3820,11 @@ static int blk_mq_hctx_notify_offline(unsigned int cpu, struct hlist_node *node) struct blk_mq_hw_ctx, cpuhp_online);int ret =0 ; +if (housekeeping_enabled (HK_TYPE_MANAGED_IRQ )) { +if (!blk_mq_hctx_can_offline_hk_cpu (hctx, cpu)) +return -EINVAL ; + } +if (!hctx->nr_ctx ||blk_mq_hctx_has_online_cpu (hctx, cpu))return 0 ;
[Severity: Medium]
Can an hctx still end up active with an online isolated CPU and no online housekeeping CPU if the isolated CPU is offlined first, the housekeeping CPU is offlined next, and the isolated CPU is then brought back online?
Once all isolated CPUs mapped to an hctx are offline, blk_mq_hctx_can_offline_hk_cpu() allows the last housekeeping CPU to go offline, setting BLK_MQ_S_INACTIVE and shutting down the hctx's managed IRQ. When an isolated CPU mapped to that hctx is brought back online before any housekeeping CPU, blk_mq_hctx_notify_online() clears BLK_MQ_S_INACTIVE without checking whether a housekeeping CPU is online:
block/blk-mq.c:blk_mq_hctx_notify_online() {
...
if (blk_mq_cpu_mapped_to_hctx(cpu, hctx))
clear_bit(BLK_MQ_S_INACTIVE, &hctx->state);
return 0;
}Because the isolated CPU is excluded from the managed IRQ affinity mask and hctx->cpumask, the hctx becomes active for I/O from the isolated CPU with its managed IRQ still shut down and no online housekeeping CPU to run its block kworkers, causing I/O stalls.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
PATCH v17 7/10 virtio-fs: honour isolcpus=managed_irq in queue mapping
In reply to Aaron TomlinSince commit df28040c7f24 ("virtio_fs: store actual queue index in mq_map"), fs->mq_map is zero-initialised to 0 (VQ_HIPRIO, the forget-only virtqueue) while virtio_fs_map_queues() populates entries only for CPUs in get_vq_affinity(). When isolcpus=managed_irq restricts managed interrupt affinity to housekeeping CPUs, isolated CPUs remain at VQ_HIPRIO; requests submitted from them are treated as forget objects by virtio_fs_hiprio_done_work() and freed via kfree() without calling fuse_request_end(), corrupting slab state and hanging the caller.
Cap fs->num_request_queues to the HK_TYPE_MANAGED_IRQ housekeeping CPU count when enabled (or num_possible_cpus() otherwise), use group_mask_cpus_evenly() in the software fallback path, and spread any unmapped possible CPUs across active request queues so every possible CPU maps to a valid VQ_REQUEST queue.
--- fs/fuse/virtio_fs.c | 98 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 75 insertions(+), 23 deletions(-)
fs/fuse/virtio_fs.c +75 -23
@@ -9,6 +9,7 @@#include <linux/pci.h> #include <linux/interrupt.h> #include <linux/group_cpus.h> +#include <linux/sched/isolation.h> #include <linux/memremap.h> #include <linux/module.h> #include <linux/virtio.h>
@@ -923,40 +924,88 @@ static void virtio_fs_requests_done_work(struct work_struct *work)static void virtio_fs_map_queues (struct virtio_device *vdev,struct virtio_fs *fs) { -const struct cpumask *mask, *masks; -unsigned int q, cpu, nr_masks; +const struct cpumask *mask, *constraint; +unsigned int q, idx, cpu, nr_masks; +unsigned long *active_vqs; +struct cpumask *masks; +cpumask_var_t mapped; + + active_vqs =bitmap_zalloc (fs->num_request_queues,GFP_KERNEL ); +if (!active_vqs) + goto fallback; + +if (!zalloc_cpumask_var (&mapped,GFP_KERNEL )) + goto free_active_vqs; + +if (housekeeping_enabled (HK_TYPE_MANAGED_IRQ )) + constraint =housekeeping_cpumask (HK_TYPE_MANAGED_IRQ ); +else + constraint = cpu_possible_mask;/* First attempt to map using existing transport layer affinities * e.g. PCIe MSI-X */ if (!vdev->config->get_vq_affinity) - goto fallback; + goto map_software;for (q =0 ; q < fs->num_request_queues; q++) { mask = vdev->config->get_vq_affinity (vdev,VQ_REQUEST + q);if (!mask) - goto fallback; + goto map_software; -for_each_cpu (cpu, mask) - fs->mq_map[cpu] = q +VQ_REQUEST ; +for_each_cpu_and (cpu, mask, constraint) { + fs->mq_map[cpu] =VQ_REQUEST + q; +cpumask_set_cpu (cpu, mapped); + } } -return ; -fallback: -/* Attempt to map evenly in groups over the CPUs */ - masks =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) - fs->mq_map[cpu] =VQ_REQUEST ; -return ; - } +for_each_cpu (cpu, mapped) +__set_bit (fs->mq_map[cpu] -VQ_REQUEST , active_vqs); -for (q =0 ; q < fs->num_request_queues; q++) { -for_each_cpu (cpu, &masks[q % nr_masks]) - fs->mq_map[cpu] = q +VQ_REQUEST ; + goto map_unassigned; + +map_software: +cpumask_clear (mapped); + masks =group_mask_cpus_evenly (fs->num_request_queues, constraint, + &nr_masks); +if (!masks) + goto free_mapped; + +for (idx =0 ; idx < nr_masks; idx++) { + q = fs->num_request_queues -1 - + ((fs->num_request_queues -1 - idx) % nr_masks); + +for_each_cpu (cpu, &masks[idx]) { + fs->mq_map[cpu] =VQ_REQUEST + q; +cpumask_set_cpu (cpu, mapped); + } +__set_bit (q, active_vqs); }kfree (masks); + +map_unassigned: +if (bitmap_empty (active_vqs, fs->num_request_queues)) + goto free_mapped; + + q =find_first_bit (active_vqs, fs->num_request_queues); +for_each_cpu_andnot (cpu, cpu_possible_mask, mapped) { + fs->mq_map[cpu] =VQ_REQUEST + q; + q =find_next_bit_wrap (active_vqs, fs->num_request_queues, + q +1 ); + } + +free_cpumask_var (mapped); +bitmap_free (active_vqs); +return ; + +free_mapped: +free_cpumask_var (mapped); +free_active_vqs: +bitmap_free (active_vqs); +fallback: +/* If even this fails we default to all CPUs use first request queue */ +for_each_possible_cpu (cpu) + fs->mq_map[cpu] =VQ_REQUEST ; }/* Virtqueue interrupt handler */
@@ -1000,7 +1049,7 @@ static int virtio_fs_setup_vqs(struct virtio_device *vdev,* the multi-queue mapping and interrupt affinities */ struct irq_affinity desc = { .pre_vectors =VQ_REQUEST }; -unsigned int i; +unsigned int i, max_queues;int ret =0 ; virtio_cread_le(vdev, struct virtio_fs_config, num_request_queues,
@@ -1008,9 +1057,12 @@ static int virtio_fs_setup_vqs(struct virtio_device *vdev,if (fs->num_request_queues ==0 )return -EINVAL ; -/* Truncate nr of request queues to nr_cpu_id */ - fs->num_request_queues = min_t(unsigned int, fs->num_request_queues, - nr_cpu_ids); +if (housekeeping_enabled (HK_TYPE_MANAGED_IRQ )) + max_queues =cpumask_weight (housekeeping_cpumask (HK_TYPE_MANAGED_IRQ )); +else + max_queues =num_possible_cpus (); + + fs->num_request_queues =min (fs->num_request_queues, max_queues); fs->nvqs =VQ_REQUEST + fs->num_request_queues; fs->vqs =kzalloc_objs (fs->vqs[VQ_HIPRIO ], fs->nvqs);if (!fs->vqs)
Signature · 1 line
2.55.0
PATCH v17 8/10 genirq/affinity: Restrict managed IRQ affinity to housekeeping CPUs
In reply to Aaron TomlinAt present, the managed interrupt spreading algorithm distributes vectors across all available CPUs within a given node or system. On systems employing CPU isolation (e.g. "isolcpus=managed_irq"), this behaviour defeats the primary purpose of isolation by routing hardware interrupts (such as NVMe completion queues) directly to isolated cores.
Update irq_create_affinity_masks() to respect the housekeeping CPU mask. By passing the HK_TYPE_MANAGED_IRQ mask directly to the topological distribution function (group_mask_cpus_evenly()), we ensure that managed interrupts are kept strictly off isolated CPUs.
This patch additionally addresses the architectural constraints of restricted vector distribution:
1. Vector limits and multi-set scaling
Updated irq_calc_affinity_vectors() to bound the maximum number of allocated vectors to the weight of the housekeeping mask for single-set drivers. For drivers providing a calc_sets() callback, vector calculations continue to scale with the driver's requested set sizes (maxvec - resv), preventing unnecessary queue contention across distinct functional sets while irq_create_affinity_masks() guarantees that all allocated vectors remain strictly restricted to housekeeping CPUs.
2. Multi-set alignment and leak prevention
When isolation constraints result in fewer available masks than requested vectors for a given set, the remaining vector slots are padded with the housekeeping mask. This replaces the historical irq_default_affinity padding, ensuring excess managed queues do not leak interrupts onto isolated CPUs.
3. Minimum vector safety net
To prevent fatal -ENOSPC device probe aborts on heavily isolated systems (where the housekeeping CPU count might be lower than a device's structural minimum), the final vector calculation is safeguarded to never drop below minvec. Queues will safely share the available housekeeping CPUs instead of failing the probe.
4. Zero overhead
The housekeeping mask is conditionally assigned via a direct pointer, completely avoiding temporary mask allocations (e.g. alloc_cpumask_var) and bitwise operations when CPU isolation is disabled. This guarantees zero performance or memory overhead for standard configurations.
--- kernel/irq/affinity.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-)
kernel/irq/affinity.c +22 -7
@@ -8,6 +8,7 @@#include <linux/slab.h> #include <linux/cpu.h> #include <linux/group_cpus.h> +#include <linux/sched/isolation.h> static void default_calc_sets (struct irq_affinity *affd,unsigned int affvecs) {
@@ -25,8 +26,10 @@ static void default_calc_sets(struct irq_affinity *affd, unsigned int affvecs)struct irq_affinity_desc *irq_create_affinity_masks (unsigned int nvecs,struct irq_affinity *affd) { -unsigned int affvecs, curvec, usedvecs, i; +unsigned int affvecs, curvec, usedvecs, i, j;struct irq_affinity_desc *masks =NULL ; +const struct cpumask *hk_mask =housekeeping_cpumask (HK_TYPE_MANAGED_IRQ ); +bool hk_enabled =housekeeping_enabled (HK_TYPE_MANAGED_IRQ );/* * Determine the number of vectors which need interrupt affinities
@@ -70,19 +73,29 @@ irq_create_affinity_masks(unsigned int nvecs, struct irq_affinity *affd)*/ for (i =0 , usedvecs =0 ; i < affd->nr_sets; i++) {unsigned int nr_masks, this_vecs = affd->set_size[i]; -struct cpumask *result =group_cpus_evenly (this_vecs, &nr_masks); +struct cpumask *result; +const struct cpumask *mask; +if (hk_enabled) + mask = hk_mask; +else + mask = cpu_possible_mask; + + result =group_mask_cpus_evenly (this_vecs, mask, + &nr_masks);if (!result) {kfree (masks);return NULL ; } - -for (int j =0 ; j < nr_masks; j++) +for (j =0 ; j < nr_masks; j++)cpumask_copy (&masks[curvec + j].mask, &result[j]); +for (j = nr_masks; j < this_vecs; j++) +cpumask_copy (&masks[curvec + j].mask, mask); +kfree (result); - curvec += nr_masks; - usedvecs += nr_masks; + curvec += this_vecs; + usedvecs += this_vecs; }/* Fill out vectors at the end that don't need affinity */
@@ -117,8 +130,10 @@ unsigned int irq_calc_affinity_vectors(unsigned int minvec, unsigned int maxvec,if (affd->calc_sets) set_vecs = maxvec - resv; +else if (housekeeping_enabled (HK_TYPE_MANAGED_IRQ )) + set_vecs =cpumask_weight (housekeeping_cpumask (HK_TYPE_MANAGED_IRQ ));else set_vecs =cpumask_weight (cpu_possible_mask); -return resv +min (set_vecs, maxvec - resv); +return max (minvec, resv +min (set_vecs, maxvec - resv)); }
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v17 08/10] genirq/affinity: Restrict managed IRQ affinity to housekeeping CPUs
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] genirq/affinity: padding excess vectors with full mask collapses queues --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] genirq/affinity: padding excess vectors with full mask collapses queues
--
commit 7b34278d7bc0891686d6faff17c09feec98bd7c5
Author: Aaron Tomlin <[email protected]>
genirq/affinity: Restrict managed IRQ affinity to housekeeping CPUs
This commit restricts managed interrupt affinity masks and vector count calculations to HK_TYPE_MANAGED_IRQ housekeeping CPUs, while clamping the calculated vector count to at least minvec and padding excess vector slots with the housekeeping mask.
Quoted 4 lines
kernel/irq/affinity.c +0
-0
[ ... ]
Quoted 23 lines
kernel/irq/affinity.c +10
-3@@ -70,19 +73,29 @@ irq_create_affinity_masks(unsigned int nvecs, struct irq_affinity *affd)*/ for (i =0 , usedvecs =0 ; i < affd->nr_sets; i++) {unsigned int nr_masks, this_vecs = affd->set_size[i]; -struct cpumask *result =group_cpus_evenly (this_vecs, &nr_masks); +struct cpumask *result; +const struct cpumask *mask; +if (hk_enabled) + mask = hk_mask; +else + mask = cpu_possible_mask; + + result =group_mask_cpus_evenly (this_vecs, mask, + &nr_masks);if (!result) {kfree (masks);return NULL ; } - -for (int j =0 ; j < nr_masks; j++) +for (j =0 ; j < nr_masks; j++)cpumask_copy (&masks[curvec + j].mask, &result[j]);
kernel/irq/affinity.c irq_create_affinity_masks(unsigned int nvecs, struct irq_affinity *affd) · line 99
Quoted 10 lines
kernel/irq/affinity.c +5
-2+for (j = nr_masks; j < this_vecs; j++) +cpumask_copy (&masks[curvec + j].mask, mask); +kfree (result); - curvec += nr_masks; - usedvecs += nr_masks; + curvec += this_vecs; + usedvecs += this_vecs; }
[Severity: High]
When this_vecs exceeds nr_masks, does copying the full mask into the trailing vector slots collapse all CPUs onto the last hardware queue in blk_mq_map_hw_queues()?
This happens under isolcpus=managed_irq whenever a set requests more vectors than the number of housekeeping CPUs, such as via a driver affd->calc_sets callback (e.g. nvme_calc_irq_sets()) or via the new max(minvec, ...) floor in irq_calc_affinity_vectors(). group_mask_cpus_evenly() returns nr_masks equal to the housekeeping CPU count, and the second loop assigns the full housekeeping mask to every remaining vector j >= nr_masks.
In blk_mq_map_hw_queues(), each queue maps its affinity mask in order:
block/blk-mq-cpumap.c:blk_mq_map_hw_queues() {
...
for (queue = 0; queue < qmap->nr_queues; queue++) {
...
for_each_cpu_and(cpu, affinity_mask, constraint) {
qmap->mq_map[cpu] = qmap->queue_offset + queue;
cpumask_set_cpu(cpu, mask);
}
}
...
}Because the padded vectors cover every housekeeping CPU, the final vector (this_vecs - 1) overwrites qmap->mq_map[cpu] for all housekeeping CPUs (and subsequently all isolated CPUs), leaving queues 0 .. this_vecs - 2 unused. In addition, on architectures using the generic IRQ matrix allocator, irq_matrix_reserve_managed() reserves a vector slot on every CPU in the mask, so each excess vector consumes a vector on all housekeeping CPUs instead of one group.
Wrapping the excess slots across the grouped masks (&result[j % nr_masks]) would keep each excess vector scoped to a single group.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
PATCH v17 9/10 docs: update isolcpus=managed_irq documentation
In reply to Aaron TomlinFrom: Daniel Wagner <[email protected]>
The managed_irq flag now strictly restricts multiqueue hardware queue allocation and managed interrupt affinity to housekeeping CPUs rather than relying on best-effort post-allocation steering. Update its description across the kernel command-line parameters, core housekeeping guide, managed interrupt guide, and CPU isolation documentation accordingly.
--- Documentation/admin-guide/cpu-isolation.rst | 3 +- .../admin-guide/kernel-parameters.txt | 54 +++++++++++-------- Documentation/core-api/housekeeping.rst | 5 +- Documentation/core-api/irq/managed_irq.rst | 52 +++++++++--------- 4 files changed, 62 insertions(+), 52 deletions(-)
Documentation/admin-guide/cpu-isolation.rst +2 -1
@@ -131,7 +131,8 @@ Interfaces - The "irqaffinity=" kernel boot parameter for a default setting. - The "managed_irq" flag in the "isolcpus=" kernel boot parameter - tries a best effort affinity override for managed IRQs. + restricts multiqueue hardware queue allocation and managed IRQ + affinity to housekeeping CPUs. Full Dynticks (aka nohz_full) -----------------------------
Documentation/admin-guide/kernel-parameters.txt +32 -22
60 lines not on this page · raw message
Documentation/core-api/housekeeping.rst +3 -2
@@ -36,8 +36,9 @@ by the ``enum hk_type type``: parameters. This includes remote scheduler tick, vmstat and lockup watchdog. -3. HK_TYPE_MANAGED_IRQ matches the IRQ handlers moved away by managed - IRQ isolation performed through ``isolcpus=managed_irq``. +3. HK_TYPE_MANAGED_IRQ matches the multiqueue block devices and + managed IRQs strictly restricted to housekeeping CPUs through + ``isolcpus=managed_irq``. 4. HK_TYPE_DOMAIN_BOOT matches the work moved away by scheduler domain isolation performed through ``isolcpus=domain`` only. It is similar
Documentation/core-api/irq/managed_irq.rst +25 -27
78 lines not on this page · raw message
Signature · 1 line
2.55.0
PATCH v17 10/10 genirq/affinity: Confine pre and post vectors to housekeeping CPUs
In reply to Aaron TomlinFrom: Ionut Nechita <[email protected]>
The managed IRQ affinity spreading for isolcpus=managed_irq restricts the affinity of managed interrupt vectors to housekeeping CPUs. However, the reserved vectors that do not participate in managed affinity spreading, namely affd->pre_vectors (such as the NVMe admin completion queue) and trailing affd->post_vectors, are still initialised unconditionally from irq_default_affinity, which by default spans all CPUs including isolated ones.
Although these reserved vectors are not marked managed, on a freshly probed device an admin queue interrupt can still be delivered to an isolated CPU before user space has a chance to re-steer it. On PREEMPT_RT this stray threaded interrupt wakeup on an isolated core directly perturbs latency-critical workloads.
When HK_TYPE_MANAGED_IRQ is enabled, confine these reserved pre/post vectors to the intersection of irq_default_affinity and the housekeeping mask (falling back to the housekeeping mask if that intersection contains no online CPUs). This keeps unmanaged device queues off isolated CPUs from the moment of allocation while still honouring irqaffinity= when configured, and leaves behaviour unchanged when managed_irq isolation is disabled. Update the managed_irq documentation to reflect this behaviour.
--- .../admin-guide/kernel-parameters.txt | 3 +- Documentation/core-api/irq/managed_irq.rst | 9 ++++-- kernel/irq/affinity.c | 28 +++++++++++++++++-- 3 files changed, 34 insertions(+), 6 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +2 -1
@@ -2827,7 +2827,8 @@ Kernel parameters In the block layer (blk-mq), device queue allocation and CPU-to-queue mappings are restricted to housekeeping CPUs. For devices using managed - interrupts, hardware completion interrupts are routed + interrupts, hardware completion interrupts and any + associated reserved pre/post vectors are routed exclusively to housekeeping cores, shielding isolated CPUs from I/O interruptions even if they initiated the request.
Documentation/core-api/irq/managed_irq.rst +6 -3
@@ -56,7 +56,9 @@ queue allocation and managed interrupt affinity strictly to housekeeping CPUs (the CPUs outside the specified isolated mask). Isolated CPUs are excluded when computing the managed interrupt affinity masks -and vector limits. In the block layer (blk-mq), isolated CPUs share hardware +and vector limits, and any unmanaged pre/post reserved vectors allocated +alongside them are confined to housekeeping CPUs (intersected with irqaffinity= +when configured). In the block layer (blk-mq), isolated CPUs share hardware queues owned by housekeeping CPUs so that completion interrupts never target isolated cores, and offlining the last housekeeping CPU serving an active isolated CPU is rejected.
@@ -94,8 +96,9 @@ The following examples assume a system with 8 CPUs. and the kernel command line includes: "irqaffinity=0,1 isolcpus=domain,2-7 isolcpus=managed_irq,1-3,5-7". The MSI-X device exposes 5 interrupts: 3 management interrupts and 2 queue - interrupts. The management interrupts follow the irqaffinity= setting. The - queue interrupts are spread strictly across the housekeeping CPUs (0 and 4):: + interrupts. The management interrupts are confined to the intersection of + irqaffinity= and the housekeeping mask (CPU0). The queue interrupts are + spread strictly across the housekeeping CPUs (0 and 4):: /proc/irq/47/effective_affinity_list:0 /proc/irq/47/smp_affinity_list:0
kernel/irq/affinity.c +26 -2
@@ -16,6 +16,30 @@ static void default_calc_sets(struct irq_affinity *affd, unsigned int affvecs) affd->set_size[0 ] = affvecs; } +/** +* irq_set_reserved_mask - Populate affinity mask for reserved pre/post vectors +* @dst: Destination cpumask to populate +* @hk_mask: Housekeeping cpumask for HK_TYPE_MANAGED_IRQ +* @hk_enabled: True when HK_TYPE_MANAGED_IRQ isolation is active +* +* Confine unmanaged pre/post vectors (e.g. the NVMe admin queue) to +* housekeeping CPUs while still honouring irq_default_affinity when the +* intersection contains at least one online CPU. +*/ +static void irq_set_reserved_mask (struct cpumask *dst, +const struct cpumask *hk_mask, +bool hk_enabled) +{ +if (!hk_enabled) { +cpumask_copy (dst, irq_default_affinity); +return ; + } + +if (!cpumask_and (dst, irq_default_affinity, hk_mask) || + !cpumask_intersects (dst, cpu_online_mask)) +cpumask_copy (dst, hk_mask); +} +/** * irq_create_affinity_masks - Create affinity masks for multiqueue spreading * @nvecs: The total number of vectors
@@ -65,7 +89,7 @@ irq_create_affinity_masks(unsigned int nvecs, struct irq_affinity *affd)/* Fill out vectors at the beginning that don't need affinity */ for (curvec =0 ; curvec < affd->pre_vectors; curvec++) -cpumask_copy (&masks[curvec].mask, irq_default_affinity); +irq_set_reserved_mask (&masks[curvec].mask, hk_mask, hk_enabled);/* * Spread on present CPUs starting from affd->pre_vectors. If we
@@ -104,7 +128,7 @@ irq_create_affinity_masks(unsigned int nvecs, struct irq_affinity *affd)else curvec = affd->pre_vectors + usedvecs;for (; curvec < nvecs; curvec++) -cpumask_copy (&masks[curvec].mask, irq_default_affinity); +irq_set_reserved_mask (&masks[curvec].mask, hk_mask, hk_enabled);/* Mark the managed interrupts */ for (i = affd->pre_vectors; i < nvecs - affd->post_vectors; i++)
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v17 10/10] genirq/affinity: Confine pre and post vectors to housekeeping CPUs
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] genirq/affinity: pre/post vector masks lost in irq_update_affinity_desc() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] genirq/affinity: pre/post vector masks lost in irq_update_affinity_desc()
--
commit e45cd1796d752098cee854c7f5d5fc65f6221300
Author: Ionut Nechita <[email protected]>
genirq/affinity: Confine pre and post vectors to housekeeping CPUs
When HK_TYPE_MANAGED_IRQ is enabled, this commit confines unmanaged pre_vectors and post_vectors in irq_create_affinity_masks() to the housekeeping cpumask (intersected with irq_default_affinity) instead of copying irq_default_affinity directly.
Quoted 45 lines
kernel/irq/affinity.c +25
-1@@ -16,6 +16,30 @@ static void default_calc_sets(struct irq_affinity *affd, unsigned int affvecs) affd->set_size[0 ] = affvecs; } +/** +* irq_set_reserved_mask - Populate affinity mask for reserved pre/post vectors +* @dst: Destination cpumask to populate +* @hk_mask: Housekeeping cpumask for HK_TYPE_MANAGED_IRQ +* @hk_enabled: True when HK_TYPE_MANAGED_IRQ isolation is active +* +* Confine unmanaged pre/post vectors (e.g. the NVMe admin queue) to +* housekeeping CPUs while still honouring irq_default_affinity when the +* intersection contains at least one online CPU. +*/ +static void irq_set_reserved_mask (struct cpumask *dst, +const struct cpumask *hk_mask, +bool hk_enabled) +{ +if (!hk_enabled) { +cpumask_copy (dst, irq_default_affinity); +return ; + } + +if (!cpumask_and (dst, irq_default_affinity, hk_mask) || + !cpumask_intersects (dst, cpu_online_mask)) +cpumask_copy (dst, hk_mask); +} +/** * irq_create_affinity_masks - Create affinity masks for multiqueue spreading * @nvecs: The total number of vectors @@ -65,7 +89,7 @@ irq_create_affinity_masks(unsigned int nvecs, struct irq_affinity *affd)/* Fill out vectors at the beginning that don't need affinity */ for (curvec =0 ; curvec < affd->pre_vectors; curvec++) -cpumask_copy (&masks[curvec].mask, irq_default_affinity); +irq_set_reserved_mask (&masks[curvec].mask, hk_mask, hk_enabled);/* * Spread on present CPUs starting from affd->pre_vectors. If we
kernel/irq/affinity.c irq_create_affinity_masks(unsigned int nvecs, struct irq_affinity *affd) · line 131
Quoted 5 lines
kernel/irq/affinity.c +1
-1@@ -104,7 +128,7 @@ irq_create_affinity_masks(unsigned int nvecs, struct irq_affinity *affd)else curvec = affd->pre_vectors + usedvecs;for (; curvec < nvecs; curvec++) -cpumask_copy (&masks[curvec].mask, irq_default_affinity); +irq_set_reserved_mask (&masks[curvec].mask, hk_mask, hk_enabled);
[Severity: Medium]
Are the housekeeping-confined masks for pre_vectors and post_vectors discarded on the platform IRQ path?
Unlike alloc_descs(), which sets IRQD_AFFINITY_SET on every descriptor initialized from a struct irq_affinity_desc, devm_platform_get_irqs_affinity() applies the masks via irq_update_affinity_desc(), which does not set IRQD_AFFINITY_SET when affinity->is_managed is false:
kernel/irq/manage.c:irq_update_affinity_desc() {
...
if (affinity->is_managed) {
irqd_set(&desc->irq_data, IRQD_AFFINITY_MANAGED);
irqd_set(&desc->irq_data, IRQD_MANAGED_SHUTDOWN);
}
cpumask_copy(desc->irq_common_data.affinity, &affinity->mask);
...
}When a platform driver requests and starts an unmanaged pre/post vector, irq_startup() -> irq_setup_affinity() sees neither IRQD_AFFINITY_MANAGED nor IRQD_AFFINITY_SET and overwrites desc->irq_common_data.affinity with irq_default_affinity, allowing the interrupt to target isolated CPUs. Setting IRQD_AFFINITY_SET in irq_update_affinity_desc() to match alloc_descs() would preserve the confined mask on startup.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
[email protected]
AI reviewSashiko has finished reviewing this 10-patch series:
Sashiko has finished reviewing this 10-patch series:
- Patches 1, 2, 4, 7, and 9: no issues found.
- Patch 3 ("lib/group_cpus: Add group_mask_cpus_evenly()"): 1 potential issue (1 Low)
- Patch 5 ("blk-mq: use hk cpus only when isolcpus=managed_irq i..."): 2 potential issues (1 High, 1 Low)
- Patch 6 ("blk-mq: prevent offlining hk CPUs with associated on..."): 2 potential issues (1 High, 1 Medium)
- Patch 8 ("genirq/affinity: Restrict managed IRQ affinity to ho..."): 1 potential issue (1 High)
- Patch 10 ("genirq/affinity: Confine pre and post vectors to hou..."):
1 potential issue (1 Medium)
Detailed reviews have been sent as separate replies to patches 3, 5, 6, 8, and 10.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]