MM: use ihk_mc_{alloc/free}_pages() everywhere and fix free_pages() on kmalloc()ed object bug

This commit is contained in:
Balazs Gerofi
2016-08-17 18:02:05 +09:00
parent 01d2ea1605
commit f5857cfc9e
6 changed files with 39 additions and 42 deletions

View File

@@ -23,6 +23,7 @@
#include <process.h> #include <process.h>
#include <page.h> #include <page.h>
#include <cls.h> #include <cls.h>
#include <kmalloc.h>
#define dkprintf(...) do { if (0) kprintf(__VA_ARGS__); } while (0) #define dkprintf(...) do { if (0) kprintf(__VA_ARGS__); } while (0)
#define ekprintf(...) kprintf(__VA_ARGS__) #define ekprintf(...) kprintf(__VA_ARGS__)
@@ -84,20 +85,14 @@ void ihk_mc_free_pages(void *p, int npages)
pa_ops->free_page(p, 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 kmalloc(size, IHK_MC_AP_NOWAIT);
return pa_ops->alloc(size, flag);
else
return ihk_mc_alloc_pages(1, flag);
} }
void ihk_mc_free(void *p) void ihk_mc_free(void *p)
{ {
if (pa_ops && pa_ops->free) kfree(p);
return pa_ops->free(p);
else
return ihk_mc_free_pages(p, 1);
} }
void *get_last_early_heap(void) void *get_last_early_heap(void)

View File

@@ -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)); 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) { if (!obj->pfn_table) {
error = -ENOMEM; error = -ENOMEM;
kprintf("%s: error: fd: %d, len: %lu, off: %lu allocating PFN failed.\n", 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: out:
if (obj) { if (obj) {
if (obj->pfn_table) { if (obj->pfn_table) {
free_pages(obj->pfn_table, pfn_npages); ihk_mc_free_pages(obj->pfn_table, pfn_npages);
} }
kfree(obj); kfree(obj);
} }
@@ -196,7 +196,7 @@ static void devobj_release(struct memobj *memobj)
} }
if (obj->pfn_table) { if (obj->pfn_table) {
free_pages(obj->pfn_table, pfn_npages); ihk_mc_free_pages(obj->pfn_table, pfn_npages);
} }
kfree(free_obj); kfree(free_obj);
} }

View File

@@ -375,8 +375,9 @@ static int process_msg_prepare_process(unsigned long rphys)
n = p->num_sections; n = p->num_sections;
dkprintf("# of sections: %d\n", n); dkprintf("# of sections: %d\n", n);
if((pn = ihk_mc_allocate(sizeof(struct program_load_desc) if((pn = kmalloc(sizeof(struct program_load_desc)
+ sizeof(struct program_image_section) * n, IHK_MC_AP_NOWAIT)) == NULL){ + sizeof(struct program_image_section) * n,
IHK_MC_AP_NOWAIT)) == NULL){
ihk_mc_unmap_virtual(p, npages, 0); ihk_mc_unmap_virtual(p, npages, 0);
ihk_mc_unmap_memory(NULL, phys, sz); ihk_mc_unmap_memory(NULL, phys, sz);
return -ENOMEM; return -ENOMEM;
@@ -385,7 +386,7 @@ static int process_msg_prepare_process(unsigned long rphys)
+ sizeof(struct program_image_section) * n); + sizeof(struct program_image_section) * n);
if((thread = create_thread(p->entry)) == NULL){ if((thread = create_thread(p->entry)) == NULL){
ihk_mc_free(pn); kfree(pn);
ihk_mc_unmap_virtual(p, npages, 1); ihk_mc_unmap_virtual(p, npages, 1);
ihk_mc_unmap_memory(NULL, phys, sz); ihk_mc_unmap_memory(NULL, phys, sz);
return -ENOMEM; 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, dkprintf("new process : %p [%d] / table : %p\n", proc, proc->pid,
vm->address_space->page_table); vm->address_space->page_table);
ihk_mc_free(pn); kfree(pn);
ihk_mc_unmap_virtual(p, npages, 1); ihk_mc_unmap_virtual(p, npages, 1);
ihk_mc_unmap_memory(NULL, phys, sz); ihk_mc_unmap_memory(NULL, phys, sz);
@@ -443,7 +444,7 @@ static int process_msg_prepare_process(unsigned long rphys)
return 0; return 0;
err: err:
ihk_mc_free(pn); kfree(pn);
ihk_mc_unmap_virtual(p, npages, 1); ihk_mc_unmap_virtual(p, npages, 1);
ihk_mc_unmap_memory(NULL, phys, sz); ihk_mc_unmap_memory(NULL, phys, sz);
destroy_thread(thread); destroy_thread(thread);
@@ -452,7 +453,7 @@ err:
static void process_msg_init(struct ikc_scd_init_param *pcp, struct syscall_params *lparam) 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); lparam->response_pa = virt_to_phys(lparam->response_va);
pcp->request_page = 0; pcp->request_page = 0;

