linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
* [PATCH kernel] powerpc/powernv: Check kzalloc() return in pnv_pci_table_alloc
@ 2017-03-27  3:28 Alexey Kardashevskiy
  2017-03-27  4:24 ` David Gibson
  2017-03-27  7:45 ` Michael Ellerman
  0 siblings, 2 replies; 3+ messages in thread
From: Alexey Kardashevskiy @ 2017-03-27  3:28 UTC (permalink / raw)
  To: linuxppc-dev; +Cc: Alexey Kardashevskiy, David Gibson, Gavin Shan

pnv_pci_table_alloc() ignores posible failure from kzalloc_node(),
this adds a check. There are 2 callers of pnv_pci_table_alloc(),
one already checks for tbl!=NULL, this adds BUG_ON() to the other path
which only happens during boot time in IODA1 and not expected to fail.

Fixes: 0eaf4defc7c4 ("powerpc/spapr: vfio: Switch from iommu_table to new iommu_table_group")
Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
---
 arch/powerpc/platforms/powernv/pci-ioda.c | 1 +
 arch/powerpc/platforms/powernv/pci.c      | 3 +++
 2 files changed, 4 insertions(+)

diff --git a/arch/powerpc/platforms/powernv/pci-ioda.c b/arch/powerpc/platforms/powernv/pci-ioda.c
index e36738291c32..dcda3f6b7d36 100644
--- a/arch/powerpc/platforms/powernv/pci-ioda.c
+++ b/arch/powerpc/platforms/powernv/pci-ioda.c
@@ -2128,6 +2128,7 @@ static void pnv_pci_ioda1_setup_dma_pe(struct pnv_phb *phb,
 
 found:
 	tbl = pnv_pci_table_alloc(phb->hose->node);
+	BUG_ON(!tbl);
 	iommu_register_group(&pe->table_group, phb->hose->global_number,
 			pe->pe_number);
 	pnv_pci_link_table_and_group(phb->hose->node, 0, tbl, &pe->table_group);
diff --git a/arch/powerpc/platforms/powernv/pci.c b/arch/powerpc/platforms/powernv/pci.c
index eb835e977e33..9acdf6889c0d 100644
--- a/arch/powerpc/platforms/powernv/pci.c
+++ b/arch/powerpc/platforms/powernv/pci.c
@@ -766,6 +766,9 @@ struct iommu_table *pnv_pci_table_alloc(int nid)
 	struct iommu_table *tbl;
 
 	tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, nid);
+	if (!tbl)
+		return NULL;
+
 	INIT_LIST_HEAD_RCU(&tbl->it_group_list);
 
 	return tbl;
-- 
2.11.0

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

* Re: [PATCH kernel] powerpc/powernv: Check kzalloc() return in pnv_pci_table_alloc
  2017-03-27  3:28 [PATCH kernel] powerpc/powernv: Check kzalloc() return in pnv_pci_table_alloc Alexey Kardashevskiy
@ 2017-03-27  4:24 ` David Gibson
  2017-03-27  7:45 ` Michael Ellerman
  1 sibling, 0 replies; 3+ messages in thread
From: David Gibson @ 2017-03-27  4:24 UTC (permalink / raw)
  To: Alexey Kardashevskiy; +Cc: linuxppc-dev, Gavin Shan

