All of lore.kernel.org
 help / color / mirror / Atom feed
From: Petr Vorel <pvorel@suse.cz>
To: Pavithra <pavrampu@linux.ibm.com>
Cc: ltp@lists.linux.it
Subject: Re: [LTP] [PATCH 3/3] [PATCH] [PATCH] Migrating the libhugetlbfs/testcases/straddle_4GB.c v3
Date: Thu, 27 Nov 2025 12:33:10 +0100	[thread overview]
Message-ID: <20251127113310.GB227467@pevik> (raw)
In-Reply-To: <20251127072231.2104445-3-pavrampu@linux.ibm.com>

Hi Pavithra, Geetika,

I very briefly looked into the original source file (I'd personally put that
link into the commit message).

> This testcase attempts to map a memory range that straddles the 4GB boundary using mmap() with and without the MAP_FIXED flag.

> Changes in v3:

FYI I tried to search in the mailing history [2] [3], but I found only v1, sent
by Geetika. Is there any other discussion than I found?

[1] https://github.com/libhugetlbfs/libhugetlbfs/blob/master/tests/straddle_4GB.c
[2] https://lore.kernel.org/ltp/?q=straddle_4GB.c
[3] https://lore.kernel.org/ltp/?q=hugemmap40

> - Added magic definition to include/tst_fs.h as separate patch.
> - Moved CFLAGS to make file.
> - Added read_maps definition to separate patch.
> - Removed \n from tst_res

> diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap40.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap40.c
> new file mode 100644
> index 000000000..7b4ad7b05
> --- /dev/null
> +++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap40.c
> @@ -0,0 +1,147 @@
> +// SPDX-License-Identifier: LGPL-2.1-or-later
> +/*
> + * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation.
> + * Copyright (C) 2006 Hugh Dickins <hugh@veritas.com>
> + */
> +
> +/*\
> + * [Description]
nit: we don't use "[Description]" any more, because /*\ is enough for parsing
comments.
Please remove the line.

> + *
> + * Test tries to allocate hugepages to cover a memory range that straddles the
> + * 4GB boundary, using mmap(2) with and without MAP_FIXED.
nit: Could you please use :man2:`mmap` (this converts to a man page link [4] in
our docs, see e.g. stack_clash [5]).

[4] https://man7.org/linux/man-pages/man2/mmap.2.html
[5] https://linux-test-project.readthedocs.io/en/latest/users/test_catalog.html#stack-clash

> + */
> +
> +#define MAPS_BUF_SZ 4096
> +#define FOURGB (1UL << 32)
FYI we also have TST_GB in include/tst_fs.h.

