[LTP] [PATCH v2 6/6] mremap08: Extract test from mremap05
Cyril Hrubis
chrubis@suse.cz
Fri Aug 7 11:39:26 CEST 2026
Hi!
> diff --git a/testcases/kernel/syscalls/mremap/mremap08.c b/testcases/kernel/syscalls/mremap/mremap08.c
> new file mode 100644
> index 000000000..8d8e82d8b
> --- /dev/null
> +++ b/testcases/kernel/syscalls/mremap/mremap08.c
> @@ -0,0 +1,128 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2026 Linux Test Project
> + */
> +
> +/*\
> + * Verify the behavior of the ``MREMAP_FIXED`` flag of :manpage:`mremap(2)`:
> + *
> + * - ``MREMAP_FIXED | MREMAP_MAYMOVE`` can move a mapping to a new, free
> + * address, preserving its content.
> + * - ``MREMAP_FIXED | MREMAP_MAYMOVE`` unmaps a previously existing mapping at
> + * the target address range and replaces it with the moved mapping.
> + */
> +
> +#define _GNU_SOURCE
> +#include <sys/mman.h>
> +#include "tst_test.h"
> +
> +static int pagesize;
> +
> +static struct tcase {
> + size_t old_pages;
> + size_t new_pages;
> + int flags;
> + int free_dst;
> + const char *msg;
> +} tcases[] = {
> + {
> + .old_pages = 1,
> + .new_pages = 1,
> + .flags = MREMAP_FIXED | MREMAP_MAYMOVE,
> + .free_dst = 1,
> + .msg = "move mapping to free address",
> + },
> + {
> + .old_pages = 1,
> + .new_pages = 1,
> + .flags = MREMAP_FIXED | MREMAP_MAYMOVE,
> + .free_dst = 0,
> + .msg = "move mapping onto an occupied address",
> + },
> +};
> +
> +static void *get_test_area(size_t size, int free_area)
> +{
> + void *p;
> +
> + p = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE,
> + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
> +
> + if (free_area)
> + SAFE_MUNMAP(p, size);
> +
> + return p;
> +}
> +
> +static void setup(void)
> +{
> + pagesize = getpagesize();
> +}
> +
> +static void fill_pattern(char *addr, size_t pages)
> +{
> + size_t i;
> +
> + for (i = 0; i < pages; i++)
> + addr[i * pagesize] = (char)(0x1 + i);
> +}
> +
> +static int check_pattern(const char *msg, char *addr, size_t pages)
> +{
> + size_t i;
> +
> + for (i = 0; i < pages; i++) {
> + char got = addr[i * pagesize];
> + char exp = (char)(0x1 + i);
> +
> + if (got != exp) {
> + tst_res(TFAIL, "%s: page %zu content 0x%x, expected 0x%x",
> + msg, i, (unsigned char)got, (unsigned char)exp);
> + return 1;
> + }
> + }
> +
> + return 0;
> +}
> +
> +static void run(unsigned int n)
> +{
> + struct tcase *tc = &tcases[n];
> + char *old_address;
> + char *new_address;
> + char *ret;
> + size_t old_size = tc->old_pages * pagesize;
> + size_t new_size = tc->new_pages * pagesize;
> +
> + old_address = get_test_area(old_size, 0);
> + new_address = get_test_area(new_size, tc->free_dst);
> +
> + fill_pattern(old_address, tc->old_pages);
> +
> + if (!tc->free_dst)
> + *new_address = 0x7f;
This should be moved into check_patter()
> + TESTPTR(mremap(old_address, old_size, new_size, tc->flags, new_address));
> + ret = TST_RET_PTR;
> +
> + if (ret == MAP_FAILED) {
> + tst_res(TFAIL | TTERRNO, "%s failed", tc->msg);
> + SAFE_MUNMAP(old_address, old_size);
> + if (!tc->free_dst)
> + SAFE_MUNMAP(new_address, new_size);
> + return;
> + }
> +
> + if (ret != new_address)
> + tst_res(TFAIL, "%s: ret %p, expected %p", tc->msg, ret, new_address);
> + else if (check_pattern(tc->msg, ret, tc->new_pages) == 0)
> + tst_res(TPASS, "%s", tc->msg);
This looks incomplete, we will not produce result if pattern was wrong.
It should be:
if (ret != new_address)
tst_res(TFAIL, ...);
else if (check_pattern(...))
tst_res(TFAIL, ...);
else
tst_res(TPASS, ...);
Otherwise:
Reviewed-by: Cyril Hrubis <chrubis@suse.cz>
--
Cyril Hrubis
chrubis@suse.cz
More information about the ltp
mailing list