Skip to contentSkip to conversation

PCI: dwc: Reject a negative nr_irqs value in dw_pcie_edma_irq_verify()

PATCH In review Niklas Cassel 2 participants 5 messages Dec 20, 2024 – Jan 2, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI: dwc: Reject a negative nr_irqs value in dw_pcie_edma_irq_verify()

Platforms that do not have (one or more) dedicated IRQs for the eDMA need to set nr_irqs to a non-zero value in their DWC glue driver.

Platforms that do have (one or more) dedicated IRQs do not need to initialize nr_irqs. DWC common code will automatically set nr_irqs.

Since a glue driver can initialize nr_irqs, dw_pcie_edma_irq_verify() should verify that nr_irqs, if non-zero, is a valid value. Thus, add a check in dw_pcie_edma_irq_verify() to reject a negative nr_irqs value.

This fixes the following build warning when compiling with W=1:

drivers/pci/controller/dwc/pcie-designware.c: In function ‘dw_pcie_edma_detect’:
drivers/pci/controller/dwc/pcie-designware.c:989:50: warning: ‘%d’ directive output may be truncated writing between 1 and 11 bytes into a region of size 3 [-Wformat-truncation=]
989 | snprintf(name, sizeof(name), "dma%d", pci->edma.nr_irqs);
| ^~

---
 drivers/pci/controller/dwc/pcie-designware.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/pci/controller/dwc/pcie-designware.c +2 -0
index 3c683b6119c3..d7f695d5dbc4 100644
@@ -978,6 +978,8 @@ static int dw_pcie_edma_irq_verify(struct dw_pcie *pci)
 		return 0;
 	else if (pci->edma.nr_irqs > 1)
 		return pci->edma.nr_irqs != ch_cnt ? -EINVAL : 0;