> +#define MNTPOINT "hugetlbfs/"
> +
> +#include "hugetlb.h"
> +
> +static unsigned long hpage_size;
> +static int fd = -1;
> +static unsigned long straddle_addr;
> +
> +static int test_addr_huge(void *p)
> +{
> +	char name[256];
> +	char *dirend;
> +	int ret;
> +	struct statfs64 sb;
> +
> +	ret = read_maps((unsigned long)p, name);
> +	if (ret < 0)
> +		return ret;
> +
> +	if (ret == 0) {
> +		tst_res(TINFO, "Couldn't find address %p in /proc/self/maps", p);
Having this as info message which is later followed by tst_brk(TFAIL) is is not
optimal. I suspect test is unnecessary complicated, but I'm not sure now how to
improve it.

> +		return -1;
> +	}
> +
> +	/* looks like a filename? */
> +	if (name[0] != '/')
> +		return 0;
> +
> +	/* Truncate the filename portion */
> +
nit: empty line above, please remove it.
> +	dirend = strrchr(name, '/');
> +	if (dirend && dirend > name)
> +		*dirend = '\0';
> +
> +	ret = statfs64(name, &sb);
> +	if (ret)
> +		return -1;
I guess we really need statfs64() not just statfs(), right?
In case of the failure test should quit with tst_brk(TBROK | TERRNO, "...").

> +
> +	return (sb.f_type == HUGETLBFS_MAGIC);
> +}
> +
> +static void run_test(void)
> +{
> +	void *p;
> +
> +	/* We first try to get the mapping without MAP_FIXED */
> +	tst_res(TINFO, "Mapping without MAP_FIXED at %lx...", straddle_addr);
> +	p = mmap((void *)straddle_addr, 2*hpage_size, PROT_READ|PROT_WRITE,
> +			MAP_SHARED, fd, 0);
> +	if (p == (void *)straddle_addr) {
> +		/* These tests irrelevant if we didn't get the straddle address*/
> +		if (test_addr_huge(p) != 1) {
> +			tst_brk(TFAIL, "1st Mapped address is not hugepage");
> +			goto windup;
FYI tst_brk() quits testing (test executes only cleanup function. You need to
use tst_res(TFAIL, ...).

> +		}
> +		if (test_addr_huge(p + hpage_size) != 1) {
> +			tst_brk(TFAIL, "2nd Mapped address is not hugepage");
> +			goto windup;
> +		}
> +		memset(p, 0, hpage_size);
> +		memset(p + hpage_size, 0, hpage_size);
> +		tst_res(TPASS, "Mapping without MAP_FIXED at %lx... completed", straddle_addr);
> +	} else {
> +		tst_res(TINFO, "Got %p instead, never mind, let's move to mapping with MAP_FIXED", p);
> +		munmap(p, 2*hpage_size);
We have SAFE_MUNMAP().
> +	}
> +
> +	tst_res(TINFO, "Mapping with MAP_FIXED at %lx...", straddle_addr);
> +	p = mmap((void *)straddle_addr, 2*hpage_size, PROT_READ|PROT_WRITE,
> +				MAP_SHARED|MAP_FIXED, fd, 0);
> +
> +	if (p == MAP_FAILED) {
> +		/* this area crosses last low slice and first high slice */
> +		unsigned long below_start = FOURGB - 256L*1024*1024;
> +		unsigned long above_end = 1024L*1024*1024*1024;
> +
> +		if (tst_mapping_in_range(below_start, above_end) == 1) {
> +			tst_res(TINFO, "region (4G-256M)-1T is not free");
> +			tst_res(TINFO, "mmap() failed: %s", strerror(errno));
> +			tst_res(TWARN, "Pass Inconclusive!");
I don't understand "Pass Inconclusive!". Could it be just described what happen?

We have TERRNO which we use instead strerror(). Instead of all 3 tst_res()
mesagess above I would use something like:
			tst_res(TFAIL | TERRNO, "region (4G-256M)-1T is not free");

> +			goto windup;
> +		} else
> +			tst_res(TFAIL, "mmap() FIXED failed: %s", strerror(errno));
I also don't understand "FIXED". I guess it was supposed to be "mmap() with MAP_FIXED failed".
Does it even make sense to continue on mmap() failure? The original test does,
but I'm not sure if that's ok.

very nit: if you use { } on if, they should be also on else.
} else {
> 		tst_res(TFAIL | TERRNO, "mmap() failed: %s", strerror(errno));
}


> +	}
> +
The code below uses wrong indent
> +		if (p != (void *)straddle_addr) {
> +			tst_res(TINFO, "got %p instead", p);
> +			tst_res(TFAIL, "Wrong address with MAP_FIXED");
> +			goto windup;
> +		}
> +
> +		if (test_addr_huge(p) != 1) {
> +			tst_brk(TFAIL, "1st Mapped address is not hugepage");
> +			goto windup;
> +		}
> +
> +		if (test_addr_huge(p + hpage_size) != 1) {
> +			tst_brk(TFAIL, "2nd Mapped address is not hugepage");
> +			goto windup;
> +		}
> +		memset(p, 0, hpage_size);
> +		memset(p + hpage_size, 0, hpage_size);
Why is this memset() for? If needed for testing, shouldn't it be at the
beginning?

> +		tst_res(TPASS, "Mapping with MAP_FIXED at %lx... completed", straddle_addr);
> +
> +windup:
> +	SAFE_MUNMAP(p, 2*hpage_size);
> +}
> +
> +static void setup(void)
> +{
> +	straddle_addr = FOURGB - hpage_size;
> +	hpage_size = tst_get_hugepage_size();
> +	fd = tst_creat_unlinked(MNTPOINT, 0, 0600);

> +	if (hpage_size > FOURGB)
> +		tst_brk(TCONF, "Huge page size is too large!");
I suppose the original check is really needed.

The original test does this check for this:

static inline long check_hugepagesize()
{
	long __hpage_size = gethugepagesize();
	if (__hpage_size < 0) {
		if (errno == ENOSYS)
			CONFIG("No hugepage kernel support\n");
		else if (errno == EOVERFLOW)
			CONFIG("Hugepage size too large");
		else
			CONFIG("Hugepage size (%s)", strerror(errno));
	}
	return __hpage_size;
}

Kind regards,
Petr

[6] https://github.com/libhugetlbfs/libhugetlbfs/blob/master/tests/hugetests.h#L126-L138

> +}
> +
> +static void cleanup(void)
> +{
> +	if (fd >= 0)
> +		SAFE_CLOSE(fd);
> +}
> +
> +static struct tst_test test = {
> +	.needs_root = 1,
> +	.mntpoint = MNTPOINT,
> +	.needs_hugetlbfs = 1,
> +	.hugepages = {2, TST_NEEDS},
> +	.setup = setup,
> +	.cleanup = cleanup,
> +	.test_all = run_test,
> +};

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

  reply	other threads:[~2025-11-27 11:33 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-27  7:22 [LTP] [PATCH 1/3] Adding new function read_maps required for hugemmap40 Pavithra
2025-11-27  7:22 ` [LTP] [PATCH 2/3] Adding magic definition " Pavithra
2025-11-27  8:19   ` Petr Vorel
2025-11-27  7:22 ` [LTP] [PATCH 3/3] [PATCH] [PATCH] Migrating the libhugetlbfs/testcases/straddle_4GB.c v3 Pavithra
2025-11-27 11:33   ` Petr Vorel [this message]
2025-11-27 10:31 ` [LTP] [PATCH 1/3] Adding new function read_maps required for hugemmap40 Petr Vorel

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=20251127113310.GB227467@pevik \
    --to=pvorel@suse.cz \
    --cc=ltp@lists.linux.it \
    --cc=pavrampu@linux.ibm.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.