Skip to content

Commit

Permalink
Kernel syscall.
Browse files Browse the repository at this point in the history
  • Loading branch information
kilobyte committed Nov 25, 2021
1 parent 3063197 commit afbca82
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 57 deletions.
2 changes: 1 addition & 1 deletion include/memkind/internal/memkind_memtier.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void memtier_delete_memtier_memory(struct memtier_memory *memory);
void *memtier_malloc(struct memtier_memory *memory, size_t size);

// comment TODO
void *memtier_mmap(void* mmap_func_ptr, struct memtier_memory *memory,
void *memtier_mmap(struct memtier_memory *memory,
void *addr, size_t length, int prot, int flags, int fd, off_t offset);
void memtier_munmap(void* ptr);

Expand Down
32 changes: 25 additions & 7 deletions src/memkind_memtier.c
Original file line number Diff line number Diff line change
Expand Up @@ -1203,17 +1203,35 @@ MEMKIND_EXPORT void *memtier_malloc(struct memtier_memory *memory, size_t size)
return ptr;
}

void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t off)
{
long ret = syscall(SYS_mmap, addr, length, prot, flags, fd, off);
if (ret == -EPERM && !off && (flags&MAP_ANON) && !(flags&MAP_FIXED))
ret = -ENOMEM;
if (ret > -4096 && ret < 0) {
errno = -ret;
return MAP_FAILED;
}

return (void*)ret;
}

int munmap(void *addr, size_t length)
{
long ret = syscall(SYS_munmap, addr, length);
if (!ret)
return 0;
errno = -ret;
return -1;
}

#include <pthread.h>
pthread_mutex_t mutex;

MEMKIND_EXPORT void* memtier_mmap(void* real_mmap_ptr, struct memtier_memory *memory, void *addr, size_t length, int prot, int flags, int fd, off_t offset)
MEMKIND_EXPORT void* memtier_mmap(struct memtier_memory *memory, void *addr, size_t length, int prot, int flags, int fd, off_t offset)
{
if ((memory == NULL) || dont_mmap) {
void *(*real_mmap)(void *, size_t, int, int, int, off_t)
= (void* (*)(void *, size_t, int, int, int, off_t))real_mmap_ptr;
//log_err("dont mmap!");
return real_mmap(addr, length, prot, flags, fd, offset);
}
if ((memory == NULL) || dont_mmap)
return sys_mmap(addr, length, prot, flags, fd, offset);

// prevent recursive mmap
dont_mmap = true;
Expand Down
54 changes: 5 additions & 49 deletions tiering/memtier.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,8 @@ static int destructed;

static struct memtier_memory *current_memory;

void *(*real_mmap)(void *, size_t, int, int, int, off_t);
int (*real_munmap)(void *, size_t);
static bool init = false;
void *sys_mmap(void *, size_t, int, int, int, off_t);
int sys_munmap(void *, size_t);

// only used for few initial m/callocs that calls mmap
static thread_local unsigned char bytes[100000];
Expand All @@ -110,33 +109,8 @@ static thread_local void* mmap_map[1000];
static thread_local int num_mmaps = 0;
extern thread_local bool dont_mmap;

void do_init()
{
if (init)
return;
init = true;

real_mmap = (void * (*)(void *, size_t, int, int, int, off_t))dlsym(
RTLD_NEXT , "mmap");
real_munmap = (int (*)(void *, size_t))dlsym(RTLD_NEXT , "munmap");
log_err("real_mmap %p", real_mmap);
log_err("real_munmap %p", real_munmap);

init = false;
}

MEMTIER_EXPORT void *malloc(size_t size)
{
// TODO!!!
if (real_mmap == 0)
{
do_init();
void* ret = (void*)&bytes[last_byte];
last_byte += size;

return ret;
}

if (MEMTIER_LIKELY(current_memory)) {
return memtier_malloc(current_memory, size);
} else if (destructed == 0) {
Expand All @@ -147,15 +121,6 @@ MEMTIER_EXPORT void *malloc(size_t size)

MEMTIER_EXPORT void *calloc(size_t num, size_t size)
{
// TODO!!!
if (real_mmap == 0)
{
do_init();
void* ret = (void*)&bytes[last_byte];
last_byte += size;
return ret;
}

if (MEMTIER_LIKELY(current_memory)) {
return memtier_calloc(current_memory, num, size);
} else if (destructed == 0) {
Expand All @@ -169,7 +134,6 @@ MEMTIER_EXPORT void *realloc(void *ptr, size_t size)
// TODO!!!
if ((ptr >= (void*)bytes) && ptr < (void*)(bytes + last_byte))
{
do_init();
void* ret = (void*)&bytes[last_byte];
last_byte += size;
return ret;
Expand Down Expand Up @@ -219,10 +183,6 @@ MEMTIER_EXPORT size_t malloc_usable_size(void *ptr)

MEMTIER_EXPORT void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
{
if (!real_mmap) {
do_init();
}

// TODO tweaked for MLC - find other valid flag combinations
if ((current_memory == 0) || (
(addr == NULL) &&
Expand All @@ -235,18 +195,14 @@ MEMTIER_EXPORT void *mmap(void *addr, size_t length, int prot, int flags, int fd
mmap_map[num_mmaps] = addr;
num_mmaps++;

return memtier_mmap((void*)real_mmap, current_memory, addr, length, prot, flags, fd, offset);
return memtier_mmap(current_memory, addr, length, prot, flags, fd, offset);
}

return real_mmap(addr, length, prot, flags, fd, offset);
return sys_mmap(addr, length, prot, flags, fd, offset);
}

MEMTIER_EXPORT int munmap(void *addr, size_t length)
{
if (!real_mmap) {
do_init();
}

int i;
for(i = 0; i < num_mmaps; i++)
{
Expand All @@ -257,7 +213,7 @@ MEMTIER_EXPORT int munmap(void *addr, size_t length)
}
}

return real_munmap(addr, length);
return sys_munmap(addr, length);
}

static pthread_once_t init_once = PTHREAD_ONCE_INIT;
Expand Down

0 comments on commit afbca82

Please sign in to comment.