From patchwork Tue Jun 11 21:55:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiaqi Yan X-Patchwork-Id: 803719 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 74255153835 for ; Tue, 11 Jun 2024 21:55:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718142951; cv=none; b=IogqoyzQtQUmS4kogr1UnUUkM0FUO7ZRxL/C3GGQlAay5LK8oAhgWXfbAmhmbwTZex4Y/1ddfrlhHzxlD6P71ri205mhbu8B9cpVXqSU5DMNmptlswx2kti8hzr2WGaqDvGHe1gxU7JAjkMI25FfGlLowrdobmDBSu8PFkweh8w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718142951; c=relaxed/simple; bh=TeIh4ZRdAXW2Hf9mptFLzKRzEalNeXXyGMOPn0cwp2c=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=T8tUaL3Mpb9UrdnaSlHO4qhYHi4VBt0MZyVJ6HC5sCxXDFHeL/zoD2BZAh3QBEFCoOWKOGPKt/BRJmJYXFFxqpXgUtFtlsaxWszMr+LCAxm3wX2LCDcfY1N0mgeUKMqe7TfBcGpbxMVNFlhZDzLRxEad2+k5OVPaqQ3dR3WXyIE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jiaqiyan.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=3D9EYABG; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jiaqiyan.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="3D9EYABG" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-1f65539265dso13553305ad.2 for ; Tue, 11 Jun 2024 14:55:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1718142950; x=1718747750; darn=vger.kernel.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=mS9HbFalMzShQT+POycEgy4uFajHDbIF3DAxgE060+U=; b=3D9EYABGpiHgQJNDnhD1LpLVuqluX+BDVEwVdbIxHS7TioT3IadTRxyveIZgyKnpj5 4SsHH6gbzxK6PXQTLrRPz8LHrGBETRUmPaa7anVNXy79lwBrod0tZcE9MAdgbdG3dQiD LkhNK4w/h5p/nwozuIVrsAAz448MZMmhsOZghV9jclFUK4px2LLbSL07yok6SybIzGy/ I9Ei+0ahP4knNEIlrdYYt1gtcCjpvHQjXZrK1+YM30lQLwmS6Q8HxLjaBasvjHOSJvIx EY/MhEpOzTEpSjbmuc7alw4WXsSDZcYLuqHvgPmujnJI3cCGrBH+nEWHiA3r9a6US9e1 M0bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718142950; x=1718747750; 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=mS9HbFalMzShQT+POycEgy4uFajHDbIF3DAxgE060+U=; b=PsQLw14i3UDjmmtwHe7YYG1VXCYcZlSJ8USSddiA7IWlDwAjBBlrgIOmPuXJZ6/ZQl OYvBTPNz+Jwky2DF+e7FWqDHvXV+1zPGiwmrcG2NwVLbpr2eFI6TWG5VYViSTz/ivzfY r/347GcoOOzGvq+JXsAp2iM67noMajrsL4zjraZvt2HIAeMYqkqWUY8bTFGvISbDClVn FRQt44CcfMPh1Zdb0ZHEKh+NcwU5akHsU6OFrWw/QIHDh+O1XmCdFAf/27mVunB3mwpZ fshzLNsQrUvCoLLod9oQXUgoDg6RqRvUpErC86w0CTXg5WkVk+Vmm6vErNicXoM8oaMr DRXA== X-Forwarded-Encrypted: i=1; AJvYcCXpnKU7icfjLFliD43fDLvS7Dm0b5rsoUK6U0AjYjMDe1eGviYzwN9juxQQvYkEv5L4KwKfDXz79SMtorExkKTCAtlOH5T+buZS9g9PlKKE X-Gm-Message-State: AOJu0YzBe/hCWfAvjD8VTkdVzxp9hBbf7NlpNbcdf+Y22GD0u9mYOh8U hRoiIan2CmpzqdoyLSgynlZdODILcfiUqxVf+v95mceq4HlzK6neKRUf9YeCLqwdWf5SppS1J+3 fOY1RHovKDQ== X-Google-Smtp-Source: AGHT+IGVkIg7JWk4rLuAJDkogTWXTNBHCMXhvn7voGGCzQonk6iU8r5R/xu3mAl2mpNPj9KUz+/q9QkmMw61NQ== X-Received: from yjq3.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:272f]) (user=jiaqiyan job=sendgmr) by 2002:a17:902:c411:b0:1f6:daf7:1c18 with SMTP id d9443c01a7336-1f83b5da3b1mr6705ad.2.1718142949584; Tue, 11 Jun 2024 14:55:49 -0700 (PDT) Date: Tue, 11 Jun 2024 21:55:42 +0000 In-Reply-To: <20240611215544.2105970-1-jiaqiyan@google.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240611215544.2105970-1-jiaqiyan@google.com> X-Mailer: git-send-email 2.45.2.505.gda0bf45e8d-goog Message-ID: <20240611215544.2105970-2-jiaqiyan@google.com> Subject: [PATCH v2 1/3] mm/memory-failure: userspace controls soft-offlining pages From: Jiaqi Yan To: nao.horiguchi@gmail.com, linmiaohe@huawei.com, jane.chu@oracle.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 Correctable memory errors are very common on servers with large amount of memory, and are corrected by ECC. Soft offline is kernel's additional recovery handling for memory pages having (excessive) corrected memory errors. Impacted page is migrated to a healthy page if inuse; the original page is discarded for any future use. The actual policy on whether (and when) to soft offline should be maintained by userspace, especially in case of an 1G HugeTLB page. Soft-offline dissolves the HugeTLB page, either in-use or free, into chunks of 4K pages, reducing HugeTLB pool capacity by 1 hugepage. If userspace has not acknowledged such behavior, it may be surprised when later mmap hugepages MAP_FAILED due to lack of hugepages. In case of a transparent hugepage, it will be split into 4K pages as well; userspace will stop enjoying the transparent performance. In addition, discarding the entire 1G HugeTLB page only because of corrected memory errors sounds very costly and kernel better not doing under the hood. But today there are at least 2 such cases: 1. GHES driver sees both GHES_SEV_CORRECTED and CPER_SEC_ERROR_THRESHOLD_EXCEEDED after parsing CPER. 2. RAS Correctable Errors Collector counts correctable errors per PFN and when the counter for a PFN reaches threshold In both cases, userspace has no control of the soft offline performed by kernel's memory failure recovery. This commit gives userspace the control of softofflining any page: kernel only soft offlines raw page / transparent hugepage / HugeTLB hugepage if userspace has agreed to. The interface to userspace is a new sysctl called enable_soft_offline under /proc/sys/vm. By default enable_soft_line is 1 to preserve existing behavior in kernel. Signed-off-by: Jiaqi Yan --- mm/memory-failure.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/mm/memory-failure.c b/mm/memory-failure.c index d3c830e817e3..23415fe03318 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -68,6 +68,8 @@ static int sysctl_memory_failure_early_kill __read_mostly; static int sysctl_memory_failure_recovery __read_mostly = 1; +static int sysctl_enable_soft_offline __read_mostly = 1; + atomic_long_t num_poisoned_pages __read_mostly = ATOMIC_LONG_INIT(0); static bool hw_memory_failure __read_mostly = false; @@ -141,6 +143,15 @@ static struct ctl_table memory_failure_table[] = { .extra1 = SYSCTL_ZERO, .extra2 = SYSCTL_ONE, }, + { + .procname = "enable_soft_offline", + .data = &sysctl_enable_soft_offline, + .maxlen = sizeof(sysctl_enable_soft_offline), + .mode = 0644, + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ZERO, + .extra2 = SYSCTL_ONE, + } }; /* @@ -2771,6 +2782,11 @@ int soft_offline_page(unsigned long pfn, int flags) bool try_again = true; struct page *page; + if (!sysctl_enable_soft_offline) { + pr_info("soft offline: %#lx: OS-wide disabled\n", pfn); + return -EINVAL; + } + if (!pfn_valid(pfn)) { WARN_ON_ONCE(flags & MF_COUNT_INCREASED); return -ENXIO; From patchwork Tue Jun 11 21:55:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiaqi Yan X-Patchwork-Id: 803359 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5C9C315383F for ; Tue, 11 Jun 2024 21:55:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718142954; cv=none; b=I04QwYkUXS3g6gcb+WU79dUC1+l0XbLCAP0R5BLoVfNKJU/WhOENt059d+nXZlRaXU0SiPmug8r4WN50M6rA3OksFEuDzrVttI8WDkZK0hVteHUysfR+7V3pgK2ZxRxQj+YIA6Z4ytoTPfYFs1/6RCzKfxJW6BwFF4oub+oNMkM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718142954; c=relaxed/simple; bh=GBgET4fmwGb9lrtGJyLygNttYS2f5thC1cbI31Pc68k=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oTIMYP5iuXmoUTBONEUZUON3gS2+lX76GfY4jc3sWRq1RG8NuJjbOvCxXcPMdgyU4vKY+Yb5XiWlZQa1vH8pCxGonePNAVlUmA0LTlLcVOw+Pl6GoMHtG1yN3Xxn7UAzPrEmZDNDBQlkps5v875anlblmbKS8lByvHoTS1APzoA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jiaqiyan.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=4yMO8slo; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jiaqiyan.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="4yMO8slo" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dee902341c0so8456338276.3 for ; Tue, 11 Jun 2024 14:55:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1718142951; x=1718747751; darn=vger.kernel.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=gyQTVglq7Cwm4lMbb1naXFnTTx4rj2liHiRoRZq6pME=; b=4yMO8slo8a6cR3UUMQS7Mn0iqLcHUzGPWgsQ+mxCLtPLkA/jfwbpKK2DAc6QoM0Ukv QYK9XgSOyJos/a2Ey69fuJ8LLWWBhGpc3/IYDtSrKcXrT+Z/0hT+9wRKmbzdY4nzT/Fg CKw8i0COwsZJzwj3c4mwWqm0xUsiGTSHlUn9hLFYLX4yqj9X84xWBvVmrC/9WNFg0Zvu VNF2xEIwlQIl6yAFZ4QzOSRiHpa3HfIlrig6l7bhs3VuDn322XUTgFEenFI0dK0ww5DO +htkDVHwdv/0ROp163o5IvZnIvQ6drAhEaXIXNwVkSwthOPSHSPplA3Utu1o4hWLXzcy dYrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718142951; x=1718747751; 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=gyQTVglq7Cwm4lMbb1naXFnTTx4rj2liHiRoRZq6pME=; b=X6spCla4cJfidEYqJAb8Jmlnhje9tJ47+AxPjUc4fboreWe9YnEOQCR/o0LjaQa4+i elLaBjJvMsjXj+4TDmLbca4EGOex9RIDik1KvErJP8sd3JzYor3xFRl48y5RF5skSH5A zoeNb02vnHewzW9w5I+H0oH8UAudr8tHdNeijbZF7NUeIa5dMIzSKDASfoP2h6mBVOc7 wlb9KJOqaa3S1QBHJ7a2iHKvhk93SepwkNLb32qwkIQjjx6ZFJeGMqhOX0T/BuJeCfN1 XeG5+YeIgeLBql9nwUiy4UHXIMpNJ8ypj0/Mic6uGEhLFJLF3klZyzeBHhxHDjuRMDe2 UnoQ== X-Forwarded-Encrypted: i=1; AJvYcCUgII8YBekzSHcgQTlMhVcXf8Mwfve/7VlXmgSR5pssGcz9lNDo1AuWPOnoN4khtBWLWQk+lZli1/SuKDKDknXAPmecBkXsTZg/8YIybIQ7 X-Gm-Message-State: AOJu0Yy/ibkg5yfRlq/VczjuLMPFRLU8ZDGKQZJgpEEv3elUp7xPb3G1 p4gaJqLuwvdHeEAK6uPW+2A+C5sBYzHaXgQnDwPhSpjh4kMuO3NifypmpCqXrecqKzMLxcl3qYO AMUYTaV42Uw== X-Google-Smtp-Source: AGHT+IEHVqmqrs5fNX0G2Rm+19MY544u/GPqOHKCJXOP123n1O4Le3p4ZuHF3jDVgujwWTc0zwNSeaeRWk5Crg== X-Received: from yjq3.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:272f]) (user=jiaqiyan job=sendgmr) by 2002:a05:6902:1248:b0:df4:a393:8769 with SMTP id 3f1490d57ef6-dfaf65ba959mr1328238276.9.1718142951291; Tue, 11 Jun 2024 14:55:51 -0700 (PDT) Date: Tue, 11 Jun 2024 21:55:43 +0000 In-Reply-To: <20240611215544.2105970-1-jiaqiyan@google.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240611215544.2105970-1-jiaqiyan@google.com> X-Mailer: git-send-email 2.45.2.505.gda0bf45e8d-goog Message-ID: <20240611215544.2105970-3-jiaqiyan@google.com> Subject: [PATCH v2 2/3] selftest/mm: test enable_soft_offline behaviors From: Jiaqi Yan To: nao.horiguchi@gmail.com, linmiaohe@huawei.com, jane.chu@oracle.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 Add regression and new tests when hugepage has correctable memory errors, and how userspace wants to deal with it: * if enable_soft_offline=0, mapped hugepage is soft offlined * if enable_soft_offline=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 --- tools/testing/selftests/mm/.gitignore | 1 + tools/testing/selftests/mm/Makefile | 1 + .../selftests/mm/hugetlb-soft-offline.c | 258 ++++++++++++++++++ tools/testing/selftests/mm/run_vmtests.sh | 4 + 4 files changed, 264 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..d37d68a433e7 --- /dev/null +++ b/tools/testing/selftests/mm/hugetlb-soft-offline.c @@ -0,0 +1,258 @@ +// 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 + +#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_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 == NULL) + perror(EPREFIX "failed to set enable_soft_offline"); + else + printf(PREFIX "enable_soft_offline => %d\n", value); + + 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_enable_soft_offline(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_enable_soft_offline(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..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 From patchwork Tue Jun 11 21:55:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiaqi Yan X-Patchwork-Id: 803718 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C15C8153BFA for ; Tue, 11 Jun 2024 21:55:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718142955; cv=none; b=RX52gqof7icjoyryrn5H0+0BMhrhDCdV0zdXm9ct9DskyZpLmGnCfMHfkCgD9WCcEhICsNL4losZMcC/xUHKtoFLtNMQSx7pAfwxYKu1yIhJCxkIWI7AQlQd8wV4RVb4Ru68zbTc9caALGONDO6UbMIV3gLdQiKv6Z8dJHxeV1Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718142955; c=relaxed/simple; bh=M8ixCXnJy/op/03sAWshle8LFOVsdt5mEX96fhYZ3tI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ivU6iX+bzPQmM5SmHM2JD/ETDHyEeGHOXrmNs/0rL3hzkFvhNAdL3vKZEEdKh2Jc9RfvRBH/zVJjvcOneYG1IMnKxX0tBVZQZzbTx0V0o78HUn9ij3rRA1y+rizRvb1IBQP70Sse803mWpS1ma7c7H/7eOgkxvFPVYjl7CDm0rc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jiaqiyan.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=hDZASKO5; arc=none smtp.client-ip=209.85.215.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jiaqiyan.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="hDZASKO5" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-6e3341c8865so4130565a12.1 for ; Tue, 11 Jun 2024 14:55:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1718142953; x=1718747753; darn=vger.kernel.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=WHQrsHsxDopyLkSSwJmPkWDGR/C1iA1OobsIIfIGPV4=; b=hDZASKO57jsQmxgDQTpAc2CA5Fzc5fG7fYI/lj6fg7tAv2QUw53qiIQyX2M+YdmrCk yh3weVCYEdomXFCN4+VHqhEpd3I3Ws/ZjktdCFaYqQA8xNFV2I7QIpAyU9ksMAeEUdca m0GWn5wwWjUEArUIjjXZLtobGDPQMW8I/3ilOlaeuqrEXX6DDgVsK7+8UWh19U95gBd8 La4sgQ+fEpEdQ6oQ3kKRDNGfPtuHYLG9rs9WZHDqH3lHsl+vNfqarBn6fzwyXFWC9A7W t++XFbJoxX+ZFSMMv5I+6/mqeLSJ10nFghcFrOdmZ7QjuClgGpbqqbpb9io16BBPhzU8 s6sA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718142953; x=1718747753; 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=WHQrsHsxDopyLkSSwJmPkWDGR/C1iA1OobsIIfIGPV4=; b=RTMQIRINIXiiJ2V2miiOWmeEOm7L5XZze5RtYkdaKsqG39x+alU53iYbhYxA/Ul2Y9 jVAiGiowK1ZcQI22C2/A5QMpu+K93VFotQwnm5cUg2l/At8s3YyQVYyrxbNS1/mRJzo6 X0+q1wQVojwweve29KRiQ+Kztg0h4h2K1FC2zPXgoJlzF0vUBX43viXI1F8tXropNLcQ CB6n53bMKGQuyax+hVQ8m6FLrw2RGBoXxc9PLw5LUrO7hkkE+NU1339G6JmDFuS3XYBF SOKnmsiIVGWT+7djeBH5tp2s6WUHXJHt0sooKgzhEJU+qdeai4VM1qxXqbsVPzhp/ynB 1AJA== X-Forwarded-Encrypted: i=1; AJvYcCWWQC6IV/b5a4ZY3ROoPScGInHg/BMVYjYtYokV0LYWMJ9TehfdlEhuMibtTQ0dlE3VFDRU3LnyaNm1Y8+Yq/rAfPar0sESNCsYuhzSYtUx X-Gm-Message-State: AOJu0Yw9UcdmOGMK9dzidNNY31C0VFycRtRsPLGUoKfMIqxaeRElGOnx glviAzsq2PGz/svvNKiY1Sre+o11UkthwcwvNu62MmNLb1bUprnof7iltMyANLIr9aEpsPemPGt bmAyYYVp63g== X-Google-Smtp-Source: AGHT+IE7Hu1Utk6hKZYc2/5v0BAUMc85WtlS2vZJkVYtta2I3YTIOk0Jt9c2VGJfWWlmwXFp1yRF39LVPqKQZw== X-Received: from yjq3.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:272f]) (user=jiaqiyan job=sendgmr) by 2002:a63:20a:0:b0:673:9f86:3f23 with SMTP id 41be03b00d2f7-6e156ce6eccmr32320a12.3.1718142952846; Tue, 11 Jun 2024 14:55:52 -0700 (PDT) Date: Tue, 11 Jun 2024 21:55:44 +0000 In-Reply-To: <20240611215544.2105970-1-jiaqiyan@google.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240611215544.2105970-1-jiaqiyan@google.com> X-Mailer: git-send-email 2.45.2.505.gda0bf45e8d-goog Message-ID: <20240611215544.2105970-4-jiaqiyan@google.com> Subject: [PATCH v2 3/3] docs: mm: add enable_soft_offline sysctl From: Jiaqi Yan To: nao.horiguchi@gmail.com, linmiaohe@huawei.com, jane.chu@oracle.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 Add the documentation for what enable_soft_offline sysctl is for. Signed-off-by: Jiaqi Yan --- Documentation/admin-guide/sysctl/vm.rst | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Documentation/admin-guide/sysctl/vm.rst b/Documentation/admin-guide/sysctl/vm.rst index e86c968a7a0e..856bb17c07bc 100644 --- a/Documentation/admin-guide/sysctl/vm.rst +++ b/Documentation/admin-guide/sysctl/vm.rst @@ -36,6 +36,7 @@ Currently, these files are in /proc/sys/vm: - dirtytime_expire_seconds - dirty_writeback_centisecs - drop_caches +- enable_soft_offline - extfrag_threshold - highmem_is_dirtyable - hugetlb_shm_group @@ -267,6 +268,20 @@ used:: These are informational only. They do not mean that anything is wrong with your system. To disable them, echo 4 (bit 2) into drop_caches. +enable_soft_offline +=================== +Control whether to soft offline memory pages that have (excessive) correctable +memory errors. It is your call to choose between reliability (stay away from +fragile physical memory) vs performance (brought by HugeTLB or transparent +hugepages). + +When setting to 1, kernel attempts to soft offline the page when it thinks +needed. For in-use page, page content will be migrated to a new page. If +the oringinal hugepage is a HugeTLB hugepage, regardless of in-use or free, +it will be dissolved into raw pages, and the capacity of the HugeTLB pool +will reduce by 1. If the original hugepage is a transparent hugepage, it +will be split into raw pages. When setting to 0, kernel won't attempt to +soft offline the page. Its default value is 1. extfrag_threshold =================