PCI: mediatek: Fix integer truncation and handle oversized resources
PATCH v4 1/1 PCI: mediatek: Fix integer truncation and handle oversized resources
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.
Furthermore, AHB2PCIE_SIZE() uses a 5-bit mask GENMASK(4, 0). If a resource size exceeds 2 GiB (order > 31), the log2 size order overflows the 5-bit mask. Clamping the value silently truncates the hardware window while leaving the OS resource intact, causing bus errors when accessing BARs in the unmapped upper region.
Fix this by using fls64(size - 1) to accurately calculate log2 size orders without off-by-one errors and returning -EINVAL if the resource size exceeds the maximum supported 31-bit window order.
--- drivers/pci/controller/pcie-mediatek.c | 29 ++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-)
drivers/pci/controller/pcie-mediatek.c +27 -2
@@ -8,6 +8,7 @@*/ #include <linux/bitfield.h> +#include <linux/bitops.h> #include <linux/clk.h> #include <linux/delay.h> #include <linux/errno.h>
@@ -686,6 +687,8 @@ static int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port)const struct mtk_pcie_soc *soc = port->pcie->soc;u32 val;int err; +resource_size_t size; +int size_order; entry =resource_list_first_type (&host->windows,IORESOURCE_MEM );if (entry)
@@ -753,8 +756,18 @@ static int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port)mtk_pcie_enable_msi (port);/* Set AHB to PCIe translation windows */ + size =resource_size (mem); +if (!size) +return -EINVAL ; + + size_order =fls64 (size -1 ); +if (size_order >31 ) { +dev_err (pcie->dev,"Memory resource size too large: %pa\n" , &size); +return -EINVAL ; + } + val =lower_32_bits (mem->start) | -AHB2PCIE_SIZE (fls (resource_size (mem))); +AHB2PCIE_SIZE (size_order);writel (val, port->base +PCIE_AHB_TRANS_BASE0_L ); val =upper_32_bits (mem->start);
@@ -775,6 +788,8 @@ static int mtk_pcie_startup_port_en7528(struct mtk_pcie_port *port)struct resource_entry *entry;u32 val, link_mask;int err; +resource_size_t size; +int size_order; entry =resource_list_first_type (&host->windows,IORESOURCE_MEM );if (entry)
@@ -829,8 +844,18 @@ static int mtk_pcie_startup_port_en7528(struct mtk_pcie_port *port)mtk_pcie_enable_msi (port);/* Set AHB to PCIe translation windows */ + size =resource_size (mem); +if (!size) +return -EINVAL ; + + size_order =fls64 (size -1 ); +if (size_order >31 ) { +dev_err (pcie->dev,"Memory resource size too large: %pa\n" , &size); +return -EINVAL ; + } + val =lower_32_bits (mem->start) | -AHB2PCIE_SIZE (fls (resource_size (mem))); +AHB2PCIE_SIZE (size_order);writel (val, port->base +PCIE_AHB_TRANS_BASE0_L ); val =upper_32_bits (mem->start);
Signature · 1 line
2.55.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines
resource_size()returns aresource_size_t, which is 64-bit on 64-bit architectures or 32-bit systems with LPAE/PAE enabled. Passing this directly tofls(), which accepts an unsigned int, implicitly truncates the upper 32 bits.Furthermore,
AHB2PCIE_SIZE()uses a 5-bit maskGENMASK(4, 0). If a resource size exceeds 2 GiB (order > 31), the log2 size order overflows the 5-bit mask. Clamping the value silently truncates the hardware window while leaving the OS resource intact, causing bus errors when accessing BARs in the unmapped upper region.
Quoted 5 lines
Fix this by using fls64(size - 1) to accurately calculate log2 size orders without off-by-one errors and returning
-EINVALif the resource size exceeds the maximum supported 31-bit window order.Signed-off-by Sreeraj S Kurup <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Hi PCI Maintainers,
Apologies for omitting the revision history in the main patch post. Here is the complete changelog for v4:
Changelog:
- v4: Fixed struct device pointer access (pcie->dev) for dev_err(). Verified build and passed Sashiko review checks. [Subject: PCI: mediatek: Fix integer truncation and handle oversized resources]
- v3: Replaced clamping logic with strict size validation (size_order > 31 returns -EINVAL), added !size check, and updated size calculation to fls64(size - 1) to handle power-of-two sizes correctly.
[Subject: PCI: mediatek: Fix integer truncation and handle oversized resources]
- v2: Added size clamping for size_order > 31 to prevent overflowing the 5-bit AHB2PCIE_SIZE() macro mask.
[Subject: PCI: mediatek: Fix integer truncation in fls() and clamp size]
- v1: Initial submission using fls64() to handle 64-bit resource sizes and prevent integer truncation.
[Subject: PCI: mediatek: Fix integer truncation in fls() for resource size]
Best regards,
Sreeraj
Manivannan Sadhasivam
maintainerQuoted 42 lines · Sreeraj S Kurup
resource_size()returns aresource_size_t, which is 64-bit on 64-bit architectures or 32-bit systems with LPAE/PAE enabled. Passing this directly tofls(), which accepts an unsigned int, implicitly truncates the upper 32 bits.Furthermore,
AHB2PCIE_SIZE()uses a 5-bit maskGENMASK(4, 0). If a resource size exceeds 2 GiB (order > 31), the log2 size order overflows the 5-bit mask. Clamping the value silently truncates the hardware window while leaving the OS resource intact, causing bus errors when accessing BARs in the unmapped upper region.Fix this by using fls64(size - 1) to accurately calculate log2 size orders without off-by-one errors and returning
-EINVALif the resource size exceeds the maximum supported 31-bit window order.Signed-off-by Sreeraj S Kurup <[email protected]>--- drivers/pci/controller/pcie-mediatek.c | 29 ++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-)drivers/pci/controller/pcie-mediatek.c +3
-0@@ -8,6 +8,7 @@*/ #include <linux/bitfield.h> +#include <linux/bitops.h> #include <linux/clk.h> #include <linux/delay.h> #include <linux/errno.h> @@ -686,6 +687,8 @@ static int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port)const struct mtk_pcie_soc *soc = port->pcie->soc;u32 val;int err; +resource_size_t size; +int size_order; entry =resource_list_first_type (&host->windows,IORESOURCE_MEM );if (entry)
drivers/pci/controller/pcie-mediatek.c static int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port) · line 761
Quoted 6 lines
drivers/pci/controller/pcie-mediatek.c +3
-0@@ -753,8 +756,18 @@ static int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port)mtk_pcie_enable_msi (port);/* Set AHB to PCIe translation windows */ + size =resource_size (mem); +if (!size) +return -EINVAL ;
PCI core already rejects the zero sized MEM regions. So this check is a dead code.
drivers/pci/controller/pcie-mediatek.c static int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port) · line 763
Quoted 2 lines
drivers/pci/controller/pcie-mediatek.c +2
-0+ + size_order =fls64 (size -1 );
This is wrong. AHB2PCIE_SIZE() register needs the full size value to enable the whole window. Check:
c61df57343bf ("PCI: mediatek: Fix memory mapped IO range size computation")
drivers/pci/controller/pcie-mediatek.c static int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port) · line 770
Quoted 8 lines
drivers/pci/controller/pcie-mediatek.c +6
-1+if (size_order >31 ) { +dev_err (pcie->dev,"Memory resource size too large: %pa\n" , &size); +return -EINVAL ; + } + val =lower_32_bits (mem->start) | -AHB2PCIE_SIZE (fls (resource_size (mem))); +AHB2PCIE_SIZE (size_order);
Why can't you just do, fls64(resource_size(mem))?
- Mani
Signature · 1 line
மணிவண்ணன் சதாசிவம்
Hi Mani,
Thanks for the review.
I've addressed your comments in v5:
- Removed the redundant zero-size check.
- Changed fls64(size - 1) to fls64(size).
- Kept the full resource size calculation and added validation to reject oversized windows rather than silently clamping the size order.
I'll send the updated patch shortly.
Best regards,
Sreeraj
PATCH v5 1/1 PCI: mediatek: Fix integer truncation and handle oversized resources
In reply to Manivannan Sadhasivamresource_size() returns a resource_size_t, which can be 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 bits.
Furthermore, AHB2PCIE_SIZE() uses a 5-bit mask GENMASK(4, 0). If the size order exceeds the range representable by this field, higher bits are discarded by the mask. This can leave the hardware translation window smaller than the OS resource, causing bus errors when accessing BARs in the unmapped upper region.
Fix this by using fls64(size) to calculate the size order without truncating the resource size, and return -EINVAL if the size order exceeds the supported range.
Changelog:
- v5: Use fls64(size) instead of fls64(size - 1) and remove the redundant zero-size check.
- v4: Fix struct device pointer access (pcie->dev) for dev_err(). Verified build and passed Sashiko review checks. [Subject: PCI: mediatek: Fix integer truncation and handle oversized resources]
- v3: Replace clamping logic with strict size validation (size_order > 31 returns -EINVAL), add a zero-size check, and update size calculation to fls64(size - 1). [Subject: PCI: mediatek: Fix integer truncation and handle oversized resources]
- v2: Add size clamping for size_order > 31 to prevent overflowing the 5-bit AHB2PCIE_SIZE() macro mask.
[Subject: PCI: mediatek: Fix integer truncation in fls() and clamp size]
- v1: Initial submission using fls64() to handle 64-bit resource sizes and prevent integer truncation.
[Subject: PCI: mediatek: Fix integer truncation in fls() for resource size]
--- drivers/pci/controller/pcie-mediatek.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-)
drivers/pci/controller/pcie-mediatek.c +21 -2
@@ -8,6 +8,7 @@*/ #include <linux/bitfield.h> +#include <linux/bitops.h> #include <linux/clk.h> #include <linux/delay.h> #include <linux/errno.h>
@@ -686,6 +687,8 @@ static int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port)const struct mtk_pcie_soc *soc = port->pcie->soc;u32 val;int err; +resource_size_t size; +int size_order; entry =resource_list_first_type (&host->windows,IORESOURCE_MEM );if (entry)
@@ -753,8 +756,15 @@ static int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port)mtk_pcie_enable_msi (port);/* Set AHB to PCIe translation windows */ + size =resource_size (mem); + size_order =fls64 (size); +if (size_order >31 ) { +dev_err (pcie->dev,"Memory resource size too large: %pa\n" , &size); +return -EINVAL ; + } + val =lower_32_bits (mem->start) | -AHB2PCIE_SIZE (fls (resource_size (mem))); +AHB2PCIE_SIZE (size_order);writel (val, port->base +PCIE_AHB_TRANS_BASE0_L ); val =upper_32_bits (mem->start);
@@ -775,6 +785,8 @@ static int mtk_pcie_startup_port_en7528(struct mtk_pcie_port *port)struct resource_entry *entry;u32 val, link_mask;int err; +resource_size_t size; +int size_order; entry =resource_list_first_type (&host->windows,IORESOURCE_MEM );if (entry)
@@ -829,8 +841,15 @@ static int mtk_pcie_startup_port_en7528(struct mtk_pcie_port *port)mtk_pcie_enable_msi (port);/* Set AHB to PCIe translation windows */ + size =resource_size (mem); + size_order =fls64 (size); +if (size_order >31 ) { +dev_err (pcie->dev,"Memory resource size too large: %pa\n" , &size); +return -EINVAL ; + } + val =lower_32_bits (mem->start) | -AHB2PCIE_SIZE (fls (resource_size (mem))); +AHB2PCIE_SIZE (size_order);writel (val, port->base +PCIE_AHB_TRANS_BASE0_L ); val =upper_32_bits (mem->start);
Signature · 1 line
2.55.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] PCI: mediatek: oversized resource check after unmasking IRQs and link-up --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] PCI: mediatek: oversized resource check after unmasking IRQs and link-up --
commit 1000c85227f12ca140c68660a5dc51d66a32f949
Author: Sreeraj S Kurup <[email protected]>
PCI: mediatek: Fix integer truncation and handle oversized resources
This commit uses fls64() instead of fls() when calculating the AHB-to-PCIe translation window size order to avoid truncating 64-bit resource_size_t values. It also rejects memory resources whose size order exceeds 31 with -EINVAL so that the 5-bit AHB2PCIE_SIZE() field does not silently truncate the window size.
Quoted 4 lines
drivers/pci/controller/pcie-mediatek.c +0
-0
[ ... ]
Quoted 36 lines
drivers/pci/controller/pcie-mediatek.c +12
-1@@ -686,6 +687,8 @@ static int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port)const struct mtk_pcie_soc *soc = port->pcie->soc;u32 val;int err; +resource_size_t size; +int size_order; entry =resource_list_first_type (&host->windows,IORESOURCE_MEM );if (entry)@@ -753,8 +756,15 @@ static int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port)mtk_pcie_enable_msi (port);/* Set AHB to PCIe translation windows */ + size =resource_size (mem); + size_order =fls64 (size); +if (size_order >31 ) { +dev_err (pcie->dev,"Memory resource size too large: %pa\n" , &size); +return -EINVAL ; + } + val =lower_32_bits (mem->start) | -AHB2PCIE_SIZE (fls (resource_size (mem))); +AHB2PCIE_SIZE (size_order);writel (val, port->base +PCIE_AHB_TRANS_BASE0_L ); val =upper_32_bits (mem->start);@@ -775,6 +785,8 @@ static int mtk_pcie_startup_port_en7528(struct mtk_pcie_port *port)struct resource_entry *entry;u32 val, link_mask;int err; +resource_size_t size; +int size_order; entry =resource_list_first_type (&host->windows,IORESOURCE_MEM );if (entry)
drivers/pci/controller/pcie-mediatek.c static int mtk_pcie_startup_port_en7528(struct mtk_pcie_port *port) · line 849
Quoted 9 lines
drivers/pci/controller/pcie-mediatek.c +6
-0@@ -829,8 +841,15 @@ static int mtk_pcie_startup_port_en7528(struct mtk_pcie_port *port)mtk_pcie_enable_msi (port);/* Set AHB to PCIe translation windows */ + size =resource_size (mem); + size_order =fls64 (size); +if (size_order >31 ) { +dev_err (pcie->dev,"Memory resource size too large: %pa\n" , &size); +return -EINVAL ; + }
[Severity: Medium]
In both mtk_pcie_startup_port_v2() and mtk_pcie_startup_port_en7528(), mem is retrieved at the very start of the function, but the size_order > 31 check is performed after enabling LTSSM, de-asserting port resets, waiting for the PCIe link to come up, and unmasking INTx and MSI interrupts in PCIE_INT_MASK.
During mtk_pcie_probe() -> mtk_pcie_setup(), mtk_pcie_parse_port() -> mtk_pcie_setup_irq() registers the chained interrupt handler mtk_pcie_intr_handler() before mtk_pcie_enable_port() is called. When soc->startup() returns -EINVAL here, mtk_pcie_enable_port() jumps to err_soc_startup:
drivers/pci/controller/pcie-mediatek.c:mtk_pcie_enable_port() {
...
err = pcie->soc->startup(port);
if (err) {
dev_info(dev, "Port%d link down\n", port->slot);
goto err_soc_startup;
}
return 0;err_soc_startup:
phy_power_off(port->phy); err_phy_on: phy_exit(port->phy); err_phy_init: clk_disable_unprepare(port->pipe_ck); ... clk_disable_unprepare(port->ahb_ck); err_ahb_clk: clk_disable_unprepare(port->sys_ck); }
This powers off the PHY and disables the port clocks (including ahb_ck and sys_ck) while the port resets remain de-asserted and hardware interrupts remain unmasked in PCIE_INT_MASK with the chained IRQ handler still active. If an endpoint asserts an interrupt before mtk_pcie_setup() tears down the IRQ, mtk_pcie_intr_handler() will read PCIE_INT_STATUS with the port clocks disabled.
Could the size_order > 31 check be moved to the top of mtk_pcie_startup_port_v2() and mtk_pcie_startup_port_en7528() right after validating !mem, before touching the hardware state and unmasking interrupts?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v6 1/1 PCI: mediatek: Fix integer truncation and handle oversized resources
In reply to Manivannan Sadhasivamresource_size() returns a resource_size_t, which can be 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 bits.
Furthermore, AHB2PCIE_SIZE() uses a 5-bit mask GENMASK(4, 0). If the size order exceeds the range representable by this field, higher bits are discarded by the mask. This can leave the hardware translation window smaller than the OS resource, causing bus errors when accessing BARs in the unmapped upper region.
Fix this by using fls64(size) to calculate the size order without truncating the resource size, and return -EINVAL if the size order exceeds the supported range.
Changelog:
- v6: Move oversized resource validation before hardware initialization to avoid leaving interrupts unmasked on the error path.
- v5: Use fls64(size) instead of fls64(size - 1) and remove the redundant zero-size check.
- v4: Fix struct device pointer access (pcie->dev) for dev_err(). Verified build and passed Sashiko review checks. [Subject: PCI: mediatek: Fix integer truncation and handle oversized resources]
- v3: Replace clamping logic with strict size validation (size_order > 31 returns -EINVAL), add a zero-size check, and update size calculation to fls64(size - 1). [Subject: PCI: mediatek: Fix integer truncation and handle oversized resources]
- v2: Add size clamping for size_order > 31 to prevent overflowing the 5-bit AHB2PCIE_SIZE() macro mask.
[Subject: PCI: mediatek: Fix integer truncation in fls() and clamp size]
- v1: Initial submission using fls64() to handle 64-bit resource sizes and prevent integer truncation.
[Subject: PCI: mediatek: Fix integer truncation in fls() for resource size]
--- drivers/pci/controller/pcie-mediatek.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-)
drivers/pci/controller/pcie-mediatek.c +21 -2
@@ -8,6 +8,7 @@*/ #include <linux/bitfield.h> +#include <linux/bitops.h> #include <linux/clk.h> #include <linux/delay.h> #include <linux/errno.h>
@@ -686,6 +687,8 @@ static int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port)const struct mtk_pcie_soc *soc = port->pcie->soc;u32 val;int err; +resource_size_t size; +int size_order; entry =resource_list_first_type (&host->windows,IORESOURCE_MEM );if (entry)
@@ -693,6 +696,13 @@ static int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port)if (!mem)return -EINVAL ; + size =resource_size (mem); + size_order =fls64 (size); +if (size_order >31 ) { +dev_err (pcie->dev,"Memory resource size too large: %pa\n" , &size); +return -EINVAL ; + } +/* MT7622 platforms need to enable LTSSM and ASPM from PCIe subsys */ if (pcie->base) { val =readl (pcie->base +PCIE_SYS_CFG_V2 );
@@ -754,7 +764,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 (size_order);writel (val, port->base +PCIE_AHB_TRANS_BASE0_L ); val =upper_32_bits (mem->start);
@@ -775,6 +785,8 @@ static int mtk_pcie_startup_port_en7528(struct mtk_pcie_port *port)struct resource_entry *entry;u32 val, link_mask;int err; +resource_size_t size; +int size_order; entry =resource_list_first_type (&host->windows,IORESOURCE_MEM );if (entry)
@@ -782,6 +794,13 @@ static int mtk_pcie_startup_port_en7528(struct mtk_pcie_port *port)if (!mem)return -EINVAL ; + size =resource_size (mem); + size_order =fls64 (size); +if (size_order >31 ) { +dev_err (pcie->dev,"Memory resource size too large: %pa\n" , &size); +return -EINVAL ; + } +if (!pcie->cfg) {dev_err (pcie->dev,"EN7528: pciecfg syscon not available\n" );return -EINVAL ;
@@ -830,7 +849,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 (size_order);writel (val, port->base +PCIE_AHB_TRANS_BASE0_L ); val =upper_32_bits (mem->start);
Signature · 1 line
2.55.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 15 lines
resource_size()returns aresource_size_t, which can be 64-bit on 64-bit architectures or 32-bit systems with LPAE/PAE enabled. Passing this directly tofls(), which accepts an unsigned int, implicitly truncates the upper bits.Furthermore,
AHB2PCIE_SIZE()uses a 5-bit maskGENMASK(4, 0). If the size order exceeds the range representable by this field, higher bits are discarded by the mask. This can leave the hardware translation window smaller than the OS resource, causing bus errors when accessing BARs in the unmapped upper region.Fix this by using fls64(size) to calculate the size order without truncating the resource size, and return
-EINVALif the size order exceeds the supported range.
Quoted 6 lines
[ ... ]
sizes and prevent integer truncation.
[Subject: PCI: mediatek: Fix integer truncation infls()for resource size]Signed-off-by Sreeraj S Kurup <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 1 line · Sreeraj S Kurup
PCI: mediatek: Fix integer truncation and handle oversized resources
Applied, thanks!
[1/1] PCI: mediatek: Fix integer truncation and handle oversized resources
commit: 0abd664998828adc30df21c1ca052d3a5de910d9
Best regards,
Signature · 1 line
மணிவண்ணன் சதாசிவம்
Hi,
Thanks for applying the patch!
Best regards,
Sreeraj