All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrea Cervesato via ltp <ltp@lists.linux.it>
To: "Ricardo B. Marliere" <rbm@suse.com>,
	Linux Test Project <ltp@lists.linux.it>
Subject: Re: [LTP] [PATCH 1/3] mmap001: Convert to new API
Date: Wed, 15 Jan 2025 14:14:38 +0100	[thread overview]
Message-ID: <7f1c0594-e6ef-497b-852a-e8fe6ee2afe4@suse.com> (raw)
In-Reply-To: <20250114-conversions-v1-1-7869a9f786d0@suse.com>

Hi!

On 1/14/25 23:26, Ricardo B. Marliere via ltp wrote:
> From: Ricardo B. Marliere <rbm@suse.com>
>
> Signed-off-by: Ricardo B. Marliere <rbm@suse.com>
> ---
>   testcases/kernel/syscalls/mmap/mmap001.c | 206 ++++++++-----------------------
>   1 file changed, 49 insertions(+), 157 deletions(-)
>
> diff --git a/testcases/kernel/syscalls/mmap/mmap001.c b/testcases/kernel/syscalls/mmap/mmap001.c
> index dabb7d1e4998b1097e179abe23555926f5841117..bc9b4155e8b53f942ef694fdf3187c0e544a97cd 100644
> --- a/testcases/kernel/syscalls/mmap/mmap001.c
> +++ b/testcases/kernel/syscalls/mmap/mmap001.c
> @@ -1,183 +1,75 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
>   /*
>    * Copyright (C) 2000 Juan Quintela <quintela@fi.udc.es>
>    *                    Aaron Laffin <alaffin@sgi.com>
> + * Copyright (c) 2025 Linux Test Project
> + */
> +
> +/*\
> + * [Description]
>    *
> - * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
> - *
> - * mmap001.c - Tests mmapping a big file and writing it once
> + * Tests mmapping a big file and writing it once
Description is a bit short and it needs a bit more information. For 
example, what we expect to see and what could happen during test (SEGV 
for example).
>    */
> -#include <sys/types.h>
> -#include <sys/stat.h>
> -#include <fcntl.h>
> -#include <sys/mman.h>
> -#include <stdlib.h>
> -#include <stdio.h>
> -#include <unistd.h>
> -#include <errno.h>
> -#include <string.h>
>   
> -#include "test.h"
> +#include "tst_test.h"
>   
> -char *TCID = "mmap001";
> -int TST_TOTAL = 5;
> -static char *filename = NULL;
> -static int m_opt = 0;
> +static int fd = -1;
> +static int m_opt = 1000;
>   static char *m_copt;
>   
> -static void cleanup(void)
> -{
> -	free(filename);
> -
> -	tst_rmdir();
> -}
> -
>   static void setup(void)
>   {
> -	char buf[1024];
> -	/*
> -	 * setup a default signal hander and a
> -	 * temporary working directory.
> -	 */
> -	tst_sig(FORK, DEF_HANDLER, cleanup);
> -
> -	TEST_PAUSE;
> -
> -	tst_tmpdir();
> -
> -	snprintf(buf, 1024, "testfile.%d", getpid());
> -
> -	if ((filename = strdup(buf)) == NULL) {
> -		tst_brkm(TBROK | TERRNO, cleanup, "strdup failed");
> -	}
> -
> -}
> -
> -static void help(void)
> -{
> -	printf("  -m x    size of mmap in pages (default 1000)\n");
> +	if (tst_parse_int(m_copt, &m_opt, 1, INT_MAX))
> +		tst_brk(TBROK, "Invalid size of mmap '%s'", m_copt);
>   }
>   
> -/*
> - * add the -m option whose parameter is the
> - * pages that should be mapped.
> - */
> -option_t options[] = {
> -	{"m:", &m_opt, &m_copt},
> -	{NULL, NULL, NULL}
> -};
> -
> -int main(int argc, char *argv[])
> +static void run(void)
>   {
>   	char *array;
> -	int lc;
>   	unsigned int i;
> -	int fd;
>   	unsigned int pages, memsize;
>   
> -	tst_parse_opts(argc, argv, options, help);
> -
> -	if (m_opt) {
> -		memsize = pages = atoi(m_copt);
> -
> -		if (memsize < 1) {
> -			tst_brkm(TBROK, cleanup, "Invalid arg for -m: %s",
> -				 m_copt);
> -		}
> -
> -		memsize *= getpagesize();	/* N PAGES */
> -
> -	} else {
> -		/*
> -		 * default size 1000 pages;
> -		 */
> -		memsize = pages = 1000;
> -		memsize *= getpagesize();
> -	}
> -
> -	tst_resm(TINFO, "mmap()ing file of %u pages or %u bytes", pages,
> -		 memsize);
> -
> -	setup();
> -
> -	for (lc = 0; TEST_LOOPING(lc); lc++) {
> -		tst_count = 0;
> -
> -		fd = open(filename, O_RDWR | O_CREAT, 0666);
> -		if ((fd == -1))
> -			tst_brkm(TBROK | TERRNO, cleanup,
> -				 "opening %s failed", filename);
> -
> -		if (lseek(fd, memsize, SEEK_SET) != memsize) {
> -			TEST_ERRNO = errno;
> -			close(fd);
> -			tst_brkm(TBROK | TTERRNO, cleanup, "lseek failed");
> -		}
> +	memsize = m_opt;
> +	pages = m_opt;
> +	memsize *= getpagesize();
>   
> -		if (write(fd, "\0", 1) != 1) {
> -			TEST_ERRNO = errno;
> -			close(fd);
> -			tst_brkm(TBROK | TTERRNO, cleanup,
> -				 "writing to %s failed", filename);
> -		}
> +	tst_res(TINFO, "mmap()ing file of %u pages or %u bytes", pages,
> +		memsize);
>   
> -		array = mmap(0, memsize, PROT_WRITE, MAP_SHARED, fd, 0);
> -		if (array == MAP_FAILED) {
> -			TEST_ERRNO = errno;
> -			close(fd);
> -			tst_brkm(TBROK | TTERRNO, cleanup,
> -				 "mmapping %s failed", filename);
> -		} else {
> -			tst_resm(TPASS, "mmap() completed successfully.");
> -		}
> +	fd = SAFE_OPEN("testfile", O_RDWR | O_CREAT);
> +	SAFE_LSEEK(fd, memsize, SEEK_SET);
> +	SAFE_WRITE(SAFE_WRITE_ALL, fd, "\0", 1);
>   
> -		tst_resm(TINFO, "touching mmaped memory");
> +	array = SAFE_MMAP(NULL, memsize, PROT_WRITE, MAP_SHARED, fd, 0);
>   
> -		for (i = 0; i < memsize; i++) {
> -			array[i] = (char)i;
> -		}
> +	tst_res(TINFO, "touching mmaped memory");
> +	for (i = 0; i < memsize; i++)
> +		array[i] = (char)i;
>   
> -		/*
> -		 * seems that if the map area was bad, we'd get SEGV,
> -		 * hence we can indicate a PASS.
> -		 */
> -		tst_resm(TPASS,
> -			 "we're still here, mmaped area must be good");
> -
> -		TEST(msync(array, memsize, MS_SYNC));
> -
> -		if (TEST_RETURN == -1) {
> -			tst_resm(TFAIL | TTERRNO,
> -				 "synchronizing mmapped page failed");
> -		} else {
> -			tst_resm(TPASS,
> -				 "synchronizing mmapped page passed");
> -		}
> -
> -		TEST(munmap(array, memsize));
> -
> -		if (TEST_RETURN == -1) {
> -			tst_resm(TFAIL | TTERRNO,
> -				 "munmapping %s failed", filename);
> -		} else {
> -			tst_resm(TPASS, "munmapping %s successful", filename);
> -		}
> +	/*
> +	 * Seems that if the map area was bad, we'd get SEGV,
> +	 * hence we can indicate a PASS.
> +	 */
If this is true, we need to find a way to test that specific scenario. 
This can e done by forking a process where test is running and to check 
if SIGSEGV killed it after calling SAFE_WAITPID()
> +	tst_res(TPASS, "we're still here, mmaped area must be good");
>   
> -		close(fd);
> -		unlink(filename);
> +	SAFE_MSYNC(array, memsize, MS_SYNC);
> +	SAFE_MUNMAP(array, memsize);
> +}
>   
> -	}
> -	cleanup();
> -	tst_exit();
> +static void cleanup(void)
> +{
> +	if (fd > 0)
> +		SAFE_CLOSE(fd);
>   }
> +
> +static struct tst_test test = {
> +	.setup = setup,
> +	.test_all = run,
> +	.cleanup = cleanup,
> +	.options =
> +		(struct tst_option[]){
> +			{ "m:", &m_copt,
> +			  "Size of mmap in pages (default 1000)" },
> +			{},
> +		},
> +};
>
Kind regards,
Andrea

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

  reply	other threads:[~2025-01-15 13:15 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-14 22:26 [LTP] [PATCH 0/3] syscalls/mmap: Refactor a few tests to the new API Ricardo B. Marliere via ltp
2025-01-14 22:26 ` [LTP] [PATCH 1/3] mmap001: Convert to " Ricardo B. Marliere via ltp
2025-01-15 13:14   ` Andrea Cervesato via ltp [this message]
2025-01-29 17:19     ` Ricardo B. Marliere via ltp
2025-02-10  9:04       ` Andrea Cervesato via ltp
2025-01-14 22:26 ` [LTP] [PATCH 2/3] mmap03: " Ricardo B. Marliere via ltp
2025-02-27 14:31   ` Petr Vorel
2025-01-14 22:26 ` [LTP] [PATCH 3/3] mmap10: " Ricardo B. Marliere via ltp

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=7f1c0594-e6ef-497b-852a-e8fe6ee2afe4@suse.com \
    --to=ltp@lists.linux.it \
    --cc=andrea.cervesato@suse.com \
    --cc=rbm@suse.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.