diff mbox series

[v3,3/3] hugemmap34: Test to detect bug with migrating gigantic pages

Message ID 20231106093031.1844129-4-liwang@redhat.com
State Changes Requested
Headers show
Series Introduce new field .hptype for reserving gigantic page | expand

Commit Message

Li Wang Nov. 6, 2023, 9:30 a.m. UTC
Signed-off-by: Li Wang <liwang@redhat.com>
---
 runtest/hugetlb                               |   1 +
 testcases/kernel/mem/.gitignore               |   1 +
 .../kernel/mem/hugetlb/hugemmap/hugemmap34.c  | 129 ++++++++++++++++++
 3 files changed, 131 insertions(+)
 create mode 100644 testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c

Comments

Petr Vorel Feb. 16, 2024, 12:01 p.m. UTC | #1
Hi Li,

FYI hugemmap34 fails on x86_64 and s390x various SLES kernels (tested: x86_64
from 4.4 to 6.4, s390x from 4.12. to 6.4):

tst_test.c:1627: TINFO: Timeout per run is 0h 01m 30s
hugemmap34.c:85: TBROK: mmap() failed: EINVAL (22)

Removing MAP_HUGE_1GB shows TBROK due ENOMEM.
I suppose low memory resource for this VM.
=> Maybe we need .min_mem_avail = 1024?

+ define the value and reuse also here (so that we don't forget to update on
other place if later changed):
 hugetlbsize = 1 * 1024 * 1024 * 1024u;
(also remove '1 *')

Although you do #include <linux/mman.h>, for some reason I get some failures on
aarch64 and s390x on old gcc 4.8.5:

hugemmap34.c:82:39: error: ‘MAP_HUGE_1GB’ undeclared (first use in this function)
      MAP_PRIVATE|MAP_ANON|MAP_HUGETLB|MAP_HUGE_1GB,
                                       ^
But works on the same toolchain with gcc 7.5.0, also x86_64 and ppc64le works
fine on them).  It looks to me MAP_HUGE_1GB will need to be backported to lapi.
It works on x86_64 and ppc64le.

Kind regards,
Petr
Petr Vorel Feb. 16, 2024, 12:12 p.m. UTC | #2
> Hi Li,

> FYI hugemmap34 fails on x86_64 and s390x various SLES kernels (tested: x86_64
> from 4.4 to 6.4, s390x from 4.12. to 6.4):

Also Tumbleweed with 6.8.0-rc4 fails

hugemmap34.c:85: TBROK: mmap() failed: EINVAL (22)

(no code modification - with MAP_HUGE_1GB).
But in CI I reproduced also tst_hugepage.c:119: TCONF: nr_hugepages = 0, but
expect 2. Not enough hugepages for testing.

Kind regards,
Petr
diff mbox series

Patch

diff --git a/runtest/hugetlb b/runtest/hugetlb
index 299c07ac9..0c812c780 100644
--- a/runtest/hugetlb
+++ b/runtest/hugetlb
@@ -35,6 +35,7 @@  hugemmap29 hugemmap29
 hugemmap30 hugemmap30
 hugemmap31 hugemmap31
 hugemmap32 hugemmap32
+hugemmap34 hugemmap34
 hugemmap05_1 hugemmap05 -m
 hugemmap05_2 hugemmap05 -s
 hugemmap05_3 hugemmap05 -s -m
diff --git a/testcases/kernel/mem/.gitignore b/testcases/kernel/mem/.gitignore
index 7258489ed..41f547edf 100644
--- a/testcases/kernel/mem/.gitignore
+++ b/testcases/kernel/mem/.gitignore
@@ -34,6 +34,7 @@ 
 /hugetlb/hugemmap/hugemmap30
 /hugetlb/hugemmap/hugemmap31
 /hugetlb/hugemmap/hugemmap32