+	else if (pci->edma.nr_irqs < 0)
+		return -EINVAL;
 
 	ret = platform_get_irq_byname_optional(pdev, "dma");
 	if (ret > 0) {
Signature · 1 line
2.47.1
#1 lore raw
Quoted 10 lines · Niklas Cassel

Platforms that do not have (one or more) dedicated IRQs for the eDMA need to set nr_irqs to a non-zero value in their DWC glue driver.

Platforms that do have (one or more) dedicated IRQs do not need to initialize nr_irqs. DWC common code will automatically set nr_irqs.

Since a glue driver can initialize nr_irqs, dw_pcie_edma_irq_verify() should verify that nr_irqs, if non-zero, is a valid value. Thus, add a check in dw_pcie_edma_irq_verify() to reject a negative nr_irqs value.

Why can't we make dw_edma_chip::nr_irqs unsigned?

- Mani

Quoted 28 lines

This fixes the following build warning when compiling with W=1:

drivers/pci/controller/dwc/pcie-designware.c: In function ‘dw_pcie_edma_detect’:
drivers/pci/controller/dwc/pcie-designware.c:989:50: warning: ‘%d’ directive output may be truncated writing between 1 and 11 bytes into a region of size 3 [-Wformat-truncation=]
989 | snprintf(name, sizeof(name), "dma%d", pci->edma.nr_irqs);
| ^~

---
 drivers/pci/controller/dwc/pcie-designware.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/pci/controller/dwc/pcie-designware.c +2 -0
index 3c683b6119c3..d7f695d5dbc4 100644
@@ -978,6 +978,8 @@ static int dw_pcie_edma_irq_verify(struct dw_pcie *pci)
 		return 0;
 	else if (pci->edma.nr_irqs > 1)
 		return pci->edma.nr_irqs != ch_cnt ? -EINVAL : 0;
+	else if (pci->edma.nr_irqs < 0)
+		return -EINVAL;
 
 	ret = platform_get_irq_byname_optional(pdev, "dma");
 	if (ret > 0) {
Signature · 1 line
2.47.1
Signature · 1 line
மணிவண்ணன் சதாசிவம்
#2 lore raw
Quoted 7 lines · Manivannan Sadhasivam
Quoted 6 lines · Niklas Cassel

Platforms that do not have (one or more) dedicated IRQs for the eDMA need to set nr_irqs to a non-zero value in their DWC glue driver.

Platforms that do have (one or more) dedicated IRQs do not need to initialize nr_irqs. DWC common code will automatically set nr_irqs.

Quoted 6 lines
Quoted 4 lines

Since a glue driver can initialize nr_irqs, dw_pcie_edma_irq_verify() should verify that nr_irqs, if non-zero, is a valid value. Thus, add a check in dw_pcie_edma_irq_verify() to reject a negative nr_irqs value.

Why can't we make dw_edma_chip::nr_irqs unsigned?

dw_edma is defined in drivers/dma/dw-edma/dw-edma-core.h in struct dw_edma.

struct dw_pcie (defined in drivers/pci/controller/dwc/pcie-designware.h) simply has a struct dw_edma as a struct member.

If you bounce on nr_irqs in:
drivers/dma/dw-edma/dw-edma-core.c
and in
drivers/dma/dw-edma/dw-edma-pcie.c
you can see that this driver uses signed int for this everywhere.

I didn't feel like refactoring a whole DMA driver.

dw_pcie_edma_irq_verify() is supposed to verify that nr_irqs is either initialized to a valid value by a DWC PCIe glue driver, or that common code initializes it.

If nr_irqs is initialized by a glue driver && "pci->edma.nr_irqs != ch_cnt", dw_pcie_edma_irq_verify() returns error and avoids calling dw_edma_probe(), thus it made sense for dw_pcie_edma_irq_verify() to also return error on negative nr_irqs.

Kind regards,
Niklas

#3 lore raw
Quoted 21 lines · Niklas Cassel
Quoted 7 lines · Manivannan Sadhasivam
Quoted 6 lines · Niklas Cassel

Platforms that do not have (one or more) dedicated IRQs for the eDMA need to set nr_irqs to a non-zero value in their DWC glue driver.

Platforms that do have (one or more) dedicated IRQs do not need to initialize nr_irqs. DWC common code will automatically set nr_irqs.

Quoted 6 lines
Quoted 4 lines

Since a glue driver can initialize nr_irqs, dw_pcie_edma_irq_verify() should verify that nr_irqs, if non-zero, is a valid value. Thus, add a check in dw_pcie_edma_irq_verify() to reject a negative nr_irqs value.

Why can't we make dw_edma_chip::nr_irqs unsigned?

dw_edma is defined in drivers/dma/dw-edma/dw-edma-core.h in struct dw_edma.

struct dw_pcie (defined in drivers/pci/controller/dwc/pcie-designware.h) simply has a struct dw_edma as a struct member.

Quoted 8 lines

If you bounce on nr_irqs in:
drivers/dma/dw-edma/dw-edma-core.c
and in
drivers/dma/dw-edma/dw-edma-pcie.c
you can see that this driver uses signed int for this everywhere.

I didn't feel like refactoring a whole DMA driver.

There is no need to refactor. Both 'dma' and 'dwc' drivers do not assume that 'nr_irqs' is signed. So simply changing the type to 'unsigned int' is enough. I don't see a valid reason to keep it signed and check for negative value.

Quoted 5 lines

dw_pcie_edma_irq_verify() is supposed to verify that nr_irqs is either initialized to a valid value by a DWC PCIe glue driver, or that common code initializes it.

Yes, but the glue drivers do not set negative value explicitly.

- Mani

Signature · 1 line
மணிவண்ணன் சதாசிவம்
#4 lore raw
Quoted 28 lines · Manivannan Sadhasivam
Quoted 27 lines · Niklas Cassel
Quoted 7 lines · Manivannan Sadhasivam
Quoted 6 lines · Niklas Cassel

Platforms that do not have (one or more) dedicated IRQs for the eDMA need to set nr_irqs to a non-zero value in their DWC glue driver.

Platforms that do have (one or more) dedicated IRQs do not need to initialize nr_irqs. DWC common code will automatically set nr_irqs.

Quoted 6 lines
Quoted 4 lines

Since a glue driver can initialize nr_irqs, dw_pcie_edma_irq_verify() should verify that nr_irqs, if non-zero, is a valid value. Thus, add a check in dw_pcie_edma_irq_verify() to reject a negative nr_irqs value.

Why can't we make dw_edma_chip::nr_irqs unsigned?

dw_edma is defined in drivers/dma/dw-edma/dw-edma-core.h in struct dw_edma.

struct dw_pcie (defined in drivers/pci/controller/dwc/pcie-designware.h) simply has a struct dw_edma as a struct member.

If you bounce on nr_irqs in:
drivers/dma/dw-edma/dw-edma-core.c
and in
drivers/dma/dw-edma/dw-edma-pcie.c
you can see that this driver uses signed int for this everywhere.

Quoted 6 lines
Quoted 2 lines

I didn't feel like refactoring a whole DMA driver.

There is no need to refactor. Both 'dma' and 'dwc' drivers do not assume that 'nr_irqs' is signed. So simply changing the type to 'unsigned int' is enough. I don't see a valid reason to keep it signed and check for negative value.

If you bounce on nr_irqs in
drivers/dma/dw-edma/dw-edma-core.c
and in
drivers/dma/dw-edma/dw-edma-pcie.c

you will see that there are a lot of local variables that are nr_irqs which are signed, in addition to the struct member.

I don't want to change the whole driver simply to fix a warning when building the DWC PCIe driver with W=1.

I have a different solution and will send a V2 soon.

Kind regards,
Niklas