scsi: mpt3sas: avoid out-of-bounds cpumask_of_node() call in _base_assign_reply_queues()
Subsystems (2)
PATCH 1/1 scsi: mpt3sas: avoid out-of-bounds cpumask_of_node() call in _base_assign_reply_queues()
dev_to_node() can return NUMA_NO_NODE (-1) on systems without NUMA topology information for the PCI device, such as single-socket boards that don't expose device-to-node affinity. Passing -1 directly into cpumask_of_node() indexes node_to_cpumask_map[-1], an out-of-bounds array read caught by UBSAN:
UBSAN: array-index-out-of-bounds in arch/x86/include/asm/topology.h:72:28 index -1 is out of range for type 'cpumask *[1024]'
Fall back to cpu_online_mask when no NUMA node is available, rather than assuming dev_to_node() always returns a valid node index.
--- drivers/scsi/mpt3sas/mpt3sas_base.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/scsi/mpt3sas/mpt3sas_base.c +3 -1
@@ -3238,7 +3238,9 @@ _base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc)* corresponding to high iops queues. */ if (ioc->high_iops_queues) { - mask =cpumask_of_node (dev_to_node (&ioc->pdev->dev)); +int node = (dev_to_node (&ioc->pdev->dev)); + + mask = (node ==NUMA_NO_NODE ) ? cpu_online_mask :cpumask_of_node (node);for (index =0 ; index < ioc->high_iops_queues; index++) { irq =pci_irq_vector (ioc->pdev, index);
Signature · 1 line
2.55.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Incorrect calculation of remaining MSI-X queues causes a Division by Zero kernel panic or severe IO imbalance. --
--- Patch [1]: [PATCH] scsi: mpt3sas: avoid out-of-bounds cpumask_of_node() call in _base_assign_reply_queues() --- commit 2577a38cb04664cd3aa266df2234da5f8d4aff10
Author: Ivy Lopez <[email protected]>
scsi: mpt3sas: avoid out-of-bounds cpumask_of_node() call in _base_assign_reply_queues()
Fixes an out-of-bounds array read in cpumask_of_node() by checking if dev_to_node() returns NUMA_NO_NODE. If so, it falls back to cpu_online_mask instead of using the invalid NUMA node index.
Quoted 5 lines
drivers/scsi/mpt3sas/mpt3sas_base.c +0
-0
drivers/scsi/mpt3sas/mpt3sas_base.c _base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc) · line 3246
Quoted 10 lines
drivers/scsi/mpt3sas/mpt3sas_base.c +3
-1@@ -3238,7 +3238,9 @@ _base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc)* corresponding to high iops queues. */ if (ioc->high_iops_queues) { - mask =cpumask_of_node (dev_to_node (&ioc->pdev->dev)); +int node = (dev_to_node (&ioc->pdev->dev)); + + mask = (node ==NUMA_NO_NODE ) ? cpu_online_mask :cpumask_of_node (node);for (index =0 ; index < ioc->high_iops_queues; index++) { irq =pci_irq_vector (ioc->pdev, index);
[ ... ]
[Severity: High]
This is a pre-existing issue, but I noticed a potential division by zero later in drivers/scsi/mpt3sas/mpt3sas_base.c:_base_assign_reply_queues():
fall_back:
cpu = cpumask_first(cpu_online_mask);
nr_msix -= (ioc->high_iops_queues - iopoll_q_count);
index = 0;
list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
unsigned int i, group = nr_cpus / nr_msix;Can this result in a division by zero panic?
If iopoll_q_count == 0 and all available vectors are assigned to high_iops_queues, nr_msix becomes 0. The list loop unconditionally computes group = nr_cpus / nr_msix before checking if the queue is a default queue, triggering a division by zero.
Additionally, does the fallback logic incorrectly add the poll queues instead of subtracting them when iopoll_q_count > 0? This would shrink the CPU group size and could cause unassigned CPUs to fall back to a single queue.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 23 lines · Ivy Lopez
dev_to_node()can returnNUMA_NO_NODE(-1) on systems without NUMA topology information for the PCI device, such as single-socket boards that don't expose device-to-node affinity. Passing -1 directly intocpumask_of_node()indexes node_to_cpumask_map[-1], an out-of-bounds array read caught by UBSAN:UBSAN: array-index-out-of-bounds in arch/x86/include/asm/topology.h:72:28 index -1 is out of range for type 'cpumask *[1024]'Fall back to
cpu_online_maskwhen no NUMA node is available, rather than assumingdev_to_node()always returns a valid node index.--- drivers/scsi/mpt3sas/mpt3sas_base.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/scsi/mpt3sas/mpt3sas_base.c +0
-0
drivers/scsi/mpt3sas/mpt3sas_base.c _base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc) · line 3241
Quoted 5 lines
drivers/scsi/mpt3sas/mpt3sas_base.c +1
-1@@ -3238,7 +3238,9 @@ _base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc)* corresponding to high iops queues. */ if (ioc->high_iops_queues) { - mask =cpumask_of_node (dev_to_node (&ioc->pdev->dev)); +int node = (dev_to_node (&ioc->pdev->dev));
Why the superfluous parenthesis?
drivers/scsi/mpt3sas/mpt3sas_base.c _base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc) · line 3243
Quoted 2 lines
drivers/scsi/mpt3sas/mpt3sas_base.c +2
-0+ + mask = (node ==NUMA_NO_NODE ) ? cpu_online_mask :cpumask_of_node (node);
Overly long line here.
Quoted 7 lines
drivers/scsi/mpt3sas/mpt3sas_base.c +0
-0for (index =0 ; index < ioc->high_iops_queues; index++) { irq =pci_irq_vector (ioc->pdev, index);Signature · 1 line
2.55.0
Quoted 3 lines · Ivy Lopez
dev_to_node()can returnNUMA_NO_NODE(-1) on systems without NUMA topology information for the PCI device, such as single-socket boards that don't expose device-to-node affinity.
I think that this problem would have appeared by now for this driver (if it were really a problem).
However, for CONFIG_NUMA=n dev_to_node() always returns NUMA_NO_NODE.
Quoted 6 lines
Passing -1
directly intocpumask_of_node()indexes node_to_cpumask_map[-1], an out-of-bounds array read caught by UBSAN:UBSAN: array-index-out-of-bounds in arch/x86/include/asm/topology.h:72:28 index -1 is out of range for type 'cpumask *[1024]'
x86 seems to be only arch which has this problem now.
I have tried unsuccessfully to fix it:
https://lore.kernel.org/lkml/[email protected]/
PATCH v2 1/1 scsi: mpt3sas: avoid out-of-bounds cpumask_of_node() call in _base_assign_reply_queues()
In reply to Ivy Lopezdev_to_node() can return NUMA_NO_NODE (-1) on systems without NUMA topology information for the PCI device, such as single-socket boards that don't expose device-to-node affinity. Passing -1 directly into cpumask_of_node() indexes node_to_cpumask_map[-1], an out-of-bounds array read caught by UBSAN:
UBSAN: array-index-out-of-bounds in arch/x86/include/asm/topology.h:72:28 index -1 is out of range for type 'cpumask *[1024]'
Fall back to cpu_online_mask when no NUMA node is available, rather than assuming dev_to_node() always returns a valid node index.
---
Changes in v2:
- Drop superfluous parentheses around dev_to_node() call.
- Split overly long ternary assignment line.
- Add Fixes: tag per checkpatch --strict suggestion.
- Link to v1: https://lore.kernel.org/r/[email protected]
drivers/scsi/mpt3sas/mpt3sas_base.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/scsi/mpt3sas/mpt3sas_base.c +4 -1
@@ -3238,7 +3238,10 @@ _base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc)* corresponding to high iops queues. */ if (ioc->high_iops_queues) { - mask =cpumask_of_node (dev_to_node (&ioc->pdev->dev)); +int node =dev_to_node (&ioc->pdev->dev); + + mask = (node ==NUMA_NO_NODE ) ? + cpu_online_mask :cpumask_of_node (node);for (index =0 ; index < ioc->high_iops_queues; index++) { irq =pci_irq_vector (ioc->pdev, index);
Signature · 1 line
2.55.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but incorrect subtraction in the fall_back path causes unassigned CPUs in the msix table and a potential divide by zero if nr_msix evaluates to zero. --
commit 9e90c3b985462523aebc40d2af67de713d3be849
Author: Ivy Lopez <[email protected]>
scsi: mpt3sas: avoid out-of-bounds cpumask_of_node() call in _base_assign_reply_queues()
dev_to_node() can return NUMA_NO_NODE (-1) on systems without NUMA topology information for the PCI device, such as single-socket boards that don't expose device-to-node affinity. Fall back to cpu_online_mask when no NUMA node is available, rather than assuming dev_to_node() always returns a valid node index.
Quoted 6 lines
drivers/scsi/mpt3sas/mpt3sas_base.c +0
-0@@ -3238,7 +3238,10 @@ _base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc)* corresponding to high iops queues.
drivers/scsi/mpt3sas/mpt3sas_base.c _base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc) · line 3247
Quoted 10 lines
drivers/scsi/mpt3sas/mpt3sas_base.c +4
-1*/ if (ioc->high_iops_queues) { - mask =cpumask_of_node (dev_to_node (&ioc->pdev->dev)); +int node =dev_to_node (&ioc->pdev->dev); + + mask = (node ==NUMA_NO_NODE ) ? + cpu_online_mask :cpumask_of_node (node);for (index =0 ; index < ioc->high_iops_queues; index++) { irq =pci_irq_vector (ioc->pdev, index);
[Severity: High]
This isn't a bug introduced by this patch, but incorrect subtraction in the fall_back path causes unassigned CPUs in the msix table and a potential divide by zero.
In _base_assign_reply_queues(), the calculation evaluates incorrectly:
drivers/scsi/mpt3sas/mpt3sas_base.c:_base_assign_reply_queues() {
...fall_back:
cpu = cpumask_first(cpu_online_mask);
nr_msix -= (ioc->high_iops_queues - iopoll_q_count);
index = 0;
list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
unsigned int i, group = nr_cpus / nr_msix;...
}
If iopoll_q_count > 0, the calculation nr_msix -= (ioc->high_iops_queues - iopoll_q_count) evaluates to nr_msix - high_iops_queues + iopoll_q_count.
This mathematically adds iopoll_q_count instead of subtracting it, inflating nr_msix. This causes group = nr_cpus / nr_msix to calculate a smaller group size, leaving trailing CPUs mapped to vector 0.
Furthermore, if PCI MSI-X allocation limits nr_msix to exactly high_iops_queues (e.g. system interrupt limits), nr_msix evaluates to 0.
This would trigger a hardware divide-by-zero exception when evaluating nr_cpus / nr_msix.
Is it possible to rewrite this subtraction to avoid inflating nr_msix and prevent a potential divide-by-zero?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 10 lines · Ivy Lopez
dev_to_node()can returnNUMA_NO_NODE(-1) on systems without NUMA topology information for the PCI device, such as single-socket boards that don't expose device-to-node affinity. Passing -1 directly intocpumask_of_node()indexes node_to_cpumask_map[-1], an out-of-bounds array read caught by UBSAN:UBSAN: array-index-out-of-bounds in arch/x86/include/asm/topology.h:72:28 index -1 is out of range for type 'cpumask *[1024]'[...]
Applied to 7.3/scsi-fixes, thanks!
[1/1] scsi: mpt3sas: avoid out-of-bounds cpumask_of_node() call in _base_assign_reply_queues()
https://git.kernel.org/mkp/scsi/c/e0d26fe176a8
Signature · 1 line
Martin K. Petersen