2019-03-14 05:59:58

by Kangjie Lu

[permalink] [raw]
Subject: [PATCH] pci: pcie-xilinx: fix a missing-check bug for __get_free_pages

In case __get_free_pages fail, the fix returns to avoid NULL
pointer dereference.

Signed-off-by: Kangjie Lu <[email protected]>
---
drivers/pci/controller/pcie-xilinx.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/drivers/pci/controller/pcie-xilinx.c b/drivers/pci/controller/pcie-xilinx.c
index 9bd1a35cd5d8..b7083e995c45 100644
--- a/drivers/pci/controller/pcie-xilinx.c
+++ b/drivers/pci/controller/pcie-xilinx.c
@@ -341,6 +341,9 @@ static void xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
phys_addr_t msg_addr;

port->msi_pages = __get_free_pages(GFP_KERNEL, 0);
+ if (unlikely(!port->msi_pages))
+ return;
+
msg_addr = virt_to_phys((void *)port->msi_pages);
pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
--
2.17.1



2019-03-22 16:28:00

by Steven Price

[permalink] [raw]
Subject: Re: [PATCH] pci: pcie-xilinx: fix a missing-check bug for __get_free_pages

On 14/03/2019 05:58, Kangjie Lu wrote:
> In case __get_free_pages fail, the fix returns to avoid NULL
> pointer dereference.
>
> Signed-off-by: Kangjie Lu <[email protected]>
> ---
> drivers/pci/controller/pcie-xilinx.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/drivers/pci/controller/pcie-xilinx.c b/drivers/pci/controller/pcie-xilinx.c
> index 9bd1a35cd5d8..b7083e995c45 100644
> --- a/drivers/pci/controller/pcie-xilinx.c
> +++ b/drivers/pci/controller/pcie-xilinx.c
> @@ -341,6 +341,9 @@ static void xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
> phys_addr_t msg_addr;
>
> port->msi_pages = __get_free_pages(GFP_KERNEL, 0);
> + if (unlikely(!port->msi_pages))
> + return;

Shouldn't the function return an error code for the calling function to
see? With this change the caller (xilinx_pcie_init_irq_domain) will
think that MSIs were enabled, but actually they weren't.

Steve

> +
> msg_addr = virt_to_phys((void *)port->msi_pages);
> pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
> pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
>


2019-03-25 21:32:09

by Kangjie Lu

[permalink] [raw]
Subject: [PATCH v2] pci: pcie-xilinx: fix a missing-check bug for __get_free_pages

In case __get_free_pages fail, the fix returns -ENOMEMto avoid
NULL pointer dereference.

Signed-off-by: Kangjie Lu <[email protected]>
Reviewed-by: Steven Price <[email protected]>
---
v2: caller is redefined to accept the error code, as suggested by
Steven Price <[email protected]>
---
drivers/pci/controller/pcie-xilinx.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/drivers/pci/controller/pcie-xilinx.c b/drivers/pci/controller/pcie-xilinx.c
index 9bd1a35cd5d8..abc214e94f7c 100644
--- a/drivers/pci/controller/pcie-xilinx.c
+++ b/drivers/pci/controller/pcie-xilinx.c
@@ -336,14 +336,19 @@ static const struct irq_domain_ops msi_domain_ops = {
* xilinx_pcie_enable_msi - Enable MSI support
* @port: PCIe port information
*/
-static void xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
+static int xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
{
phys_addr_t msg_addr;

port->msi_pages = __get_free_pages(GFP_KERNEL, 0);
+ if (unlikely(!port->msi_pages))
+ return -ENOMEM;
+
msg_addr = virt_to_phys((void *)port->msi_pages);
pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
+
+ return 0;
}

/* INTx Functions */
@@ -498,6 +503,7 @@ static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
struct device *dev = port->dev;
struct device_node *node = dev->of_node;
struct device_node *pcie_intc_node;
+ int ret;

/* Setup INTx */
pcie_intc_node = of_get_next_child(node, NULL);
@@ -526,7 +532,9 @@ static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
return -ENODEV;
}