[-- Attachment #1: Type: text/plain, Size: 2022 bytes --]

On Mon, Mar 27, 2017 at 02:28:03PM +1100, Alexey Kardashevskiy wrote:
> pnv_pci_table_alloc() ignores posible failure from kzalloc_node(),
> this adds a check. There are 2 callers of pnv_pci_table_alloc(),
> one already checks for tbl!=NULL, this adds BUG_ON() to the other path
> which only happens during boot time in IODA1 and not expected to fail.
> 
> Fixes: 0eaf4defc7c4 ("powerpc/spapr: vfio: Switch from iommu_table to new iommu_table_group")
> Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>

Reviewed-by: David Gibson <david@gibson.dropbear.id.au>

> ---
>  arch/powerpc/platforms/powernv/pci-ioda.c | 1 +
>  arch/powerpc/platforms/powernv/pci.c      | 3 +++
>  2 files changed, 4 insertions(+)
> 
> diff --git a/arch/powerpc/platforms/powernv/pci-ioda.c b/arch/powerpc/platforms/powernv/pci-ioda.c
> index e36738291c32..dcda3f6b7d36 100644
> --- a/arch/powerpc/platforms/powernv/pci-ioda.c
> +++ b/arch/powerpc/platforms/powernv/pci-ioda.c
> @@ -2128,6 +2128,7 @@ static void pnv_pci_ioda1_setup_dma_pe(struct pnv_phb *phb,
>  
>  found:
>  	tbl = pnv_pci_table_alloc(phb->hose->node);
> +	BUG_ON(!tbl);
>  	iommu_register_group(&pe->table_group, phb->hose->global_number,
>  			pe->pe_number);
>  	pnv_pci_link_table_and_group(phb->hose->node, 0, tbl, &pe->table_group);
> diff --git a/arch/powerpc/platforms/powernv/pci.c b/arch/powerpc/platforms/powernv/pci.c
> index eb835e977e33..9acdf6889c0d 100644
> --- a/arch/powerpc/platforms/powernv/pci.c
> +++ b/arch/powerpc/platforms/powernv/pci.c
> @@ -766,6 +766,9 @@ struct iommu_table *pnv_pci_table_alloc(int nid)
>  	struct iommu_table *tbl;
>  
>  	tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, nid);
> +	if (!tbl)
> +		return NULL;
> +
>  	INIT_LIST_HEAD_RCU(&tbl->it_group_list);
>  
>  	return tbl;

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

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

* Re: [PATCH kernel] powerpc/powernv: Check kzalloc() return in pnv_pci_table_alloc
  2017-03-27  3:28 [PATCH kernel] powerpc/powernv: Check kzalloc() return in pnv_pci_table_alloc Alexey Kardashevskiy
  2017-03-27  4:24 ` David Gibson
@ 2017-03-27  7:45 ` Michael Ellerman
  1 sibling, 0 replies; 3+ messages in thread
From: Michael Ellerman @ 2017-03-27  7:45 UTC (permalink / raw)
  To: Alexey Kardashevskiy, linuxppc-dev
  Cc: Alexey Kardashevskiy, Gavin Shan, David Gibson

Alexey Kardashevskiy <aik@ozlabs.ru> writes:

> pnv_pci_table_alloc() ignores posible failure from kzalloc_node(),
> this adds a check. There are 2 callers of pnv_pci_table_alloc(),
> one already checks for tbl!=NULL, this adds BUG_ON() to the other path
> which only happens during boot time in IODA1 and not expected to fail.
>
> Fixes: 0eaf4defc7c4 ("powerpc/spapr: vfio: Switch from iommu_table to new iommu_table_group")
> Signed-off-by: Alexey Kardashevskiy <aik@ozlabs.ru>
> ---
>  arch/powerpc/platforms/powernv/pci-ioda.c | 1 +
>  arch/powerpc/platforms/powernv/pci.c      | 3 +++
>  2 files changed, 4 insertions(+)
>
> diff --git a/arch/powerpc/platforms/powernv/pci-ioda.c b/arch/powerpc/platforms/powernv/pci-ioda.c
> index e36738291c32..dcda3f6b7d36 100644
> --- a/arch/powerpc/platforms/powernv/pci-ioda.c
> +++ b/arch/powerpc/platforms/powernv/pci-ioda.c
> @@ -2128,6 +2128,7 @@ static void pnv_pci_ioda1_setup_dma_pe(struct pnv_phb *phb,
>  
>  found:
>  	tbl = pnv_pci_table_alloc(phb->hose->node);
> +	BUG_ON(!tbl);
>  	iommu_register_group(&pe->table_group, phb->hose->global_number,
>  			pe->pe_number);
>  	pnv_pci_link_table_and_group(phb->hose->node, 0, tbl, &pe->table_group);

Please don't add BUG_ON()'s for non-critical things like this.

I know memory allocation isn't expected to fail during boot, but don't
make people's life harder by BUG'ing.

Ideally you'd just return failure to the caller and eventually we'd
print a message at some higher level. In a case like this where the
function returns void, rather than restructuring everything to pass
a return value you can just do:

if (WARN_ON(!tbl))
	return;

cheers

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

end of thread, other threads:[~2017-03-27  7:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-03-27  3:28 [PATCH kernel] powerpc/powernv: Check kzalloc() return in pnv_pci_table_alloc Alexey Kardashevskiy
2017-03-27  4:24 ` David Gibson
2017-03-27  7:45 ` Michael Ellerman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).