dm: pci: fix uninitialized fdt_pci_addr fall-through in pci_get_devfn
PATCH 1/1 dm: pci: fix uninitialized fdt_pci_addr fall-through in pci_get_devfn
From: Dustin Kirkland <[email protected]>
pci_get_devfn() reads addr.phys_hi and returns the low bits of it as the requested devfn even on the -ENOENT branch, where ofnode_read_pci_addr() has NOT written *addr. The C standard leaves that read undefined; in practice the returned value depends on the compilers stack layout and on -ftrivial-auto-var-init. That value ends up in pplat->devfn (set by pci_uclass_child_post_bind()) and is later compared for equality in pci_bus_find_devfn() during PCI enumeration, so an under-determined value produces an under-determined driver-binding outcome.
Concretely, this fires on Raspberry Pi 5 (BCM2712) with vendor U-Boot v2026.07 when the tree is compiled with GCC -ftrivial-auto-var-init=zero. In that build addr.phys_hi is zeroed rather than left as stack junk, so pci_get_devfn() returns 0 for every DT-declared non-PCI child of a PCI bus. The Pi 5 device tree includes one such child under the second root complex -- the rp1 simple-bus node representing the on-SoC RP1 south bridge as seen from the OS side. Under zero-init:
* pci_uclass_child_post_bind(rp1) sets pplat->devfn = 0
* pci_bind_bus_devices() of the second root complex reads vendor at
bdf 02:00.0, calls pci_bus_find_devfn(bus, 0x0000, &dev)
* pci_bus_find_devfn() finds rp1 with pplat->devfn == 0x0000 == the requested devfn, returns it as the pre-bound match
* pci_find_and_bind_driver() is skipped; the RP1 root port is never bound as pci_bridge_drv and its downstream bus is never enumerated
* dm_pciauto_postscan_setup_bridge() then writes PCI_SUBORDINATE_BUS = 0 and PCI_MEMORY_LIMIT = 0 on the root port; Linux flags the bridge as "bridge configuration invalid ([bus 01-00])", the rp1 driver fails to enable the endpoint with -EINVAL, and every RP1-hosted peripheral (USB, onboard Ethernet, ttyAMA10) is non-functional for the rest of boot.
Under the default -ftrivial-auto-var-init=uninitialized, addr.phys_hi happens to be non-zero stack residue that does not collide with the requested devfn, so the same code path just returns -ENODEV from pci_bus_find_devfn() and pci_find_and_bind_driver() correctly runs the fallback. The bug has been latent since introduction; -zero exposes it deterministically.
The only observable behaviour change is that DT-declared non-PCI children of PCI buses now consistently report -EINVAL from pci_get_devfn() rather than a value that depends on stack state.
--- drivers/core/util.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/core/util.c +2 -4
@@ -20,10 +20,8 @@ int pci_get_devfn(struct udevice *dev)/* Extract the devfn from fdt_pci_addr */ ret =ofnode_read_pci_addr (dev_ofnode (dev),FDT_PCI_SPACE_CONFIG ,"reg" , &addr,NULL ); -if (ret) { -if (ret != -ENOENT ) -return -EINVAL ; - } +if (ret) +return -EINVAL ;return addr.phys_hi &0xff00 ; }
Signature · 1 line
2.43.0
Hi Dustin,
Quoted 1 line · Scott Moser
dm: pci: fix uninitialized
fdt_pci_addrfall-through inpci_get_devfn
Thanks for tracking this down.
Quoted 21 lines
pci_get_devfn()reads addr.phys_hi and returns the low bits of it as the requested devfn even on the-ENOENTbranch, whereofnode_read_pci_addr()has NOT written *addr. The C standard leaves that read undefined; in practice the returned value depends on the compilers stack layout and on -ftrivial-auto-var-init. That value ends up inpplat->devfn(set bypci_uclass_child_post_bind()) and is later compared for equality inpci_bus_find_devfn()during PCI enumeration, so an under-determined value produces an under-determined driver-binding outcome.Concretely, this fires on Raspberry Pi 5 (BCM2712) with vendor U-Boot v2026.07 when the tree is compiled with GCC -ftrivial-auto-var-init=zero. In that build addr.phys_hi is zeroed rather than left as stack junk, so
pci_get_devfn()returns 0 for every DT-declared non-PCI child of a PCI bus. The Pi 5 device tree includes one such child under the second root complex -- the rp1 simple-bus node representing the on-SoC RP1 south bridge as seen from the OS side. Under zero-init:*
pci_uclass_child_post_bind(rp1)setspplat->devfn= 0
[...]
Quoted 8 lines
drivers/core/util.c| 6 ++----1 file changed, 2 insertions(+), 4 deletions(-)Reported-by Dustin Kirkland <[email protected]>Fixes b52142004fbd ("pci: Add pci_get_devfn() to extract devfn from the fdt_pci_addr")Signed-off-by Dustin Kirkland <[email protected]>Investigated-by Claude Opus 4.7 <[email protected]>Co-authored-by Claude Opus 4.7 <[email protected]>
This is a good find!
Please drop Reported-by when the reporter is also the Signed-off-by - also U-Boot doesn't accept AI contributions, so probably best to just remove those tags. Also not that useful to mention a vendor tree - we mostly just worry about mainline here.
Quoted 4 lines
drivers/core/util.c +0
-0@@ -20,10 +20,8 @@ int pci_get_devfn(struct udevice *dev)/* Extract the devfn from fdt_pci_addr */ ret = ofnode_read_pci_addr(dev_ofnode(dev), FDT_PCI_SPACE_CONFIG,
drivers/core/util.c int pci_get_devfn(struct udevice *dev) · line 27
Quoted 10 lines
drivers/core/util.c +2
-4"reg", &addr, NULL); -if (ret) { -if (ret != -ENOENT ) -return -EINVAL ; - } +if (ret) +return -EINVAL ;return addr.phys_hi &0xff00 ; }
The fix is right, but the kerneldoc in include/dm/pci.h still says:
Return: devfn in bits 15...8 if found (pci_dev_t format), or -ENODEV if not
found
Since this patch makes the error path observable, please either update that comment to match (-EINVAL on any lookup failure) or return -ENODEV for the ret == -ENOENT case so the documented contract holds. I'd lean towards -ENODEV for the not-present case and -EINVAL only for a malformed reg - that also reads more naturally at the callers in arch/x86/cpu/apollolake/{pmc,hostbridge}.c and intel_common/p2sb.c, which only test < 0. What do you think?
Regards,
Simon
[1] https://lore.kernel.org/u-boot/20251113133038.GF6688@bill-the-cat/
PATCH v2 1/1 dm: pci: fix uninitialized fdt_pci_addr fall-through in pci_get_devfn
In reply to Scott MoserFrom: Dustin Kirkland <[email protected]>
pci_get_devfn() reads addr.phys_hi and returns the low bits of it as the requested devfn even on the -ENOENT branch, where ofnode_read_pci_addr() has NOT written *addr. The C standard leaves that read undefined; in practice the returned value depends on the compilers stack layout and on -ftrivial-auto-var-init. That value ends up in pplat->devfn (set by pci_uclass_child_post_bind()) and is later compared for equality in pci_bus_find_devfn() during PCI enumeration, so an under-determined value produces an under-determined driver-binding outcome.
Concretely, this fires on Raspberry Pi 5 (BCM2712) when built with GCC -ftrivial-auto-var-init=zero. In that build addr.phys_hi is zeroed rather than left as stack junk, so pci_get_devfn() returns 0 for every DT-declared non-PCI child of a PCI bus. The Pi 5 device tree includes one such child under the second root complex -- the rp1 simple-bus node representing the on-SoC RP1 south bridge as seen from the OS side. Under zero-init:
* pci_uclass_child_post_bind(rp1) sets pplat->devfn = 0
* pci_bind_bus_devices() of the second root complex reads vendor at
bdf 02:00.0, calls pci_bus_find_devfn(bus, 0x0000, &dev)
* pci_bus_find_devfn() finds rp1 with pplat->devfn == 0x0000 == the requested devfn, returns it as the pre-bound match
* pci_find_and_bind_driver() is skipped; the RP1 root port is never bound as pci_bridge_drv and its downstream bus is never enumerated
* dm_pciauto_postscan_setup_bridge() then writes PCI_SUBORDINATE_BUS = 0 and PCI_MEMORY_LIMIT = 0 on the root port; Linux flags the bridge as "bridge configuration invalid ([bus 01-00])", the rp1 driver fails to enable the endpoint with -EINVAL, and every RP1-hosted peripheral (USB, onboard Ethernet, ttyAMA10) is non-functional for the rest of boot.
Under the default -ftrivial-auto-var-init=uninitialized, addr.phys_hi happens to be non-zero stack residue that does not collide with the requested devfn, so the same code path just returns -ENODEV from pci_bus_find_devfn() and pci_find_and_bind_driver() correctly runs the fallback. The bug has been latent since introduction; -zero exposes it deterministically.
A "reg" property that is simply absent now consistently returns -ENODEV, matching the documented contract in include/dm/pci.h, instead of a value that depends on stack state. A malformed "reg" property still returns -EINVAL, as it did before this fix.
--- drivers/core/util.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/core/util.c +4 -4
@@ -20,10 +20,10 @@ int pci_get_devfn(struct udevice *dev)/* Extract the devfn from fdt_pci_addr */ ret =ofnode_read_pci_addr (dev_ofnode (dev),FDT_PCI_SPACE_CONFIG ,"reg" , &addr,NULL ); -if (ret) { -if (ret != -ENOENT ) -return -EINVAL ; - } +if (ret == -ENOENT ) +return -ENODEV ; +if (ret) +return -EINVAL ;return addr.phys_hi &0xff00 ; }
Signature · 1 line
2.43.0
Quoted 45 lines · Scott Moser
dm: pci: fix uninitialized
fdt_pci_addrfall-through inpci_get_devfn
pci_get_devfn()reads addr.phys_hi and returns the low bits of it as the requested devfn even on the-ENOENTbranch, whereofnode_read_pci_addr()has NOT written *addr. The C standard leaves that read undefined; in practice the returned value depends on the compilers stack layout and on -ftrivial-auto-var-init. That value ends up inpplat->devfn(set bypci_uclass_child_post_bind()) and is later compared for equality inpci_bus_find_devfn()during PCI enumeration, so an under-determined value produces an under-determined driver-binding outcome.Concretely, this fires on Raspberry Pi 5 (BCM2712) when built with GCC -ftrivial-auto-var-init=zero. In that build addr.phys_hi is zeroed rather than left as stack junk, so
pci_get_devfn()returns 0 for every DT-declared non-PCI child of a PCI bus. The Pi 5 device tree includes one such child under the second root complex -- the rp1 simple-bus node representing the on-SoC RP1 south bridge as seen from the OS side. Under zero-init:*
pci_uclass_child_post_bind(rp1)setspplat->devfn= 0
*pci_bind_bus_devices()of the second root complex reads vendor atbdf 02:00.0, calls pci_bus_find_devfn(bus, 0x0000, &dev)*
pci_bus_find_devfn()finds rp1 withpplat->devfn==0x0000== the requested devfn, returns it as the pre-bound match
*pci_find_and_bind_driver()is skipped; the RP1 root port is never bound aspci_bridge_drvand its downstream bus is never enumerated
*dm_pciauto_postscan_setup_bridge()then writesPCI_SUBORDINATE_BUS= 0 andPCI_MEMORY_LIMIT= 0 on the root port; Linux flags the bridge as "bridge configuration invalid ([bus 01-00])", the rp1 driver fails to enable the endpoint with-EINVAL, and every RP1-hosted peripheral (USB, onboard Ethernet, ttyAMA10) is non-functional for the rest of boot.Under the default -ftrivial-auto-var-init=uninitialized, addr.phys_hi happens to be non-zero stack residue that does not collide with the requested devfn, so the same code path just returns
-ENODEVfrompci_bus_find_devfn()andpci_find_and_bind_driver()correctly runs the fallback. The bug has been latent since introduction; -zero exposes it deterministically.A "reg" property that is simply absent now consistently returns
-ENODEV, matching the documented contract ininclude/dm/pci.h, instead of a value that depends on stack state. A malformed "reg" property still returns-EINVAL, as it did before this fix.
Quoted 6 lines
Fixes b52142004fbd ("pci: Add pci_get_devfn() to extract devfn from the fdt_pci_addr")Signed-off-by Dustin Kirkland <[email protected]>Cc Scott Moser <[email protected]>
drivers/core/util.c| 8 ++++----1 file changed, 4 insertions(+), 4 deletions(-)
Quoted 61 lines · Scott Moser
From: Dustin Kirkland <[email protected]>
pci_get_devfn()reads addr.phys_hi and returns the low bits of it as the requested devfn even on the-ENOENTbranch, whereofnode_read_pci_addr()has NOT written *addr. The C standard leaves that read undefined; in practice the returned value depends on the compilers stack layout and on -ftrivial-auto-var-init. That value ends up inpplat->devfn(set bypci_uclass_child_post_bind()) and is later compared for equality inpci_bus_find_devfn()during PCI enumeration, so an under-determined value produces an under-determined driver-binding outcome.Concretely, this fires on Raspberry Pi 5 (BCM2712) when built with GCC -ftrivial-auto-var-init=zero. In that build addr.phys_hi is zeroed rather than left as stack junk, so
pci_get_devfn()returns 0 for every DT-declared non-PCI child of a PCI bus. The Pi 5 device tree includes one such child under the second root complex -- the rp1 simple-bus node representing the on-SoC RP1 south bridge as seen from the OS side. Under zero-init:*
pci_uclass_child_post_bind(rp1)setspplat->devfn= 0
*pci_bind_bus_devices()of the second root complex reads vendor atbdf 02:00.0, calls pci_bus_find_devfn(bus, 0x0000, &dev)*
pci_bus_find_devfn()finds rp1 withpplat->devfn==0x0000== the requested devfn, returns it as the pre-bound match
*pci_find_and_bind_driver()is skipped; the RP1 root port is never bound aspci_bridge_drvand its downstream bus is never enumerated
*dm_pciauto_postscan_setup_bridge()then writesPCI_SUBORDINATE_BUS= 0 andPCI_MEMORY_LIMIT= 0 on the root port; Linux flags the bridge as "bridge configuration invalid ([bus 01-00])", the rp1 driver fails to enable the endpoint with-EINVAL, and every RP1-hosted peripheral (USB, onboard Ethernet, ttyAMA10) is non-functional for the rest of boot.Under the default -ftrivial-auto-var-init=uninitialized, addr.phys_hi happens to be non-zero stack residue that does not collide with the requested devfn, so the same code path just returns
-ENODEVfrompci_bus_find_devfn()andpci_find_and_bind_driver()correctly runs the fallback. The bug has been latent since introduction; -zero exposes it deterministically.A "reg" property that is simply absent now consistently returns
-ENODEV, matching the documented contract ininclude/dm/pci.h, instead of a value that depends on stack state. A malformed "reg" property still returns-EINVAL, as it did before this fix.Fixes b52142004fbd ("pci: Add pci_get_devfn() to extract devfn from the fdt_pci_addr")Signed-off-by Dustin Kirkland <[email protected]>Cc Scott Moser <[email protected]>--- drivers/core/util.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)drivers/core/util.c +0
-1@@ -20,10 +20,10 @@ int pci_get_devfn(struct udevice *dev)/* Extract the devfn from fdt_pci_addr */ ret =ofnode_read_pci_addr (dev_ofnode (dev),FDT_PCI_SPACE_CONFIG ,"reg" , &addr,NULL ); -if (ret) {
drivers/core/util.c int pci_get_devfn(struct udevice *dev) · line 29
Quoted 10 lines
drivers/core/util.c +4
-3-if (ret != -ENOENT ) -return -EINVAL ; - } +if (ret == -ENOENT ) +return -ENODEV ; +if (ret) +return -EINVAL ;return addr.phys_hi &0xff00 ; }
Thanks,
Neil
Hi,
Quoted 10 lines · Scott Moser
pci_get_devfn()reads addr.phys_hi and returns the low bits of it as the requested devfn even on the-ENOENTbranch, whereofnode_read_pci_addr()has NOT written *addr. The C standard leaves that read undefined; in practice the returned value depends on the compilers stack layout and on -ftrivial-auto-var-init. That value ends up inpplat->devfn(set bypci_uclass_child_post_bind()) and is later compared for equality inpci_bus_find_devfn()during PCI enumeration, so an under-determined value produces an under-determined driver-binding outcome.[...]
Thanks, Applied to https://source.denx.de/u-boot/custodians/u-boot-ufs (u-boot-pci-fixes)
[1/1] dm: pci: fix uninitialized fdt_pci_addr fall-through in pci_get_devfn
https://source.denx.de/u-boot/custodians/u-boot-ufs/-/commit/85a7290d4bc976a57c7ecbcfcc3d6aa67665ffaf
Signature · 1 line
Neil