Linux Test Project
 help / color / mirror / Atom feed
From: Cyril Hrubis <chrubis@suse.cz>
To: Andrea Cervesato <andrea.cervesato@suse.de>
Cc: Linux Test Project <ltp@lists.linux.it>
Subject: Re: [LTP] [PATCH v2 6/6] mremap08: Extract test from mremap05
Date: Fri, 7 Aug 2026 11:39:26 +0200	[thread overview]
Message-ID: <anWnzne4v83HxM1K@yuki.lan> (raw)
In-Reply-To: <20260807-rewrite_mremap_testing_suite-v2-6-1760ae8e3b8b@suse.com>

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

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

      reply	other threads:[~2026-08-07  9:39 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-07  6:59 [LTP] [PATCH v2 0/6] Rewrite mremap testing suite Andrea Cervesato
2026-08-07  6:59 ` [LTP] [PATCH v2 1/6] mremap01: Convert to new API Andrea Cervesato
2026-08-07  8:19   ` [LTP] " linuxtestproject.agent
2026-08-07  6:59 ` [LTP] [PATCH v2 2/6] mremap02: " Andrea Cervesato
2026-08-07  6:59 ` [LTP] [PATCH v2 3/6] mremap03: " Andrea Cervesato
2026-08-07  6:59 ` [LTP] [PATCH v2 4/6] mremap04: " Andrea Cervesato
2026-08-07  8:06   ` Cyril Hrubis
2026-08-07  6:59 ` [LTP] [PATCH v2 5/6] mremap05: " Andrea Cervesato
2026-08-07  9:17   ` Cyril Hrubis
2026-08-07  6:59 ` [LTP] [PATCH v2 6/6] mremap08: Extract test from mremap05 Andrea Cervesato
2026-08-07  9:39   ` Cyril Hrubis [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=anWnzne4v83HxM1K@yuki.lan \
    --to=chrubis@suse.cz \
    --cc=andrea.cervesato@suse.de \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox