linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Nhat Pham <nphamcs@gmail.com>
To: Suresh K C <suresh.k.chandrappa@gmail.com>
Cc: hannes@cmpxchg.org, shuah@kernel.org, linux-mm@kvack.org,
	 linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] selftests: cachestat: add tests for mmap
Date: Mon, 30 Jun 2025 15:30:32 -0700	[thread overview]
Message-ID: <CAKEwX=PRdkDEmP5N3t70qg7ugreLvyNHVxR0SGwU8pwasgqG4A@mail.gmail.com> (raw)
In-Reply-To: <20250630180803.12866-1-suresh.k.chandrappa@gmail.com>

On Mon, Jun 30, 2025 at 11:08 AM Suresh K C
<suresh.k.chandrappa@gmail.com> wrote:
>
> From: Suresh K C <suresh.k.chandrappa@gmail.com>
>
> Add a test case to verify cachestat behavior with memory-mapped files
> using mmap(). This ensures that pages accessed via mmap are correctly
> accounted for in the page cache.
>
> Tested on x86_64 with default kernel config
>
> Signed-off-by: Suresh K C <suresh.k.chandrappa@gmail.com>

FWIW, the tests passed :)

> ---
>  .../selftests/cachestat/test_cachestat.c      | 39 ++++++++++++++++---
>  1 file changed, 33 insertions(+), 6 deletions(-)
>
> diff --git a/tools/testing/selftests/cachestat/test_cachestat.c b/tools/testing/selftests/cachestat/test_cachestat.c
> index 632ab44737ec..b6452978dae0 100644
> --- a/tools/testing/selftests/cachestat/test_cachestat.c
> +++ b/tools/testing/selftests/cachestat/test_cachestat.c
> @@ -33,6 +33,11 @@ void print_cachestat(struct cachestat *cs)
>         cs->nr_evicted, cs->nr_recently_evicted);
>  }
>
> +enum file_type {
> +       FILE_MMAP,
> +       FILE_SHMEM
> +};
> +
>  bool write_exactly(int fd, size_t filesize)
>  {
>         int random_fd = open("/dev/urandom", O_RDONLY);
> @@ -202,7 +207,7 @@ static int test_cachestat(const char *filename, bool write_random, bool create,
>         return ret;
>  }
>
> -bool test_cachestat_shmem(void)
> +bool run_cachestat_test(enum file_type type)
>  {
>         size_t PS = sysconf(_SC_PAGESIZE);
>         size_t filesize = PS * 512 * 2; /* 2 2MB huge pages */
> @@ -212,27 +217,43 @@ bool test_cachestat_shmem(void)
>         char *filename = "tmpshmcstat";
>         struct cachestat cs;
>         bool ret = true;
> +       int fd;
>         unsigned long num_pages = compute_len / PS;
> -       int fd = shm_open(filename, O_CREAT | O_RDWR, 0600);
> +       if (type == FILE_SHMEM)
> +               fd = shm_open(filename, O_CREAT | O_RDWR, 0600);
> +       else
> +               fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0666);
>
>         if (fd < 0) {
> -               ksft_print_msg("Unable to create shmem file.\n");
> +               ksft_print_msg("Unable to create file.\n");
>                 ret = false;
>                 goto out;
>         }
>
>         if (ftruncate(fd, filesize)) {
> -               ksft_print_msg("Unable to truncate shmem file.\n");
> +               ksft_print_msg("Unable to truncate file.\n");
>                 ret = false;
>                 goto close_fd;
>         }
>
>         if (!write_exactly(fd, filesize)) {
> -               ksft_print_msg("Unable to write to shmem file.\n");
> +               ksft_print_msg("Unable to write to file.\n");
>                 ret = false;
>                 goto close_fd;
>         }
>
> +       if (type == FILE_MMAP){
> +               char *map = mmap(NULL, filesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
> +               if (map == MAP_FAILED) {
> +                       ksft_print_msg("mmap failed.\n");
> +                       ret = false;
> +                       goto close_fd;
> +               }
> +               for (int i = 0; i < filesize; i++) {
> +                       map[i] = 'A';
> +               }
> +               map[filesize - 1] = 'X';
> +       }

Joshua is right. We're already doing the write_exactly() for both
cases? Let's write_exactly() (i.e using the file descriptor-based
write syscall) only for FILE_SHMEM.

And why do we need the 'X' at the end?


      parent reply	other threads:[~2025-06-30 22:30 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-30 18:08 Suresh K C
2025-06-30 20:47 ` Joshua Hahn
2025-06-30 22:30 ` Nhat Pham [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='CAKEwX=PRdkDEmP5N3t70qg7ugreLvyNHVxR0SGwU8pwasgqG4A@mail.gmail.com' \
    --to=nphamcs@gmail.com \
    --cc=hannes@cmpxchg.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=shuah@kernel.org \
    --cc=suresh.k.chandrappa@gmail.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