diff options
author | Thomas Weißschuh <thomas.weissschuh@linutronix.de> | 2025-04-28 15:40:05 +0300 |
---|---|---|
committer | Thomas Weißschuh <linux@weissschuh.net> | 2025-05-21 16:32:03 +0300 |
commit | 55175d8659d22bfde30d4a1277328f090d8c0c2f (patch) | |
tree | 3d8e726b1d41541d0e26b077d4cbc03931b12839 | |
parent | 2337d39f7233fc3fb9d90489f0d48904eb129a2e (diff) | |
download | linux-55175d8659d22bfde30d4a1277328f090d8c0c2f.tar.xz |
tools/nolibc: add mremap()
This is used in various selftests and will be handy when integrating
those with nolibc.
Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Acked-by: Willy Tarreau <w@1wt.eu>
Link: https://lore.kernel.org/r/20250428-nolibc-misc-v2-4-3c043eeab06c@linutronix.de
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
-rw-r--r-- | tools/include/nolibc/sys/mman.h | 19 | ||||
-rw-r--r-- | tools/testing/selftests/nolibc/nolibc-test.c | 14 |
2 files changed, 30 insertions, 3 deletions
diff --git a/tools/include/nolibc/sys/mman.h b/tools/include/nolibc/sys/mman.h index 41c7bf45e427..5228751b458c 100644 --- a/tools/include/nolibc/sys/mman.h +++ b/tools/include/nolibc/sys/mman.h @@ -49,6 +49,25 @@ void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) } static __attribute__((unused)) +void *sys_mremap(void *old_address, size_t old_size, size_t new_size, int flags, void *new_address) +{ + return (void *)my_syscall5(__NR_mremap, old_address, old_size, + new_size, flags, new_address); +} + +static __attribute__((unused)) +void *mremap(void *old_address, size_t old_size, size_t new_size, int flags, void *new_address) +{ + void *ret = sys_mremap(old_address, old_size, new_size, flags, new_address); + + if ((unsigned long)ret >= -4095UL) { + SET_ERRNO(-(long)ret); + ret = MAP_FAILED; + } + return ret; +} + +static __attribute__((unused)) int sys_munmap(void *addr, size_t length) { return my_syscall2(__NR_munmap, addr, length); diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index b7440a667db6..abe0ae794208 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -926,7 +926,7 @@ int test_mmap_munmap(void) { int ret, fd, i, page_size; void *mem; - size_t file_size, length; + size_t file_size, length, mem_length; off_t offset, pa_offset; struct stat stat_buf; const char * const files[] = { @@ -966,14 +966,22 @@ int test_mmap_munmap(void) offset = 0; length = file_size - offset; pa_offset = offset & ~(page_size - 1); + mem_length = length + offset - pa_offset; - mem = mmap(NULL, length + offset - pa_offset, PROT_READ, MAP_SHARED, fd, pa_offset); + mem = mmap(NULL, mem_length, PROT_READ, MAP_SHARED, fd, pa_offset); if (mem == MAP_FAILED) { ret = 1; goto end; } - ret = munmap(mem, length + offset - pa_offset); + mem = mremap(mem, mem_length, mem_length * 2, MREMAP_MAYMOVE, 0); + if (mem == MAP_FAILED) { + munmap(mem, mem_length); + ret = 1; + goto end; + } + + ret = munmap(mem, mem_length * 2); end: close(fd); |