]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
tools/nolibc: add mremap()
authorThomas Weißschuh <thomas.weissschuh@linutronix.de>
Mon, 28 Apr 2025 12:40:05 +0000 (14:40 +0200)
committerThomas Weißschuh <linux@weissschuh.net>
Wed, 21 May 2025 13:32:03 +0000 (15:32 +0200)
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>
tools/include/nolibc/sys/mman.h
tools/testing/selftests/nolibc/nolibc-test.c

index 41c7bf45e427b96efec949364890e9c2b9c226a5..5228751b458c8d8a1bdfb5006623d016a48155fb 100644 (file)
@@ -48,6 +48,25 @@ void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
        return ret;
 }
 
+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)
 {
index b7440a667db6b541a2548bdf5182bee0277100ed..abe0ae794208762f6d91ad81e902fbf77253a1c1 100644 (file)
@@ -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);