Skip to content

Commit

Permalink
Only use do and dont dump on pagemap
Browse files Browse the repository at this point in the history
The do and dont dump calls were costings a lot during start up of snmalloc.  This reduces the times they are called to only be for the pagemap.
  • Loading branch information
mjp41 committed Jun 19, 2024
1 parent aec908f commit e3b6929
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 36 deletions.
10 changes: 10 additions & 0 deletions src/snmalloc/ds/pagemap.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ namespace snmalloc
auto page_end = pointer_align_up<OS_PAGE_SIZE, char>(last);
size_t using_size = pointer_diff(page_start, page_end);
PAL::template notify_using<NoZero>(page_start, using_size);
if constexpr (pal_supports<CoreDump, PAL>)
{
PAL::notify_do_dump(page_start, using_size);
}
}

constexpr FlatPagemap() = default;
Expand Down Expand Up @@ -192,6 +196,12 @@ namespace snmalloc

auto new_body_untyped = PAL::reserve(request_size);

if constexpr (pal_supports<CoreDump, PAL>)
{
// Pagemap should not be in core dump except where it is non-zero.
PAL::notify_do_not_dump(new_body_untyped, request_size);
}

if (new_body_untyped == nullptr)
{
PAL::error("Failed to initialise snmalloc.");
Expand Down
6 changes: 6 additions & 0 deletions src/snmalloc/pal/pal_consts.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ namespace snmalloc
* This Pal provides a millisecond time source
*/
Time = (1 << 5),

/**
* This Pal provides selective core dumps, so
* modify which parts get dumped.
*/
CoreDump = (1 << 6),
};

/**
Expand Down
31 changes: 10 additions & 21 deletions src/snmalloc/pal/pal_freebsd.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ namespace snmalloc
* field is declared explicitly to remind anyone modifying this class to
* add new features that they should add any required feature flags.
*/
static constexpr uint64_t pal_features = PALBSD_Aligned::pal_features;
static constexpr uint64_t pal_features =
PALBSD_Aligned::pal_features | CoreDump;

/**
* FreeBSD uses atypically small address spaces on its 64 bit RISC machines.
Expand All @@ -68,10 +69,8 @@ namespace snmalloc
/**
* Notify platform that we will not be using these pages.
*
* We use the `MADV_FREE` and `NADV_NOCORE` flags to `madvise`. The first
* allows the system to discard the page and replace it with a CoW mapping
* of the zero page. The second prevents this mapping from appearing in
* core files.
* We use the `MADV_FREE` flag to `madvise`. This allows the system to
* discard the page and replace it with a CoW mapping of the zero page.
*/
static void notify_not_using(void* p, size_t size) noexcept
{
Expand All @@ -80,7 +79,6 @@ namespace snmalloc
if constexpr (DEBUG)
memset(p, 0x5a, size);

madvise(p, size, MADV_NOCORE);
madvise(p, size, MADV_FREE);

if constexpr (mitigations(pal_enforce_access))
Expand All @@ -90,28 +88,19 @@ namespace snmalloc
}

/**
* Notify platform that we will be using these pages for reading.
*
* This is used only for pages full of zeroes and so we exclude them from
* core dumps.
* Notify platform that these pages should be included in a core dump.
*/
static void notify_using_readonly(void* p, size_t size) noexcept
static void notify_do_dump(void* p, size_t size) noexcept
{
PALBSD_Aligned<PALFreeBSD>::notify_using_readonly(p, size);
madvise(p, size, MADV_NOCORE);
madvise(p, size, MADV_CORE);
}

/**
* Notify platform that we will be using these pages.
*
* We may have previously marked this memory as not being included in core
* files, so mark it for inclusion again.
* Notify platform that these pages should not be included in a core dump.
*/
template<ZeroMem zero_mem>
static void notify_using(void* p, size_t size) noexcept
static void notify_do_not_dump(void* p, size_t size) noexcept
{
PALBSD_Aligned<PALFreeBSD>::notify_using<zero_mem>(p, size);
madvise(p, size, MADV_CORE);
madvise(p, size, MADV_NOCORE);
}

# if defined(__CHERI_PURE_CAPABILITY__)
Expand Down
23 changes: 8 additions & 15 deletions src/snmalloc/pal/pal_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ namespace snmalloc
*
* We always make sure that linux has entropy support.
*/
static constexpr uint64_t pal_features = PALPOSIX::pal_features | Entropy;
static constexpr uint64_t pal_features =
PALPOSIX::pal_features | Entropy | CoreDump;

static constexpr size_t page_size =
Aal::aal_name == PowerPC ? 0x10000 : PALPOSIX::page_size;
Expand Down Expand Up @@ -58,7 +59,6 @@ namespace snmalloc
void* p = PALPOSIX<PALLinux>::reserve(size);
if (p)
{
madvise(p, size, MADV_DONTDUMP);
# ifdef SNMALLOC_PAGEID
# ifndef PR_SET_VMA
# define PR_SET_VMA 0x53564d41
Expand Down Expand Up @@ -125,7 +125,6 @@ namespace snmalloc
if constexpr (DEBUG)
memset(p, 0x5a, size);

madvise(p, size, MADV_DONTDUMP);
madvise(p, size, madvise_free_flags);

if constexpr (mitigations(pal_enforce_access))
Expand All @@ -135,25 +134,19 @@ namespace snmalloc
}

/**
* Notify platform that we will be using these pages for reading.
*
* This is used only for pages full of zeroes and so we exclude them from
* core dumps.
* Notify platform that these pages should be included in a core dump.
*/
static void notify_using_readonly(void* p, size_t size) noexcept
static void notify_do_dump(void* p, size_t size) noexcept
{
PALPOSIX<PALLinux>::notify_using_readonly(p, size);
madvise(p, size, MADV_DONTDUMP);
madvise(p, size, MADV_DODUMP);
}

/**
* Notify platform that we will be using these pages.
* Notify platform that these pages should not be included in a core dump.
*/
template<ZeroMem zero_mem>
static void notify_using(void* p, size_t size) noexcept
static void notify_do_not_dump(void* p, size_t size) noexcept
{
PALPOSIX<PALLinux>::notify_using<zero_mem>(p, size);
madvise(p, size, MADV_DODUMP);
madvise(p, size, MADV_DONTDUMP);
}

static uint64_t get_entropy64()
Expand Down

0 comments on commit e3b6929

Please sign in to comment.