- xilinx_pcie_enable_msi(port);
+ ret = xilinx_pcie_enable_msi(port);
+ if (ret)
+ return ret;
}

return 0;
--
2.17.1


2019-03-25 21:52:00

by Bjorn Helgaas

[permalink] [raw]
Subject: Re: [PATCH v2] pci: pcie-xilinx: fix a missing-check bug for __get_free_pages

Hi Kangjie,

Thanks for the patch!

Please update the subject line like this:

PCI: xilinx: Check for __get_free_pages() failure

You can always get a good idea of the style for subject lines by doing
something like this:

git log --oneline --follow drivers/pci/controller/pcie-xilinx.c

On Mon, Mar 25, 2019 at 04:31:13PM -0500, Kangjie Lu wrote:
> In case __get_free_pages fail, the fix returns -ENOMEMto avoid
> NULL pointer dereference.

s/In case/If/
s/__get_free_pages/__get_free_pages()/
s/fail/fails/
s/the fix returns/return/
s/-ENOMEMto/-ENOMEM to/

> Signed-off-by: Kangjie Lu <[email protected]>
> Reviewed-by: Steven Price <[email protected]>

I didn't see Steven's reviewed-by on the mailing list. I did see his
*review*, but his "Reviewed-by: Steven Price <[email protected]>"
implies that he reviewed it *and* believes it to ready for merging
(see Documentation/process/submitting-patches.rst for all the
details).

So we should only add the Reviewed-by tag after Steven himself posts
it.

> ---
> v2: caller is redefined to accept the error code, as suggested by
> Steven Price <[email protected]>
> ---
> drivers/pci/controller/pcie-xilinx.c | 12 ++++++++++--
> 1 file changed, 10 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/pci/controller/pcie-xilinx.c b/drivers/pci/controller/pcie-xilinx.c
> index 9bd1a35cd5d8..abc214e94f7c 100644
> --- a/drivers/pci/controller/pcie-xilinx.c
> +++ b/drivers/pci/controller/pcie-xilinx.c
> @@ -336,14 +336,19 @@ static const struct irq_domain_ops msi_domain_ops = {
> * xilinx_pcie_enable_msi - Enable MSI support
> * @port: PCIe port information
> */
> -static void xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
> +static int xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
> {
> phys_addr_t msg_addr;
>
> port->msi_pages = __get_free_pages(GFP_KERNEL, 0);
> + if (unlikely(!port->msi_pages))
> + return -ENOMEM;

No need to use "unlikely()" here. It *is* unlikely that
__get_free_pages() will fail, but the annotation clutters the code a
bit, so I prefer to avoid it except for performance paths.

This should probably be documented somewhere in
Documentation/process/, but regrettably, it isn't (yet).

> msg_addr = virt_to_phys((void *)port->msi_pages);
> pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
> pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
> +
> + return 0;
> }
>
> /* INTx Functions */
> @@ -498,6 +503,7 @@ static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
> struct device *dev = port->dev;
> struct device_node *node = dev->of_node;
> struct device_node *pcie_intc_node;
> + int ret;
>
> /* Setup INTx */
> pcie_intc_node = of_get_next_child(node, NULL);
> @@ -526,7 +532,9 @@ static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
> return -ENODEV;
> }
>
> - xilinx_pcie_enable_msi(port);
> + ret = xilinx_pcie_enable_msi(port);
> + if (ret)
> + return ret;
> }
>
> return 0;
> --
> 2.17.1
>
>
> _______________________________________________
> linux-arm-kernel mailing list
> [email protected]
> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

2019-03-25 22:21:39

by Kangjie Lu

[permalink] [raw]
Subject: [PATCH v3] PCI: xilinx: Check for __get_free_pages() failure

If __get_free_pages() fails, the patch returns -ENOMEM to avoid
NULL pointer dereference.