+/hugetlb/hugemmap/hugemmap34
 /hugetlb/hugeshmat/hugeshmat01
 /hugetlb/hugeshmat/hugeshmat02
 /hugetlb/hugeshmat/hugeshmat03
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c
new file mode 100644
index 000000000..2e55e5f15
--- /dev/null
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c
@@ -0,0 +1,129 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) Linux Test Project, 2023
+ * Copyright (C) 2023, Red Hat, Inc.
+ *
+ * Author: David Hildenbrand <david@redhat.com>
+ * Port-to-LTP: Li Wang <liwang@redhat.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Migration code will first unmap the old page and replace the present PTE
+ * by a migration entry. Then migrate the page. Once that succeeded (and there
+ * are no unexpected page references), we replace the migration entries by
+ * proper present PTEs pointing at the new page.
+ *
+ * For ordinary pages we handle PTEs. For 2 MiB hugetlb/THP, it's PMDs.
+ * For 1 GiB hugetlb, it's PUDs.
+ *
+ * So without below commit, GUP-fast code was simply not aware that we could
+ * have migration entries stored in PUDs. Migration + GUP-fast code should be
+ * able to handle any such races.
+ *
+ * For example, GUP-fast will re-verify the PUD after pinning to make sure it
+ * didn't change. If it did change, it backs off.
+ *
+ * Migration code should detect the additional page references and back off
+ * as well.
+ *
+ *  commit 15494520b776aa2eadc3e2fefae524764cab9cea
+ *  Author: Qiujun Huang <hqjagain@gmail.com>
+ *  Date:   Thu Jan 30 22:12:10 2020 -0800
+ *
+ *      mm: fix gup_pud_range
+ *
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <pthread.h>
+#include <unistd.h>
+#include <stdbool.h>
+#include <sys/mman.h>
+#include <sys/syscall.h>
+#include <sys/ioctl.h>
+#include <linux/mempolicy.h>
+#include <linux/mman.h>
+
+#include "lapi/syscalls.h"
+#include "tst_safe_pthread.h"
+#include "numa_helper.h"
+#include "hugetlb.h"
+
+static char *mem;
+static size_t pagesize;
+static size_t hugetlbsize;
+static volatile int looping = 1;
+
+static void *migration_thread_fn(void *arg LTP_ATTRIBUTE_UNUSED)
+{
+	while (looping) {
+		TST_EXP_PASS_SILENT(syscall(__NR_mbind, mem, hugetlbsize,
+			MPOL_LOCAL, NULL, 0x7fful, MPOL_MF_MOVE));
+	}
+
+	return NULL;
+}
+
+static void run_test(void)
+{
+	ssize_t transferred;
+	struct iovec iov;
+	int fds[2];
+
+	pthread_t migration_thread;
+
+	pagesize = getpagesize();
+	hugetlbsize = 1 * 1024 * 1024 * 1024u;
+
+	mem = mmap(NULL, hugetlbsize, PROT_READ|PROT_WRITE,
+		   MAP_PRIVATE|MAP_ANON|MAP_HUGETLB|MAP_HUGE_1GB,
+		   -1, 0);
+	if (mem == MAP_FAILED)
+		tst_brk(TBROK | TERRNO, "mmap() failed");
+
+	memset(mem, 1, hugetlbsize);
+
+	/* Keep migrating the page around ... */
+	SAFE_PTHREAD_CREATE(&migration_thread, NULL, migration_thread_fn, NULL);
+
+	while (looping) {
+		SAFE_PIPE(fds);
+
+		iov.iov_base = mem;
+		iov.iov_len = pagesize;
+		transferred = vmsplice(fds[1], &iov, 1, 0);
+		if (transferred <= 0)
+			tst_brk(TBROK | TERRNO, "vmsplice() failed");
+
+		SAFE_CLOSE(fds[0]);
+		SAFE_CLOSE(fds[1]);
+
+		if (!tst_remaining_runtime()) {
+			tst_res(TINFO, "Runtime exhausted, exiting");
+			looping = 0;
+		}
+	}
+
+	SAFE_PTHREAD_JOIN(migration_thread, NULL);
+
+	tst_res(TPASS, "Test completed successfully");
+}
+
+static struct tst_test test = {
+	.needs_root = 1,
+	.test_all = run_test,
+	.max_runtime = 60,
+	.taint_check = TST_TAINT_W | TST_TAINT_D,
+	.hugepages = {2, TST_NEEDS, TST_GIGANTIC},
+	.needs_kconfigs = (const char *const[]){
+		"CONFIG_NUMA=y",
+		NULL
+	},
+	.tags = (struct tst_tag[]) {
+	    {"linux-git", "15494520b776"},
+	    {}
+	},
+};