Skip to contentSkip to conversation

8250_lpss: check null return when calling pci_ioremap_bar

PATCH Superseded Navid Emamdoost 5 participants 11 messages Jul 19, 2019 – Sep 7, 2019 2 messages not in the archive

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

PATCH 1/1 8250_lpss: check null return when calling pci_ioremap_bar

pci_ioremap_bar may return null. This is eventually de-referenced at drivers/dma/dw/core.c:1154 and drivers/dma/dw/core.c:1168. A null check is
needed to prevent null de-reference. I am adding the check and in case of failure returning -ENOMEM (I am not sure this is the best errno, you may
consider it as a placeholder), and subsequently changing the caller’s return type, and propagating the error.

---
 drivers/tty/serial/8250/8250_lpss.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/tty/serial/8250/8250_lpss.c +8 -3
index 53ca9ba6ab4b..5954b2e09b76 100644
@@ -161,7 +161,7 @@ static const struct dw_dma_platform_data qrk_serial_dma_pdata = {
 	.multi_block = {0},
 };
 
-static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port)
+static int qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port)
 {
 	struct uart_8250_dma *dma = &lpss->dma;
 	struct dw_dma_chip *chip = &lpss->dma_chip;
@@ -172,12 +172,14 @@ static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port)
 	chip->dev = &pdev->dev;
 	chip->irq = pci_irq_vector(pdev, 0);
 	chip->regs = pci_ioremap_bar(pdev, 1);
+	if (!chip->regs)
+		return -ENOMEM;
 	chip->pdata = &qrk_serial_dma_pdata;
 
 	/* Falling back to PIO mode if DMA probing fails */
 	ret = dw_dma_probe(chip);
 	if (ret)
-		return;
+		return 0;
 
 	pci_try_set_mwi(pdev);
 
@@ -191,6 +193,7 @@ static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port)
 	param->hs_polarity = true;
 
 	lpss->dma_maxburst = 8;
+	return 0;
 }
 
 static void qrk_serial_exit_dma(struct lpss8250 *lpss)
@@ -219,7 +222,9 @@ static int qrk_serial_setup(struct lpss8250 *lpss, struct uart_port *port)
 
 	port->irq = pci_irq_vector(pdev, 0);
 
-	qrk_serial_setup_dma(lpss, port);
+	ret = qrk_serial_setup_dma(lpss, port);
+	if (ret < 0)
+		return ret;
 	return 0;
 }
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 6 lines · Navid Emamdoost

pci_ioremap_bar may return null. This is eventually de-referenced at drivers/dma/dw/core.c:1154 and drivers/dma/dw/core.c:1168. A null check is
needed to prevent null de-reference. I am adding the check and in case of failure returning -ENOMEM (I am not sure this is the best errno, you may
consider it as a placeholder), and subsequently changing the caller’s return type, and propagating the error.

Thanks for the patch, my comments below.

drivers/tty/serial/8250/8250_lpss.c static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port) · line 176

Quoted 4 lines
drivers/tty/serial/8250/8250_lpss.c +2 -0
 	chip->irq = pci_irq_vector(pdev, 0);
 	chip->regs = pci_ioremap_bar(pdev, 1);
+	if (!chip->regs)
+		return -ENOMEM;

This is the same case as below, it's fine to go on without DMA support.

Quoted 1 line

chip->pdata = &qrk_serial_dma_pdata;

So, I would rather to put like this...

Hold on, I remember someone already tried to fix this [1].

I dunno why it wasn't v5, due to [2].

Also, similar to yours, but wrong [3].

Thus, please, collaborate guys, and send one compiling solution based on [1].

drivers/tty/serial/8250/8250_lpss.c static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port) · line 182

Quoted 5 lines
drivers/tty/serial/8250/8250_lpss.c +1 -1
 	/* Falling back to PIO mode if DMA probing fails */
 	ret = dw_dma_probe(chip);
 	if (ret)
-		return;
+		return 0;

[1]: https://www.spinics.net/lists/linux-serial/msg33965.html
[2]: https://lists.01.org/pipermail/kbuild-all/2019-March/059215.html
[3]: https://lore.kernel.org/patchwork/patch/1051000/

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 5 lines · Andy Shevchenko

On Thu, Jul 18, 2019 at 09:54:42PM -0500, Navid Emamdoost wrote:

Thus, please, collaborate guys, and send one compiling solution based on [1].

[1]: https://www.spinics.net/lists/linux-serial/msg33965.html

For your convenience (what has to be applied to v4):

