2020-07-21 22:09:15 +00:00
|
|
|
/*
|
|
|
|
* BIRD Internet Routing Daemon -- Raw allocation
|
|
|
|
*
|
|
|
|
* (c) 2020 Maria Matejka <mq@ucw.cz>
|
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "lib/resource.h"
|
2021-11-24 16:30:13 +00:00
|
|
|
#include "lib/lists.h"
|
|
|
|
#include "lib/event.h"
|
2020-07-21 22:09:15 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_MMAP
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_MMAP
|
2021-11-24 16:30:13 +00:00
|
|
|
#define KEEP_PAGES 512
|
|
|
|
|
2020-07-21 22:09:15 +00:00
|
|
|
static u64 page_size = 0;
|
|
|
|
static _Bool use_fake = 0;
|
2021-11-24 16:30:13 +00:00
|
|
|
|
|
|
|
uint pages_kept = 0;
|
|
|
|
static list pages_list;
|
|
|
|
|
|
|
|
static void cleanup_pages(void *data);
|
|
|
|
static event page_cleanup_event = { .hook = cleanup_pages };
|
|
|
|
|
2020-07-21 22:09:15 +00:00
|
|
|
#else
|
|
|
|
static const u64 page_size = 4096; /* Fake page size */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
u64 get_page_size(void)
|
|
|
|
{
|
|
|
|
if (page_size)
|
|
|
|
return page_size;
|
|
|
|
|
|
|
|
#ifdef HAVE_MMAP
|
|
|
|
if (page_size = sysconf(_SC_PAGESIZE))
|
|
|
|
{
|
|
|
|
if ((u64_popcount(page_size) > 1) || (page_size > 16384))
|
|
|
|
{
|
|
|
|
/* Too big or strange page, use the aligned allocator instead */
|
|
|
|
page_size = 4096;
|
|
|
|
use_fake = 1;
|
|
|
|
}
|
|
|
|
return page_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
bug("Page size must be non-zero");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
alloc_page(void)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_MMAP
|
2021-11-24 16:30:13 +00:00
|
|
|
if (pages_kept)
|
|
|
|
{
|
|
|
|
node *page = TAIL(pages_list);
|
|
|
|
rem_node(page);
|
|
|
|
pages_kept--;
|
|
|
|
memset(page, 0, get_page_size());
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
2020-07-21 22:09:15 +00:00
|
|
|
if (!use_fake)
|
|
|
|
{
|
|
|
|
void *ret = mmap(NULL, get_page_size(), PROT_WRITE | PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
|
|
|
if (ret == MAP_FAILED)
|
2022-02-08 21:42:00 +00:00
|
|
|
bug("mmap(%lu) failed: %m", (long unsigned int) page_size);
|
2020-07-21 22:09:15 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2022-02-08 21:42:00 +00:00
|
|
|
void *ptr = NULL;
|
|
|
|
int err = posix_memalign(&ptr, page_size, page_size);
|
|
|
|
if (err || !ptr)
|
|
|
|
bug("posix_memalign(%lu) failed", (long unsigned int) page_size);
|
|
|
|
return ptr;
|
2020-07-21 22:09:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
free_page(void *ptr)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_MMAP
|
|
|
|
if (!use_fake)
|
|
|
|
{
|
2021-11-24 16:30:13 +00:00
|
|
|
if (!pages_kept)
|
|
|
|
init_list(&pages_list);
|
|
|
|
|
|
|
|
memset(ptr, 0, sizeof(node));
|
|
|
|
add_tail(&pages_list, ptr);
|
|
|
|
|
|
|
|
if (++pages_kept > KEEP_PAGES)
|
|
|
|
ev_schedule(&page_cleanup_event);
|
2020-07-21 22:09:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
free(ptr);
|
|
|
|
}
|
2021-11-24 16:30:13 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_MMAP
|
|
|
|
static void
|
|
|
|
cleanup_pages(void *data UNUSED)
|
|
|
|
{
|
|
|
|
for (uint seen = 0; (pages_kept > KEEP_PAGES) && (seen < KEEP_PAGES); seen++)
|
|
|
|
{
|
|
|
|
void *ptr = HEAD(pages_list);
|
|
|
|
rem_node(ptr);
|
|
|
|
if (munmap(ptr, get_page_size()) == 0)
|
|
|
|
pages_kept--;
|
|
|
|
#ifdef ENOMEM
|
|
|
|
else if (errno == ENOMEM)
|
|
|
|
add_tail(&pages_list, ptr);
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
bug("munmap(%p) failed: %m", ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pages_kept > KEEP_PAGES)
|
|
|
|
ev_schedule(&page_cleanup_event);
|
|
|
|
}
|
|
|
|
#endif
|