Signed-off-by: Kangjie Lu <[email protected]>
---
v3: remove "unlikely", as suggested by Bjorn Helgaas.
v2: caller is redefined to accept the error code, as suggested by
Steven Price <[email protected]>
---
drivers/pci/controller/pcie-xilinx.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/drivers/pci/controller/pcie-xilinx.c b/drivers/pci/controller/pcie-xilinx.c
index 9bd1a35cd5d8..abc214e94f7c 100644
--- a/drivers/pci/controller/pcie-xilinx.c
+++ b/drivers/pci/controller/pcie-xilinx.c
@@ -336,14 +336,19 @@ static const struct irq_domain_ops msi_domain_ops = {
* xilinx_pcie_enable_msi - Enable MSI support
* @port: PCIe port information
*/
-static void xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
+static int xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
{
phys_addr_t msg_addr;

port->msi_pages = __get_free_pages(GFP_KERNEL, 0);
+ if (!port->msi_pages)
+ return -ENOMEM;
+
msg_addr = virt_to_phys((void *)port->msi_pages);
pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
+
+ return 0;
}

/* INTx Functions */
@@ -498,6 +503,7 @@ static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
struct device *dev = port->dev;
struct device_node *node = dev->of_node;
struct device_node *pcie_intc_node;
+ int ret;

/* Setup INTx */
pcie_intc_node = of_get_next_child(node, NULL);
@@ -526,7 +532,9 @@ static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
return -ENODEV;
}

- xilinx_pcie_enable_msi(port);
+ ret = xilinx_pcie_enable_msi(port);
+ if (ret)
+ return ret;
}

return 0;
--
2.17.1


2019-03-26 11:41:48

by Steven Price

[permalink] [raw]
Subject: Re: [PATCH v3] PCI: xilinx: Check for __get_free_pages() failure

On 25/03/2019 22:19, Kangjie Lu wrote:
> If __get_free_pages() fails, the patch returns -ENOMEM to avoid

As Bjorn suggested s/the patch returns/return/ would suffice and is
slightly easier to read. But I'm happy either way.

> NULL pointer dereference.
>
> Signed-off-by: Kangjie Lu <[email protected]>

Reviewed-by: Steven Price <[email protected]>

> ---
> v3: remove "unlikely", as suggested by Bjorn Helgaas.
> v2: caller is redefined to accept the error code, as suggested by
> Steven Price <[email protected]>
> ---
> drivers/pci/controller/pcie-xilinx.c | 12 ++++++++++--
> 1 file changed, 10 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/pci/controller/pcie-xilinx.c b/drivers/pci/controller/pcie-xilinx.c
> index 9bd1a35cd5d8..abc214e94f7c 100644
> --- a/drivers/pci/controller/pcie-xilinx.c
> +++ b/drivers/pci/controller/pcie-xilinx.c
> @@ -336,14 +336,19 @@ static const struct irq_domain_ops msi_domain_ops = {
> * xilinx_pcie_enable_msi - Enable MSI support
> * @port: PCIe port information
> */
> -static void xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
> +static int xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
> {
> phys_addr_t msg_addr;
>
> port->msi_pages = __get_free_pages(GFP_KERNEL, 0);
> + if (!port->msi_pages)
> + return -ENOMEM;
> +
> msg_addr = virt_to_phys((void *)port->msi_pages);
> pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
> pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
> +
> + return 0;
> }
>
> /* INTx Functions */
> @@ -498,6 +503,7 @@ static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
> struct device *dev = port->dev;
> struct device_node *node = dev->of_node;
> struct device_node *pcie_intc_node;
> + int ret;
>
> /* Setup INTx */
> pcie_intc_node = of_get_next_child(node, NULL);
> @@ -526,7 +532,9 @@ static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
> return -ENODEV;
> }
>
> - xilinx_pcie_enable_msi(port);
> + ret = xilinx_pcie_enable_msi(port);
> + if (ret)
> + return ret;
> }
>
> return 0;
>


2019-03-27 13:39:06

by Mukesh Ojha

[permalink] [raw]
Subject: Re: [PATCH v3] PCI: xilinx: Check for __get_free_pages() failure


On 3/26/2019 3:49 AM, Kangjie Lu wrote:
> If __get_free_pages() fails, the patch returns -ENOMEM to avoid
> NULL pointer dereference.
>
> Signed-off-by: Kangjie Lu <[email protected]>


Reviewed-by: Mukesh Ojha <[email protected]>

-Mukesh

