public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "David Hildenbrand (Arm)" <david@kernel.org>
To: Zi Yan <ziy@nvidia.com>
Cc: Vineet Agarwal <agarwal.vineet2006@gmail.com>,
	akpm@linux-foundation.org, ljs@kernel.org, shuah@kernel.org,
	linux-mm@kvack.org, linux-kselftest@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v4] selftests/mm: khugepaged: initialize file contents via mmap
Date: Wed, 29 Apr 2026 08:24:07 +0200	[thread overview]
Message-ID: <1c8a5cf4-e6e0-40da-b1e9-83f41a8b3f7a@kernel.org> (raw)
In-Reply-To: <8D71AC0C-A10D-4B1E-B5B9-606422E0B99B@nvidia.com>

On 4/28/26 22:23, Zi Yan wrote:
> On 28 Apr 2026, at 16:22, Zi Yan wrote:
> 
>> On 28 Apr 2026, at 16:19, David Hildenbrand (Arm) wrote:
>>
>>>
>>> But why doesn't the write() make the folios dirty? Or are we running into some
>>> race condition where the old code just worked by chance?
>>>
>>
>> The fd is opened with O_DSYNC, so the data from write() is transferred to
>> disk when write() returns.
> 
> And the folio is clean.

Ah, I missed the O_DSYNC. That makes sense, thanks.

v2 actually contained the msync. So I assume we could then drop the O_DSYNC.

Note: there is no need to close the file or munmap before the exit on the error
path.

The following works for me


diff --git a/tools/testing/selftests/mm/khugepaged.c
b/tools/testing/selftests/mm/khugepaged.c
index 3fe7ef04ac62..5862a3213840 100644
--- a/tools/testing/selftests/mm/khugepaged.c
+++ b/tools/testing/selftests/mm/khugepaged.c
@@ -373,7 +373,7 @@ static void *file_setup_area(int nr_hpages)
        unlink(finfo.path);  /* Cleanup from previous failed tests */
        printf("Creating %s for collapse%s...", finfo.path,
               finfo.type == VMA_SHMEM ? " (tmpfs)" : "");
-       fd = open(finfo.path, O_DSYNC | O_CREAT | O_RDWR | O_TRUNC | O_EXCL,
+       fd = open(finfo.path, O_CREAT | O_RDWR | O_TRUNC | O_EXCL,
                  777);
        if (fd < 0) {
                perror("open()");
@@ -381,9 +381,21 @@ static void *file_setup_area(int nr_hpages)
        }

        size = nr_hpages * hpage_pmd_size;
-       p = alloc_mapping(nr_hpages);
+       if (ftruncate(fd, size)) {
+               perror("ftruncate()");
+               exit(EXIT_FAILURE);
+       }
+       p = mmap(BASE_ADDR, size, PROT_READ | PROT_WRITE,
+               MAP_SHARED, fd, 0);
+       if (p != BASE_ADDR) {
+               perror("mmap()");
+               exit(EXIT_FAILURE);
+       }
        fill_memory(p, 0, size);
-       write(fd, p, size);
+       if (msync(p, size, MS_SYNC)) {
+               perror("msync()");
+               exit(EXIT_FAILURE);
+       }
        close(fd);
        munmap(p, size);
        success("OK");


-- 
Cheers,

David

      reply	other threads:[~2026-04-29  6:24 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-28 13:31 [PATCH v4] selftests/mm: khugepaged: initialize file contents via mmap Vineet Agarwal
2026-04-28 13:49 ` David Hildenbrand (Arm)
2026-04-28 19:56 ` Zi Yan
2026-04-28 20:07   ` David Hildenbrand (Arm)
2026-04-28 20:17     ` Zi Yan
2026-04-28 20:19       ` David Hildenbrand (Arm)
2026-04-28 20:22         ` Zi Yan
2026-04-28 20:23           ` Zi Yan
2026-04-29  6:24             ` David Hildenbrand (Arm) [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=1c8a5cf4-e6e0-40da-b1e9-83f41a8b3f7a@kernel.org \
    --to=david@kernel.org \
    --cc=agarwal.vineet2006@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=shuah@kernel.org \
    --cc=ziy@nvidia.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox