kmalloc: add fallback for failed DMA allocations
This commit is contained in:
parent
36c6f7b92c
commit
06b02fc9a6
3 changed files with 31 additions and 2 deletions
|
|
@ -17,6 +17,7 @@ struct rbnode {
|
|||
uint64_t key;
|
||||
uint64_t value; /* extra values */
|
||||
uint64_t value2;
|
||||
uint64_t value3;
|
||||
};
|
||||
|
||||
/* we use this rbtree structure because the root node will potentially be
|
||||
|
|
|
|||
|
|
@ -130,6 +130,10 @@ struct rbnode *rbt_insert(struct rbtree *tree, uint64_t key)
|
|||
new->right = NULL;
|
||||
new->color = RB_RED;
|
||||
|
||||
new->value = 0;
|
||||
new->value2 = 0;
|
||||
new->value3 = 0;
|
||||
|
||||
spinlock_acquire(&tree->lock);
|
||||
|
||||
if (parent == NULL)
|
||||
|
|
|
|||
28
mem/slab.c
28
mem/slab.c
|
|
@ -7,6 +7,8 @@
|
|||
|
||||
#define SLAB_ALIGN 7
|
||||
|
||||
#define KMT_DMA 0xDE11A000
|
||||
|
||||
static size_t slab_sizes[] = { 16, 32, 64, 128, 256, 512, 1024, 2048, 4096 };
|
||||
static slab_t *slab_cache[ARRAY_SIZE(slab_sizes)];
|
||||
static size_t dma_sizes[] = { 0x1000, 0x10000, 0x100000, 0x1000000 };
|
||||
|
|
@ -200,6 +202,22 @@ void *kmalloc(size_t size, uint64_t flags)
|
|||
|
||||
if (flags & ALLOC_DMA) {
|
||||
ret = kmalloc_table(size, dma_sizes, ARRAY_SIZE(dma_sizes), slab_cache_dma, true);
|
||||
if (ret == NULL) {
|
||||
ret = buddy_alloc(npow2(size));
|
||||
if (ret == NULL)
|
||||
return NULL;
|
||||
|
||||
struct rbnode *n = rbt_insert(&kmalloc_tree, (uint64_t)ret);
|
||||
if (n == NULL) {
|
||||
buddy_free(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
n->value = 0;
|
||||
n->value2 = size;
|
||||
n->value3 = KMT_DMA;
|
||||
}
|
||||
|
||||
} else {
|
||||
ret = kmalloc_table(size, slab_sizes, ARRAY_SIZE(slab_sizes), slab_cache, false);
|
||||
}
|
||||
|
|
@ -229,11 +247,17 @@ void kfree(void *ptr)
|
|||
}
|
||||
|
||||
slab_t *slab = (slab_t *)n->value;
|
||||
if (slab != NULL)
|
||||
if (slab != NULL) {
|
||||
slab_free(slab, ptr);
|
||||
else
|
||||
} else {
|
||||
buddy_free(ptr);
|
||||
|
||||
if(n->value3 == KMT_DMA) {
|
||||
uintptr_t p = (uintptr_t)ptr;
|
||||
kmap(p & (~hhdm_start), p, n->value2, kdefault_attrs.val);
|
||||
}
|
||||
}
|
||||
|
||||
rbt_delete(&kmalloc_tree, n);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue