[LTP] [PATCH 6/7] syscalls: mremap04: Convert to new API

Andrea Cervesato andrea.cervesato@suse.com
Thu Jul 10 13:48:54 CEST 2025


Hi!

On 7/8/25 10:12 PM, Ricardo B. Marlière via ltp wrote:
> From: Ricardo B. Marlière <rbm@suse.com>
>
> Signed-off-by: Ricardo B. Marlière <rbm@suse.com>
> ---
>   testcases/kernel/syscalls/mremap/Makefile   |   4 +-
>   testcases/kernel/syscalls/mremap/mremap04.c | 265 +++++-----------------------
>   2 files changed, 42 insertions(+), 227 deletions(-)
>
> diff --git a/testcases/kernel/syscalls/mremap/Makefile b/testcases/kernel/syscalls/mremap/Makefile
> index 9f5aca9ec99fa49863e4b829d3b4b7fc7297652b..2d089eb649802a5db6eb29348b91fc64c52f6345 100644
> --- a/testcases/kernel/syscalls/mremap/Makefile
> +++ b/testcases/kernel/syscalls/mremap/Makefile
> @@ -3,10 +3,10 @@
>   
>   top_srcdir		?= ../../../..
>   
> -LTPLIBS = ipc
> +LTPLIBS = newipc
>   
>   include $(top_srcdir)/include/mk/testcases.mk
>   
> -mremap04: LTPLDLIBS  = -lltpipc
> +mremap04: LTPLDLIBS  = -lltpnewipc
>   
>   include $(top_srcdir)/include/mk/generic_leaf_target.mk
> diff --git a/testcases/kernel/syscalls/mremap/mremap04.c b/testcases/kernel/syscalls/mremap/mremap04.c
> index 53902df73f0ed177acfa064e7e15c1f9f321c9d2..988adb01590645f94adde3d09444bda3104a30d6 100644
> --- a/testcases/kernel/syscalls/mremap/mremap04.c
> +++ b/testcases/kernel/syscalls/mremap/mremap04.c
> @@ -1,245 +1,60 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
>   /*
> - *
> - *   Copyright (c) International Business Machines  Corp., 2001
> - *
> - *   This program is free software;  you can redistribute it and/or modify
> - *   it under the terms of the GNU General Public License as published by
> - *   the Free Software Foundation; either version 2 of the License, or
> - *   (at your option) any later version.
> - *
> - *   This program is distributed in the hope that it will be useful,
> - *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
> - *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
> - *   the GNU General Public License for more details.
> - *
> - *   You should have received a copy of the GNU General Public License
> - *   along with this program;  if not, write to the Free Software
> - *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> - */
> -
> -/*
> - * Test Name: mremap04
> - *
> - * Test Description:
> - *  Verify that,
> - *   mremap() fails when used to expand the existing virtual memory mapped
> - *   region to the requested size, if the memory area cannot be expanded at
> - *   the current virtual address and MREMAP_MAYMOVE flag not set.
> - *
> - * Expected Result:
> - *  mremap() should return -1 and set errno to ENOMEM.
> - *
> - * Algorithm:
> - *  Setup:
> - *   Setup signal handling.
> - *   Create temporary directory.
> - *   Pause for SIGUSR1 if option specified.
> - *
> - *  Test:
> - *   Loop if the proper options are given.
> - *   Execute system call
> - *   Check return code, if system call failed (return=-1)
> - *	if errno set == expected errno
> - *		Issue sys call failed with expected return value and errno.
> - *	Otherwise,
> - *		Issue sys call failed with unexpected errno.
> - *   Otherwise,
> - *	Issue sys call returns unexpected value.
> - *
> - *  Cleanup:
> - *   Print errno log and/or timing stats if options given
> - *   Delete the temporary directory(s)/file(s) created.
> - *
> - * Usage:  <for command-line>
> - *  mremap04 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
> - *     where,  -c n : Run n copies concurrently.
> - *             -e   : Turn on errno logging.
> - *	       -i n : Execute test n times.
> - *	       -I x : Execute test for x seconds.
> - *	       -p x : Pause for x seconds between iterations.
> - *	       -t   : Turn on syscall timing.
> - *
> - * HISTORY
> + * Copyright (c) International Business Machines  Corp., 2001
>    *	07/2001 Ported by Wayne Boyer
> - *
> - *      11/09/2001 Manoj Iyer (manjo@austin.ibm.com)
> - *      Modified.
> - *      - #include <linux/mman.h> should not be included as per man page for
> - *        mremap, #include <sys/mman.h> alone should do the job. But inorder
> - *        to include definition of MREMAP_MAYMOVE defined in bits/mman.h
> - *        (included by sys/mman.h) __USE_GNU needs to be defined.
> - *        There may be a more elegant way of doing this...
> - *
> - *      26/02/2008 Renaud Lottiaux (Renaud.Lottiaux@kerlabs.com)
> - *      - Fix concurrency issue. Use a shm key from getipckey instead of
> - *        a fixed hard-coded value.
> - *
> - * RESTRICTIONS:
> - *  None.
> + *	11/09/2001 Manoj Iyer (manjo@austin.ibm.com)
> + * Copyright (c) 2025 SUSE LLC Ricardo B. Marlière <rbm@suse.com>
>    */
> -#define _GNU_SOURCE
> -#include <errno.h>
> -#include <unistd.h>
> -#include <sys/mman.h>
> -#include <sys/ipc.h>
> -#include <sys/shm.h>
>   
> -#include "test.h"
> +/*\
> + * Verify that mremap() fails with ENOMEM when used to expand the existing
> + * virtual memory mapped region to the requested size, if the memory area
> + * cannot be expanded at the current virtual address and MREMAP_MAYMOVE flag
> + * not set.
> + */
>   
/*\
  * Verify that mremap() fails with ENOMEM when attempting to expand
  * an existing mmap’ed region to the requested size if it cannot be
  * grown while MREMAP_MAYMOVE flag is not set.
  */
