[PATCH v2] phy: berlin-sata: count available child nodes
From: Haoran Wang
Date: Wed Dec 02 2026 - 03:02:44 EST
phy_berlin_sata_probe() sizes priv->nphys with of_get_child_count(), which
counts every child, but fills priv->phys[] only from available children. A
disabled child therefore leaves trailing NULL entries, and
phy_berlin_sata_phy_xlate() dereferences priv->phys[i]->index for them. A
device tree that disables a PHY but still points a consumer at it crashes
with a NULL dereference in the generic ahci_platform path during probe.
Count available children instead so priv->phys[] has no trailing NULL
entries, and bound-check the PHY index in phy_berlin_sata_phy_xlate()
against ARRAY_SIZE(phy_berlin_power_down_bits) rather than priv->nphys:
the index is the hardware PHY number from the reg property and need not be
contiguous when a lower-numbered PHY is disabled.
Fixes: 942a31b52191 ("phy: add a driver for the Berlin SATA PHY")
Signed-off-by: Haoran Wang <wanghr2026@xxxxxxxxxxxxxxxxxx>
---
Changes in v2:
- Bound-check the PHY index in phy_berlin_sata_phy_xlate() against
ARRAY_SIZE(phy_berlin_power_down_bits) rather than priv->nphys, so a
higher-numbered PHY stays addressable when a lower-numbered PHY is
disabled (Sashiko AI review).
- Put the Fixes: tag before Signed-off-by: and generate the patch with
git format-patch (Documentation/process/5.Posting.rst).
drivers/phy/marvell/phy-berlin-sata.c | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/drivers/phy/marvell/phy-berlin-sata.c b/drivers/phy/marvell/phy-berlin-sata.c
index 084671ee7d56..e58c7e7ae2e5 100644
--- a/drivers/phy/marvell/phy-berlin-sata.c
+++ b/drivers/phy/marvell/phy-berlin-sata.c
@@ -154,13 +154,18 @@ static int phy_berlin_sata_power_off(struct phy *phy)
return 0;
}
+static u32 phy_berlin_power_down_bits[] = {
+ POWER_DOWN_PHY0,
+ POWER_DOWN_PHY1,
+};
+
static struct phy *phy_berlin_sata_phy_xlate(struct device *dev,
const struct of_phandle_args *args)
{
struct phy_berlin_priv *priv = dev_get_drvdata(dev);
int i;
- if (WARN_ON(args->args[0] >= priv->nphys))
+ if (WARN_ON(args->args[0] >= ARRAY_SIZE(phy_berlin_power_down_bits)))
return ERR_PTR(-ENODEV);
for (i = 0; i < priv->nphys; i++) {
@@ -180,11 +185,6 @@ static const struct phy_ops phy_berlin_sata_ops = {
.owner = THIS_MODULE,
};
-static u32 phy_berlin_power_down_bits[] = {
- POWER_DOWN_PHY0,
- POWER_DOWN_PHY1,
-};
-
static int phy_berlin_sata_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
@@ -212,7 +212,7 @@ static int phy_berlin_sata_probe(struct platform_device *pdev)
if (IS_ERR(priv->clk))
return PTR_ERR(priv->clk);
- priv->nphys = of_get_child_count(dev->of_node);
+ priv->nphys = of_get_available_child_count(dev->of_node);
if (priv->nphys == 0)
return -ENODEV;
---
PoC reproduction (7.4.0-rc2, arm64 QEMU virt, unmodified kernel):
Device tree injection, spliced into the QEMU virt machine DT:
osc25m: clock-25m { compatible = "fixed-clock"; #clock-cells = <0>;
clock-frequency = <25000000>; };
reserved-memory { #address-cells = <2>; #size-cells = <2>; ranges;
phy-regs@7f100000 { no-map; reg = <0x0 0x7f100000 0x0 0x1000>; };
ahci-regs@7f110000 { no-map; reg = <0x0 0x7f110000 0x0 0x1000>; }; };
sata_phy: sata-phy@7f100000 { compatible = "marvell,berlin2q-sata-phy";
reg = <0x0 0x7f100000 0x0 0x1000>; clocks = <&osc25m>;
#phy-cells = <1>;
phy@0 { reg = <0>; };
phy@1 { reg = <1>; status = "disabled"; }; };
sata@7f110000 { compatible = "generic-ahci"; reg = <0x0 0x7f110000 0x0 0x1000>;
interrupt-parent = <&gpio0>; interrupts = <5 8>; phys = <&sata_phy 1>; };
phy@1 is disabled but the generic-ahci consumer still points at it via
phys = <&sata_phy 1>, so xlate() walks a NULL slot.
Trigger:
modprobe phy-berlin-sata
modprobe ahci_platform
Result:
Unable to handle kernel NULL pointer dereference at virtual address 000000000000000c
pc : phy_berlin_sata_phy_xlate+0x30/0x60 [phy_berlin_sata]
Call trace: ahci_platform_get_phy then ahci_probe
Note: the PoC was generated with AI assistance and the vulnerability was
found by static analysis. The device tree is trusted input (a bad DT
requires root or physical access), so this is not a security fix, but it
prevents a potential resource leak or system failure on a failed probe or
an old or malformed DT.