protect slub access from multithread

This commit is contained in:
Mathieu Maret 2020-08-21 00:05:53 +02:00
parent 0ae9ad7d92
commit ba9e0f1bff

View File

@ -2,6 +2,7 @@
//#define DEBUG //#define DEBUG
#include "alloc.h" #include "alloc.h"
#include "errno.h" #include "errno.h"
#include "irq.h"
#include "klibc.h" #include "klibc.h"
#include "list.h" #include "list.h"
#include "math.h" #include "math.h"
@ -46,9 +47,13 @@ int allocBookSlab(size_t size, int selfContained)
struct slabDesc *slab = NULL; struct slabDesc *slab = NULL;
int slabIdx; int slabIdx;
int ret; int ret;
int flags;
disable_IRQs(flags);
list_foreach(slub, slab, slabIdx) list_foreach(slub, slab, slabIdx)
{ {
if (slab->size == size) { if (slab->size == size) {
restore_IRQs(flags);
return -EEXIST; return -EEXIST;
} }
if (slab->size > size) { if (slab->size > size) {
@ -56,13 +61,16 @@ int allocBookSlab(size_t size, int selfContained)
} }
} }
struct slabDesc *newSlab; struct slabDesc *newSlab;
if ((ret = allocSlab(&newSlab, size, selfContained))) if ((ret = allocSlab(&newSlab, size, selfContained))) {
restore_IRQs(flags);
return ret; return ret;
}
if (list_foreach_early_break(slub, slab, slabIdx)) { if (list_foreach_early_break(slub, slab, slabIdx)) {
list_insert_before(slub, slab, newSlab); list_insert_before(slub, slab, newSlab);
} else { } else {
list_add_tail(slub, newSlab); list_add_tail(slub, newSlab);
} }
restore_IRQs(flags);
return 0; return 0;
} }
@ -148,12 +156,17 @@ static void *allocFromSlab(struct slabEntry *slab)
void *malloc(size_t size) void *malloc(size_t size)
{ {
int flags;
void *ret;
if (size > (1U << SLUB_SIZE)) { if (size > (1U << SLUB_SIZE)) {
printf("implement malloc for big size\n"); printf("implement malloc for big size\n");
return NULL; return NULL;
} }
struct slabDesc *slab; struct slabDesc *slab;
uint slubIdx; uint slubIdx;
disable_IRQs(flags);
list_foreach(slub, slab, slubIdx) list_foreach(slub, slab, slubIdx)
{ {
if (size <= slab->size) if (size <= slab->size)
@ -166,21 +179,27 @@ void *malloc(size_t size)
if (!slabEntry->full) { if (!slabEntry->full) {
// pr_devel("found place in slub %d at idx %d for size %d\n", slubIdx, // pr_devel("found place in slub %d at idx %d for size %d\n", slubIdx,
// slabIdx, size); // slabIdx, size);
return allocFromSlab(slabEntry); ret = allocFromSlab(slabEntry);
restore_IRQs(flags);
return ret;
} }
} }
// No room found // No room found
struct slabEntry *newSlabEntry; struct slabEntry *newSlabEntry;
struct slabEntry *slabList = &slab->slab; struct slabEntry *slabList = &slab->slab;
int ret; int retSlab;
if ((ret = allocSlabEntry(&newSlabEntry, slab->size, IS_SELF_CONTAINED(&slab->slab)))) { if ((retSlab =
pr_devel("Fail to allocSlabEntry %d\n", ret); allocSlabEntry(&newSlabEntry, slab->size, IS_SELF_CONTAINED(&slab->slab)))) {
pr_devel("Fail to allocSlabEntry %d\n", retSlab);
restore_IRQs(flags);
return NULL; return NULL;
} }
pr_devel("Allocate new slab for object of size %d\n", slab->size); pr_devel("Allocate new slab for object of size %d\n", slab->size);
list_add_tail(slabList, newSlabEntry); list_add_tail(slabList, newSlabEntry);
return allocFromSlab(newSlabEntry); ret = allocFromSlab(newSlabEntry);
restore_IRQs(flags);
return ret;
} }
int freeFromSlab(void *ptr, struct slabEntry *slab) int freeFromSlab(void *ptr, struct slabEntry *slab)
@ -211,15 +230,21 @@ void free(void *ptr)
struct slabDesc *slab; struct slabDesc *slab;
int slabIdx; int slabIdx;
int flags;
disable_IRQs(flags);
list_foreach(slub, slab, slabIdx) list_foreach(slub, slab, slabIdx)
{ {
struct slabEntry *slabEntry; struct slabEntry *slabEntry;
int entryIdx; int entryIdx;
list_foreach(&slab->slab, slabEntry, entryIdx) list_foreach(&slab->slab, slabEntry, entryIdx)
{ {
if (freeFromSlab(ptr, slabEntry)) if (freeFromSlab(ptr, slabEntry)) {
restore_IRQs(flags);
return; return;
}
} }
} }
restore_IRQs(flags);
pr_devel("free: slab not found\n"); pr_devel("free: slab not found\n");
} }