From 7ebc34ddccf343029273c88fd3f8fa8b40976bf2 Mon Sep 17 00:00:00 2001 From: Balazs Gerofi Date: Thu, 18 Aug 2016 14:31:52 +0900 Subject: [PATCH] do_fork(): fix tids memory leak; additional sanity checks --- arch/x86/kernel/memory.c | 8 ++++++++ kernel/host.c | 5 +++++ kernel/include/cls.h | 1 + kernel/mem.c | 11 +++++++---- kernel/process.c | 1 - kernel/syscall.c | 2 ++ 6 files changed, 23 insertions(+), 5 deletions(-) diff --git a/arch/x86/kernel/memory.c b/arch/x86/kernel/memory.c index e9b70a45..782df6c2 100644 --- a/arch/x86/kernel/memory.c +++ b/arch/x86/kernel/memory.c @@ -87,11 +87,19 @@ void ihk_mc_free_pages(void *p, int npages) void *ihk_mc_allocate(int size, int flag) { + if (!cpu_local_var(kmalloc_initialized)) { + kprintf("%s: error, kmalloc not yet initialized\n", __FUNCTION__); + return NULL; + } return kmalloc(size, IHK_MC_AP_NOWAIT); } void ihk_mc_free(void *p) { + if (!cpu_local_var(kmalloc_initialized)) { + kprintf("%s: error, kmalloc not yet initialized\n", __FUNCTION__); + return; + } kfree(p); } diff --git a/kernel/host.c b/kernel/host.c index 3444a349..5cec52e2 100644 --- a/kernel/host.c +++ b/kernel/host.c @@ -373,6 +373,11 @@ static int process_msg_prepare_process(unsigned long rphys) } n = p->num_sections; + if (n > 16) { + kprintf("%s: ERROR: more ELF sections than 16??\n", + __FUNCTION__); + return -ENOMEM; + } dkprintf("# of sections: %d\n", n); if((pn = kmalloc(sizeof(struct program_load_desc) diff --git a/kernel/include/cls.h b/kernel/include/cls.h index 58532c08..d2521b11 100644 --- a/kernel/include/cls.h +++ b/kernel/include/cls.h @@ -73,6 +73,7 @@ struct cpu_local_var { int in_interrupt; int no_preempt; int timer_enabled; + int kmalloc_initialized; } __attribute__((aligned(64))); diff --git a/kernel/mem.c b/kernel/mem.c index f0e4c12b..fc8f109d 100644 --- a/kernel/mem.c +++ b/kernel/mem.c @@ -664,8 +664,8 @@ static struct alloc *allochash[HASHNUM]; static struct location *lochash[HASHNUM]; static ihk_spinlock_t alloclock; int runcount; -static unsigned char *page; -static int space; +static unsigned char *page = NULL; +static int space = 0; static void *dalloc(unsigned long size) { @@ -896,14 +896,17 @@ void kmalloc_init(void) h->size = 0; register_kmalloc(); + v->kmalloc_initialized = 1; memdebug = find_command_line("memdebug"); for (i = 0; i < HASHNUM; i++) { allochash[i] = NULL; lochash[i] = NULL; } - page = allocate_pages(16, IHK_MC_AP_NOWAIT); - space = 16 * 4096; + if (!page) { + page = allocate_pages(16, IHK_MC_AP_NOWAIT); + space = 16 * 4096; + } ihk_mc_spinlock_init(&alloclock); } diff --git a/kernel/process.c b/kernel/process.c index 7ed1f98a..12e43eb8 100644 --- a/kernel/process.c +++ b/kernel/process.c @@ -963,7 +963,6 @@ enum ihk_mc_pt_attribute common_vrflag_to_ptattr(unsigned long flag, uint64_t fa return attr; } -/* XXX: インデントを揃える必要がある */ int add_process_memory_range(struct process_vm *vm, unsigned long start, unsigned long end, unsigned long phys, unsigned long flag, diff --git a/kernel/syscall.c b/kernel/syscall.c index 90ce79c1..9688f871 100644 --- a/kernel/syscall.c +++ b/kernel/syscall.c @@ -1937,6 +1937,8 @@ unsigned long do_fork(int clone_flags, unsigned long newsp, newproc->tids[i].thread = NULL; ++newproc->nr_tids; } + + kfree(tids); } /* Find an unused TID */