> -#define SHM_MODE	(SHM_R | SHM_W)	/* mode permissions of shared memory */
> +#define _GNU_SOURCE
>   
> -char *TCID = "mremap04";
> -int TST_TOTAL = 1;
> -char *addr;			/* addr of memory mapped region */
> -char *shmaddr;			/* pointer to shared memory segment */
> -int shmid;			/* shared memory identifier. */
> -int memsize;			/* memory mapped size */
> -int newsize;			/* new size of virtual memory block */
> +#include "tst_test.h"
> +#include "tst_safe_sysv_ipc.h"
> +#include "libnewipc.h"
>   
> -void setup();			/* Main setup function of test */
> -void cleanup();			/* cleanup function for the test */
> +#define SHM_MODE (SHM_R | SHM_W)
>   
> -extern int getipckey();
> +static char *shmaddr;
> +static int shmid;
> +static int memsize;
> +static int newsize;
>   
> -int main(int ac, char **av)
> +static void run(void)
>   {
> -	int lc;
> -
> -	tst_parse_opts(ac, av, NULL, NULL);
> -
> -	setup();
> -
> -	for (lc = 0; TEST_LOOPING(lc); lc++) {
> -
> -		tst_count = 0;
> -
> -		/*
> -		 * Attempt to expand the existing shared
> -		 * memory region of newsize by newsize limits
> -		 * using mremap() should fail as specified
> -		 * memory area already locked and MREMAP_MAYMOVE
> -		 * flag unset.
> -		 */
> -		errno = 0;
> -		addr = mremap(shmaddr, memsize, newsize, 0);
> -		TEST_ERRNO = errno;
> -
> -		/* Check for the return value of mremap() */
> -		if (addr != MAP_FAILED) {
> -			tst_resm(TFAIL,
> -				 "mremap returned invalid value, expected: -1");
> -
> -			/* Unmap the mapped memory region */
> -			if (munmap(addr, newsize) != 0) {
> -				tst_brkm(TFAIL, cleanup, "munmap failed to "
> -					 "unmap the expanded memory region, "
> -					 "error=%d", errno);
> -			}
> -			continue;
> -		}
> -
> -		if (TEST_ERRNO == ENOMEM) {
> -			tst_resm(TPASS, "mremap() failed, "
> -				 "'MREMAP_MAYMOVE flag unset', "
> -				 "errno %d", TEST_ERRNO);
> -		} else {
> -			tst_resm(TFAIL, "mremap() failed, "
> -				 "Unexpected errno %d", TEST_ERRNO);
> -		}
> -	}
> -
> -	cleanup();
> -	tst_exit();
> -
> +	TST_EXP_FAIL_PTR_VOID(mremap(shmaddr, memsize, newsize, 0), ENOMEM);
> +	if (TST_RET_PTR != MAP_FAILED)
> +		SAFE_MUNMAP(TST_RET_PTR, memsize);
If it passes, we have "newsize" size. Also we need to set shmaddr to 
NULL after munmap, otherwise that will be done inside cleanup() as well.
>   }
>   
> -/*
> - * setup() - performs all ONE TIME setup for this test.
> - *
> - * Get system page size, Set the size of virtual memory area and the
> - * newsize after resize,
> - * Create a named shared memory segment SHMKEY of newsize and mode SHM_MODE
> - * by using shmget() which returns a shared memory identifier associated
> - * with the created shared memory segment.
> - * Call shmat() to attach the shared memory segment to the data segment of the
> - * calling process. The segment is attached at the first available address as
> - * selected by the system.
> - */
> -void setup(void)
> +static void setup(void)
>   {
>   	key_t shmkey;
> +	size_t memsize = getpagesize();
>   
> -	tst_sig(FORK, DEF_HANDLER, cleanup);
> -
> -	TEST_PAUSE;
> -
> -	tst_tmpdir();
> -
> -	/* Get the system page size */
> -	if ((memsize = getpagesize()) < 0) {
> -		tst_brkm(TBROK, NULL,
> -			 "getpagesize() failed to get system page size");
> -	}
> -
> -	/* Get the New size of virtual memory block after resize */
>   	newsize = (memsize * 2);
> -
> -	/* get an IPC resource key */
> -	shmkey = getipckey();
> -
> -	/*
> -	 * Create a shared memory segment represented by SHMKEY of
> -	 * specified size 'newsize' and mode permissions 'SHM_MODE'.
> -	 */
> -	shmid = shmget(shmkey, newsize, IPC_CREAT | SHM_MODE);
> -	if (shmid == -1) {
> -		tst_brkm(TBROK, NULL, "shmget() Failed to create a shared "
> -			 "memory, error:%d", errno);
> -	}
> -
> -	/*
> -	 * Attach  the shared memory segment associated with the shared
> -	 * memory identifier specified by "shmid" to the data segment of
> -	 * the calling process at the first available address as selected
> -	 * by the system.
> -	 */
> -	shmaddr = shmat(shmid, NULL, 0);
> -	if (shmaddr == (void *)-1) {
> -		tst_brkm(TBROK, cleanup, "shmat() Failed to attach shared "
> -			 "memory, error:%d", errno);
> -	}
> +	shmkey = GETIPCKEY();
> +	shmid = SAFE_SHMGET(shmkey, newsize, IPC_CREAT | SHM_MODE);
> +	shmaddr = SAFE_SHMAT(shmid, NULL, 0);
I would just remove every reference to SHM and use mmap() straight here. 
There's really no reason why mixing memory mapping with shared memory if 
we want to obtain ENOMEM error.
>   }
>   
> -/*
> - * cleanup() - performs all ONE TIME cleanup for this test at
> - *             completion or premature exit.
> - *	       Detach the shared memory segment and remove the shared memory
> - *	       identifier associated with the shared memory.
> - */
> -void cleanup(void)
> +static void cleanup(void)
>   {
> -
> -	/*
> -	 * Detach the shared memory segment attached to
> -	 * the calling process's data segment
> -	 */
> -	if (shmdt(shmaddr) < 0) {
> -		tst_brkm(TFAIL, NULL, "shmdt() Failed to detach shared "
> -			 "memory, error:%d", errno);
> -	}
> -
> -	/*
> -	 * Remove the shared memory identifier associated with
> -	 * the shared memory segment and destroy the shared memory
> -	 * segment.
> -	 */
> -	if (shmctl(shmid, IPC_RMID, 0) < 0) {
> -		tst_brkm(TFAIL, NULL, "shmctl() Failed to remove shared "
> -			 "memory, error:%d", errno);
> -	}
> -
> -	tst_rmdir();
> -
> -	/* Exit the program */
> -
> +	if (shmaddr)
> +		SAFE_SHMDT(shmaddr);
> +	if (shmid)
> +		SAFE_SHMCTL(shmid, IPC_RMID, NULL);
And here we will use munmap() instead.
>   }
> +
> +static struct tst_test test = {
> +	.test_all = run,
> +	.setup = setup,
> +	.cleanup = cleanup,
> +	.needs_tmpdir = 1,
> +};
>
- Andrea


More information about the ltp mailing list