Message ID | 20240624163348.1751454-4-jiaqiyan@google.com (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | Userspace controls soft-offline pages | expand |
On 2024/6/25 0:33, Jiaqi Yan wrote: > Add regression and new tests when hugepage has correctable memory ... > 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..16fe52f972e2 > --- /dev/null > +++ b/tools/testing/selftests/mm/hugetlb-soft-offline.c > @@ -0,0 +1,227 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * Test soft offline behavior for HugeTLB pages: > + * - if enable_soft_offline = 0, hugepages should stay intact and soft > + * offlining failed with EINVAL. s/failed with EINVAL/failed with EOPNOTSUPP/g > + * - if enable_soft_offline = 1, a hugepage should be dissolved and > + * nr_hugepages/free_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 > + */ > + ... > +static void test_soft_offline_common(int enable_soft_offline) > +{ > + int fd; > + int expect_errno = enable_soft_offline ? 0 : EOPNOTSUPP; > + struct statfs file_stat; > + unsigned long hugepagesize_kb = 0; > + unsigned long nr_hugepages_before = 0; > + unsigned long nr_hugepages_after = 0; > + int ret; > + > + ksft_print_msg("Test soft-offline when enabled_soft_offline=%d\n", > + enable_soft_offline); > + > + fd = create_hugetlbfs_file(&file_stat); > + if (fd < 0) { > + ksft_exit_fail_msg("Failed to create hugetlbfs file\n"); > + return; > + } > + > + hugepagesize_kb = file_stat.f_bsize / 1024; > + ksft_print_msg("Hugepagesize is %ldkB\n", hugepagesize_kb); > + > + if (set_enable_soft_offline(enable_soft_offline)) { > + ksft_exit_fail_msg("Failed to set enable_soft_offline\n"); Call destroy_hugetlbfs_file() in error path? > + return; > + } > + > + if (read_nr_hugepages(hugepagesize_kb, &nr_hugepages_before) != 0) { > + ksft_exit_fail_msg("Failed to read nr_hugepages\n"); > + return; > + } > + > + ksft_print_msg("Before MADV_SOFT_OFFLINE nr_hugepages=%ld\n", > + nr_hugepages_before); > + > + ret = do_soft_offline(fd, 2 * file_stat.f_bsize, expect_errno); > + > + if (read_nr_hugepages(hugepagesize_kb, &nr_hugepages_after) != 0) { > + ksft_exit_fail_msg("Failed to read nr_hugepages\n"); > + return; > + } > + > + ksft_print_msg("After MADV_SOFT_OFFLINE nr_hugepages=%ld\n", > + nr_hugepages_after); > + > + if (enable_soft_offline) { > + if (nr_hugepages_before != nr_hugepages_after + 1) { > + ksft_test_result_fail("MADV_SOFT_OFFLINE should reduced 1 hugepage\n"); > + return; > + } > + } else { > + if (nr_hugepages_before != nr_hugepages_after) { > + ksft_test_result_fail("MADV_SOFT_OFFLINE reduced %lu hugepages\n", > + nr_hugepages_before - nr_hugepages_after); > + return; > + } > + } > + > + ksft_test_result(ret == 0, > + "Test soft-offline when enabled_soft_offline=%d\n", > + enable_soft_offline); Call destroy_hugetlbfs_file() when test finished ? Thanks. .
On 2024/6/26 7:57, Jiaqi Yan wrote: > On Tue, Jun 25, 2024 at 12:05 AM Miaohe Lin <linmiaohe@huawei.com> wrote: >> >> On 2024/6/25 0:33, Jiaqi Yan wrote: >>> Add regression and new tests when hugepage has correctable memory >> ... >>> 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..16fe52f972e2 >>> --- /dev/null >>> +++ b/tools/testing/selftests/mm/hugetlb-soft-offline.c >>> @@ -0,0 +1,227 @@ >>> +// SPDX-License-Identifier: GPL-2.0 >>> +/* >>> + * Test soft offline behavior for HugeTLB pages: >>> + * - if enable_soft_offline = 0, hugepages should stay intact and soft >>> + * offlining failed with EINVAL. >> >> s/failed with EINVAL/failed with EOPNOTSUPP/g > > To be fixed in v6. > >> >>> + * - if enable_soft_offline = 1, a hugepage should be dissolved and >>> + * nr_hugepages/free_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 >>> + */ >>> + >> ... >>> +static void test_soft_offline_common(int enable_soft_offline) >>> +{ >>> + int fd; >>> + int expect_errno = enable_soft_offline ? 0 : EOPNOTSUPP; >>> + struct statfs file_stat; >>> + unsigned long hugepagesize_kb = 0; >>> + unsigned long nr_hugepages_before = 0; >>> + unsigned long nr_hugepages_after = 0; >>> + int ret; >>> + >>> + ksft_print_msg("Test soft-offline when enabled_soft_offline=%d\n", >>> + enable_soft_offline); >>> + >>> + fd = create_hugetlbfs_file(&file_stat); >>> + if (fd < 0) { >>> + ksft_exit_fail_msg("Failed to create hugetlbfs file\n"); >>> + return; >>> + } >>> + >>> + hugepagesize_kb = file_stat.f_bsize / 1024; >>> + ksft_print_msg("Hugepagesize is %ldkB\n", hugepagesize_kb); >>> + >>> + if (set_enable_soft_offline(enable_soft_offline)) { >>> + ksft_exit_fail_msg("Failed to set enable_soft_offline\n"); >> >> Call destroy_hugetlbfs_file() in error path? > > As the counterpart of destroy_hugetlbfs_file, I think the test only > needs to close(fd). Will add it in v6. > >> >>> + return; >>> + } >>> + >>> + if (read_nr_hugepages(hugepagesize_kb, &nr_hugepages_before) != 0) { >>> + ksft_exit_fail_msg("Failed to read nr_hugepages\n"); >>> + return; >>> + } >>> + >>> + ksft_print_msg("Before MADV_SOFT_OFFLINE nr_hugepages=%ld\n", >>> + nr_hugepages_before); >>> + >>> + ret = do_soft_offline(fd, 2 * file_stat.f_bsize, expect_errno); >>> + >>> + if (read_nr_hugepages(hugepagesize_kb, &nr_hugepages_after) != 0) { >>> + ksft_exit_fail_msg("Failed to read nr_hugepages\n"); >>> + return; >>> + } >>> + >>> + ksft_print_msg("After MADV_SOFT_OFFLINE nr_hugepages=%ld\n", >>> + nr_hugepages_after); >>> + >>> + if (enable_soft_offline) { >>> + if (nr_hugepages_before != nr_hugepages_after + 1) { >>> + ksft_test_result_fail("MADV_SOFT_OFFLINE should reduced 1 hugepage\n"); >>> + return; >>> + } >>> + } else { >>> + if (nr_hugepages_before != nr_hugepages_after) { >>> + ksft_test_result_fail("MADV_SOFT_OFFLINE reduced %lu hugepages\n", >>> + nr_hugepages_before - nr_hugepages_after); >>> + return; >>> + } >>> + } >>> + >>> + ksft_test_result(ret == 0, >>> + "Test soft-offline when enabled_soft_offline=%d\n", >>> + enable_soft_offline); >> >> Call destroy_hugetlbfs_file() when test finished ? > > Test can just close(fd) once nr_hugepages_after is read. I'm sorry but I can't find the code to call close(fd) after nr_hugepages_after is read. IMO create_hugetlbfs_file() would fail to create a new hugetlb file later if close(fd) is not called when testing previous enable_soft_offline = 1 testcase. Because a hugetlb file with same name is already there. But I might miss something. Thanks. .
On Tue, Jun 25, 2024 at 6:54 PM Miaohe Lin <linmiaohe@huawei.com> wrote: > > On 2024/6/26 7:57, Jiaqi Yan wrote: > > On Tue, Jun 25, 2024 at 12:05 AM Miaohe Lin <linmiaohe@huawei.com> wrote: > >> > >> On 2024/6/25 0:33, Jiaqi Yan wrote: > >>> Add regression and new tests when hugepage has correctable memory > >> ... > >>> 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..16fe52f972e2 > >>> --- /dev/null > >>> +++ b/tools/testing/selftests/mm/hugetlb-soft-offline.c > >>> @@ -0,0 +1,227 @@ > >>> +// SPDX-License-Identifier: GPL-2.0 > >>> +/* > >>> + * Test soft offline behavior for HugeTLB pages: > >>> + * - if enable_soft_offline = 0, hugepages should stay intact and soft > >>> + * offlining failed with EINVAL. > >> > >> s/failed with EINVAL/failed with EOPNOTSUPP/g > > > > To be fixed in v6. > > > >> > >>> + * - if enable_soft_offline = 1, a hugepage should be dissolved and > >>> + * nr_hugepages/free_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 > >>> + */ > >>> + > >> ... > >>> +static void test_soft_offline_common(int enable_soft_offline) > >>> +{ > >>> + int fd; > >>> + int expect_errno = enable_soft_offline ? 0 : EOPNOTSUPP; > >>> + struct statfs file_stat; > >>> + unsigned long hugepagesize_kb = 0; > >>> + unsigned long nr_hugepages_before = 0; > >>> + unsigned long nr_hugepages_after = 0; > >>> + int ret; > >>> + > >>> + ksft_print_msg("Test soft-offline when enabled_soft_offline=%d\n", > >>> + enable_soft_offline); > >>> + > >>> + fd = create_hugetlbfs_file(&file_stat); > >>> + if (fd < 0) { > >>> + ksft_exit_fail_msg("Failed to create hugetlbfs file\n"); > >>> + return; > >>> + } > >>> + > >>> + hugepagesize_kb = file_stat.f_bsize / 1024; > >>> + ksft_print_msg("Hugepagesize is %ldkB\n", hugepagesize_kb); > >>> + > >>> + if (set_enable_soft_offline(enable_soft_offline)) { > >>> + ksft_exit_fail_msg("Failed to set enable_soft_offline\n"); > >> > >> Call destroy_hugetlbfs_file() in error path? > > > > As the counterpart of destroy_hugetlbfs_file, I think the test only > > needs to close(fd). Will add it in v6. > > > >> > >>> + return; > >>> + } > >>> + > >>> + if (read_nr_hugepages(hugepagesize_kb, &nr_hugepages_before) != 0) { > >>> + ksft_exit_fail_msg("Failed to read nr_hugepages\n"); > >>> + return; > >>> + } > >>> + > >>> + ksft_print_msg("Before MADV_SOFT_OFFLINE nr_hugepages=%ld\n", > >>> + nr_hugepages_before); > >>> + > >>> + ret = do_soft_offline(fd, 2 * file_stat.f_bsize, expect_errno); > >>> + > >>> + if (read_nr_hugepages(hugepagesize_kb, &nr_hugepages_after) != 0) { > >>> + ksft_exit_fail_msg("Failed to read nr_hugepages\n"); > >>> + return; > >>> + } > >>> + > >>> + ksft_print_msg("After MADV_SOFT_OFFLINE nr_hugepages=%ld\n", > >>> + nr_hugepages_after); > >>> + > >>> + if (enable_soft_offline) { > >>> + if (nr_hugepages_before != nr_hugepages_after + 1) { > >>> + ksft_test_result_fail("MADV_SOFT_OFFLINE should reduced 1 hugepage\n"); > >>> + return; > >>> + } > >>> + } else { > >>> + if (nr_hugepages_before != nr_hugepages_after) { > >>> + ksft_test_result_fail("MADV_SOFT_OFFLINE reduced %lu hugepages\n", > >>> + nr_hugepages_before - nr_hugepages_after); > >>> + return; > >>> + } > >>> + } > >>> + > >>> + ksft_test_result(ret == 0, > >>> + "Test soft-offline when enabled_soft_offline=%d\n", > >>> + enable_soft_offline); > >> > >> Call destroy_hugetlbfs_file() when test finished ? > > > > Test can just close(fd) once nr_hugepages_after is read. > > I'm sorry but I can't find the code to call close(fd) after nr_hugepages_after is read. Sorry, I meant in v6 I will add close(fd) after nr_hugepages_after. > IMO create_hugetlbfs_file() would fail to create a new hugetlb file later if close(fd) > is not called when testing previous enable_soft_offline = 1 testcase. Because a hugetlb > file with same name is already there. But I might miss something. Yes, this is an absolutely valid concern, and should be address in v6 by adding close(fd) after nr_hugepages_after. > > Thanks. > . >
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..16fe52f972e2 --- /dev/null +++ b/tools/testing/selftests/mm/hugetlb-soft-offline.c @@ -0,0 +1,227 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Test soft offline behavior for HugeTLB pages: + * - if enable_soft_offline = 0, hugepages should stay intact and soft + * offlining failed with EINVAL. + * - if enable_soft_offline = 1, a hugepage should be dissolved and + * nr_hugepages/free_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> + +#include "../kselftest.h" + +#ifndef MADV_SOFT_OFFLINE +#define MADV_SOFT_OFFLINE 101 +#endif + +#define EPREFIX " !!! " + +static int do_soft_offline(int fd, size_t len, int expect_errno) +{ + char *filemap = NULL; + char *hwp_addr = NULL; + const unsigned long pagesize = getpagesize(); + int ret = 0; + + if (ftruncate(fd, len) < 0) { + ksft_perror(EPREFIX "ftruncate to len failed"); + return -1; + } + + filemap = mmap(NULL, len, PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_POPULATE, fd, 0); + if (filemap == MAP_FAILED) { + ksft_perror(EPREFIX "mmap failed"); + ret = -1; + goto untruncate; + } + + memset(filemap, 0xab, len); + ksft_print_msg("Allocated %#lx bytes of hugetlb pages\n", len); + + hwp_addr = filemap + len / 2; + ret = madvise(hwp_addr, pagesize, MADV_SOFT_OFFLINE); + ksft_print_msg("MADV_SOFT_OFFLINE %p ret=%d, errno=%d\n", + hwp_addr, ret, errno); + if (ret != 0) + ksft_perror(EPREFIX "madvise failed"); + + if (errno == expect_errno) + ret = 0; + else { + ksft_print_msg("MADV_SOFT_OFFLINE should ret %d\n", + expect_errno); + ret = -1; + } + + munmap(filemap, len); +untruncate: + if (ftruncate(fd, 0) < 0) + ksft_perror(EPREFIX "ftruncate back to 0 failed"); + + return ret; +} + +static int set_enable_soft_offline(int value) +{ + char cmd[256] = {0}; + FILE *cmdfile = NULL; + + if (value != 0 && value != 1) + return -EINVAL; + + sprintf(cmd, "echo %d > /proc/sys/vm/enable_soft_offline", value); + cmdfile = popen(cmd, "r"); + + if (cmdfile) + ksft_print_msg("enable_soft_offline => %d\n", value); + else { + ksft_perror(EPREFIX "failed to set enable_soft_offline"); + return errno; + } + + 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 (cmdfile == NULL) { + ksft_perror(EPREFIX "failed to popen nr_hugepages"); + return -1; + } + + if (!fgets(buffer, sizeof(buffer), cmdfile)) { + ksft_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) { + ksft_perror(EPREFIX "could not open hugetlbfs file"); + return -1; + } + + memset(file_stat, 0, sizeof(*file_stat)); + if (fstatfs(fd, file_stat)) { + ksft_perror(EPREFIX "fstatfs failed"); + goto close; + } + if (file_stat->f_type != HUGETLBFS_MAGIC) { + ksft_print_msg(EPREFIX "not hugetlbfs file\n"); + goto close; + } + + return fd; +close: + close(fd); + return -1; +} + +static void test_soft_offline_common(int enable_soft_offline) +{ + int fd; + int expect_errno = enable_soft_offline ? 0 : EOPNOTSUPP; + struct statfs file_stat; + unsigned long hugepagesize_kb = 0; + unsigned long nr_hugepages_before = 0; + unsigned long nr_hugepages_after = 0; + int ret; + + ksft_print_msg("Test soft-offline when enabled_soft_offline=%d\n", + enable_soft_offline); + + fd = create_hugetlbfs_file(&file_stat); + if (fd < 0) { + ksft_exit_fail_msg("Failed to create hugetlbfs file\n"); + return; + } + + hugepagesize_kb = file_stat.f_bsize / 1024; + ksft_print_msg("Hugepagesize is %ldkB\n", hugepagesize_kb); + + if (set_enable_soft_offline(enable_soft_offline)) { + ksft_exit_fail_msg("Failed to set enable_soft_offline\n"); + return; + } + + if (read_nr_hugepages(hugepagesize_kb, &nr_hugepages_before) != 0) { + ksft_exit_fail_msg("Failed to read nr_hugepages\n"); + return; + } + + ksft_print_msg("Before MADV_SOFT_OFFLINE nr_hugepages=%ld\n", + nr_hugepages_before); + + ret = do_soft_offline(fd, 2 * file_stat.f_bsize, expect_errno); + + if (read_nr_hugepages(hugepagesize_kb, &nr_hugepages_after) != 0) { + ksft_exit_fail_msg("Failed to read nr_hugepages\n"); + return; + } + + ksft_print_msg("After MADV_SOFT_OFFLINE nr_hugepages=%ld\n", + nr_hugepages_after); + + if (enable_soft_offline) { + if (nr_hugepages_before != nr_hugepages_after + 1) { + ksft_test_result_fail("MADV_SOFT_OFFLINE should reduced 1 hugepage\n"); + return; + } + } else { + if (nr_hugepages_before != nr_hugepages_after) { + ksft_test_result_fail("MADV_SOFT_OFFLINE reduced %lu hugepages\n", + nr_hugepages_before - nr_hugepages_after); + return; + } + } + + ksft_test_result(ret == 0, + "Test soft-offline when enabled_soft_offline=%d\n", + enable_soft_offline); +} + +int main(int argc, char **argv) +{ + ksft_print_header(); + ksft_set_plan(2); + + test_soft_offline_common(1); + test_soft_offline_common(0); + + ksft_finished(); +} diff --git a/tools/testing/selftests/mm/run_vmtests.sh b/tools/testing/selftests/mm/run_vmtests.sh index 3157204b9047..781117fac1ba 100755 --- a/tools/testing/selftests/mm/run_vmtests.sh +++ b/tools/testing/selftests/mm/run_vmtests.sh @@ -331,6 +331,10 @@ CATEGORY="hugetlb" run_test ./thuge-gen CATEGORY="hugetlb" run_test ./charge_reserved_hugetlb.sh -cgroup-v2 CATEGORY="hugetlb" run_test ./hugetlb_reparenting_test.sh -cgroup-v2 if $RUN_DESTRUCTIVE; then +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 CATEGORY="hugetlb" run_test ./hugetlb-read-hwpoison fi
Add regression and new tests when hugepage has correctable memory errors, and how userspace wants to deal with it: * if enable_soft_offline=1, mapped hugepage is soft offlined * if enable_soft_offline=0, 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 | 227 ++++++++++++++++++ tools/testing/selftests/mm/run_vmtests.sh | 4 + 4 files changed, 233 insertions(+) create mode 100644 tools/testing/selftests/mm/hugetlb-soft-offline.c