View File

@@ -739,7 +739,7 @@ int join_process_memory_range(struct process_vm *vm,
memobj_release(merging->memobj); memobj_release(merging->memobj);
} }
list_del(&merging->list); list_del(&merging->list);
ihk_mc_free(merging); kfree(merging);
error = 0; error = 0;
out: out:
@@ -835,8 +835,9 @@ int free_process_memory_range(struct process_vm *vm, struct vm_range *range)
if (range->memobj) { if (range->memobj) {
memobj_release(range->memobj); memobj_release(range->memobj);
} }
list_del(&range->list); list_del(&range->list);
ihk_mc_free(range); kfree(range);
dkprintf("free_process_memory_range(%p,%lx-%lx): 0\n", dkprintf("free_process_memory_range(%p,%lx-%lx): 0\n",
vm, start0, end0); 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; aligned_end = (aligned_end + (LARGE_PAGE_SIZE - 1)) & LARGE_PAGE_MASK;
/* Fill in the gap between old_aligned_end and aligned_end /* Fill in the gap between old_aligned_end and aligned_end
* with regular pages */ * 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){ IHK_MC_AP_NOWAIT)) == NULL){
return end; return end;
} }
if((rc = add_process_memory_range(vm, old_aligned_end, if((rc = add_process_memory_range(vm, old_aligned_end,
aligned_end, virt_to_phys(p), flag, aligned_end, virt_to_phys(p), flag,
LARGE_PAGE_SHIFT)) != 0){ 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; return end;
} }
@@ -1908,7 +1909,7 @@ unsigned long extend_process_region(struct process_vm *vm,
(LARGE_PAGE_SIZE - 1)) & LARGE_PAGE_MASK; (LARGE_PAGE_SIZE - 1)) & LARGE_PAGE_MASK;
address = aligned_new_end; 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){ IHK_MC_AP_NOWAIT)) == NULL){
return end; 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; p_aligned = ((unsigned long)p + (LARGE_PAGE_SIZE - 1)) & LARGE_PAGE_MASK;
if (p_aligned > (unsigned long)p) { 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), (void *)(p_aligned + aligned_new_end - aligned_end),
(LARGE_PAGE_SIZE - (p_aligned - (unsigned long)p)) >> PAGE_SHIFT); (LARGE_PAGE_SIZE - (p_aligned - (unsigned long)p)) >> PAGE_SHIFT);
if((rc = add_process_memory_range(vm, aligned_end, if((rc = add_process_memory_range(vm, aligned_end,
aligned_new_end, virt_to_phys((void *)p_aligned), aligned_new_end, virt_to_phys((void *)p_aligned),
flag, LARGE_PAGE_SHIFT)) != 0){ 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; return end;
} }
@@ -1943,7 +1944,7 @@ unsigned long extend_process_region(struct process_vm *vm,
p=0; p=0;
}else{ }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) { if (!p) {
return end; 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, if((rc = add_process_memory_range(vm, aligned_end, aligned_new_end,
(p==0?0:virt_to_phys(p)), flag, NULL, 0, (p==0?0:virt_to_phys(p)), flag, NULL, 0,
PAGE_SHIFT)) != 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; return end;
} }

View File

@@ -75,7 +75,7 @@ sysfs_createf(struct sysfs_ops *ops, void *instance, int mode,
dkprintf("sysfs_createf(%p,%p,%#o,%s,...)\n", dkprintf("sysfs_createf(%p,%p,%#o,%s,...)\n",
ops, instance, mode, fmt); ops, instance, mode, fmt);
param = allocate_pages(1, IHK_MC_AP_NOWAIT); param = ihk_mc_alloc_pages(1, IHK_MC_AP_NOWAIT);
if (!param) { if (!param) {
error = -ENOMEM; error = -ENOMEM;
ekprintf("sysfs_createf:allocate_pages failed. %d\n", error); 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; error = 0;
out: out:
if (param) { if (param) {
free_pages(param, 1); ihk_mc_free_pages(param, 1);
} }
if (error) { if (error) {
ekprintf("sysfs_createf(%p,%p,%#o,%s,...): %d\n", 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); 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) { if (!param) {
error = -ENOMEM; error = -ENOMEM;
ekprintf("sysfs_mkdirf:allocate_pages failed. %d\n", error); ekprintf("sysfs_mkdirf:allocate_pages failed. %d\n", error);
@@ -208,7 +208,7 @@ sysfs_mkdirf(sysfs_handle_t *dirhp, const char *fmt, ...)
out: out:
if (param) { if (param) {
free_pages(param, 1); ihk_mc_free_pages(param, 1);
} }
if (error) { if (error) {
ekprintf("sysfs_mkdirf(%p,%s,...): %d\n", dirhp, fmt, 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); 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) { if (!param) {
error = -ENOMEM; error = -ENOMEM;
ekprintf("sysfs_symlinkf:allocate_pages failed. %d\n", error); ekprintf("sysfs_symlinkf:allocate_pages failed. %d\n", error);
@@ -279,7 +279,7 @@ sysfs_symlinkf(sysfs_handle_t targeth, const char *fmt, ...)
error = 0; error = 0;
out: out:
if (param) { if (param) {
free_pages(param, 1); ihk_mc_free_pages(param, 1);
} }
if (error) { if (error) {
ekprintf("sysfs_symlinkf(%#lx,%s,...): %d\n", 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); 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) { if (!param) {
error = -ENOMEM; error = -ENOMEM;
ekprintf("sysfs_lookupf:allocate_pages failed. %d\n", error); ekprintf("sysfs_lookupf:allocate_pages failed. %d\n", error);
@@ -353,7 +353,7 @@ sysfs_lookupf(sysfs_handle_t *objhp, const char *fmt, ...)
out: out:
if (param) { if (param) {
free_pages(param, 1); ihk_mc_free_pages(param, 1);
} }
if (error) { if (error) {
ekprintf("sysfs_lookupf(%p,%s,...): %d\n", objhp, fmt, 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); 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) { if (!param) {
error = -ENOMEM; error = -ENOMEM;
ekprintf("sysfs_unlinkf:allocate_pages failed. %d\n", error); ekprintf("sysfs_unlinkf:allocate_pages failed. %d\n", error);
@@ -423,7 +423,7 @@ sysfs_unlinkf(int flags, const char *fmt, ...)
error = 0; error = 0;
out: out:
if (param) { if (param) {
free_pages(param, 1); ihk_mc_free_pages(param, 1);
} }
if (error) { if (error) {
ekprintf("sysfs_unlinkf(%#x,%s,...): %d\n", flags, fmt, 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_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) { if (!sysfs_data_buf) {
error = -ENOMEM; error = -ENOMEM;
ekprintf("sysfs_init:allocate_pages(buf) failed. %d\n", error); ekprintf("sysfs_init:allocate_pages(buf) failed. %d\n", error);
goto out; goto out;
} }
param = allocate_pages(1, IHK_MC_AP_NOWAIT); param = ihk_mc_alloc_pages(1, IHK_MC_AP_NOWAIT);
if (!param) { if (!param) {
error = -ENOMEM; error = -ENOMEM;
ekprintf("sysfs_init:allocate_pages(param) failed. %d\n", ekprintf("sysfs_init:allocate_pages(param) failed. %d\n",
@@ -644,7 +644,7 @@ sysfs_init(void)
error = 0; error = 0;
out: out:
if (param) { if (param) {
free_pages(param, 1); ihk_mc_free_pages(param, 1);
} }
if (error) { if (error) {
ekprintf("sysfs_init(): %d\n", error); ekprintf("sysfs_init(): %d\n", error);

View File

@@ -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_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_alloc_pages(int npages, enum ihk_mc_ap_flag flag);
void ihk_mc_free_pages(void *p, int npages); 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 ihk_mc_free(void *p);
void *arch_alloc_page(enum ihk_mc_ap_flag flag); void *arch_alloc_page(enum ihk_mc_ap_flag flag);