Generic lock-free kmalloc cache implementation

Conflicts:
	kernel/mem.c
This commit is contained in:
Balazs Gerofi
2017-08-20 09:44:48 +09:00
parent 744ebacf65
commit 28eb649056
3 changed files with 66 additions and 1 deletions

View File

@ -19,6 +19,15 @@
void panic(const char *); void panic(const char *);
int kprintf(const char *format, ...); 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) ({\ #define kmalloc(size, flag) ({\
void *r = _kmalloc(size, flag, __FILE__, __LINE__);\ void *r = _kmalloc(size, flag, __FILE__, __LINE__);\
if(r == NULL){\ if(r == NULL){\

View File

@ -2611,3 +2611,60 @@ int ihk_mc_get_mem_user_page(void *arg0, page_table_t pt, pte_t *ptep, void *pga
return 0; 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;
}
}

View File

@ -138,7 +138,6 @@ init_process(struct process *proc, struct process *parent)
INIT_LIST_HEAD(&proc->ptraced_siblings_list); INIT_LIST_HEAD(&proc->ptraced_siblings_list);
mcs_rwlock_init(&proc->update_lock); mcs_rwlock_init(&proc->update_lock);
#endif /* POSTK_DEBUG_ARCH_DEP_63 */ #endif /* POSTK_DEBUG_ARCH_DEP_63 */
}
// Double check the inheritance from parent // Double check the inheritance from parent
memset(proc->fd_priv_table, 0, 256 * sizeof(void *)); memset(proc->fd_priv_table, 0, 256 * sizeof(void *));