From: Jiaqi Yan <jiaqiyan@google.com>
To: naoya.horiguchi@nec.com, muchun.song@linux.dev, linmiaohe@huawei.com
Cc: akpm@linux-foundation.org, mike.kravetz@oracle.com,
shuah@kernel.org, corbet@lwn.net, osalvador@suse.de,
rientjes@google.com, duenwen@google.com, fvdl@google.com,
linux-mm@kvack.org, linux-kselftest@vger.kernel.org,
linux-doc@vger.kernel.org, Jiaqi Yan <jiaqiyan@google.com>
Subject: [PATCH v1 2/3] selftest/mm: test softoffline_corrected_errors behaviors
Date: Fri, 31 May 2024 21:34:38 +0000 [thread overview]
Message-ID: <20240531213439.2958891-3-jiaqiyan@google.com> (raw)
In-Reply-To: <20240531213439.2958891-1-jiaqiyan@google.com>
Add regression and new tests when hugepage has correctable memory
errors, and how userspace wants to deal with it:
* if softoffline_corrected_errors=0, mapped hugepage is soft offlined
* if softoffline_corrected_errors=1, mapped hugepage is intact
Free hugepages case is not explicitly covered by the tests.
Hugepage having corrected memory errors is emulated with
MADV_SOFT_OFFLINE.
Signed-off-by: Jiaqi Yan <jiaqiyan@google.com>
---
tools/testing/selftests/mm/.gitignore | 1 +
tools/testing/selftests/mm/Makefile | 1 +
.../selftests/mm/hugetlb-soft-offline.c | 262 ++++++++++++++++++
tools/testing/selftests/mm/run_vmtests.sh | 4 +
4 files changed, 268 insertions(+)
create mode 100644 tools/testing/selftests/mm/hugetlb-soft-offline.c
diff --git a/tools/testing/selftests/mm/.gitignore b/tools/testing/selftests/mm/.gitignore
index 0b9ab987601c..064e7b125643 100644
--- a/tools/testing/selftests/mm/.gitignore
+++ b/tools/testing/selftests/mm/.gitignore
@@ -6,6 +6,7 @@ hugepage-shm
hugepage-vmemmap
hugetlb-madvise
hugetlb-read-hwpoison
+hugetlb-soft-offline
khugepaged
map_hugetlb
map_populate
diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile
index 3b49bc3d0a3b..d166067d75ef 100644
--- a/tools/testing/selftests/mm/Makefile
+++ b/tools/testing/selftests/mm/Makefile
@@ -42,6 +42,7 @@ TEST_GEN_FILES += gup_test
TEST_GEN_FILES += hmm-tests
TEST_GEN_FILES += hugetlb-madvise
TEST_GEN_FILES += hugetlb-read-hwpoison
+TEST_GEN_FILES += hugetlb-soft-offline
TEST_GEN_FILES += hugepage-mmap
TEST_GEN_FILES += hugepage-mremap
TEST_GEN_FILES += hugepage-shm
diff --git a/tools/testing/selftests/mm/hugetlb-soft-offline.c b/tools/testing/selftests/mm/hugetlb-soft-offline.c
new file mode 100644
index 000000000000..8d1d7d4a84d8
--- /dev/null
+++ b/tools/testing/selftests/mm/hugetlb-soft-offline.c
@@ -0,0 +1,262 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Test soft offline behavior for HugeTLB pages:
+ * - if softoffline_corrected_errors = 0, hugepages should stay intact and soft
+ * offlining failed with EINVAL.
+ * - if softoffline_corrected_errors > 0, a hugepage should be dissolved and
+ * nr_hugepages should be reduced by 1.
+ *
+ * Before running, make sure more than 2 hugepages of default_hugepagesz
+ * are allocated. For example, if /proc/meminfo/Hugepagesize is 2048kB:
+ * echo 8 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
+ */
+
+#define _GNU_SOURCE
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <linux/magic.h>
+#include <linux/memfd.h>
+#include <sys/mman.h>
+#include <sys/statfs.h>
+#include <sys/types.h>
+
+#ifndef MADV_SOFT_OFFLINE
+#define MADV_SOFT_OFFLINE 101
+#endif
+
+#define PREFIX " ... "
+#define EPREFIX " !!! "
+
+enum test_status {
+ TEST_PASS = 0,
+ TEST_FAILED = 1,
+ // From ${ksft_skip} in run_vmtests.sh.
+ TEST_SKIPPED = 4,
+};
+
+static enum test_status do_soft_offline(int fd, size_t len, int expect_ret)
+{
+ char *filemap = NULL;
+ char *hwp_addr = NULL;
+ const unsigned long pagesize = getpagesize();
+ int ret = 0;
+ enum test_status status = TEST_SKIPPED;
+
+ if (ftruncate(fd, len) < 0) {
+ perror(EPREFIX "ftruncate to len failed");
+ return status;
+ }
+
+ filemap = mmap(NULL, len, PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_POPULATE, fd, 0);
+ if (filemap == MAP_FAILED) {
+ perror(EPREFIX "mmap failed");
+ goto untruncate;
+ }
+
+ memset(filemap, 0xab, len);
+ printf(PREFIX "Allocated %#lx bytes of hugetlb pages\n", len);
+
+ hwp_addr = filemap + len / 2;
+ ret = madvise(hwp_addr, pagesize, MADV_SOFT_OFFLINE);
+ printf(PREFIX "MADV_SOFT_OFFLINE %p ret=%d, errno=%d\n",
+ hwp_addr, ret, errno);
+ if (ret != 0)
+ perror(EPREFIX "madvise failed");
+
+ if (errno == expect_ret)
+ status = TEST_PASS;
+ else {
+ printf(EPREFIX "MADV_SOFT_OFFLINE should ret %d\n", expect_ret);
+ status = TEST_FAILED;
+ }
+
+ munmap(filemap, len);
+untruncate:
+ if (ftruncate(fd, 0) < 0)
+ perror(EPREFIX "ftruncate back to 0 failed");
+
+ return status;
+}
+
+static int set_softoffline_corrected_errors(unsigned long hugepage_size, int value)
+{
+ char cmd[256] = {0};
+ FILE *cmdfile = NULL;
+
+ if (value != 0 && value != 1)
+ return -EINVAL;
+
+ sprintf(cmd,
+ "echo %d > /sys/kernel/mm/hugepages/hugepages-%ldkB/softoffline_corrected_errors",
+ value, hugepage_size);
+ cmdfile = popen(cmd, "r");
+
+ if (cmdfile == NULL)
+ perror(EPREFIX "failed to set softoffline_corrected_errors");
+ else
+ printf(PREFIX
+ "softoffline_corrected_errors=%d for %ldkB hugepages\n",
+ value, hugepage_size);
+
+ pclose(cmdfile);
+ return 0;
+}
+
+static int read_nr_hugepages(unsigned long hugepage_size,
+ unsigned long *nr_hugepages)
+{
+ char buffer[256] = {0};
+ char cmd[256] = {0};
+
+ sprintf(cmd, "cat /sys/kernel/mm/hugepages/hugepages-%ldkB/nr_hugepages",
+ hugepage_size);
+ FILE *cmdfile = popen(cmd, "r");
+
+ if (!fgets(buffer, sizeof(buffer), cmdfile)) {
+ perror(EPREFIX "failed to read nr_hugepages");
+ pclose(cmdfile);
+ return -1;
+ }
+
+ *nr_hugepages = atoll(buffer);
+ pclose(cmdfile);
+ return 0;
+}
+
+static int create_hugetlbfs_file(struct statfs *file_stat)
+{
+ int fd;
+
+ fd = memfd_create("hugetlb_tmp", MFD_HUGETLB);
+ if (fd < 0) {
+ perror(EPREFIX "could not open hugetlbfs file");
+ return -1;
+ }
+
+ memset(file_stat, 0, sizeof(*file_stat));
+ if (fstatfs(fd, file_stat)) {
+ perror(EPREFIX "fstatfs failed");
+ goto close;
+ }
+ if (file_stat->f_type != HUGETLBFS_MAGIC) {
+ printf(EPREFIX "not hugetlbfs file\n");
+ goto close;
+ }
+
+ return fd;
+close:
+ close(fd);
+ return -1;
+}
+
+static enum test_status test_soft_offline(void)
+{
+ int fd;
+ struct statfs file_stat;
+ unsigned long hugepagesize_kb = 0;
+ unsigned long nr_hugepages_before = 0;
+ unsigned long nr_hugepages_after = 0;
+ enum test_status status = TEST_SKIPPED;
+
+ printf("Test Soft Offline When softoffline_corrected_errors=1\n");
+
+ fd = create_hugetlbfs_file(&file_stat);
+ if (fd < 0) {
+ printf(EPREFIX "Failed to create hugetlbfs file\n");
+ return status;
+ }
+
+ hugepagesize_kb = file_stat.f_bsize / 1024;
+ printf(PREFIX "Hugepagesize is %ldkB\n", hugepagesize_kb);
+
+ if (set_softoffline_corrected_errors(hugepagesize_kb, 1))
+ return TEST_FAILED;
+
+ if (read_nr_hugepages(hugepagesize_kb, &nr_hugepages_before) != 0)
+ return TEST_FAILED;
+
+ printf(PREFIX "Before MADV_SOFT_OFFLINE nr_hugepages=%ld\n",
+ nr_hugepages_before);
+
+ status = do_soft_offline(fd, 2 * file_stat.f_bsize, /*expect_ret=*/0);
+
+ if (read_nr_hugepages(hugepagesize_kb, &nr_hugepages_after) != 0)
+ return TEST_FAILED;
+
+ printf(PREFIX "After MADV_SOFT_OFFLINE nr_hugepages=%ld\n",
+ nr_hugepages_after);
+
+ if (nr_hugepages_before != nr_hugepages_after + 1) {
+ printf(EPREFIX "MADV_SOFT_OFFLINE should reduced 1 hugepage\n");
+ return TEST_FAILED;
+ }
+
+ return status;
+}
+
+static enum test_status test_disable_soft_offline(void)
+{
+ int fd;
+ struct statfs file_stat;
+ unsigned long hugepagesize_kb = 0;
+ unsigned long nr_hugepages_before = 0;
+ unsigned long nr_hugepages_after = 0;
+ enum test_status status = TEST_SKIPPED;
+
+ printf("Test Soft Offline When softoffline_corrected_errors=0\n");
+
+ fd = create_hugetlbfs_file(&file_stat);
+ if (fd < 0) {
+ printf(EPREFIX "Failed to create hugetlbfs file\n");
+ return status;
+ }
+
+ hugepagesize_kb = file_stat.f_bsize / 1024;
+ printf(PREFIX "Hugepagesize is %ldkB\n", hugepagesize_kb);
+
+ if (set_softoffline_corrected_errors(hugepagesize_kb, 0))
+ return TEST_FAILED;
+
+ if (read_nr_hugepages(hugepagesize_kb, &nr_hugepages_before) != 0)
+ return TEST_FAILED;
+
+ printf(PREFIX "Before MADV_SOFT_OFFLINE nr_hugepages=%ld\n",
+ nr_hugepages_before);
+
+ status = do_soft_offline(fd, 2 * file_stat.f_bsize, /*expect_ret=*/EINVAL);
+
+ if (read_nr_hugepages(hugepagesize_kb, &nr_hugepages_after) != 0)
+ return TEST_FAILED;
+
+ printf(PREFIX "After MADV_SOFT_OFFLINE nr_hugepages=%ld\n",
+ nr_hugepages_after);
+
+ if (nr_hugepages_before != nr_hugepages_after) {
+ printf(EPREFIX "MADV_SOFT_OFFLINE reduced %lu hugepages\n",
+ nr_hugepages_before - nr_hugepages_after);
+ return TEST_FAILED;
+ }
+
+ return status;
+}
+
+int main(void)
+{
+ enum test_status status;
+
+ status = test_soft_offline();
+ if (status != TEST_PASS)
+ return status;
+
+ status = test_disable_soft_offline();
+ if (status != TEST_PASS)
+ return status;
+
+ printf("Test Soft Offline All Good!\n");
+ return TEST_PASS;
+}
diff --git a/tools/testing/selftests/mm/run_vmtests.sh b/tools/testing/selftests/mm/run_vmtests.sh
index 3157204b9047..91db9971ba69 100755
--- a/tools/testing/selftests/mm/run_vmtests.sh
+++ b/tools/testing/selftests/mm/run_vmtests.sh
@@ -332,6 +332,10 @@ CATEGORY="hugetlb" run_test ./charge_reserved_hugetlb.sh -cgroup-v2
CATEGORY="hugetlb" run_test ./hugetlb_reparenting_test.sh -cgroup-v2
if $RUN_DESTRUCTIVE; then
CATEGORY="hugetlb" run_test ./hugetlb-read-hwpoison
+nr_hugepages_tmp=$(cat /proc/sys/vm/nr_hugepages)
+echo 8 > /proc/sys/vm/nr_hugepages
+CATEGORY="hugetlb" run_test ./hugetlb-soft-offline
+echo "$nr_hugepages_tmp" > /proc/sys/vm/nr_hugepages
fi
if [ $VADDR64 -ne 0 ]; then
--
2.45.1.288.g0e0cd299f1-goog
next prev parent reply other threads:[~2024-05-31 21:34 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-31 21:34 [PATCH v1 0/3] Userspace controls soft-offline HugeTLB pages Jiaqi Yan
2024-05-31 21:34 ` [PATCH v1 1/3] mm/memory-failure: userspace controls soft-offlining hugetlb pages Jiaqi Yan
2024-06-07 22:26 ` Jiaqi Yan
2024-05-31 21:34 ` Jiaqi Yan [this message]
2024-05-31 21:34 ` [PATCH v1 3/3] docs: hugetlbpage.rst: add softoffline_corrected_errors Jiaqi Yan
2024-06-04 7:19 ` [PATCH v1 0/3] Userspace controls soft-offline HugeTLB pages Miaohe Lin
2024-06-07 22:22 ` Jiaqi Yan
2024-06-10 19:41 ` Jane Chu
2024-06-10 22:55 ` Jiaqi Yan
2024-06-11 17:55 ` Jane Chu
2024-06-11 18:12 ` Jiaqi Yan
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=20240531213439.2958891-3-jiaqiyan@google.com \
--to=jiaqiyan@google.com \
--cc=akpm@linux-foundation.org \
--cc=corbet@lwn.net \
--cc=duenwen@google.com \
--cc=fvdl@google.com \
--cc=linmiaohe@huawei.com \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mike.kravetz@oracle.com \
--cc=muchun.song@linux.dev \
--cc=naoya.horiguchi@nec.com \
--cc=osalvador@suse.de \
--cc=rientjes@google.com \
--cc=shuah@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