-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1337 from nanovms/heap-locking
As enumerated in #1302, there are many paths in the kernel where the kernel lock is not held and yet allocations or deallocations are being made from one of the kernel heaps. To address this issue, these changes introduce a "locked" kernel heap. Like general, this is an mcache heap, accessed through a locking wrapper heap which guards accesses with a spinlock. The "backed" heap is also accessed via a locking wrapper. Since id_heap-specific methods wouldn't be covered by a generic wrapper, a "locking" flag is specified on id_heap creation. The virtual huge and page heaps are now locking and thus safe to use from any context. Allocations made (and released) under protection of the kernel lock should continue to use the general heap, as this will avoid unnecessary spinlock operations.
- Loading branch information
Showing
31 changed files
with
300 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include <kernel.h> | ||
|
||
typedef struct heaplock { | ||
struct heap h; | ||
struct spinlock lock; | ||
heap parent; | ||
heap meta; | ||
} *heaplock; | ||
|
||
#define lock_heap(hl) u64 _flags = spin_lock_irq(&hl->lock) | ||
#define unlock_heap(hl) spin_unlock_irq(&hl->lock, _flags) | ||
|
||
static u64 heaplock_alloc(heap h, bytes size) | ||
{ | ||
heaplock hl = (heaplock)h; | ||
lock_heap(hl); | ||
u64 a = allocate_u64(hl->parent, size); | ||
unlock_heap(hl); | ||
return a; | ||
} | ||
|
||
static void heaplock_dealloc(heap h, u64 x, bytes size) | ||
{ | ||
heaplock hl = (heaplock)h; | ||
lock_heap(hl); | ||
deallocate_u64(hl->parent, x, size); | ||
unlock_heap(hl); | ||
} | ||
|
||
/* assuming no contention on destroy */ | ||
static void heaplock_destroy(heap h) | ||
{ | ||
heaplock hl = (heaplock)h; | ||
destroy_heap(hl->parent); | ||
deallocate(hl->meta, hl, sizeof(*hl)); | ||
} | ||
|
||
static bytes heaplock_allocated(heap h) | ||
{ | ||
heaplock hl = (heaplock)h; | ||
lock_heap(hl); | ||
bytes count = heap_allocated(hl->parent); | ||
unlock_heap(hl); | ||
return count; | ||
} | ||
|
||
static bytes heaplock_total(heap h) | ||
{ | ||
heaplock hl = (heaplock)h; | ||
lock_heap(hl); | ||
bytes count = heap_total(hl->parent); | ||
unlock_heap(hl); | ||
return count; | ||
} | ||
|
||
/* meta only used on creation */ | ||
heap locking_heap_wrapper(heap meta, heap parent) | ||
{ | ||
heaplock hl = allocate(meta, sizeof(*hl)); | ||
if (hl == INVALID_ADDRESS) | ||
return INVALID_ADDRESS; | ||
hl->h.alloc = heaplock_alloc; | ||
hl->h.dealloc = heaplock_dealloc; | ||
hl->h.destroy = heaplock_destroy; | ||
hl->h.allocated = heaplock_allocated; | ||
hl->h.total = heaplock_total; | ||
hl->h.pagesize = parent->pagesize; | ||
hl->parent = parent; | ||
hl->meta = meta; | ||
spin_lock_init(&hl->lock); | ||
return (heap)hl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.