buddy: fix page fault

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-07-14 23:34:31 -04:00
commit 4096f03d32

View file

@ -88,6 +88,7 @@ static void alloc_split(struct buddy_zone *bzone, void *alloc, size_t cur_order,
for (size_t i = new_step_size; i < cur_full_size; i += new_step_size) {
struct buddy_page_header *h = (void *)(((uintptr_t)alloc) + i);
h->order = new_order;
h->zone = bzone;
h->prev = prev;
if (prev)
@ -200,19 +201,10 @@ static inline void init_zones()
}
}
static struct pm_entry *get_pm_entry(struct buddy_page_header *h)
{
size_t offset = (vaddr_t)h - h->zone->usable_base;
size_t page_no = offset / PAGE_SIZE;
return h->zone->page_map + page_no;
}
static struct buddy_page_header *get_buddy(struct buddy_page_header *h)
{
struct pm_entry *entry = get_pm_entry(h);
size_t offset = (vaddr_t)h - h->zone->usable_base;
size_t order = entry->alloc_order;
size_t buddy_offset = offset ^ (PAGE_SIZE << order);
size_t buddy_offset = offset ^ (PAGE_SIZE << h->order);
if (buddy_offset >= h->zone->usable_len)
return NULL;
@ -220,22 +212,24 @@ static struct buddy_page_header *get_buddy(struct buddy_page_header *h)
return (void *)(h->zone->usable_base + buddy_offset);
}
static bool allocated(struct buddy_page_header *h)
{
if (!h)
return false;
struct pm_entry *entry = get_pm_entry(h);
return !!entry->alloc_order;
}
void buddy_zone_free(struct buddy_zone *bzone, void *ptr)
{
struct buddy_page_header *h = ptr;
size_t offset = (vaddr_t)h - bzone->usable_base;
struct pm_entry *entry = bzone->page_map + offset / PAGE_SIZE;
h->order = entry->alloc_order;
h->zone = bzone;
struct buddy_page_header *buddy = get_buddy(h);
while (buddy && !allocated(buddy)) {
entry->alloc_order = 0;
while (buddy) {
size_t b_off = (vaddr_t)buddy - bzone->usable_base;
struct pm_entry *b_entry = bzone->page_map + b_off / PAGE_SIZE;
if (b_entry->alloc_order)
break;
if (buddy->next)
buddy->next->prev = buddy->prev;
if (buddy->prev)