From 28eb6490565c9e6562e7047e0a59ef51e11b2ff2 Mon Sep 17 00:00:00 2001 From: Balazs Gerofi Date: Sun, 20 Aug 2017 09:44:48 +0900 Subject: [PATCH] Generic lock-free kmalloc cache implementation Conflicts: kernel/mem.c --- kernel/include/kmalloc.h | 9 +++++++ kernel/mem.c | 57 ++++++++++++++++++++++++++++++++++++++++ kernel/process.c | 1 - 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/kernel/include/kmalloc.h b/kernel/include/kmalloc.h index 4574486b..c97feede 100644 --- a/kernel/include/kmalloc.h +++ b/kernel/include/kmalloc.h @@ -19,6 +19,15 @@ void panic(const char *); int kprintf(const char *format, ...); +struct kmalloc_cache_header { + struct kmalloc_cache_header *next; +}; + +void *kmalloc_cache_alloc(struct kmalloc_cache_header *cache, + size_t size); +void kmalloc_cache_free(struct kmalloc_cache_header *cache, + void *elem); + #define kmalloc(size, flag) ({\ void *r = _kmalloc(size, flag, __FILE__, __LINE__);\ if(r == NULL){\ diff --git a/kernel/mem.c b/kernel/mem.c index fc1c4c01..5d987f08 100644 --- a/kernel/mem.c +++ b/kernel/mem.c @@ -2611,3 +2611,60 @@ int ihk_mc_get_mem_user_page(void *arg0, page_table_t pt, pte_t *ptep, void *pga return 0; } + +/* + * Generic lockless kmalloc cache. + */ +void *kmalloc_cache_alloc(struct kmalloc_cache_header *cache, + size_t size) +{ + struct kmalloc_cache_header *first, *next; + +retry: + next = NULL; + first = cache->next; + + if (first) { + next = first->next; + + if (!__sync_bool_compare_and_swap(&cache->next, + first, next)) { + goto retry; + } + } + else { + int i; + kprintf("%s: cache empty, allocating ...\n", __FUNCTION__); + for (i = 0; i < 100; ++i) { + first = (struct kmalloc_cache_header *) + kmalloc(size, IHK_MC_AP_NOWAIT); + + if (!first) { + kprintf("%s: ERROR: allocating cache element\n", __FUNCTION__); + continue; + } + + kmalloc_cache_free(cache, first); + } + + goto retry; + } + + return (void *)first; +} + +void kmalloc_cache_free(struct kmalloc_cache_header *cache, void *elem) +{ + struct kmalloc_cache_header *current = NULL; + struct kmalloc_cache_header *new = + (struct kmalloc_cache_header *)elem; + +retry: + current = cache->next; + new->next = current; + + if (!__sync_bool_compare_and_swap(&cache->next, current, new)) { + goto retry; + } +} + diff --git a/kernel/process.c b/kernel/process.c index c97c57a1..7c9afaf4 100644 --- a/kernel/process.c +++ b/kernel/process.c @@ -138,7 +138,6 @@ init_process(struct process *proc, struct process *parent) INIT_LIST_HEAD(&proc->ptraced_siblings_list); mcs_rwlock_init(&proc->update_lock); #endif /* POSTK_DEBUG_ARCH_DEP_63 */ - } // Double check the inheritance from parent memset(proc->fd_priv_table, 0, 256 * sizeof(void *));