From patchwork Wed Jun 26 05:08:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiaqi Yan X-Patchwork-Id: 13712309 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2BEA1C3064D for ; Wed, 26 Jun 2024 05:08:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0D4346B009A; Wed, 26 Jun 2024 01:08:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EFE966B009B; Wed, 26 Jun 2024 01:08:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D53A76B009E; Wed, 26 Jun 2024 01:08:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id B08876B009A for ; Wed, 26 Jun 2024 01:08:31 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 5B41840995 for ; Wed, 26 Jun 2024 05:08:31 +0000 (UTC) X-FDA: 82271859222.20.173CE7F Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) by imf07.hostedemail.com (Postfix) with ESMTP id 863CE4000E for ; Wed, 26 Jun 2024 05:08:29 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="or6/n4UV"; spf=pass (imf07.hostedemail.com: domain of 3TKJ7ZggKCLkihZphxZmfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--jiaqiyan.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3TKJ7ZggKCLkihZphxZmfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--jiaqiyan.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719378501; a=rsa-sha256; cv=none; b=bFI3SQKt6odiKQ4Cv7SHi0MZf43WsM2Bez6x0lF+yEQOZLmAMOk0Hu4FdliaF0RXecHxQo CrLIcALBh17TIANZf2x0TQt+lP+eF6Q10hkTocoN+ZdamhR3E0YHtAuTMYBJmegraBroaW B/jHET5VEuVenuifOfr3PhFwVLr3pjQ= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="or6/n4UV"; spf=pass (imf07.hostedemail.com: domain of 3TKJ7ZggKCLkihZphxZmfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--jiaqiyan.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=3TKJ7ZggKCLkihZphxZmfnnfkd.bnlkhmtw-lljuZbj.nqf@flex--jiaqiyan.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1719378501; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=xJd557bj0v0anBZaK3fJ4hYak7G76iVHGHBQnSWuRrw=; b=GLNx4cCwz9BIK+intg7ZA4xLuPe/ufE0+Iik13Fm4nC92WMO+/0BIDGYzZ8eVEs9Q13OMa n0MRuxiJDTK5wB4s6wsHl0Cpj1OYIG0abjTi+INDfiGo8J+m7FDF+Be3TGlDumE67kPwOm ge9MqT4HIGY6XpjMQbIh9cNNABvtRHY= Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2c7430b3c4bso8008709a91.1 for ; Tue, 25 Jun 2024 22:08:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1719378508; x=1719983308; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=xJd557bj0v0anBZaK3fJ4hYak7G76iVHGHBQnSWuRrw=; b=or6/n4UVfEJo13xEdRuSDUk0FiPIj5dak0famV9zNKIjbRT+Id142BadM9/Jk/fzSg l5IE30In3xXuvKZX/ufDg+y3ZLxiQwOGRPgG547c3jjd3HBTT3Qlu6vxvHpCa0sEl+kK UO88z9STCvgPtOA37QhieY8SXa7EW7LpzVOwjSeDGISf5hfzZK0vHEjeNXjGLySon+sr 5WEExeoRFFKlsgvTulzp9qRgoNvBSyhYULpACKUKo2YGfzefLynBpYio5W5ISkYzhpYo q2+ibPH6EqkFsrXOLiZ2L2AR7Karq6HGZq7F/1zsUXgECwCuTwrwYGp3eQ5SbptgSoxO iUnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719378508; x=1719983308; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=xJd557bj0v0anBZaK3fJ4hYak7G76iVHGHBQnSWuRrw=; b=ofWsVhO/BInrx/I3nZkQIRoq6MFq8BZhNYL2r9KQLWRdXiBKZIN4o0QpKmiG7hKAaw 759bk77aikhLg+g2VYhEfmSWsqIDKvL+ie7Go0gfIBgjUlaGmusYQi8mCj4PkyQJMtCC ad4rIqyzQrI5/QviRugO7D+riWE1Etskg+zxBnNN/jT7gjZGChEY6ViSjdtvQBhvRJjo l8S3UFkME3+fP3xFQhd/l6ieydEIqX5N5Vp+mFGSu805QbbJ635Ji2b/oOLd7ZByArVW cZQgKf8pHilqxnTRNlkQSSeu8U9kG2S/HEkiQJ3IvEuxIu8DYzRxtete//Wy6PCWvzxz Tqkg== X-Forwarded-Encrypted: i=1; AJvYcCUwBAxrhXFDDA19XeepY6AiIxKA7WzVBu307qy28tdOjQ9ZBq8WBIMhOQwGCKiXWonLF3qcFHEnn7Q0QqzGUUbmK6Q= X-Gm-Message-State: AOJu0YxknqSwUIDC6ez+XMyDF9J3FZnJWzIlE7uKaqpmwPdtKd4YZLBF 3nJnDakB8TZ+C/akknoBpYv1FnJo7M8zTwyin+GeJ0OMN5AouA4F8TUi/QZVYGfPB0a4yoXe/C6 XBu/PnEaWZQ== X-Google-Smtp-Source: AGHT+IGLzJKqNE8nNJFCYfxrPfVH9NDTHcNE6vKZzW5PjJBzTU2L1tGxUO1tqukBPzHMTIE1Jt7R8nK+3yAFHQ== X-Received: from yjq3.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:272f]) (user=jiaqiyan job=sendgmr) by 2002:a17:902:e752:b0:1f9:aafc:1ebb with SMTP id d9443c01a7336-1fa1d6b39f4mr15984145ad.13.1719378508065; Tue, 25 Jun 2024 22:08:28 -0700 (PDT) Date: Wed, 26 Jun 2024 05:08:17 +0000 In-Reply-To: <20240626050818.2277273-1-jiaqiyan@google.com> Mime-Version: 1.0 References: <20240626050818.2277273-1-jiaqiyan@google.com> X-Mailer: git-send-email 2.45.2.741.gdbec12cfda-goog Message-ID: <20240626050818.2277273-4-jiaqiyan@google.com> Subject: [PATCH v6 3/4] selftest/mm: test enable_soft_offline behaviors From: Jiaqi Yan To: nao.horiguchi@gmail.com, linmiaohe@huawei.com Cc: jane.chu@oracle.com, rdunlap@infradead.org, ioworker0@gmail.com, muchun.song@linux.dev, akpm@linux-foundation.org, 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 X-Stat-Signature: 7qqksxf1344py9zuyme91mfsidsddqh4 X-Rspamd-Queue-Id: 863CE4000E X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1719378509-578474 X-HE-Meta: U2FsdGVkX1/QVGkFXLft7h0s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cnPjI87AytAJjrHMJmm1grWIb9/f3mUSMTBqg/S7wR5ITM6XscoYhJLAxbajnNyTUUaff6+jCDEs1zz13fRJLUc5LqaddbhBcVBcAHQv3Lr3n9QLBpnrHKPJjEg29buLEK5whe64N9o0cSwEnZ5fNg2soMtB/02fEuJ+vwjIkSMqlG3I5V3vHHw5Av9qNjr1unXrgsEoUavexTfjTj+DP4cukYaheVINa0MR9vIvMwYedEc8VJ4eKiBi3CAuNSVg1s6RiIZgqGPhsXLLraYlj+cLwCpSuJV47WC4Nxy4P/AiKFW3okD/gG9Q6afS9teZ/bSqUdcUmrArzcOCxpeli9G2hVu8m6ybebSx4+7q3M91YM6ryCq+sGKCdXXj6lCWw+cETER8qtDlHazk0O3IoyEQKoDp3J9p+FyjQHkbB99pVJQcT4CikdlYybfWWmsZ3+Rwk0Rpaoy/+6FbVt/VF2ontb79ABUj8JoS2b8xst94yvW4M3ZHyugrYU5SyoxcaT6xp4n0aXcS3XecdK+th3pv6XbsrPY5431DeSiRTqwZuT/gK1vXzNANcP8L2ExUw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 Acked-by: Miaohe Lin --- tools/testing/selftests/mm/.gitignore | 1 + tools/testing/selftests/mm/Makefile | 1 + .../selftests/mm/hugetlb-soft-offline.c | 228 ++++++++++++++++++ tools/testing/selftests/mm/run_vmtests.sh | 4 + 4 files changed, 234 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..cde82705499b --- /dev/null +++ b/tools/testing/selftests/mm/hugetlb-soft-offline.c @@ -0,0 +1,228 @@ +// 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 EOPNOTSUPP. + * - 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 +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#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"); + + hugepagesize_kb = file_stat.f_bsize / 1024; + ksft_print_msg("Hugepagesize is %ldkB\n", hugepagesize_kb); + + if (set_enable_soft_offline(enable_soft_offline)) { + close(fd); + ksft_exit_fail_msg("Failed to set enable_soft_offline\n"); + } + + if (read_nr_hugepages(hugepagesize_kb, &nr_hugepages_before) != 0) { + close(fd); + ksft_exit_fail_msg("Failed to read nr_hugepages\n"); + } + + 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) { + close(fd); + ksft_exit_fail_msg("Failed to read nr_hugepages\n"); + } + + ksft_print_msg("After MADV_SOFT_OFFLINE nr_hugepages=%ld\n", + nr_hugepages_after); + + // No need for the hugetlbfs file from now on. + close(fd); + + 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