> ---
> v3: remove "unlikely", as suggested by Bjorn Helgaas.
> v2: caller is redefined to accept the error code, as suggested by
> Steven Price <[email protected]>
> ---
> drivers/pci/controller/pcie-xilinx.c | 12 ++++++++++--
> 1 file changed, 10 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/pci/controller/pcie-xilinx.c b/drivers/pci/controller/pcie-xilinx.c
> index 9bd1a35cd5d8..abc214e94f7c 100644
> --- a/drivers/pci/controller/pcie-xilinx.c
> +++ b/drivers/pci/controller/pcie-xilinx.c
> @@ -336,14 +336,19 @@ static const struct irq_domain_ops msi_domain_ops = {
> * xilinx_pcie_enable_msi - Enable MSI support
> * @port: PCIe port information
> */
> -static void xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
> +static int xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
> {
> phys_addr_t msg_addr;
>
> port->msi_pages = __get_free_pages(GFP_KERNEL, 0);
> + if (!port->msi_pages)
> + return -ENOMEM;
> +
> msg_addr = virt_to_phys((void *)port->msi_pages);
> pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
> pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
> +
> + return 0;
> }
>
> /* INTx Functions */
> @@ -498,6 +503,7 @@ static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
> struct device *dev = port->dev;
> struct device_node *node = dev->of_node;
> struct device_node *pcie_intc_node;
> + int ret;
>
> /* Setup INTx */
> pcie_intc_node = of_get_next_child(node, NULL);
> @@ -526,7 +532,9 @@ static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
> return -ENODEV;
> }
>
> - xilinx_pcie_enable_msi(port);
> + ret = xilinx_pcie_enable_msi(port);
> + if (ret)
> + return ret;
> }
>
> return 0;

2019-03-29 16:37:54

by Lorenzo Pieralisi

[permalink] [raw]
Subject: Re: [PATCH v3] PCI: xilinx: Check for __get_free_pages() failure

On Mon, Mar 25, 2019 at 05:19:09PM -0500, Kangjie Lu wrote:
> If __get_free_pages() fails, the patch returns -ENOMEM to avoid
> NULL pointer dereference.
>
> Signed-off-by: Kangjie Lu <[email protected]>
> ---
> v3: remove "unlikely", as suggested by Bjorn Helgaas.
> v2: caller is redefined to accept the error code, as suggested by
> Steven Price <[email protected]>
> ---
> drivers/pci/controller/pcie-xilinx.c | 12 ++++++++++--
> 1 file changed, 10 insertions(+), 2 deletions(-)

Updated commit log and applied to pci/xilinx for v5.2.

Thanks,
Lorenzo

> diff --git a/drivers/pci/controller/pcie-xilinx.c b/drivers/pci/controller/pcie-xilinx.c
> index 9bd1a35cd5d8..abc214e94f7c 100644
> --- a/drivers/pci/controller/pcie-xilinx.c
> +++ b/drivers/pci/controller/pcie-xilinx.c
> @@ -336,14 +336,19 @@ static const struct irq_domain_ops msi_domain_ops = {
> * xilinx_pcie_enable_msi - Enable MSI support
> * @port: PCIe port information
> */
> -static void xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
> +static int xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
> {
> phys_addr_t msg_addr;
>
> port->msi_pages = __get_free_pages(GFP_KERNEL, 0);
> + if (!port->msi_pages)
> + return -ENOMEM;
> +
> msg_addr = virt_to_phys((void *)port->msi_pages);
> pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
> pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
> +
> + return 0;
> }
>
> /* INTx Functions */
> @@ -498,6 +503,7 @@ static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
> struct device *dev = port->dev;
> struct device_node *node = dev->of_node;
> struct device_node *pcie_intc_node;
> + int ret;
>
> /* Setup INTx */
> pcie_intc_node = of_get_next_child(node, NULL);
> @@ -526,7 +532,9 @@ static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
> return -ENODEV;
> }
>
> - xilinx_pcie_enable_msi(port);
> + ret = xilinx_pcie_enable_msi(port);
> + if (ret)
> + return ret;
> }
>
> return 0;
> --
> 2.17.1
>