Skip to content

Commit c4086c6

Browse files
liyouhongfloatious
authored andcommitted
ata: ahci: fail probe if BAR too small for claimed ports
When an AHCI controller is disabled in BIOS, its HOST_CAP register may contain a bogus value, e.g. 0xFFFFFFFF. Since CAP.NP (Number of Ports) is a zeroes based 5-bit register field, a value of 0x1f means 32 ports. If CAP.NP claims more ports than can physically fit within the mapped BAR region, accessing port registers beyond the BAR boundary causes a kernel panic. Add validation in ahci_init_one() to check that the BAR size is sufficient for the number of ports claimed in CAP.NP. The check calculates the required MMIO size as: required_size = 0x100 (global registers) + max_ports * 0x80 If required_size exceeds the actual BAR size, the probe fails with -ENODEV, preventing the panic and providing a clear error message. Reported-by: liyouhong <liyouhong@kylinos.cn> Closes: https://lore.kernel.org/all/20260422080322.1006592-1-dayou5941@163.com/ Suggested-by: Damien Le Moal <dlemoal@kernel.org> Suggested-by: Niklas Cassel <cassel@kernel.org> Reviewed-by: Damien Le Moal <dlemoal@kernel.org> Signed-off-by: liyouhong <liyouhong@kylinos.cn> [cassel: commit log] Signed-off-by: Niklas Cassel <cassel@kernel.org>
1 parent 3f8e214 commit c4086c6

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

drivers/ata/ahci.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,6 +1888,24 @@ static ssize_t remapped_nvme_show(struct device *dev,
18881888

18891889
static DEVICE_ATTR_RO(remapped_nvme);
18901890

1891+
static int ahci_validate_bar_size(struct pci_dev *pdev, int bar,
1892+
struct ahci_host_priv *hpriv)
1893+
{
1894+
u32 cap = readl(hpriv->mmio + HOST_CAP);
1895+
unsigned int max_ports = ahci_nr_ports(cap);
1896+
u32 last_port_end = 0x100 + (max_ports * 0x80);
1897+
resource_size_t bar_size = pci_resource_len(pdev, bar);
1898+
1899+
if (last_port_end > bar_size) {
1900+
dev_warn(&pdev->dev,
1901+
"BAR%d too small for %u ports (last port ends at %#x, BAR %pa)\n",
1902+
bar, max_ports, last_port_end, &bar_size);
1903+
return -ENODEV;
1904+
}
1905+
1906+
return 0;
1907+
}
1908+
18911909
static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
18921910
{
18931911
unsigned int board_id = ent->driver_data;
@@ -1988,6 +2006,10 @@ static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
19882006
if (!hpriv->mmio)
19892007
return -ENOMEM;
19902008

2009+
rc = ahci_validate_bar_size(pdev, ahci_pci_bar, hpriv);
2010+
if (rc)
2011+
return rc;
2012+
19912013
/* detect remapped nvme devices */
19922014
ahci_remap_check(pdev, ahci_pci_bar, hpriv);
19932015

0 commit comments

Comments
 (0)