drivers/tty/serial/8250/8250_lpss.c +5 -1
index 53ca9ba6ab4b..6214da67e9b3 100644
@@ -195,11 +195,15 @@ static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port)
 
 static void qrk_serial_exit_dma(struct lpss8250 *lpss)
 {
+	struct dw_dma_chip *chip = &lpss->dma_chip;
 	struct dw_dma_slave *param = &lpss->dma_param;
 
 	if (!param->dma_dev)
 		return;
-	dw_dma_remove(&lpss->dma_chip);
+
+	dw_dma_remove(chip);
+
+	pci_iounmap(to_pci_dev(chip->dev), chip->regs);
 }
 #else	/* CONFIG_SERIAL_8250_DMA */
 static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port) {}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw

PATCH v2 1/1 8250_lpss: check null return when calling pci_ioremap_bar

pci_ioremap_bar may return null. This is eventually de-referenced at drivers/dma/dw/core.c:1154 and drivers/dma/dw/core.c:1168. A null check is needed to prevent null de-reference. I am adding the check and in case
of failure. Thanks to Andy Shevchenko for the hint on the necessity of
pci_iounmap when exiting.

---
 drivers/tty/serial/8250/8250_lpss.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/tty/serial/8250/8250_lpss.c +8 -2
index 53ca9ba6ab4b..d07e431110d9 100644
@@ -169,10 +169,12 @@ static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port)
 	struct pci_dev *pdev = to_pci_dev(port->dev);
 	int ret;
 
+	chip->pdata = &qrk_serial_dma_pdata;
 	chip->dev = &pdev->dev;
 	chip->irq = pci_irq_vector(pdev, 0);
 	chip->regs = pci_ioremap_bar(pdev, 1);
-	chip->pdata = &qrk_serial_dma_pdata;
+	if (!chip->regs)
+		return;
 
 	/* Falling back to PIO mode if DMA probing fails */
 	ret = dw_dma_probe(chip);
@@ -195,11 +197,15 @@ static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port)
 
 static void qrk_serial_exit_dma(struct lpss8250 *lpss)
 {
+	struct dw_dma_chip *chip = &lpss->dma_chip;
 	struct dw_dma_slave *param = &lpss->dma_param;
 
 	if (!param->dma_dev)
 		return;
-	dw_dma_remove(&lpss->dma_chip);
+
+	dw_dma_remove(chip);
+
+	pci_iounmap(to_pci_dev(chip->dev), chip->regs);
 }
 #else	/* CONFIG_SERIAL_8250_DMA */
 static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port) {}
Signature · 1 line
2.17.1
Reviewed-by on v2 1/1
#4 lore raw
Quoted 7 lines · Navid Emamdoost

pci_ioremap_bar may return null. This is eventually de-referenced at drivers/dma/dw/core.c:1154 and drivers/dma/dw/core.c:1168. A null check is needed to prevent null de-reference. I am adding the check and in case
of failure. Thanks to Andy Shevchenko for the hint on the necessity of
pci_iounmap when exiting.

Quoted 43 lines
---
 drivers/tty/serial/8250/8250_lpss.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/tty/serial/8250/8250_lpss.c +8 -3
index 53ca9ba6ab4b..d07e431110d9 100644
@@ -169,10 +169,12 @@ static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port)
        struct pci_dev *pdev = to_pci_dev(port->dev);
        int ret;
 
+       chip->pdata = &qrk_serial_dma_pdata;
        chip->dev = &pdev->dev;
        chip->irq = pci_irq_vector(pdev, 0);
        chip->regs = pci_ioremap_bar(pdev, 1);
-       chip->pdata = &qrk_serial_dma_pdata;
+       if (!chip->regs)
+               return;
 
        /* Falling back to PIO mode if DMA probing fails */
        ret = dw_dma_probe(chip);
@@ -195,11 +197,15 @@ static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port)
 
 static void qrk_serial_exit_dma(struct lpss8250 *lpss)
 {
+       struct dw_dma_chip *chip = &lpss->dma_chip;
        struct dw_dma_slave *param = &lpss->dma_param;
 
        if (!param->dma_dev)
                return;
-       dw_dma_remove(&lpss->dma_chip);
+
+       dw_dma_remove(chip);
+
+       pci_iounmap(to_pci_dev(chip->dev), chip->regs);
 }
 #else  /* CONFIG_SERIAL_8250_DMA */
 static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port) {}
--

2.17.1

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
In reply to Navid Emamdoost

Hi,

Quoted 19 lines · Navid Emamdoost

