From: Richard Cheng <icheng@nvidia.com>
To: akpm@linux-foundation.org, david@kernel.org
Cc: jgg@ziepe.ca, jhubbard@nvidia.com, peterx@redhat.com,
linux-mm@kvack.org, linux-kernel@vger.kernel.org, ljs@kernel.org,
liam@infradead.org, vbabka@kernel.org, rppt@kernel.org,
surenb@google.com, mhocko@suse.com, shuah@kernel.org,
linux-kselftest@vger.kernel.org, kobak@nvidia.com,
kaihengf@nvidia.com, newtonl@nvidia.com, kristinc@nvidia.com,
Richard Cheng <icheng@nvidia.com>
Subject: [RFC PATCH 2/2] selftests/mm: Test per-open gup_test pin ownership
Date: Mon, 21 Sep 2026 17:42:43 +0800 [thread overview]
Message-ID: <20260921094243.64698-3-icheng@nvidia.com> (raw)
In-Reply-To: <20260921094243.64698-1-icheng@nvidia.com>
Long-term pin state should be isolated between independently opened
gup_test file descriptions, while descriptors created with dup() should
continue to share the state associated with their struct file.
Add tests using 2 independently opened gup_test descriptors. Verify that
stopping or closing the second descriptor doesn't disturb the first
descriptor's pin, that another START on the same descriptor fails with
EINVAL, and that READ fails with EINVAL after STOP.
Close the descriptor used by the existing test matrix before running the
ownership tests. Record whether gup_test was initially available so that
a later open failure is reported as a failure instead of being hidden as
a skip.
Also exercise the shared-file-description case: start a pin through the
original descriptor, verify that START through a duplicate fails with
EINVAL, close the original descriptor, and use the duplicate to READ and
STOP the pin
Reviewed-by: Koba Ko <kobak@nvidia.com>
Signed-off-by: Richard Cheng <icheng@nvidia.com>
---
tools/testing/selftests/mm/gup_longterm.c | 258 +++++++++++++++++++++-
1 file changed, 256 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/mm/gup_longterm.c b/tools/testing/selftests/mm/gup_longterm.c
index c03b4f8910c0..ecae71ecedd3 100644
--- a/tools/testing/selftests/mm/gup_longterm.c
+++ b/tools/testing/selftests/mm/gup_longterm.c
@@ -36,6 +36,9 @@ static int nr_hugetlbsizes;
static unsigned long hugetlbsizes[10];
static int gup_fd;
+#define GUP_TEST_FILE "/sys/kernel/debug/gup_test"
+#define NR_FD_OWNERSHIP_TESTS 3
+
static __fsword_t get_fs_type(int fd)
{
struct statfs fs;
@@ -500,6 +503,246 @@ static void run_test_case(struct test_case const *test_case)
hugetlbsizes[i]);
}
+static void test_fd_isolation(bool stop_fd2, bool gup_test_was_available)
+{
+ struct pin_longterm_test args = {
+ .size = pagesize,
+ };
+ char *mem1 = MAP_FAILED, *mem2 = MAP_FAILED;
+ char *read_buf = MAP_FAILED;
+ bool pin1 = false, pin2 = false;
+ __u64 read_addr;
+ int fd1 = -1, fd2 = -1;
+ int result = KSFT_FAIL;
+
+ log_test_start("Longterm GUP pin is isolated from fd2 %s",
+ stop_fd2 ? "STOP" : "close");
+
+ fd1 = open(GUP_TEST_FILE, O_RDWR);
+ if (fd1 < 0) {
+ ksft_print_msg("gup_test not available (%s)\n",
+ strerror(errno));
+ result = gup_test_was_available ? KSFT_FAIL : KSFT_SKIP;
+ goto report;
+ }
+
+ fd2 = open(GUP_TEST_FILE, O_RDWR);
+ if (fd2 < 0) {
+ ksft_print_msg("second gup_test open failed (%s)\n",
+ strerror(errno));
+ goto report;
+ }
+
+ mem1 = mmap(NULL, pagesize, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ mem2 = mmap(NULL, pagesize, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ read_buf = mmap(NULL, pagesize, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ if (mem1 == MAP_FAILED || mem2 == MAP_FAILED ||
+ read_buf == MAP_FAILED) {
+ ksft_print_msg("mmap() failed (%s)\n", strerror(errno));
+ goto report;
+ }
+
+ memset(mem1, 0x11, pagesize);
+ memset(mem2, 0x22, pagesize);
+
+ args.addr = (__u64)(uintptr_t)mem1;
+ if (ioctl(fd1, PIN_LONGTERM_TEST_START, &args)) {
+ ksft_print_msg("fd1 PIN_LONGTERM_TEST_START failed (%s)\n",
+ strerror(errno));
+ goto report;
+ }
+ pin1 = true;
+
+ args.addr = (__u64)(uintptr_t)mem2;
+ if (ioctl(fd2, PIN_LONGTERM_TEST_START, &args)) {
+ ksft_print_msg("fd2 PIN_LONGTERM_TEST_START failed (%s)\n",
+ strerror(errno));
+ goto report;
+ }
+ pin2 = true;
+
+ args.addr = (__u64)(uintptr_t)mem1;
+ if (ioctl(fd1, PIN_LONGTERM_TEST_START, &args) != -1 ||
+ errno != EINVAL) {
+ ksft_print_msg("second START on fd1 was not rejected with EINVAL\n");
+ goto report;
+ }
+
+ if (stop_fd2) {
+ if (ioctl(fd2, PIN_LONGTERM_TEST_STOP)) {
+ ksft_print_msg("fd2 PIN_LONGTERM_TEST_STOP failed (%s)\n",
+ strerror(errno));
+ goto report;
+ }
+ pin2 = false;
+
+ read_addr = (__u64)(uintptr_t)read_buf;
+ errno = 0;
+ if (ioctl(fd2, PIN_LONGTERM_TEST_READ, &read_addr) != -1 ||
+ errno != EINVAL) {
+ ksft_print_msg("fd2 READ after STOP was not rejected with EINVAL\n");
+ goto report;
+ }
+ } else {
+ if (close(fd2)) {
+ ksft_print_msg("close(fd2) failed (%s)\n",
+ strerror(errno));
+ fd2 = -1;
+ pin2 = false;
+ goto report;
+ }
+ fd2 = -1;
+ pin2 = false;
+ }
+
+ read_addr = (__u64)(uintptr_t)read_buf;
+ if (ioctl(fd1, PIN_LONGTERM_TEST_READ, &read_addr)) {
+ ksft_print_msg("fd1 PIN_LONGTERM_TEST_READ failed (%s)\n",
+ strerror(errno));
+ goto report;
+ }
+ if (memcmp(mem1, read_buf, pagesize)) {
+ ksft_print_msg("fd1 PIN_LONGTERM_TEST_READ data mismatch\n");
+ goto report;
+ }
+
+ result = KSFT_PASS;
+
+report:
+ if (pin2 && ioctl(fd2, PIN_LONGTERM_TEST_STOP)) {
+ ksft_print_msg("fd2 cleanup STOP failed (%s)\n",
+ strerror(errno));
+ result = KSFT_FAIL;
+ }
+ if (pin1 && ioctl(fd1, PIN_LONGTERM_TEST_STOP)) {
+ ksft_print_msg("fd1 cleanup STOP failed (%s)\n",
+ strerror(errno));
+ result = KSFT_FAIL;
+ }
+ if (read_buf != MAP_FAILED)
+ munmap(read_buf, pagesize);
+ if (mem2 != MAP_FAILED)
+ munmap(mem2, pagesize);
+ if (mem1 != MAP_FAILED)
+ munmap(mem1, pagesize);
+ if (fd2 >= 0)
+ close(fd2);
+ if (fd1 >= 0)
+ close(fd1);
+ log_test_result(result);
+}
+
+static void test_dup_fd_shared_state(bool gup_test_was_available)
+{
+ struct pin_longterm_test args = {
+ .size = pagesize,
+ };
+ char *mem = MAP_FAILED, *read_buf = MAP_FAILED;
+ bool pin = false;
+ __u64 read_addr;
+ int fd = -1, dup_fd = -1;
+ int cleanup_fd;
+ int result = KSFT_FAIL;
+
+ log_test_start("Longterm GUP pin state is shared by dup() descriptors");
+
+ fd = open(GUP_TEST_FILE, O_RDWR);
+ if (fd < 0) {
+ ksft_print_msg("gup_test not available (%s)\n",
+ strerror(errno));
+ result = gup_test_was_available ? KSFT_FAIL : KSFT_SKIP;
+ goto report;
+ }
+
+ dup_fd = dup(fd);
+ if (dup_fd < 0) {
+ ksft_print_msg("dup() failed (%s)\n", strerror(errno));
+ goto report;
+ }
+
+ mem = mmap(NULL, pagesize, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ read_buf = mmap(NULL, pagesize, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ if (mem == MAP_FAILED || read_buf == MAP_FAILED) {
+ ksft_print_msg("mmap() failed (%s)\n", strerror(errno));
+ goto report;
+ }
+
+ memset(mem, 0x33, pagesize);
+
+ args.addr = (__u64)(uintptr_t)mem;
+ if (ioctl(fd, PIN_LONGTERM_TEST_START, &args)) {
+ ksft_print_msg("PIN_LONGTERM_TEST_START failed (%s)\n",
+ strerror(errno));
+ goto report;
+ }
+ pin = true;
+
+ errno = 0;
+ if (ioctl(dup_fd, PIN_LONGTERM_TEST_START, &args) != -1 ||
+ errno != EINVAL) {
+ ksft_print_msg("START through dup fd was not rejected with EINVAL\n");
+ goto report;
+ }
+
+ if (close(fd)) {
+ ksft_print_msg("close(original fd) failed (%s)\n",
+ strerror(errno));
+ fd = -1;
+ goto report;
+ }
+ fd = -1;
+
+ read_addr = (__u64)(uintptr_t)read_buf;
+ if (ioctl(dup_fd, PIN_LONGTERM_TEST_READ, &read_addr)) {
+ ksft_print_msg("READ through dup fd failed (%s)\n",
+ strerror(errno));
+ goto report;
+ }
+ if (memcmp(mem, read_buf, pagesize)) {
+ ksft_print_msg("READ through dup fd data mismatch\n");
+ goto report;
+ }
+
+ if (ioctl(dup_fd, PIN_LONGTERM_TEST_STOP)) {
+ ksft_print_msg("STOP through dup fd failed (%s)\n",
+ strerror(errno));
+ goto report;
+ }
+ pin = false;
+
+ read_addr = (__u64)(uintptr_t)read_buf;
+ errno = 0;
+ if (ioctl(dup_fd, PIN_LONGTERM_TEST_READ, &read_addr) != -1 ||
+ errno != EINVAL) {
+ ksft_print_msg("READ after STOP through dup fd was not rejected with EINVAL\n");
+ goto report;
+ }
+
+ result = KSFT_PASS;
+
+report:
+ cleanup_fd = dup_fd >= 0 ? dup_fd : fd;
+ if (pin && cleanup_fd >= 0 &&
+ ioctl(cleanup_fd, PIN_LONGTERM_TEST_STOP)) {
+ ksft_print_msg("cleanup STOP failed (%s)\n", strerror(errno));
+ result = KSFT_FAIL;
+ }
+ if (read_buf != MAP_FAILED)
+ munmap(read_buf, pagesize);
+ if (mem != MAP_FAILED)
+ munmap(mem, pagesize);
+ if (dup_fd >= 0)
+ close(dup_fd);
+ if (fd >= 0)
+ close(fd);
+ log_test_result(result);
+}
+
static int tests_per_test_case(void)
{
return 3 + nr_hugetlbsizes;
@@ -507,6 +750,7 @@ static int tests_per_test_case(void)
int main(int argc, char **argv)
{
+ bool gup_test_was_available;
int i;
pagesize = getpagesize();
@@ -514,12 +758,22 @@ int main(int argc, char **argv)
ARRAY_SIZE(hugetlbsizes));
ksft_print_header();
- ksft_set_plan(ARRAY_SIZE(test_cases) * tests_per_test_case());
+ ksft_set_plan(ARRAY_SIZE(test_cases) * tests_per_test_case() +
+ NR_FD_OWNERSHIP_TESTS);
- gup_fd = open("/sys/kernel/debug/gup_test", O_RDWR);
+ gup_fd = open(GUP_TEST_FILE, O_RDWR);
+ gup_test_was_available = gup_fd >= 0;
for (i = 0; i < ARRAY_SIZE(test_cases); i++)
run_test_case(&test_cases[i]);
+ if (gup_fd >= 0)
+ close(gup_fd);
+ gup_fd = -1;
+
+ test_fd_isolation(true, gup_test_was_available);
+ test_fd_isolation(false, gup_test_was_available);
+ test_dup_fd_shared_state(gup_test_was_available);
+
ksft_finished();
}
--
2.43.0
next prev parent reply other threads:[~2026-09-21 9:43 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-21 9:42 [RFC PATCH 0/2] Fix gup_test long-term " Richard Cheng
2026-09-21 9:42 ` [RFC PATCH 1/2] mm/gup_test: Track long-term pins per open file Richard Cheng
2026-09-21 10:26 ` David Hildenbrand (Arm)
2026-09-21 9:42 ` Richard Cheng [this message]
2026-09-21 10:28 ` [RFC PATCH 2/2] selftests/mm: Test per-open gup_test pin ownership David Hildenbrand (Arm)
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=20260921094243.64698-3-icheng@nvidia.com \
--to=icheng@nvidia.com \
--cc=akpm@linux-foundation.org \
--cc=david@kernel.org \
--cc=jgg@ziepe.ca \
--cc=jhubbard@nvidia.com \
--cc=kaihengf@nvidia.com \
--cc=kobak@nvidia.com \
--cc=kristinc@nvidia.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=newtonl@nvidia.com \
--cc=peterx@redhat.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®