Skip to contentSkip to conversation

MT-VPE : Fix the usage of kmalloc

PATCH In review tiejun.chen 2 participants 2 messages Mar 13, 2008 MIPS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 MT-VPE : Fix the usage of kmalloc

The return value of kmalloc() should be check, otherwise it is potential risk.

---
 vpe.c |    4 ++++
 1 file changed, 4 insertions(+)
arch/mips/kernel/vpe.c +4 -0
index c06eb81..35767de 100644
@@ -885,6 +885,10 @@ static int vpe_elfload(struct vpe * v)
        }
 
        v->load_addr = alloc_progmem(mod.core_size);
+#ifndef CONFIG_MIPS_VPE_LOADER_TOM
+       if (!(v->load_addr))
+               return -ENOMEM;
+#endif
        memset(v->load_addr, 0, mod.core_size);
 
        printk("VPE loader: loading to %p\n", v->load_addr);
#1 lore raw
Quoted 14 lines · tiejun.chen

The return value of kmalloc() should be check, otherwise it is potential risk.

---
 vpe.c |    4 ++++
 1 file changed, 4 insertions(+)
arch/mips/kernel/vpe.c +0 -0
index c06eb81..35767de 100644

arch/mips/kernel/vpe.c static int vpe_elfload(struct vpe * v) · line 891

Quoted 7 lines
arch/mips/kernel/vpe.c +4 -0
@@ -885,6 +885,10 @@ static int vpe_elfload(struct vpe * v)
        }
 
        v->load_addr = alloc_progmem(mod.core_size);
+#ifndef CONFIG_MIPS_VPE_LOADER_TOM
+       if (!(v->load_addr))
+               return -ENOMEM;
+#endif

Your mailer converted the tabs into whitespace so the patch doesn't apply.

Anyway, I fixed things in a slightly different way which hopefully avoids throwing in more #ifdefs and will safe a little code for the !CONFIG_MIPS_VPE_LOADER_TOM case.

Thanks,

Ralf

arch/mips/kernel/vpe.c +15 -6
index eed2dc4..39804c5 100644
@@ -262,13 +262,21 @@ void dump_mtregs(void)
 /* Find some VPE program space  */
 static void *alloc_progmem(unsigned long len)
 {
+	void *addr;
+
 #ifdef CONFIG_MIPS_VPE_LOADER_TOM
-	/* this means you must tell linux to use less memory than you physically have */
-	return pfn_to_kaddr(max_pfn);
+	/*
+	 * This means you must tell Linux to use less memory than you
+	 * physically have, for example by passing a mem= boot argument.
+	 */
+	addr = pfn_to_kaddr(max_pfn);
+	memset(addr, 0, len);
 #else
-	// simple grab some mem for now
-	return kmalloc(len, GFP_KERNEL);
+	/* simple grab some mem for now */
+	addr = kzalloc(len, GFP_KERNEL);
 #endif
+
+	return addr;
 }
 
 static void release_progmem(void *ptr)
@@ -884,9 +892,10 @@ static int vpe_elfload(struct vpe * v)
 	}
 
 	v->load_addr = alloc_progmem(mod.core_size);
-	memset(v->load_addr, 0, mod.core_size);
+	if (!v->load_addr)
+		return -ENOMEM;
 
-	printk("VPE loader: loading to %p\n", v->load_addr);
+	pr_info("VPE loader: loading to %p\n", v->load_addr);
 
 	if (relocate) {
 		for (i = 0; i < hdr->e_shnum; i++) {