pci_ioremap_bar may return null. This is eventually de-referenced at drivers/dma/dw/core.c:1154 and drivers/dma/dw/core.c:1168. A null check is needed to prevent null de-reference. I am adding the check and in case
of failure. Thanks to Andy Shevchenko for the hint on the necessity of
pci_iounmap when exiting.

---
 drivers/tty/serial/8250/8250_lpss.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/tty/serial/8250/8250_lpss.c +0 -0
index 53ca9ba6ab4b..d07e431110d9 100644
@@ -169,10 +169,12 @@ static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port)
 	struct pci_dev *pdev = to_pci_dev(port->dev);
 	int ret;
 

drivers/tty/serial/8250/8250_lpss.c static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port) · line 180

Quoted 10 lines
drivers/tty/serial/8250/8250_lpss.c +3 -1
+	chip->pdata = &qrk_serial_dma_pdata;
 	chip->dev = &pdev->dev;
 	chip->irq = pci_irq_vector(pdev, 0);
 	chip->regs = pci_ioremap_bar(pdev, 1);
-	chip->pdata = &qrk_serial_dma_pdata;
+	if (!chip->regs)
+		return;
 
 	/* Falling back to PIO mode if DMA probing fails */
 	ret = dw_dma_probe(chip);

pci_iounmap() should also be called on dw_dma_probe() failure (in such case param->dma_dev is NULL so pci_iounmap() in qrk_serial_exit_dma() won't be called during exit).

Quoted 7 lines
drivers/tty/serial/8250/8250_lpss.c +1 -0
@@ -195,11 +197,15 @@ static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port)
 pci_iounmap
 static void qrk_serial_exit_dma(struct lpss8250 *lpss)
 {
+	struct dw_dma_chip *chip = &lpss->dma_chip;
 	struct dw_dma_slave *param = &lpss->dma_param;
 

drivers/tty/serial/8250/8250_lpss.c static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port) · line 211

Quoted 10 lines
drivers/tty/serial/8250/8250_lpss.c +4 -1
 	if (!param->dma_dev)
 		return;
-	dw_dma_remove(&lpss->dma_chip);
+
+	dw_dma_remove(chip);
+
+	pci_iounmap(to_pci_dev(chip->dev), chip->regs);
 }
 #else	/* CONFIG_SERIAL_8250_DMA */
 static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port) {}

Best regards,
--
Bartlomiej Zolnierkiewicz
Samsung R&D Institute Poland
Samsung Electronics

#6 lore raw
Quoted 19 lines · Bartlomiej Zolnierkiewicz
Quoted 18 lines · Navid Emamdoost

pci_ioremap_bar may return null. This is eventually de-referenced at drivers/dma/dw/core.c:1154 and drivers/dma/dw/core.c:1168. A null check is needed to prevent null de-reference. I am adding the check and in case
of failure. Thanks to Andy Shevchenko for the hint on the necessity of
pci_iounmap when exiting.

drivers/tty/serial/8250/8250_lpss.c +3 -1
@@ -169,10 +169,12 @@ static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port)
 	struct pci_dev *pdev = to_pci_dev(port->dev);
 	int ret;
 
+	chip->pdata = &qrk_serial_dma_pdata;
 	chip->dev = &pdev->dev;
 	chip->irq = pci_irq_vector(pdev, 0);
 	chip->regs = pci_ioremap_bar(pdev, 1);
-	chip->pdata = &qrk_serial_dma_pdata;
+	if (!chip->regs)
+		return;
 
Quoted 6 lines

drivers/tty/serial/8250/8250_lpss.c static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port) · line 180

Quoted 2 lines
drivers/tty/serial/8250/8250_lpss.c +0 -0
 	/* Falling back to PIO mode if DMA probing fails */
 	ret = dw_dma_probe(chip);

pci_iounmap() should also be called on dw_dma_probe() failure (in such case param->dma_dev is NULL so pci_iounmap() in qrk_serial_exit_dma() won't be called during exit).

Oh, yes, good catch!
Navid, can you send a follow up fix?

Quoted 15 lines
Quoted 15 lines
drivers/tty/serial/8250/8250_lpss.c +5 -1
@@ -195,11 +197,15 @@ static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port)
 pci_iounmap
 static void qrk_serial_exit_dma(struct lpss8250 *lpss)
 {
+	struct dw_dma_chip *chip = &lpss->dma_chip;
 	struct dw_dma_slave *param = &lpss->dma_param;
 
 	if (!param->dma_dev)
 		return;
-	dw_dma_remove(&lpss->dma_chip);
+
+	dw_dma_remove(chip);
+
+	pci_iounmap(to_pci_dev(chip->dev), chip->regs);
 }
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

