mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Nicolin Chen <nicolinc@nvidia.com>
To: Jason Gunthorpe <jgg@nvidia.com>
Cc: "Thomas Weißschuh" <thomas.weissschuh@linutronix.de>,
	"Shuah Khan" <shuah@kernel.org>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Willy Tarreau" <w@1wt.eu>,
	"Thomas Weißschuh" <linux@weissschuh.net>,
	"Kees Cook" <kees@kernel.org>,
	"Andy Lutomirski" <luto@amacapital.net>,
	"Will Drewry" <wad@chromium.org>,
	"Mark Brown" <broonie@kernel.org>,
	"Muhammad Usama Anjum" <usama.anjum@collabora.com>,
	linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org
Subject: Re: [PATCH v4 09/14] selftests: harness: Move teardown conditional into test metadata
Date: Wed, 11 Jun 2025 23:59:00 -0700	[thread overview]
Message-ID: <aEp6tGUEFCQz1prh@nvidia.com> (raw)
In-Reply-To: <20250611235117.GR543171@nvidia.com>

On Wed, Jun 11, 2025 at 08:51:17PM -0300, Jason Gunthorpe wrote:
> On Wed, Jun 11, 2025 at 04:43:00PM -0700, Nicolin Chen wrote:
> > So, the test case sets an alignment with HUGEPAGE_SIZE=512MB while
> > allocating buffer_size=64MB:
> > 	rc = posix_memalign(&self->buffer, HUGEPAGE_SIZE, variant->buffer_size);
> > 	vrc = mmap(self->buffer, variant->buffer_size, PROT_READ | PROT_WRITE,
> > this gives the self->buffer a location that is 512MB aligned, but
> > only mmap part of one 512MB huge page.
> > 
> > On the other hand, _metadata->no_teardown was mmap() outside the
> > range of the [self->buffer, self->buffer + 64MB), but within the
> > range of [self->buffer, self->buffer + 512MB).
> > 
> > E.g.
> >    _metadata->no_teardown = 0xfffbfc610000 // inside range2 below
> >    buffer=[0xfffbe0000000, fffbe4000000) // range1
> >    buffer=[0xfffbe0000000, fffc00000000) // range2
> > 
> > Then ,the "vrc = mmap(..." overwrites the _metadata->no_teardown
> > location to NULL..
> > 
> > The following change can fix, though it feels odd that the buffer
> > has to be preserved with the entire huge page:
> > ---------------------------------------------------------------
> > @@ -2024,3 +2027,4 @@ FIXTURE_SETUP(iommufd_dirty_tracking)
> > 
> > -       rc = posix_memalign(&self->buffer, HUGEPAGE_SIZE, variant->buffer_size);
> > +       rc = posix_memalign(&self->buffer, HUGEPAGE_SIZE,
> > +                           __ALIGN_KERNEL(variant->buffer_size, HUGEPAGE_SIZE));
> >         if (rc || !self->buffer) {
> > ---------------------------------------------------------------
> > 
> > Any thought?
> 
> This seems like something, variant->buffer_size should not
> be less than HUGEPAGE_SIZE I guess that is possible on 64K ARM64
> 
> But I still don't quite get it..
> 
>         rc = posix_memalign(&self->buffer, HUGEPAGE_SIZE, variant->buffer_size);
> 
> Should allocate buffer_size
> 
>         mmap_flags = MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED;
>                 mmap_flags |= MAP_HUGETLB | MAP_POPULATE;
>         vrc = mmap(self->buffer, variant->buffer_size, PROT_READ | PROT_WRITE,
>                    mmap_flags, -1, 0);
> 
> Should fail if buffer_size is not a multiple of HUGEPAGE_SIZE? 

Yea, I think you are right. But..

> It certainly shouldn't mmap past the provided buffer_size!!!
> 
> Are you seeing the above mmap succeed and also map beyond buffer -> buffer + buffer_size?
> 
> I think that would be a kernel bug in MAP_HUGETLB!

..I did some bpftrace:

ksys_mmap_pgoff() addr=ffff80000000, len=4000000
    hugetlb_file_setup(): size=0x20000000
        hugetlb_reserve_pages() from=0, to=1
        hugetlb_reserve_pages() returned: ret=1
    hugetlb_file_setup() returned: size=0x20000000 ret=-281471746619776
    vm_mmap_pgoff() addr=ffff80000000, len=20000000
        do_mmap() addr=ffff80000000, len=20000000
            hugetlb_reserve_pages() from=0, to=1
            hugetlb_reserve_pages() returned: ret=1
        do_mmap() returned: addr=0xffff80000000 ret=ffff80000000, pop=20000000
    vm_mmap_pgoff() returned: addr=0xffff80000000 ret=ffff80000000
ksys_mmap_pgoff() returned: addr=0xffff80000000 ret=ffff80000000

We can see the 64MB was rounded up to 512MB by ksys_mmap_pgoff()
when being passed in to hugetlb_file_setup() at:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/mm/mmap.c?h=v6.16-rc1#n594
"		len = ALIGN(len, huge_page_size(hs));  "

By looking at the comments here..:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/hugetlbfs/inode.c#n1521
"
/*
 * Note that size should be aligned to proper hugepage size in caller side,
 * otherwise hugetlb_reserve_pages reserves one less hugepages than intended.
 */
struct file *hugetlb_file_setup(const char *name, size_t size,
"

..I guess this function was supposed to fail the not-a-multiple
case as you remarked? But it certainly can't do that, when that
size passed in is already hugepage-aligned..

It feels like a kernel bug as you suspect :-/


And I just found one more weird thing...

In iommufd.c selftest code, we have:
"static __attribute__((constructor)) void setup_sizes(void)"
where it does another pair of posix_memalign/mmap, although this
one doesn't flag MAP_HUGETLB and shouldn't impact what is coming
to the next...

If I keep this code, the first hugepage test case can pass (64MB
buffer_size; 512MB THP), but all the following cases will fail,
as I reported here:
https://lore.kernel.org/all/aEm6tuzy7WK12sMh@nvidia.com/

If I remove this code, the hugepage test case will fail from the
first case with signal 11. But this time, it is not because the
mmap() overwrites the _metadata->no_teardown, it's because mmap()
call itself crashed...

And, in either a failed case (crashed) or a passed case, the top
kernel function ksys_mmap_pgoff() returned successfully, which
means it seemingly crashed inside the libc?

Thanks
Nicolin

  reply	other threads:[~2025-06-12  6:59 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-05 15:15 [PATCH v4 00/14] kselftest harness and nolibc compatibility Thomas Weißschuh
2025-05-05 15:15 ` [PATCH v4 01/14] selftests: harness: Add kselftest harness selftest Thomas Weißschuh
2025-05-05 15:15 ` [PATCH v4 02/14] selftests: harness: Use C89 comment style Thomas Weißschuh
2025-05-05 15:15 ` [PATCH v4 03/14] selftests: harness: Ignore unused variant argument warning Thomas Weißschuh
2025-05-05 15:15 ` [PATCH v4 04/14] selftests: harness: Mark functions without prototypes static Thomas Weißschuh
2025-05-05 15:15 ` [PATCH v4 05/14] selftests: harness: Remove inline qualifier for wrappers Thomas Weißschuh
2025-05-05 15:15 ` [PATCH v4 06/14] selftests: harness: Remove dependency on libatomic Thomas Weißschuh
2025-05-05 15:15 ` [PATCH v4 07/14] selftests: harness: Implement test timeouts through pidfd Thomas Weißschuh
2025-05-05 15:15 ` [PATCH v4 08/14] selftests: harness: Don't set setup_completed for fixtureless tests Thomas Weißschuh
2025-05-05 15:15 ` [PATCH v4 09/14] selftests: harness: Move teardown conditional into test metadata Thomas Weißschuh
2025-06-10  6:49   ` Nicolin Chen
2025-06-10 11:38     ` Thomas Weißschuh
2025-06-10 12:09       ` Jason Gunthorpe
2025-06-10 18:48         ` Nicolin Chen
2025-06-10 23:46           ` Jason Gunthorpe
2025-06-11  7:05             ` Nicolin Chen
2025-06-11  8:04               ` Thomas Weißschuh
2025-06-11 17:19                 ` Nicolin Chen
2025-06-11 21:47                   ` Nicolin Chen
2025-06-11 23:43                     ` Nicolin Chen
2025-06-11 23:51                       ` Jason Gunthorpe
2025-06-12  6:59                         ` Nicolin Chen [this message]
2025-06-12 13:58                           ` Jason Gunthorpe
2025-06-12 14:27                             ` Thomas Weißschuh
2025-06-12 14:58                               ` Jason Gunthorpe
2025-06-12 15:23                                 ` Thomas Weißschuh
2025-06-12 15:42                                   ` Jason Gunthorpe
2025-06-12 17:53                                     ` Nicolin Chen
2025-06-12 18:53                                       ` Nicolin Chen
2025-06-12 18:56                                         ` Jason Gunthorpe
2025-06-12 19:03                                           ` Nicolin Chen
2025-06-12 23:31                                             ` Jason Gunthorpe
2025-06-13  5:55                                               ` Nicolin Chen
2025-06-11  6:53           ` Thomas Weißschuh
2025-05-05 15:15 ` [PATCH v4 10/14] selftests: harness: Add teardown callback to " Thomas Weißschuh
2025-05-05 15:15 ` [PATCH v4 11/14] selftests: harness: Add "variant" and "self" " Thomas Weißschuh
2025-05-05 15:15 ` [PATCH v4 12/14] selftests: harness: Stop using setjmp()/longjmp() Thomas Weißschuh
2025-06-10  6:40   ` Nicolin Chen
2025-06-10 12:21     ` Thomas Weißschuh
2025-06-10 23:13       ` Nicolin Chen
2025-05-05 15:15 ` [PATCH v4 13/14] selftests: harness: Guard includes on nolibc Thomas Weißschuh
2025-05-05 15:15 ` [PATCH v4 14/14] HACK: selftests/nolibc: demonstrate usage of the kselftest harness Thomas Weißschuh
2025-05-06  5:59   ` Mark Brown
2025-05-10  6:54 ` [PATCH v4 00/14] kselftest harness and nolibc compatibility Thomas Weißschuh
2025-05-12 21:32   ` Shuah Khan
2025-05-13  5:33     ` Thomas Weißschuh

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=aEp6tGUEFCQz1prh@nvidia.com \
    --to=nicolinc@nvidia.com \
    --cc=broonie@kernel.org \
    --cc=jgg@nvidia.com \
    --cc=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux@weissschuh.net \
    --cc=luto@amacapital.net \
    --cc=shuah@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=thomas.weissschuh@linutronix.de \
    --cc=usama.anjum@collabora.com \
    --cc=w@1wt.eu \
    --cc=wad@chromium.org \
    /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

all inboxes | Powered by JetHome®