Skip to content

Commit 2499539

Browse files
committed
expanding the page range id feature to e able to distinguish better
page states.
1 parent bf54eeb commit 2499539

File tree

4 files changed

+68
-23
lines changed

4 files changed

+68
-23
lines changed

src/snmalloc/pal/pal_consts.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ namespace snmalloc
9494
YesZero
9595
};
9696

97+
enum StateMem
98+
{
99+
Unused,
100+
Allocated,
101+
Pagemap,
102+
Metadata
103+
};
104+
97105
/**
98106
* Default Tag ID for the Apple class
99107
*/

src/snmalloc/pal/pal_linux.h

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,35 +35,54 @@ namespace snmalloc
3535
*/
3636
static constexpr int default_mmap_flags = MAP_NORESERVE;
3737

38-
static void* reserve(size_t size) noexcept
38+
/**
39+
* Not generalizing this handler purposely as in other platforms
40+
* mechanisms could differ greatly.
41+
*/
42+
template<StateMem state_mem>
43+
static void pageid(void* p, size_t size) noexcept
3944
{
40-
void* p = PALPOSIX<PALLinux>::reserve(size);
41-
if (p)
42-
{
43-
madvise(p, size, MADV_DONTDUMP);
4445
# ifdef SNMALLOC_PAGEID
4546
# ifndef PR_SET_VMA
4647
# define PR_SET_VMA 0x53564d41
4748
# define PR_SET_VMA_ANON_NAME 0
4849
# endif
4950

50-
/**
51-
*
52-
* If the kernel is set with CONFIG_ANON_VMA_NAME
53-
* the reserved pages would appear as follow
54-
*
55-
* 7fa5f0ceb000-7fa5f0e00000 rw-p 00000000 00:00 0 [anon:snmalloc]
56-
* 7fa5f0e00000-7fa5f1800000 rw-p 00000000 00:00 0 [anon:snmalloc]
57-
*
58-
*/
59-
60-
prctl(
61-
PR_SET_VMA,
62-
PR_SET_VMA_ANON_NAME,
63-
(unsigned long)p,
64-
size,
65-
(unsigned long)"snmalloc");
51+
/**
52+
*
53+
* If the kernel is set with CONFIG_ANON_VMA_NAME
54+
* the reserved pages would appear as follow
55+
*
56+
* 7fa5f0ceb000-7fa5f0e00000 rw-p 00000000 00:00 0 [anon:snmalloc
57+
* (<type>)] 7fa5f0e00000-7fa5f1800000 rw-p 00000000 00:00 0
58+
* [anon:snmalloc (<type>)]
59+
*
60+
* The 80 buffer limit is specific to this syscall.
61+
*/
62+
63+
char buf[80] = {0};
64+
PALPOSIX<PALLinux>::pagetype<state_mem>(buf, sizeof(buf));
65+
66+
prctl(
67+
PR_SET_VMA,
68+
PR_SET_VMA_ANON_NAME,
69+
(unsigned long)p,
70+
size,
71+
(unsigned long)buf);
72+
# else
73+
UNUSED(p);
74+
UNUSED(size);
6675
# endif
76+
}
77+
78+
template<StateMem state_mem = Unused>
79+
static void* reserve(size_t size) noexcept
80+
{
81+
void* p = PALPOSIX<PALLinux>::reserve<state_mem>(size);
82+
if (p)
83+
{
84+
madvise(p, size, MADV_DONTDUMP);
85+
pageid<state_mem>(p, size);
6786
}
6887
return p;
6988
}
@@ -131,11 +150,12 @@ namespace snmalloc
131150
/**
132151
* Notify platform that we will be using these pages.
133152
*/
134-
template<ZeroMem zero_mem>
153+
template<ZeroMem zero_mem, StateMem state_mem = Allocated>
135154
static void notify_using(void* p, size_t size) noexcept
136155
{
137156
PALPOSIX<PALLinux>::notify_using<zero_mem>(p, size);
138157
madvise(p, size, MADV_DODUMP);
158+
pageid<state_mem>(p, size);
139159
}
140160

141161
static uint64_t get_entropy64()

src/snmalloc/pal/pal_posix.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ namespace snmalloc
221221
* are also zeroing the pages in which case we call the platform's `zero`
222222
* function, or we have initially mapped the pages as PROT_NONE.
223223
*/
224-
template<ZeroMem zero_mem>
224+
template<ZeroMem zero_mem, StateMem state_mem = Allocated>
225225
static void notify_using(void* p, size_t size) noexcept
226226
{
227227
SNMALLOC_ASSERT(
@@ -316,6 +316,7 @@ namespace snmalloc
316316
* POSIX does not define a portable interface for specifying alignment
317317
* greater than a page.
318318
*/
319+
template<StateMem state_mem = Unused>
319320
static void* reserve(size_t size) noexcept
320321
{
321322
// If enforcing access, map pages initially as None, and then
@@ -428,5 +429,20 @@ namespace snmalloc
428429

429430
error("Failed to get system randomness");
430431
}
432+
433+
template<StateMem state_mem>
434+
static void pagetype(char* buf, size_t len)
435+
{
436+
ssize_t sz;
437+
if constexpr (state_mem == Unused)
438+
sz = snprintf(buf, len, "snmalloc (Unused)");
439+
else if constexpr (state_mem == Allocated)
440+
sz = snprintf(buf, len, "snmalloc (Allocated)");
441+
else if constexpr (state_mem == Pagemap)
442+
sz = snprintf(buf, len, "snmalloc (Pagemap)");
443+
else if constexpr (state_mem == Metadata)
444+
sz = snprintf(buf, len, "snmalloc (Metadata)");
445+
buf[sz] = 0;
446+
}
431447
};
432448
} // namespace snmalloc

src/snmalloc/pal/pal_windows.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ namespace snmalloc
198198
}
199199
# endif
200200

201+
template<StateMem state_mem = Unused>
201202
static void* reserve(size_t size) noexcept
202203
{
203204
void* ret = VirtualAlloc(nullptr, size, MEM_RESERVE, PAGE_READWRITE);

0 commit comments

Comments
 (0)