PATCH v3 1/1 8250_lpss: check null return when calling pci_ioremap_bar

pci_ioremap_bar may return null. This is eventually de-referenced at drivers/dma/dw/core.c:1154 and drivers/dma/dw/core.c:1168. A null check is needed to prevent null de-reference. I am adding the check and in case
of failure. Thanks to Andy Shevchenko for the hint on the necessity of
pci_iounmap when exiting.
Update: also covered the dw_dma_probe() failure.

---
 drivers/tty/serial/8250/8250_lpss.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/tty/serial/8250/8250_lpss.c +12 -3
index 53ca9ba6ab4b..19b356119ef6 100644
@@ -169,15 +169,20 @@ static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port)
 	struct pci_dev *pdev = to_pci_dev(port->dev);
 	int ret;
 
+	chip->pdata = &qrk_serial_dma_pdata;
 	chip->dev = &pdev->dev;
 	chip->irq = pci_irq_vector(pdev, 0);
 	chip->regs = pci_ioremap_bar(pdev, 1);
-	chip->pdata = &qrk_serial_dma_pdata;
+	if (!chip->regs)
+		return;
 
 	/* Falling back to PIO mode if DMA probing fails */
 	ret = dw_dma_probe(chip);
-	if (ret)
+	if (ret) {
+		dw_dma_remove(chip);
+		pci_iounmap(to_pci_dev(chip->dev), chip->regs);
 		return;
+	}
 
 	pci_try_set_mwi(pdev);
 
@@ -195,11 +200,15 @@ static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port)
 
 static void qrk_serial_exit_dma(struct lpss8250 *lpss)
 {
+	struct dw_dma_chip *chip = &lpss->dma_chip;
 	struct dw_dma_slave *param = &lpss->dma_param;
 
 	if (!param->dma_dev)
 		return;
-	dw_dma_remove(&lpss->dma_chip);
+
+	dw_dma_remove(chip);
+
+	pci_iounmap(to_pci_dev(chip->dev), chip->regs);
 }
 #else	/* CONFIG_SERIAL_8250_DMA */
 static void qrk_serial_setup_dma(struct lpss8250 *lpss, struct uart_port *port) {}
Signature · 1 line
2.17.1

A message that is not in the archive (sent off-list or not yet received): CAEkB2ERhxLj7ogoy1E3j8d4MyEZqroWS1tPRxyJXR2oLhNz+LQ@mail.gmail.com

#9 lore raw
Quoted 2 lines · Navid Emamdoost

I was wondering is anyone reviewing this patch?
https://lore.kernel.org/patchwork/patch/1106267/

Why?

The one below is a part of upstraem

commit f5d6aadf3b6434f11393e33be9fd25a56d0bc872

Author: Navid Emamdoost <[email protected]>
Date:   Fri Jul 19 12:48:45 2019 -0500

8250_lpss: check null return when calling pci_ioremap_bar

Signature · 2 lines
With Best Regards,
Andy Shevchenko

A message that is not in the archive (sent off-list or not yet received): CAEkB2ET-SQFiBbroxDFEVrPxto6a2wLJf0NM7R=ERcPargr66Q@mail.gmail.com

#11 lore raw
Quoted 2 lines · Navid Emamdoost

Cool! I thought it is forgotten, as there were no response to the v3 of the patch.

Nevertheless, you may send a follow up, since one call to pci_iounmap() is missed when dw_dma_probe() fails.

Quoted 1 line

Thanks for letting me know.

You're welcome.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#12 lore raw
In reply to Navid Emamdoost
Thank you for the patch! Yet something to improve:

Hi Navid,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linus/master]
[cannot apply to v5.2 next-20190718]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Navid-Emamdoost/8250_lpss-check-null-return-when-calling-pci_ioremap_bar/20190719-120441
config: x86_64-randconfig-h002-201928 (attached as .config)
compiler: gcc-7 (Debian 7.4.0-10) 7.4.0
reproduce:

        # save the attached .config to linux build tree
        make ARCH=x86_64 

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

drivers/tty/serial/8250/8250_lpss.c: In function 'qrk_serial_setup':

>> drivers/tty/serial/8250/8250_lpss.c:225:6: error: void value not ignored as it ought to be
     ret = qrk_serial_setup_dma(lpss, port);
         ^

vim +225 drivers/tty/serial/8250/8250_lpss.c

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation