From patchwork Mon Jun 24 16:33:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiaqi Yan X-Patchwork-Id: 13709848 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 E25BAC2D0D1 for ; Mon, 24 Jun 2024 16:34:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 190506B0397; Mon, 24 Jun 2024 12:34:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 118306B0399; Mon, 24 Jun 2024 12:34:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EFA0A6B0398; Mon, 24 Jun 2024 12:33:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id D47B36B0395 for ; Mon, 24 Jun 2024 12:33:59 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 8E7254119E for ; Mon, 24 Jun 2024 16:33:59 +0000 (UTC) X-FDA: 82266328998.03.989C71B Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) by imf09.hostedemail.com (Postfix) with ESMTP id A31D0140011 for ; Mon, 24 Jun 2024 16:33:57 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=mWHfMrka; spf=pass (imf09.hostedemail.com: domain of 39J95ZggKCFU65xD5LxA3BB381.zB985AHK-997Ixz7.BE3@flex--jiaqiyan.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=39J95ZggKCFU65xD5LxA3BB381.zB985AHK-997Ixz7.BE3@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=1719246823; 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=9vvhaZptFj6JTB0CcmvYAGAWn7bt9g4Ak/WxvFYJHHA=; b=ABE0adShOUOt4H34I3UhkwCwfGt4itSIFM3Oo4nZeqZRyna661eXb9+PrFK3cQFmlQSHS7 ZiLRR8P78vBTNy6RLeZZrVfx/vp0hGZgnuchXzO7+zlm78PFzTRX9/+jWqS/GEXExv0gzd B4zqCUiqBOHaYbPYxpDsbH90TbdG57w= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719246823; a=rsa-sha256; cv=none; b=SgNAbpA5PXmVPy/5KqdprlZ2W4bOxlsIQ1dz0txGzxZDm+5qMLGAaQJz+rx9J17fmp9GiF J/jhbAX/EHznFLYc+nW1s9TnG6f8jlYJhzmTDuBmz5XsYtKNJx0jytIByLGS45m5O9Fuk3 TS5AK8FEDTSpZpt6zLszwwwIGul41vM= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=mWHfMrka; spf=pass (imf09.hostedemail.com: domain of 39J95ZggKCFU65xD5LxA3BB381.zB985AHK-997Ixz7.BE3@flex--jiaqiyan.bounces.google.com designates 209.85.216.73 as permitted sender) smtp.mailfrom=39J95ZggKCFU65xD5LxA3BB381.zB985AHK-997Ixz7.BE3@flex--jiaqiyan.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2c6f1c0365eso5277498a91.2 for ; Mon, 24 Jun 2024 09:33:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1719246836; x=1719851636; 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=9vvhaZptFj6JTB0CcmvYAGAWn7bt9g4Ak/WxvFYJHHA=; b=mWHfMrkaVS0dU2863bvMXP5BXgiMP98rHwa5l7MfMXWmTLD2W4Mo9YdMZE256LB8XI xM3WpTY/Fcxo3/fL76fIMFem1XRXXONwFp9gIEp+4Kia7r9DXXVq9CWoNYNY9BgHfk/4 vb937HiWdKYh/MikW7b9dNXsoHNBSEIsLfmIpiulWCACLYwqvdQnnWXe4k8dB7OkfJ70 4g8OFsEZA1zYIbbBTwTdCqbfa2ftO4MU+3vEWyEiBaSXJgO5AVUSl4FcWBl42VaD+hzm 9iQK0lt1lWeHjF029ItCuCXTm/us5bbRievD7W6Mt0yMj25odDOLqnpr0iBQ+5JBUDts +AAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719246836; x=1719851636; 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=9vvhaZptFj6JTB0CcmvYAGAWn7bt9g4Ak/WxvFYJHHA=; b=OvRbhC+C6GT7kXifGWi6D1KsuM6QFku3/O7SUktU7644Z6n2wQD2JP6bhJWxoRLq9t tp1wLAtFpyQHimj9OEKsvHu8j+1WAc9ZC5aVgdK+jGrs3Flq6Aj2sPXjvd/eHetSuU25 x6j6JPhQtZjBc9JPBKYk6U3ro11gosl2BuJpMKNCfvIgdACAGVQdC4mCjC2iKYhIN02V IAr6+7iCT2dNAq544/M/aHeW7dycgz6L0RmtJ0W6Wfphh2HJjhY2Y0UkZy8YNCgHZDVh f3sQj5b3SlIqukDqFN6ic5c8CbAZiE/jvxjGwAg7DIvqNPegiFgrzFDlzKFNHFHQKOYT 4u7Q== X-Forwarded-Encrypted: i=1; AJvYcCXkhAaRhmDVh4aboErOFHyDJS+u3QXDmKYvubQEAOMqDdNObRK8byGnMmiCbeBZvZYz6KsaLjHMlJMQ2jfLk4r+cfY= X-Gm-Message-State: AOJu0YzGS/xdKobfIe451UMB2SxQKLLoqurbWRDbiJOF58n5Aq8CQMyJ Yz7NiYvx1jQpzfsCJd+KMVOtMAW3ILmVbet3T73BRn4lI/px/757XB9fGMaGBeawfyj+raLJoeE qOc3yGnPr8g== X-Google-Smtp-Source: AGHT+IGTiMcKI84dbFE5H2z0l2Fcmhs9y/kM+GPkY+GUg1Z/omR04Q3dPmkB+omY4azsFqSvT0rRbpXc5WHBqA== X-Received: from yjq3.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:272f]) (user=jiaqiyan job=sendgmr) by 2002:a17:90b:4d85:b0:2c2:fed7:d797 with SMTP id 98e67ed59e1d1-2c8507b2998mr45685a91.6.1719246836231; Mon, 24 Jun 2024 09:33:56 -0700 (PDT) Date: Mon, 24 Jun 2024 16:33:47 +0000 In-Reply-To: <20240624163348.1751454-1-jiaqiyan@google.com> Mime-Version: 1.0 References: <20240624163348.1751454-1-jiaqiyan@google.com> X-Mailer: git-send-email 2.45.2.741.gdbec12cfda-goog Message-ID: <20240624163348.1751454-4-jiaqiyan@google.com> Subject: [PATCH v5 3/4] selftest/mm: test enable_soft_offline behaviors From: Jiaqi Yan To: nao.horiguchi@gmail.com, linmiaohe@huawei.com, jane.chu@oracle.com, ioworker0@gmail.com Cc: 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: 5u3m51kpemjbfhigjyobi3hr5635n6tx X-Rspamd-Queue-Id: A31D0140011 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1719246837-838473 X-HE-Meta: U2FsdGVkX1/dIMu18B/GbH8gUYV85N5OzEp8iDZinVULncIOerIxK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8t1YpAJlkugEY35525shJyK31Uvn1xNS7DSUpRAvpd4o8LWXaDSLaDsgDxXxuOYFL3qZt2gsAvBy6+sVaqJoQXmpmKrynVT0FUd5hJm/R6inaX2PPeD0W8LQ3Dz5GxYbfhltmaCPU2MHuLIJP26Yon4bZYKCa2JANSM4qeZZbLwe29vEVHGT5prcKVeeWvIvWLiDNXyTFrNRURRtXwrz20EzjdjRJjWr6DBpd6zUo5Ekyo2pfElYGjFeeaS29/gjGAXiJcCbOAarIPQRhKc9LnJ0RkabC1iOBBIe2TWBr05BHVqLFFzi7NIU+a6GBdPhv4gxX8e2lT5MXSI6xwf/56TBNOAcRMt5lErdQSRaHpkaPEQcvsaDvShZV/G3q68wSmhM+C4e3fd1ybGpcCuLmJHfFqu05dXPlTSMaKVP8ggBd+CR7aSqSytwt/Th5p3+tddxtUk2JIH2QwJj+pScWyGhlA+UU8Uos4zeeW+noCUfXffe4PN53HkjMqylMoeTzzg== 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 --- 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 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 +#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"); + 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