diff mbox

[1/1] kernel code that do not handle NULL return of kmem_cache_zalloc

Message ID 1385549465-15945-1-git-send-email-yizhouzhou@ict.ac.cn
State New, archived
Headers show

Commit Message

Zhouyi Zhou Nov. 27, 2013, 10:51 a.m. UTC
I do a grep for kmem_cache_zalloc and kmem_cache_alloc
in kernel tree, and find some code do not handle NULL
return of kmem_cache_zalloc correctly


Signed-off-by: Zhouyi Zhou <yizhouzhou@ict.ac.cn>
---
 arch/powerpc/kvm/book3s_32_mmu_host.c |    5 +++++
 drivers/iommu/omap-iommu.c            |    3 ++-
 fs/jffs2/malloc.c                     |    4 ++++
 3 files changed, 11 insertions(+), 1 deletion(-)

Comments

Zhouyi Zhou Dec. 2, 2013, 3:07 a.m. UTC | #1
ping
> I do a grep for kmem_cache_zalloc and kmem_cache_alloc
> in kernel tree, and find some code do not handle NULL
> return of kmem_cache_zalloc correctly


> Signed-off-by: Zhouyi Zhou <yizhouzhou@ict.ac.cn>
> ---
>  arch/powerpc/kvm/book3s_32_mmu_host.c |    5 +++++
>  drivers/iommu/omap-iommu.c            |    3 ++-
>  fs/jffs2/malloc.c                     |    4 ++++
>  3 files changed, 11 insertions(+), 1 deletion(-)

