From: "Gregory Price (Meta)" <gourry@gourry.net>
To: linux-kselftest@vger.kernel.org, linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org, kernel-team@meta.com,
akpm@linux-foundation.org, david@kernel.org, ljs@kernel.org,
liam@infradead.org, vbabka@kernel.org, rppt@kernel.org,
surenb@google.com, mhocko@suse.com, shuah@kernel.org
Subject: [PATCH 3/8] selftests/mm: check MADV_COLD and MADV_PAGEOUT range handling
Date: Wed, 14 Oct 2026 13:57:51 -0400 [thread overview]
Message-ID: <20261014175756.2035176-4-gourry@gourry.net> (raw)
In-Reply-To: <20261014175756.2035176-1-gourry@gourry.net>
madvise(2) accepts a zero-length range, rejects an unaligned start, and
reports an unmapped gap in the range with ENOMEM while still applying the
advice to the mapped pages on either side.
Check each of these for both hints. Around the gap, only MADV_PAGEOUT has
an effect the test can observe, so only MADV_PAGEOUT is checked for
paging out both neighbours, after a control page shows that swap works.
Signed-off-by: Gregory Price (Meta) <gourry@gourry.net>
---
tools/testing/selftests/mm/madvise.c | 103 +++++++++++++++++++++++++++
1 file changed, 103 insertions(+)
diff --git a/tools/testing/selftests/mm/madvise.c b/tools/testing/selftests/mm/madvise.c
index 42cc08fe45ae..f122154fac05 100644
--- a/tools/testing/selftests/mm/madvise.c
+++ b/tools/testing/selftests/mm/madvise.c
@@ -5,6 +5,7 @@
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
+#include <sched.h>
#include <stdbool.h>
#include <sys/mman.h>
#include <unistd.h>
@@ -51,6 +52,21 @@ static bool pageout_until_swapped(struct __test_metadata *_metadata,
return false;
}
+/*
+ * Keep this task on one CPU, so that draining that CPU's LRU batches drains
+ * the folios this task faulted in.
+ */
+static void pin_to_current_cpu(struct __test_metadata *_metadata)
+{
+ int cpu = sched_getcpu();
+ cpu_set_t mask;
+
+ ASSERT_GE(cpu, 0);
+ CPU_ZERO(&mask);
+ CPU_SET(cpu, &mask);
+ ASSERT_EQ(sched_setaffinity(0, sizeof(mask), &mask), 0);
+}
+
/*
* Both hints are best effort, so these tests check what userspace can rely
* on: which mappings are accepted, which pages must never be reclaimed, and
@@ -111,6 +127,93 @@ TEST_F(madvise, locked_vma_is_rejected)
ASSERT_EQ(munmap(mapping, page_size), 0);
}
+/* A zero-length range is accepted. */
+TEST_F(madvise, zero_length_is_accepted)
+{
+ char *mapping;
+
+ mapping = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ ASSERT_NE(mapping, MAP_FAILED);
+
+ ASSERT_EQ(madvise(mapping, 0, MADV_COLD), 0);
+ ASSERT_EQ(madvise(mapping, 0, MADV_PAGEOUT), 0);
+
+ ASSERT_EQ(munmap(mapping, page_size), 0);
+}
+
+/* The start of the range must be page-aligned. */
+TEST_F(madvise, unaligned_start_is_rejected)
+{
+ char *mapping;
+
+ mapping = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ ASSERT_NE(mapping, MAP_FAILED);
+ mapping[0] = 1;
+
+ ASSERT_EQ(madvise(mapping + 1, page_size, MADV_COLD), -1);
+ ASSERT_EQ(errno, EINVAL);
+ ASSERT_EQ(madvise(mapping + 1, page_size, MADV_PAGEOUT), -1);
+ ASSERT_EQ(errno, EINVAL);
+ ASSERT_EQ(mapping[0], 1);
+
+ ASSERT_EQ(munmap(mapping, 2 * page_size), 0);
+}
+
+/*
+ * An unmapped gap in the range makes either hint fail with ENOMEM, after
+ * advising the mapped pages on both sides of the gap. Only MADV_PAGEOUT has
+ * an effect on those pages that the test can observe.
+ */
+TEST_F(madvise, unmapped_gap_fails_after_advising)
+{
+ char *mapping, *control;
+ bool swapped = false;
+ int retry;
+
+ pin_to_current_cpu(_metadata);
+
+ /* Make sure MADV_PAGEOUT can swap at all before relying on it. */
+ control = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ ASSERT_NE(control, MAP_FAILED);
+ control[0] = 1;
+ if (!pageout_until_swapped(_metadata, control, page_size,
+ self->pagemap_fd))
+ SKIP(return, "MADV_PAGEOUT did not swap the page");
+
+ mapping = mmap(NULL, 3 * page_size, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ ASSERT_NE(mapping, MAP_FAILED);
+ mapping[0] = 1;
+ mapping[2 * page_size] = 1;
+ ASSERT_EQ(munmap(mapping + page_size, page_size), 0);
+
+ ASSERT_EQ(madvise(mapping, 3 * page_size, MADV_COLD), -1);
+ ASSERT_EQ(errno, ENOMEM);
+ ASSERT_EQ(mapping[0], 1);
+ ASSERT_EQ(mapping[2 * page_size], 1);
+
+ for (retry = 0; retry < PAGEOUT_RETRIES && !swapped; retry++) {
+ ASSERT_EQ(madvise(mapping, 3 * page_size, MADV_PAGEOUT), -1);
+ ASSERT_EQ(errno, ENOMEM);
+ swapped = pagemap_is_swapped(self->pagemap_fd, mapping) &&
+ pagemap_is_swapped(self->pagemap_fd,
+ mapping + 2 * page_size);
+ if (!swapped)
+ usleep(10000);
+ }
+ ASSERT_TRUE(swapped)
+ TH_LOG("MADV_PAGEOUT did not page out both sides of the gap");
+ ASSERT_EQ(mapping[0], 1);
+ ASSERT_EQ(mapping[2 * page_size], 1);
+
+ ASSERT_EQ(munmap(mapping, page_size), 0);
+ ASSERT_EQ(munmap(mapping + 2 * page_size, page_size), 0);
+ ASSERT_EQ(munmap(control, page_size), 0);
+}
+
/* Neither hint may fault a swapped page back in. */
static void check_skips_swapped_pte(struct __test_metadata *_metadata,
int pagemap_fd, int advice)
--
2.56.0
next prev parent reply other threads:[~2026-10-14 17:58 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-14 17:57 [PATCH 0/8] selftests/mm: add MADV_COLD and MADV_PAGEOUT contract tests Gregory Price (Meta)
2026-10-14 17:57 ` [PATCH 1/8] " Gregory Price (Meta)
2026-10-19 4:59 ` Sarthak Sharma
2026-10-14 17:57 ` [PATCH 2/8] selftests/mm: check that MADV_PAGEOUT skips holes and swapped pages Gregory Price (Meta)
2026-10-19 5:00 ` Sarthak Sharma
2026-10-14 17:57 ` Gregory Price (Meta) [this message]
2026-10-14 21:31 ` [PATCH] fixup! selftests/mm: check MADV_COLD and MADV_PAGEOUT range handling Gregory Price (Meta)
2026-10-16 12:17 ` [PATCH 3/8] " Sarthak Sharma
2026-10-16 13:22 ` Gregory Price
2026-10-16 14:07 ` Gregory Price
2026-10-19 4:48 ` Sarthak Sharma
2026-10-14 17:57 ` [PATCH 4/8] selftests/mm: check that MADV_COLD and MADV_PAGEOUT reject hugetlb Gregory Price (Meta)
2026-10-19 5:02 ` Sarthak Sharma
2026-10-14 17:57 ` [PATCH 5/8] selftests/mm: check that MADV_COLD and MADV_PAGEOUT preserve shared memory Gregory Price (Meta)
2026-10-19 5:13 ` Sarthak Sharma
2026-10-14 17:57 ` [PATCH 6/8] selftests/mm: check MADV_COLD and MADV_PAGEOUT on partly advised THPs Gregory Price (Meta)
2026-10-19 5:16 ` Sarthak Sharma
2026-10-14 17:57 ` [PATCH 7/8] selftests/mm: check MADV_PAGEOUT file permissions Gregory Price (Meta)
2026-10-16 12:18 ` Sarthak Sharma
2026-10-16 13:48 ` Gregory Price
2026-10-19 4:51 ` Sarthak Sharma
2026-10-14 17:57 ` [PATCH 8/8] selftests/mm: check that unauthorized MADV_PAGEOUT skips large file folios Gregory Price (Meta)
2026-10-14 21:26 ` Gregory Price
2026-10-19 5:19 ` Sarthak Sharma
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=20261014175756.2035176-4-gourry@gourry.net \
--to=gourry@gourry.net \
--cc=akpm@linux-foundation.org \
--cc=david@kernel.org \
--cc=kernel-team@meta.com \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=mhocko@suse.com \
--cc=rppt@kernel.org \
--cc=shuah@kernel.org \
--cc=surenb@google.com \
--cc=vbabka@kernel.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®