From f5857cfc9e6f12f5677d34311aa2b4ba61a11e70 Mon Sep 17 00:00:00 2001 From: Balazs Gerofi Date: Wed, 17 Aug 2016 18:02:05 +0900 Subject: [PATCH] MM: use ihk_mc_{alloc/free}_pages() everywhere and fix free_pages() on kmalloc()ed object bug --- arch/x86/kernel/memory.c | 13 ++++--------- kernel/devobj.c | 6 +++--- kernel/host.c | 13 +++++++------ kernel/process.c | 21 +++++++++++---------- kernel/sysfs.c | 26 +++++++++++++------------- lib/include/ihk/mm.h | 2 +- 6 files changed, 39 insertions(+), 42 deletions(-) diff --git a/arch/x86/kernel/memory.c b/arch/x86/kernel/memory.c index b2a362cb..e9b70a45 100644 --- a/arch/x86/kernel/memory.c +++ b/arch/x86/kernel/memory.c @@ -23,6 +23,7 @@ #include #include #include +#include #define dkprintf(...) do { if (0) kprintf(__VA_ARGS__); } while (0) #define ekprintf(...) kprintf(__VA_ARGS__) @@ -84,20 +85,14 @@ void ihk_mc_free_pages(void *p, int npages) pa_ops->free_page(p, npages); } -void *ihk_mc_allocate(int size, enum ihk_mc_ap_flag flag) +void *ihk_mc_allocate(int size, int flag) { - if (pa_ops && pa_ops->alloc) - return pa_ops->alloc(size, flag); - else - return ihk_mc_alloc_pages(1, flag); + return kmalloc(size, IHK_MC_AP_NOWAIT); } void ihk_mc_free(void *p) { - if (pa_ops && pa_ops->free) - return pa_ops->free(p); - else - return ihk_mc_free_pages(p, 1); + kfree(p); } void *get_last_early_heap(void) diff --git a/kernel/devobj.c b/kernel/devobj.c index 3225454a..737d42f7 100644 --- a/kernel/devobj.c +++ b/kernel/devobj.c @@ -99,7 +99,7 @@ int devobj_create(int fd, size_t len, off_t off, struct memobj **objp, int *maxp } memset(obj, 0, sizeof(*obj)); - obj->pfn_table = allocate_pages(pfn_npages, IHK_MC_AP_NOWAIT); + obj->pfn_table = ihk_mc_alloc_pages(pfn_npages, IHK_MC_AP_NOWAIT); if (!obj->pfn_table) { error = -ENOMEM; kprintf("%s: error: fd: %d, len: %lu, off: %lu allocating PFN failed.\n", @@ -141,7 +141,7 @@ int devobj_create(int fd, size_t len, off_t off, struct memobj **objp, int *maxp out: if (obj) { if (obj->pfn_table) { - free_pages(obj->pfn_table, pfn_npages); + ihk_mc_free_pages(obj->pfn_table, pfn_npages); } kfree(obj); } @@ -196,7 +196,7 @@ static void devobj_release(struct memobj *memobj) } if (obj->pfn_table) { - free_pages(obj->pfn_table, pfn_npages); + ihk_mc_free_pages(obj->pfn_table, pfn_npages); } kfree(free_obj); } diff --git a/kernel/host.c b/kernel/host.c index 1013ebfe..3444a349 100644 --- a/kernel/host.c +++ b/kernel/host.c @@ -375,8 +375,9 @@ static int process_msg_prepare_process(unsigned long rphys) n = p->num_sections; dkprintf("# of sections: %d\n", n); - if((pn = ihk_mc_allocate(sizeof(struct program_load_desc) - + sizeof(struct program_image_section) * n, IHK_MC_AP_NOWAIT)) == NULL){ + if((pn = kmalloc(sizeof(struct program_load_desc) + + sizeof(struct program_image_section) * n, + IHK_MC_AP_NOWAIT)) == NULL){ ihk_mc_unmap_virtual(p, npages, 0); ihk_mc_unmap_memory(NULL, phys, sz); return -ENOMEM; @@ -385,7 +386,7 @@ static int process_msg_prepare_process(unsigned long rphys) + sizeof(struct program_image_section) * n); if((thread = create_thread(p->entry)) == NULL){ - ihk_mc_free(pn); + kfree(pn); ihk_mc_unmap_virtual(p, npages, 1); ihk_mc_unmap_memory(NULL, phys, sz); return -ENOMEM; @@ -435,7 +436,7 @@ static int process_msg_prepare_process(unsigned long rphys) dkprintf("new process : %p [%d] / table : %p\n", proc, proc->pid, vm->address_space->page_table); - ihk_mc_free(pn); + kfree(pn); ihk_mc_unmap_virtual(p, npages, 1); ihk_mc_unmap_memory(NULL, phys, sz); @@ -443,7 +444,7 @@ static int process_msg_prepare_process(unsigned long rphys) return 0; err: - ihk_mc_free(pn); + kfree(pn); ihk_mc_unmap_virtual(p, npages, 1); ihk_mc_unmap_memory(NULL, phys, sz); destroy_thread(thread); @@ -452,7 +453,7 @@ err: static void process_msg_init(struct ikc_scd_init_param *pcp, struct syscall_params *lparam) { - lparam->response_va = allocate_pages(RESPONSE_PAGE_COUNT, 0); + lparam->response_va = ihk_mc_alloc_pages(RESPONSE_PAGE_COUNT, 0); lparam->response_pa = virt_to_phys(lparam->response_va); pcp->request_page = 0; diff --git a/kernel/process.c b/kernel/process.c index fb1c30e0..7ed1f98a 100644 --- a/kernel/process.c +++ b/kernel/process.c @@ -739,7 +739,7 @@ int join_process_memory_range(struct process_vm *vm, memobj_release(merging->memobj); } list_del(&merging->list); - ihk_mc_free(merging); + kfree(merging); error = 0; out: @@ -835,8 +835,9 @@ int free_process_memory_range(struct process_vm *vm, struct vm_range *range) if (range->memobj) { memobj_release(range->memobj); } + list_del(&range->list); - ihk_mc_free(range); + kfree(range); dkprintf("free_process_memory_range(%p,%lx-%lx): 0\n", vm, start0, end0); @@ -1888,14 +1889,14 @@ unsigned long extend_process_region(struct process_vm *vm, aligned_end = (aligned_end + (LARGE_PAGE_SIZE - 1)) & LARGE_PAGE_MASK; /* Fill in the gap between old_aligned_end and aligned_end * with regular pages */ - if((p = allocate_pages((aligned_end - old_aligned_end) >> PAGE_SHIFT, + if((p = ihk_mc_alloc_pages((aligned_end - old_aligned_end) >> PAGE_SHIFT, IHK_MC_AP_NOWAIT)) == NULL){ return end; } if((rc = add_process_memory_range(vm, old_aligned_end, aligned_end, virt_to_phys(p), flag, LARGE_PAGE_SHIFT)) != 0){ - free_pages(p, (aligned_end - old_aligned_end) >> PAGE_SHIFT); + ihk_mc_free_pages(p, (aligned_end - old_aligned_end) >> PAGE_SHIFT); return end; } @@ -1908,7 +1909,7 @@ unsigned long extend_process_region(struct process_vm *vm, (LARGE_PAGE_SIZE - 1)) & LARGE_PAGE_MASK; address = aligned_new_end; - if((p = allocate_pages((aligned_new_end - aligned_end + LARGE_PAGE_SIZE) >> PAGE_SHIFT, + if((p = ihk_mc_alloc_pages((aligned_new_end - aligned_end + LARGE_PAGE_SIZE) >> PAGE_SHIFT, IHK_MC_AP_NOWAIT)) == NULL){ return end; } @@ -1916,16 +1917,16 @@ unsigned long extend_process_region(struct process_vm *vm, p_aligned = ((unsigned long)p + (LARGE_PAGE_SIZE - 1)) & LARGE_PAGE_MASK; if (p_aligned > (unsigned long)p) { - free_pages(p, (p_aligned - (unsigned long)p) >> PAGE_SHIFT); + ihk_mc_free_pages(p, (p_aligned - (unsigned long)p) >> PAGE_SHIFT); } - free_pages( + ihk_mc_free_pages( (void *)(p_aligned + aligned_new_end - aligned_end), (LARGE_PAGE_SIZE - (p_aligned - (unsigned long)p)) >> PAGE_SHIFT); if((rc = add_process_memory_range(vm, aligned_end, aligned_new_end, virt_to_phys((void *)p_aligned), flag, LARGE_PAGE_SHIFT)) != 0){ - free_pages(p, (aligned_new_end - aligned_end + LARGE_PAGE_SIZE) >> PAGE_SHIFT); + ihk_mc_free_pages(p, (aligned_new_end - aligned_end + LARGE_PAGE_SIZE) >> PAGE_SHIFT); return end; } @@ -1943,7 +1944,7 @@ unsigned long extend_process_region(struct process_vm *vm, p=0; }else{ - p = allocate_pages((aligned_new_end - aligned_end) >> PAGE_SHIFT, IHK_MC_AP_NOWAIT); + p = ihk_mc_alloc_pages((aligned_new_end - aligned_end) >> PAGE_SHIFT, IHK_MC_AP_NOWAIT); if (!p) { return end; @@ -1952,7 +1953,7 @@ unsigned long extend_process_region(struct process_vm *vm, if((rc = add_process_memory_range(vm, aligned_end, aligned_new_end, (p==0?0:virt_to_phys(p)), flag, NULL, 0, PAGE_SHIFT)) != 0){ - free_pages(p, (aligned_new_end - aligned_end) >> PAGE_SHIFT); + ihk_mc_free_pages(p, (aligned_new_end - aligned_end) >> PAGE_SHIFT); return end; } diff --git a/kernel/sysfs.c b/kernel/sysfs.c index a924e531..dc97e58d 100644 --- a/kernel/sysfs.c +++ b/kernel/sysfs.c @@ -75,7 +75,7 @@ sysfs_createf(struct sysfs_ops *ops, void *instance, int mode, dkprintf("sysfs_createf(%p,%p,%#o,%s,...)\n", ops, instance, mode, fmt); - param = allocate_pages(1, IHK_MC_AP_NOWAIT); + param = ihk_mc_alloc_pages(1, IHK_MC_AP_NOWAIT); if (!param) { error = -ENOMEM; ekprintf("sysfs_createf:allocate_pages failed. %d\n", error); @@ -134,7 +134,7 @@ sysfs_createf(struct sysfs_ops *ops, void *instance, int mode, error = 0; out: if (param) { - free_pages(param, 1); + ihk_mc_free_pages(param, 1); } if (error) { ekprintf("sysfs_createf(%p,%p,%#o,%s,...): %d\n", @@ -156,7 +156,7 @@ sysfs_mkdirf(sysfs_handle_t *dirhp, const char *fmt, ...) dkprintf("sysfs_mkdirf(%p,%s,...)\n", dirhp, fmt); - param = allocate_pages(1, IHK_MC_AP_NOWAIT); + param = ihk_mc_alloc_pages(1, IHK_MC_AP_NOWAIT); if (!param) { error = -ENOMEM; ekprintf("sysfs_mkdirf:allocate_pages failed. %d\n", error); @@ -208,7 +208,7 @@ sysfs_mkdirf(sysfs_handle_t *dirhp, const char *fmt, ...) out: if (param) { - free_pages(param, 1); + ihk_mc_free_pages(param, 1); } if (error) { ekprintf("sysfs_mkdirf(%p,%s,...): %d\n", dirhp, fmt, error); @@ -229,7 +229,7 @@ sysfs_symlinkf(sysfs_handle_t targeth, const char *fmt, ...) dkprintf("sysfs_symlinkf(%#lx,%s,...)\n", targeth.handle, fmt); - param = allocate_pages(1, IHK_MC_AP_NOWAIT); + param = ihk_mc_alloc_pages(1, IHK_MC_AP_NOWAIT); if (!param) { error = -ENOMEM; ekprintf("sysfs_symlinkf:allocate_pages failed. %d\n", error); @@ -279,7 +279,7 @@ sysfs_symlinkf(sysfs_handle_t targeth, const char *fmt, ...) error = 0; out: if (param) { - free_pages(param, 1); + ihk_mc_free_pages(param, 1); } if (error) { ekprintf("sysfs_symlinkf(%#lx,%s,...): %d\n", @@ -301,7 +301,7 @@ sysfs_lookupf(sysfs_handle_t *objhp, const char *fmt, ...) dkprintf("sysfs_lookupf(%p,%s,...)\n", objhp, fmt); - param = allocate_pages(1, IHK_MC_AP_NOWAIT); + param = ihk_mc_alloc_pages(1, IHK_MC_AP_NOWAIT); if (!param) { error = -ENOMEM; ekprintf("sysfs_lookupf:allocate_pages failed. %d\n", error); @@ -353,7 +353,7 @@ sysfs_lookupf(sysfs_handle_t *objhp, const char *fmt, ...) out: if (param) { - free_pages(param, 1); + ihk_mc_free_pages(param, 1); } if (error) { ekprintf("sysfs_lookupf(%p,%s,...): %d\n", objhp, fmt, error); @@ -374,7 +374,7 @@ sysfs_unlinkf(int flags, const char *fmt, ...) dkprintf("sysfs_unlinkf(%#x,%s,...)\n", flags, fmt); - param = allocate_pages(1, IHK_MC_AP_NOWAIT); + param = ihk_mc_alloc_pages(1, IHK_MC_AP_NOWAIT); if (!param) { error = -ENOMEM; ekprintf("sysfs_unlinkf:allocate_pages failed. %d\n", error); @@ -423,7 +423,7 @@ sysfs_unlinkf(int flags, const char *fmt, ...) error = 0; out: if (param) { - free_pages(param, 1); + ihk_mc_free_pages(param, 1); } if (error) { ekprintf("sysfs_unlinkf(%#x,%s,...): %d\n", flags, fmt, error); @@ -601,14 +601,14 @@ sysfs_init(void) } sysfs_data_bufsize = PAGE_SIZE; - sysfs_data_buf = allocate_pages(1, IHK_MC_AP_NOWAIT); + sysfs_data_buf = ihk_mc_alloc_pages(1, IHK_MC_AP_NOWAIT); if (!sysfs_data_buf) { error = -ENOMEM; ekprintf("sysfs_init:allocate_pages(buf) failed. %d\n", error); goto out; } - param = allocate_pages(1, IHK_MC_AP_NOWAIT); + param = ihk_mc_alloc_pages(1, IHK_MC_AP_NOWAIT); if (!param) { error = -ENOMEM; ekprintf("sysfs_init:allocate_pages(param) failed. %d\n", @@ -644,7 +644,7 @@ sysfs_init(void) error = 0; out: if (param) { - free_pages(param, 1); + ihk_mc_free_pages(param, 1); } if (error) { ekprintf("sysfs_init(): %d\n", error); diff --git a/lib/include/ihk/mm.h b/lib/include/ihk/mm.h index cf2957a0..52a3c554 100644 --- a/lib/include/ihk/mm.h +++ b/lib/include/ihk/mm.h @@ -103,7 +103,7 @@ void ihk_mc_clean_micpa(void); void *ihk_mc_alloc_aligned_pages(int npages, int p2align, enum ihk_mc_ap_flag flag); void *ihk_mc_alloc_pages(int npages, enum ihk_mc_ap_flag flag); void ihk_mc_free_pages(void *p, int npages); -void *ihk_mc_allocate(int size, enum ihk_mc_ap_flag flag); +void *ihk_mc_allocate(int size, int flag); void ihk_mc_free(void *p); void *arch_alloc_page(enum ihk_mc_ap_flag flag);