From: Peter Dinda Date: Sat, 7 Jul 2012 23:33:46 +0000 (-0500) Subject: Expose kmalloc flags within linux_module + add further debugging features for heap... X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?p=palacios.git;a=commitdiff_plain;h=06ad2088c1bf59d4d6d11986d4de2195e64baaac Expose kmalloc flags within linux_module + add further debugging features for heap alloc --- diff --git a/linux_module/palacios-stubs.c b/linux_module/palacios-stubs.c index 60b481f..5ced6d2 100644 --- a/linux_module/palacios-stubs.c +++ b/linux_module/palacios-stubs.c @@ -24,6 +24,12 @@ #include "mm.h" +// The following can be used to track heap bugs +// zero memory after allocation +#define ALLOC_ZERO_MEM 0 +// pad allocations by this many bytes on both ends of block +#define ALLOC_PAD 0 + u32 pg_allocs = 0; u32 pg_frees = 0; @@ -168,32 +174,44 @@ void palacios_free_pages(void * page_paddr, int num_pages) { } +void * +palacios_alloc_extended(unsigned int size, unsigned int flags) { + void * addr = NULL; + + addr = kmalloc(size+2*ALLOC_PAD, flags); + + if (!addr) { + ERROR("ALERT ALERT kmalloc has FAILED FAILED FAILED\n"); + return NULL; + } + + mallocs++; + +#if ALLOC_ZERO_MEM + memset(addr,0,size+2*ALLOC_PAD); +#endif + + return addr+ALLOC_PAD; +} + + /** * Allocates 'size' bytes of kernel memory. * Returns the kernel virtual address of the memory allocated. */ void * palacios_alloc(unsigned int size) { - void * addr = NULL; // It is very important that this test remains since // this function is used extensively throughout palacios and the linux // module, both in places where interrupts are off and where they are on // a GFP_KERNEL call, when done with interrupts off can lead to DEADLOCK if (irqs_disabled()) { - addr = kmalloc(size, GFP_ATOMIC); + return palacios_alloc_extended(size,GFP_ATOMIC); } else { - addr = kmalloc(size, GFP_KERNEL); + return palacios_alloc_extended(size,GFP_KERNEL); } - if (!addr) { - ERROR("ALERT ALERT kmalloc has FAILED FAILED FAILED\n"); - return NULL; - } - - mallocs++; - - return addr; } /** @@ -205,7 +223,7 @@ palacios_free( ) { frees++; - kfree(addr); + kfree(addr-ALLOC_PAD); return; } diff --git a/linux_module/palacios.h b/linux_module/palacios.h index 9bee6c9..43ffa57 100644 --- a/linux_module/palacios.h +++ b/linux_module/palacios.h @@ -107,6 +107,7 @@ void palacios_print(const char *fmt, ...); void *palacios_allocate_pages(int num_pages, unsigned int alignment); void palacios_free_pages(void *page_addr, int num_pages); void *palacios_alloc(unsigned int size); +void *palacios_alloc_extended(unsigned int size, unsigned int flags); void palacios_free(void *); void *palacios_vaddr_to_paddr(void *vaddr); void *palacios_paddr_to_vaddr(void *paddr);