From: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Vlastimil Babka <vbabka@suse.cz>,
"Liam R . Howlett" <Liam.Howlett@oracle.com>,
Suren Baghdasaryan <surenb@google.com>,
Arnd Bergmann <arnd@arndb.de>,
Shakeel Butt <shakeel.butt@linux.dev>,
linux-api@vger.kernel.org, linux-mm@kvack.org,
linux-kernel@vger.kernel.org, Minchan Kim <minchan@kernel.org>,
Richard Henderson <richard.henderson@linaro.org>,
Ivan Kokshaysky <ink@jurassic.park.msu.ru>,
Matt Turner <mattst88@gmail.com>,
linux-alpha@vger.kernel.org,
Thomas Bogendoerfer <tsbogend@alpha.franken.de>,
linux-mips@vger.kernel.org,
"James E . J . Bottomley" <James.Bottomley@HansenPartnership.com>,
Helge Deller <deller@gmx.de>,
linux-parisc@vger.kernel.org, Chris Zankel <chris@zankel.net>,
Max Filippov <jcmvbkbc@gmail.com>
Subject: [PATCH v2 2/2] selftests/mm: add test for process_madvise PR_MADV_SELF flag use
Date: Tue, 24 Sep 2024 12:16:28 +0100 [thread overview]
Message-ID: <c155cc65f732705f6bd8436a00a1821b3f2a80c5.1727176176.git.lorenzo.stoakes@oracle.com> (raw)
In-Reply-To: <cover.1727176176.git.lorenzo.stoakes@oracle.com>
Add a new process_madvise() selftest, and add a test for the newly
introduced PR_MADV_SELF flag.
Assert that we can perform a vector operation of an operation that would
not be permitted on a remote mm (MADV_DONTNEED in this instance) on ones in
our own mm and that the operation is correctly peformed.
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
---
tools/testing/selftests/mm/.gitignore | 1 +
tools/testing/selftests/mm/Makefile | 1 +
tools/testing/selftests/mm/process_madvise.c | 115 +++++++++++++++++++
3 files changed, 117 insertions(+)
create mode 100644 tools/testing/selftests/mm/process_madvise.c
diff --git a/tools/testing/selftests/mm/.gitignore b/tools/testing/selftests/mm/.gitignore
index da030b43e43b..a875376601b7 100644
--- a/tools/testing/selftests/mm/.gitignore
+++ b/tools/testing/selftests/mm/.gitignore
@@ -51,3 +51,4 @@ hugetlb_madv_vs_map
mseal_test
seal_elf
droppable
+process_madvise
diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile
index 02e1204971b0..7503ec177cd2 100644
--- a/tools/testing/selftests/mm/Makefile
+++ b/tools/testing/selftests/mm/Makefile
@@ -79,6 +79,7 @@ TEST_GEN_FILES += hugetlb_fault_after_madv
TEST_GEN_FILES += hugetlb_madv_vs_map
TEST_GEN_FILES += hugetlb_dio
TEST_GEN_FILES += droppable
+TEST_GEN_FILES += process_madvise
ifneq ($(ARCH),arm64)
TEST_GEN_FILES += soft-dirty
diff --git a/tools/testing/selftests/mm/process_madvise.c b/tools/testing/selftests/mm/process_madvise.c
new file mode 100644
index 000000000000..7a29b77d837d
--- /dev/null
+++ b/tools/testing/selftests/mm/process_madvise.c
@@ -0,0 +1,115 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#define _GNU_SOURCE
+#include "../kselftest_harness.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/uio.h>
+
+/* May not be available in host system yet. */
+#ifndef PR_MADV_SELF
+#define PR_MADV_SELF (1<<0)
+#endif
+
+FIXTURE(process_madvise)
+{
+ unsigned long page_size;
+};
+
+FIXTURE_SETUP(process_madvise)
+{
+ self->page_size = (unsigned long)sysconf(_SC_PAGESIZE);
+};
+
+FIXTURE_TEARDOWN(process_madvise)
+{
+}
+
+static void populate_range(char *ptr, size_t len)
+{
+ memset(ptr, 'x', len);
+}
+
+static bool is_range_zeroed(char *ptr, size_t len)
+{
+ size_t i;
+
+ for (i = 0; i < len; i++) {
+ if (ptr[i] != '\0')
+ return false;
+ }
+
+ return true;
+}
+
+TEST_F(process_madvise, pr_madv_self)
+{
+ const unsigned long page_size = self->page_size;
+ struct iovec vec[3];
+ char *ptr_region, *ptr, *ptr2, *ptr3;
+
+ /* Establish a region in which to place VMAs. */
+ ptr_region = mmap(NULL, 100 * page_size, PROT_NONE,
+ MAP_PRIVATE | MAP_ANON, -1, 0);
+ ASSERT_NE(ptr_region, MAP_FAILED);
+
+ /* Place a 5 page mapping offset by one page into the region. */
+ ptr = mmap(&ptr_region[page_size], 5 * page_size,
+ PROT_READ | PROT_WRITE,
+ MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
+ ASSERT_NE(ptr, MAP_FAILED);
+ populate_range(ptr, 5 * page_size);
+ vec[0].iov_base = ptr;
+ vec[0].iov_len = 5 * page_size;
+ /* Free the PROT_NONE region before this region. */
+ ASSERT_EQ(munmap(ptr_region, page_size), 0);
+
+ /* Place a 10 page mapping in the middle of the region. */
+ ptr2 = mmap(&ptr_region[50 * page_size], 10 * page_size,
+ PROT_READ | PROT_WRITE,
+ MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
+ ASSERT_NE(ptr2, MAP_FAILED);
+ populate_range(ptr2, 10 * page_size);
+ vec[1].iov_base = ptr2;
+ vec[1].iov_len = 10 * page_size;
+ /* Free the PROT_NONE region before this region. */
+ ASSERT_EQ(munmap(&ptr_region[6 * page_size], 44 * page_size), 0);
+
+ /* Place a 3 page mapping at the end of the region, offset by 1. */
+ ptr3 = mmap(&ptr_region[96 * page_size], 3 * page_size,
+ PROT_READ | PROT_WRITE,
+ MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0);
+ ASSERT_NE(ptr3, MAP_FAILED);
+ populate_range(ptr3, 3 * page_size);
+ vec[2].iov_base = ptr3;
+ vec[2].iov_len = 3 * page_size;
+ /* Free the PROT_NONE region before this region. */
+ ASSERT_EQ(munmap(&ptr_region[60 * page_size], 36 * page_size), 0);
+ /* Free the PROT_NONE region after this region. */
+ ASSERT_EQ(munmap(&ptr_region[99 * page_size], page_size), 0);
+
+ /*
+ * OK now we should have three distinct regions of memory. Zap
+ * them with MADV_DONTNEED. This should clear the populated ranges and
+ * we can then assert on them being zeroed.
+ *
+ * The function returns the number of bytes advised, so assert this is
+ * equal to the total size of the three regions.
+ */
+ ASSERT_EQ(process_madvise(0, vec, 3, MADV_DONTNEED, PR_MADV_SELF),
+ (5 + 10 + 3) * page_size);
+
+ /* Make sure these ranges are now zeroed. */
+ ASSERT_TRUE(is_range_zeroed(ptr, 5 * page_size));
+ ASSERT_TRUE(is_range_zeroed(ptr2, 10 * page_size));
+ ASSERT_TRUE(is_range_zeroed(ptr2, 3 * page_size));
+
+ /* Cleanup. */
+ ASSERT_EQ(munmap(ptr, 5 * page_size), 0);
+ ASSERT_EQ(munmap(ptr2, 10 * page_size), 0);
+ ASSERT_EQ(munmap(ptr3, 3 * page_size), 0);
+}
+
+TEST_HARNESS_MAIN
--
2.46.0
next prev parent reply other threads:[~2024-09-24 11:17 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-24 11:16 [PATCH v2 0/2] unrestrict process_madvise() for current process Lorenzo Stoakes
2024-09-24 11:16 ` [PATCH v2 1/2] mm/madvise: introduce PR_MADV_SELF flag to process_madvise() Lorenzo Stoakes
2024-09-24 12:51 ` Pedro Falcato
2024-09-24 13:12 ` Lorenzo Stoakes
2024-09-25 14:02 ` Shakeel Butt
2024-09-25 14:48 ` Lorenzo Stoakes
2024-09-25 16:19 ` Shakeel Butt
2024-09-25 17:04 ` Lorenzo Stoakes
2024-09-25 18:14 ` Pedro Falcato
2024-09-25 18:31 ` Lorenzo Stoakes
2024-09-25 21:37 ` Shakeel Butt
2024-09-24 11:16 ` Lorenzo Stoakes [this message]
2024-09-26 9:44 ` [PATCH v2 0/2] unrestrict process_madvise() for current process Lorenzo Stoakes
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=c155cc65f732705f6bd8436a00a1821b3f2a80c5.1727176176.git.lorenzo.stoakes@oracle.com \
--to=lorenzo.stoakes@oracle.com \
--cc=James.Bottomley@HansenPartnership.com \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=arnd@arndb.de \
--cc=chris@zankel.net \
--cc=deller@gmx.de \
--cc=ink@jurassic.park.msu.ru \
--cc=jcmvbkbc@gmail.com \
--cc=linux-alpha@vger.kernel.org \
--cc=linux-api@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mips@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-parisc@vger.kernel.org \
--cc=mattst88@gmail.com \
--cc=minchan@kernel.org \
--cc=richard.henderson@linaro.org \
--cc=shakeel.butt@linux.dev \
--cc=surenb@google.com \
--cc=tsbogend@alpha.franken.de \
--cc=vbabka@suse.cz \
/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