> diff --git a/arch/powerpc/kvm/book3s_32_mmu_host.c b/arch/powerpc/kvm/book3s_32_mmu_host.c
> index 3a0abd2..5fac89d 100644
> --- a/arch/powerpc/kvm/book3s_32_mmu_host.c
> +++ b/arch/powerpc/kvm/book3s_32_mmu_host.c
> @@ -243,6 +243,11 @@ next_pteg:
>  	/* Now tell our Shadow PTE code about the new page */
>  
>  	pte = kvmppc_mmu_hpte_cache_next(vcpu);
> +	if (!pte) {
> +		kvm_release_pfn_clean(hpaddr >> PAGE_SHIFT);
> +		r = -EAGAIN;
> +		goto out;
> +	}
>  
>  	dprintk_mmu("KVM: %c%c Map 0x%llx: [%lx] 0x%llx (0x%llx) -> %lx\n",
>  		    orig_pte->may_write ? 'w' : '-',
> diff --git a/drivers/iommu/omap-iommu.c b/drivers/iommu/omap-iommu.c
> index bcd78a7..5155714 100644
> --- a/drivers/iommu/omap-iommu.c
> +++ b/drivers/iommu/omap-iommu.c
> @@ -551,7 +551,8 @@ static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da)
>  		dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
>  	} else {
>  		/* We raced, free the reduniovant table */
> -		iopte_free(iopte);
> +		if (iopte)
> +			iopte_free(iopte);
>  	}
>  
>  pte_ready:
> diff --git a/fs/jffs2/malloc.c b/fs/jffs2/malloc.c
> index 4f47aa2..58e2336 100644
> --- a/fs/jffs2/malloc.c
> +++ b/fs/jffs2/malloc.c
> @@ -287,6 +287,8 @@ struct jffs2_xattr_datum *jffs2_alloc_xattr_datum(void)
>  {
>  	struct jffs2_xattr_datum *xd;
>  	xd = kmem_cache_zalloc(xattr_datum_cache, GFP_KERNEL);
> +	if (!xd)
> +		return NULL;
>  	dbg_memalloc("%p\n", xd);
>  
>  	xd->class = RAWNODE_CLASS_XATTR_DATUM;
> @@ -305,6 +307,8 @@ struct jffs2_xattr_ref *jffs2_alloc_xattr_ref(void)
>  {
>  	struct jffs2_xattr_ref *ref;
>  	ref = kmem_cache_zalloc(xattr_ref_cache, GFP_KERNEL);
> +	if (!ref)
> +		return NULL;
>  	dbg_memalloc("%p\n", ref);
>  
>  	ref->class = RAWNODE_CLASS_XATTR_REF;
> -- 
> 1.7.10.4





--
To unsubscribe from this list: send the line "unsubscribe kvm-ppc" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Alexander Graf Dec. 2, 2013, 9:54 a.m. UTC | #2
On 02.12.2013, at 04:07, Zhouyi Zhou <yizhouzhou@ict.ac.cn> wrote:

> ping
>>  I do a grep for kmem_cache_zalloc and kmem_cache_alloc
>>  in kernel tree, and find some code do not handle NULL
>>  return of kmem_cache_zalloc correctly
>>  
>>  
>>  Signed-off-by: Zhouyi Zhou <yizhouzhou@ict.ac.cn>

Thanks a lot for the patch. I'd assume we want something slightly more clever even, similar to Paul's d78bca729.

Please beware that it's usually not a good idea to post patches that span multiple subtrees. I don't want to apply a patch to my kvm tree that touches jffs2 for example, as that's out of my scope. It usually makes a maintainer's life easier if you split up a patch like this according to tree responsibilities.


Alex

--
To unsubscribe from this list: send the line "unsubscribe kvm-ppc" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/arch/powerpc/kvm/book3s_32_mmu_host.c b/arch/powerpc/kvm/book3s_32_mmu_host.c
index 3a0abd2..5fac89d 100644
--- a/arch/powerpc/kvm/book3s_32_mmu_host.c
+++ b/arch/powerpc/kvm/book3s_32_mmu_host.c
@@ -243,6 +243,11 @@  next_pteg:
 	/* Now tell our Shadow PTE code about the new page */
 
 	pte = kvmppc_mmu_hpte_cache_next(vcpu);
+	if (!pte) {
+		kvm_release_pfn_clean(hpaddr >> PAGE_SHIFT);
+		r = -EAGAIN;
+		goto out;
+	}
 
 	dprintk_mmu("KVM: %c%c Map 0x%llx: [%lx] 0x%llx (0x%llx) -> %lx\n",
 		    orig_pte->may_write ? 'w' : '-',
diff --git a/drivers/iommu/omap-iommu.c b/drivers/iommu/omap-iommu.c
index bcd78a7..5155714 100644
--- a/drivers/iommu/omap-iommu.c
+++ b/drivers/iommu/omap-iommu.c
@@ -551,7 +551,8 @@  static u32 *iopte_alloc(struct omap_iommu *obj, u32 *iopgd, u32 da)
 		dev_vdbg(obj->dev, "%s: a new pte:%p\n", __func__, iopte);
 	} else {
 		/* We raced, free the reduniovant table */
-		iopte_free(iopte);
+		if (iopte)
+			iopte_free(iopte);
 	}
 
 pte_ready:
diff --git a/fs/jffs2/malloc.c b/fs/jffs2/malloc.c
index 4f47aa2..58e2336 100644
--- a/fs/jffs2/malloc.c
+++ b/fs/jffs2/malloc.c
@@ -287,6 +287,8 @@  struct jffs2_xattr_datum *jffs2_alloc_xattr_datum(void)
 {
 	struct jffs2_xattr_datum *xd;
 	xd = kmem_cache_zalloc(xattr_datum_cache, GFP_KERNEL);
+	if (!xd)
+		return NULL;
 	dbg_memalloc("%p\n", xd);
 
 	xd->class = RAWNODE_CLASS_XATTR_DATUM;
@@ -305,6 +307,8 @@  struct jffs2_xattr_ref *jffs2_alloc_xattr_ref(void)
 {
 	struct jffs2_xattr_ref *ref;
 	ref = kmem_cache_zalloc(xattr_ref_cache, GFP_KERNEL);
+	if (!ref)
+		return NULL;
 	dbg_memalloc("%p\n", ref);
 
 	ref->class = RAWNODE_CLASS_XATTR_REF;