Linux PCI subsystem development
 help / color / mirror / Atom feed
* [PATCH] PCI: mediatek: Fix integer truncation in fls() for resource size
@ 2026-09-30  9:49 Sreeraj S Kurup
  2026-09-30 10:03 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Sreeraj S Kurup @ 2026-09-30  9:49 UTC (permalink / raw)
  To: Ryder Lee, Lorenzo Pieralisi, Krzysztof Wilczyński,
	Manivannan Sadhasivam, Rob Herring, Bjorn Helgaas,
	Matthias Brugger, AngeloGioacchino Del Regno
  Cc: linux-pci, linux-mediatek, linux-kernel, linux-arm-kernel,
	Sreeraj S Kurup

resource_size() returns a resource_size_t, which is 64-bit on 64-bit
architectures or 32-bit systems with LPAE/PAE enabled. Passing this
directly to fls(), which accepts an unsigned int, implicitly truncates
the upper 32 bits.

If a memory resource window of 4 GiB or larger is passed, the size
truncates to 0, resulting in invalid AHB-to-PCIe translation window
sizes.

Fix this by using fls64() to safely handle 64-bit resource sizes.

Signed-off-by: Sreeraj S Kurup <sreekuttan2156239@gmail.com>
---
 drivers/pci/controller/pcie-mediatek.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/pci/controller/pcie-mediatek.c b/drivers/pci/controller/pcie-mediatek.c
index a60d1ae076f8..61591d0176bc 100644
--- a/drivers/pci/controller/pcie-mediatek.c
+++ b/drivers/pci/controller/pcie-mediatek.c
@@ -8,6 +8,7 @@
  */
 
 #include <linux/bitfield.h>
+#include <linux/bitops.h>
 #include <linux/clk.h>
 #include <linux/delay.h>
 #include <linux/errno.h>
@@ -754,7 +755,7 @@ static int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port)
 
 	/* Set AHB to PCIe translation windows */
 	val = lower_32_bits(mem->start) |
-	      AHB2PCIE_SIZE(fls(resource_size(mem)));
+	      AHB2PCIE_SIZE(fls64(resource_size(mem)));
 	writel(val, port->base + PCIE_AHB_TRANS_BASE0_L);
 
 	val = upper_32_bits(mem->start);
@@ -830,7 +831,7 @@ static int mtk_pcie_startup_port_en7528(struct mtk_pcie_port *port)
 
 	/* Set AHB to PCIe translation windows */
 	val = lower_32_bits(mem->start) |
-	      AHB2PCIE_SIZE(fls(resource_size(mem)));
+	      AHB2PCIE_SIZE(fls64(resource_size(mem)));
 	writel(val, port->base + PCIE_AHB_TRANS_BASE0_L);
 
 	val = upper_32_bits(mem->start);
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] PCI: mediatek: Fix integer truncation in fls() for resource size
  2026-09-30  9:49 [PATCH] PCI: mediatek: Fix integer truncation in fls() for resource size Sreeraj S Kurup
@ 2026-09-30 10:03 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-09-30 10:03 UTC (permalink / raw)
  To: Sreeraj S Kurup; +Cc: linux-pci

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] PCI: mediatek: incomplete fix for 4GiB resource size due to macro mask
--

commit 790d994ea56f0eba397f9698ea9390e70f3a8d1c
Author: Sreeraj S Kurup <sreekuttan2156239@gmail.com>

PCI: mediatek: Fix integer truncation in fls() for resource size

This commit replaces fls() with fls64() when calculating the AHB-to-PCIe
translation window size to properly handle 64-bit memory resources.

> diff --git a/drivers/pci/controller/pcie-mediatek.c b/drivers/pci/controller/pcie-mediatek.c
> --- a/drivers/pci/controller/pcie-mediatek.c
> +++ b/drivers/pci/controller/pcie-mediatek.c
[ ... ]
> @@ -754,7 +755,7 @@ static int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port)
>  
>  	/* Set AHB to PCIe translation windows */
>  	val = lower_32_bits(mem->start) |
> -	      AHB2PCIE_SIZE(fls(resource_size(mem)));
> +	      AHB2PCIE_SIZE(fls64(resource_size(mem)));

[Severity: High]
Will this still result in a truncated window size when using a 4 GiB resource?

Looking at the macro definition in drivers/pci/controller/pcie-mediatek.c:

#define AHB2PCIE_SIZE(x)    ((x) & GENMASK(4, 0))

For a 4 GiB memory resource, fls64() returns 33. The 5-bit mask in 
AHB2PCIE_SIZE() restricts values to 0-31, which truncates 33 down to 1.
Could this result in an incorrect window size being programmed, causing memory
accesses across the bridge to fail?

>  	writel(val, port->base + PCIE_AHB_TRANS_BASE0_L);
>  
>  	val = upper_32_bits(mem->start);
[ ... ]
> @@ -830,7 +831,7 @@ static int mtk_pcie_startup_port_en7528(struct mtk_pcie_port *port)
>  
>  	/* Set AHB to PCIe translation windows */
>  	val = lower_32_bits(mem->start) |
> -	      AHB2PCIE_SIZE(fls(resource_size(mem)));
> +	      AHB2PCIE_SIZE(fls64(resource_size(mem)));

[Severity: High]
Is this instance in mtk_pcie_startup_port_en7528() affected by the same
truncation issue through the AHB2PCIE_SIZE() macro?

>  	writel(val, port->base + PCIE_AHB_TRANS_BASE0_L);
>  
>  	val = upper_32_bits(mem->start);

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260930094912.3069-1-sreekuttan2156239@gmail.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-09-30 10:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-30  9:49 [PATCH] PCI: mediatek: Fix integer truncation in fls() for resource size Sreeraj S Kurup
2026-09-30 10:03 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox