linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/3] Userspace controls soft-offline pages
@ 2024-06-11 21:55 Jiaqi Yan
  2024-06-11 21:55 ` [PATCH v2 1/3] mm/memory-failure: userspace controls soft-offlining pages Jiaqi Yan
                   ` (2 more replies)
  0 siblings, 3 replies; 17+ messages in thread
From: Jiaqi Yan @ 2024-06-11 21:55 UTC (permalink / raw)
  To: nao.horiguchi, linmiaohe, jane.chu
  Cc: muchun.song, akpm, shuah, corbet, osalvador, rientjes, duenwen,
	fvdl, linux-mm, linux-kselftest, linux-doc, Jiaqi Yan

Correctable memory errors are very common on servers with large
amount of memory, and are corrected by ECC, but with two
pain points to users:
1. Correction usually happens on the fly and adds latency overhead
2. Not-fully-proved theory states excessive correctable memory
   errors can develop into uncorrectable memory error.

Soft offline is kernel's additional solution for memory pages
having (excessive) corrected memory errors. Impacted page is migrated
to healthy page if it is in use, then 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 patch series give 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.

Changelog

v1 => v2:
* incorporate feedbacks from both Miaohe Lin <linmiaohe@huawei.com> and
  Jane Chu <jane.chu@oracle.com>.
* make the switch to control all pages, instead of HugeTLB specific.
* change the API from
  /sys/kernel/mm/hugepages/hugepages-${size}kB/softoffline_corrected_errors
  to /proc/sys/vm/enable_soft_offline.
* minor update to test code.
* update documentation of the user control API.
* v2 is based on commit 83a7eefedc9b ("Linux 6.10-rc3").

Jiaqi Yan (3):
  mm/memory-failure: userspace controls soft-offlining pages
  selftest/mm: test enable_soft_offline behaviors
  docs: mm: add enable_soft_offline sysctl

 Documentation/admin-guide/sysctl/vm.rst       |  15 +
 mm/memory-failure.c                           |  16 ++
 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 +
 6 files changed, 295 insertions(+)
 create mode 100644 tools/testing/selftests/mm/hugetlb-soft-offline.c

-- 
2.45.2.505.gda0bf45e8d-goog



^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH v2 1/3] mm/memory-failure: userspace controls soft-offlining pages
  2024-06-11 21:55 [PATCH v2 0/3] Userspace controls soft-offline pages Jiaqi Yan
@ 2024-06-11 21:55 ` Jiaqi Yan
  2024-06-14  3:28   ` Miaohe Lin
  2024-06-14  8:35   ` Lance Yang
  2024-06-11 21:55 ` [PATCH v2 2/3] selftest/mm: test enable_soft_offline behaviors Jiaqi Yan
  2024-06-11 21:55 ` [PATCH v2 3/3] docs: mm: add enable_soft_offline sysctl Jiaqi Yan
  2 siblings, 2 replies; 17+ messages in thread
From: Jiaqi Yan @ 2024-06-11 21:55 UTC (permalink / raw)
  To: nao.horiguchi, linmiaohe, jane.chu
  Cc: muchun.song, akpm, shuah, corbet, osalvador, rientjes, duenwen,
	fvdl, linux-mm, linux-kselftest, linux-doc, 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 <jiaqiyan@google.com>
---
 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;
-- 
2.45.2.505.gda0bf45e8d-goog



^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH v2 2/3] selftest/mm: test enable_soft_offline behaviors
  2024-06-11 21:55 [PATCH v2 0/3] Userspace controls soft-offline pages Jiaqi Yan
  2024-06-11 21:55 ` [PATCH v2 1/3] mm/memory-failure: userspace controls soft-offlining pages Jiaqi Yan
@ 2024-06-11 21:55 ` Jiaqi Yan
  2024-06-14  3:50   ` Miaohe Lin
  2024-06-11 21:55 ` [PATCH v2 3/3] docs: mm: add enable_soft_offline sysctl Jiaqi Yan
  2 siblings, 1 reply; 17+ messages in thread
From: Jiaqi Yan @ 2024-06-11 21:55 UTC (permalink / raw)
  To: nao.horiguchi, linmiaohe, jane.chu
  Cc: muchun.song, akpm, shuah, corbet, osalvador, rientjes, duenwen,
	fvdl, linux-mm, linux-kselftest, linux-doc, 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 <jiaqiyan@google.com>
---
 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 <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_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
 
-- 
2.45.2.505.gda0bf45e8d-goog



^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH v2 3/3] docs: mm: add enable_soft_offline sysctl
  2024-06-11 21:55 [PATCH v2 0/3] Userspace controls soft-offline pages Jiaqi Yan
  2024-06-11 21:55 ` [PATCH v2 1/3] mm/memory-failure: userspace controls soft-offlining pages Jiaqi Yan
  2024-06-11 21:55 ` [PATCH v2 2/3] selftest/mm: test enable_soft_offline behaviors Jiaqi Yan
@ 2024-06-11 21:55 ` Jiaqi Yan
  2024-06-12  0:25   ` David Rientjes
  2 siblings, 1 reply; 17+ messages in thread
From: Jiaqi Yan @ 2024-06-11 21:55 UTC (permalink / raw)
  To: nao.horiguchi, linmiaohe, jane.chu
  Cc: muchun.song, akpm, shuah, corbet, osalvador, rientjes, duenwen,
	fvdl, linux-mm, linux-kselftest, linux-doc, Jiaqi Yan

Add the documentation for what enable_soft_offline sysctl is for.

Signed-off-by: Jiaqi Yan <jiaqiyan@google.com>
---
 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
 =================
-- 
2.45.2.505.gda0bf45e8d-goog



^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v2 3/3] docs: mm: add enable_soft_offline sysctl
  2024-06-11 21:55 ` [PATCH v2 3/3] docs: mm: add enable_soft_offline sysctl Jiaqi Yan
@ 2024-06-12  0:25   ` David Rientjes
  2024-06-14 23:15     ` Jiaqi Yan
  0 siblings, 1 reply; 17+ messages in thread
From: David Rientjes @ 2024-06-12  0:25 UTC (permalink / raw)
  To: Jiaqi Yan
  Cc: nao.horiguchi, linmiaohe, jane.chu, muchun.song, akpm, shuah,
	corbet, osalvador, duenwen, fvdl, linux-mm, linux-kselftest,
	linux-doc

On Tue, 11 Jun 2024, Jiaqi Yan wrote:

> @@ -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).
> +

Could you expand upon the relevance of HugeTLB or THP in this 
documentation?  I understand the need in some cases to soft offline memory 
after a number of correctable memory errors, but it's not clear how the 
performance implications plays into this.  The paragraph below goes into a 
difference in the splitting behavior, are hugepage users the only ones 
that should be concerned with this?

> +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,

s/oringinal/original/

> +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.
>  

This behavior is the same for all architectures?


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v2 1/3] mm/memory-failure: userspace controls soft-offlining pages
  2024-06-11 21:55 ` [PATCH v2 1/3] mm/memory-failure: userspace controls soft-offlining pages Jiaqi Yan
@ 2024-06-14  3:28   ` Miaohe Lin
  2024-06-14 16:40     ` Jiaqi Yan
  2024-06-14  8:35   ` Lance Yang
  1 sibling, 1 reply; 17+ messages in thread
From: Miaohe Lin @ 2024-06-14  3:28 UTC (permalink / raw)
  To: Jiaqi Yan
  Cc: muchun.song, akpm, shuah, corbet, osalvador, rientjes, duenwen,
	fvdl, linux-mm, linux-kselftest, linux-doc, nao.horiguchi,
	jane.chu

On 2024/6/12 5:55, Jiaqi Yan wrote:
> 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.
> 

Thanks for your update.

> 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.

s/mmap hugepages MAP_FAILED/fails to mmap 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:

s/doing/doing so/ ?

> 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.

s/enable_soft_line/enable_soft_offline/

> 
> Signed-off-by: Jiaqi Yan <jiaqiyan@google.com>
> ---
>  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;
> +	}
> +

IMHO, callers might reach here with page refcnt increased. So we have to take care of releasing it first?
Also will it be better to return -EOPNOTSUPP or some other better errno?

Thanks.
.


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v2 2/3] selftest/mm: test enable_soft_offline behaviors
  2024-06-11 21:55 ` [PATCH v2 2/3] selftest/mm: test enable_soft_offline behaviors Jiaqi Yan
@ 2024-06-14  3:50   ` Miaohe Lin
  2024-06-14 19:36     ` Jiaqi Yan
  0 siblings, 1 reply; 17+ messages in thread
From: Miaohe Lin @ 2024-06-14  3:50 UTC (permalink / raw)
  To: Jiaqi Yan
  Cc: muchun.song, akpm, shuah, corbet, osalvador, rientjes, duenwen,
	fvdl, linux-mm, linux-kselftest, linux-doc, nao.horiguchi,
	jane.chu

On 2024/6/12 5:55, Jiaqi Yan wrote:
> 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

This shoule be something like below ?
 if enable_soft_offline=0, mapped hugepage is intact
 if enable_soft_offline=1, mapped hugepage is soft offlined

> 
> 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       | 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 <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_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");

If fails to set enable_soft_offline, should we return errno here?

> +	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");

Check cmdfile against NULL?

> +
> +	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;
> +}

test_disable_soft_offline is almost same with test_soft_offline. Shall we extract the common pattern?

Thanks.
.




^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v2 1/3] mm/memory-failure: userspace controls soft-offlining pages
  2024-06-11 21:55 ` [PATCH v2 1/3] mm/memory-failure: userspace controls soft-offlining pages Jiaqi Yan
  2024-06-14  3:28   ` Miaohe Lin
@ 2024-06-14  8:35   ` Lance Yang
  2024-06-14 16:30     ` Jiaqi Yan
  1 sibling, 1 reply; 17+ messages in thread
From: Lance Yang @ 2024-06-14  8:35 UTC (permalink / raw)
  To: Jiaqi Yan
  Cc: nao.horiguchi, linmiaohe, jane.chu, muchun.song, akpm, shuah,
	corbet, osalvador, rientjes, duenwen, fvdl, linux-mm,
	linux-kselftest, linux-doc

Hi Jiaqi,

On Wed, Jun 12, 2024 at 5:56 AM Jiaqi Yan <jiaqiyan@google.com> wrote:
>
> 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.

s/enable_soft_line/enable_soft_offline

>
> Signed-off-by: Jiaqi Yan <jiaqiyan@google.com>
> ---
>  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;

IMO, "-EPERM" might sound better ;)

Using "-EPERM" indicates that the operation is not permitted due to
the OS-wide configuration.

Thanks,
Lance

> +       }
> +
>         if (!pfn_valid(pfn)) {
>                 WARN_ON_ONCE(flags & MF_COUNT_INCREASED);
>                 return -ENXIO;
> --
> 2.45.2.505.gda0bf45e8d-goog
>
>


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v2 1/3] mm/memory-failure: userspace controls soft-offlining pages
  2024-06-14  8:35   ` Lance Yang
@ 2024-06-14 16:30     ` Jiaqi Yan
  2024-06-17  7:31       ` Miaohe Lin
  0 siblings, 1 reply; 17+ messages in thread
From: Jiaqi Yan @ 2024-06-14 16:30 UTC (permalink / raw)
  To: Lance Yang, linmiaohe
  Cc: nao.horiguchi, jane.chu, muchun.song, akpm, shuah, corbet,
	osalvador, rientjes, duenwen, fvdl, linux-mm, linux-kselftest,
	linux-doc

On Fri, Jun 14, 2024 at 1:35 AM Lance Yang <ioworker0@gmail.com> wrote:
>
> Hi Jiaqi,
>
> On Wed, Jun 12, 2024 at 5:56 AM Jiaqi Yan <jiaqiyan@google.com> wrote:
> >
> > 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.
>
> s/enable_soft_line/enable_soft_offline

Will fix this typo in v3.

>
> >
> > Signed-off-by: Jiaqi Yan <jiaqiyan@google.com>
> > ---
> >  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;
>
> IMO, "-EPERM" might sound better ;)
>
> Using "-EPERM" indicates that the operation is not permitted due to
> the OS-wide configuration.

Miaohe suggested -EOPNOTSUPP. I agree both EOPNOTSUPP and EPERM may be
better than EINVAL. But I wonder how about EAGAIN? With EAGAIN plus
showing "disabled by /proc/sys/vm/enable_soft_offline" in dmesg, users
now should be clear that they can try again with
/proc/sys/vm/enable_soft_offline=1.

>
> Thanks,
> Lance
>
> > +       }
> > +
> >         if (!pfn_valid(pfn)) {
> >                 WARN_ON_ONCE(flags & MF_COUNT_INCREASED);
> >                 return -ENXIO;
> > --
> > 2.45.2.505.gda0bf45e8d-goog
> >
> >


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v2 1/3] mm/memory-failure: userspace controls soft-offlining pages
  2024-06-14  3:28   ` Miaohe Lin
@ 2024-06-14 16:40     ` Jiaqi Yan
  0 siblings, 0 replies; 17+ messages in thread
From: Jiaqi Yan @ 2024-06-14 16:40 UTC (permalink / raw)
  To: Miaohe Lin
  Cc: muchun.song, akpm, shuah, corbet, osalvador, rientjes, duenwen,
	fvdl, linux-mm, linux-kselftest, linux-doc, nao.horiguchi,
	jane.chu

On Thu, Jun 13, 2024 at 8:28 PM Miaohe Lin <linmiaohe@huawei.com> wrote:
>
> On 2024/6/12 5:55, Jiaqi Yan wrote:
> > 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.
> >
>
> Thanks for your update.
>
> > 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.
>
> s/mmap hugepages MAP_FAILED/fails to mmap 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:
>
> s/doing/doing so/ ?
>
> > 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.
>
> s/enable_soft_line/enable_soft_offline/

Will fix these 3 typos in v3.

>
> >
> > Signed-off-by: Jiaqi Yan <jiaqiyan@google.com>
> > ---
> >  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;
> > +     }
> > +
>
> IMHO, callers might reach here with page refcnt increased. So we have to take care of releasing it first?

Ah, I think you are right when MF_COUNT_INCREASED.

I will move this after the pfn_to_online_page check, and release if disabled.

> Also will it be better to return -EOPNOTSUPP or some other better errno?
>
> Thanks.
> .


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v2 2/3] selftest/mm: test enable_soft_offline behaviors
  2024-06-14  3:50   ` Miaohe Lin
@ 2024-06-14 19:36     ` Jiaqi Yan
  0 siblings, 0 replies; 17+ messages in thread
From: Jiaqi Yan @ 2024-06-14 19:36 UTC (permalink / raw)
  To: Miaohe Lin
  Cc: muchun.song, akpm, shuah, corbet, osalvador, rientjes, duenwen,
	fvdl, linux-mm, linux-kselftest, linux-doc, nao.horiguchi,
	jane.chu

On Thu, Jun 13, 2024 at 8:50 PM Miaohe Lin <linmiaohe@huawei.com> wrote:
>
> On 2024/6/12 5:55, Jiaqi Yan wrote:
> > 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
>
> This shoule be something like below ?
>  if enable_soft_offline=0, mapped hugepage is intact
>  if enable_soft_offline=1, mapped hugepage is soft offlined
>
> >
> > 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       | 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 <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_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");
>
> If fails to set enable_soft_offline, should we return errno here?

Yes, in v3 this will errno, and make the test TEST_FAILED.

>
> > +     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");
>
> Check cmdfile against NULL?

Will do in v3.

>
> > +
> > +     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;
> > +}
>
> test_disable_soft_offline is almost same with test_soft_offline. Shall we extract the common pattern?

Will do in v3.

>
> Thanks.
> .
>
>


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v2 3/3] docs: mm: add enable_soft_offline sysctl
  2024-06-12  0:25   ` David Rientjes
@ 2024-06-14 23:15     ` Jiaqi Yan
  0 siblings, 0 replies; 17+ messages in thread
From: Jiaqi Yan @ 2024-06-14 23:15 UTC (permalink / raw)
  To: David Rientjes
  Cc: nao.horiguchi, linmiaohe, jane.chu, muchun.song, akpm, shuah,
	corbet, osalvador, duenwen, fvdl, linux-mm, linux-kselftest,
	linux-doc, Lance Yang

Thanks for your questions, David!

On Tue, Jun 11, 2024 at 5:25 PM David Rientjes <rientjes@google.com> wrote:
>
> On Tue, 11 Jun 2024, Jiaqi Yan wrote:
>
> > @@ -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).
> > +
>
> Could you expand upon the relevance of HugeTLB or THP in this
> documentation?  I understand the need in some cases to soft offline memory
> after a number of correctable memory errors, but it's not clear how the
> performance implications plays into this.  The paragraph below goes into a

To be accurate, I should say soft offlining transparent hugepage
impacts performance, and soft offlining hugetlb hugepage impacts
capacity. It may be clearer to first explain soft-offline's behaviors
and implications, so that user knows what is the cost of soft-offline,
then talks about the behavior of enable_soft_offline:

  Correctable memory errors are very common on servers. Soft-offline is kernel's
  handling for memory pages having (excessive) corrected memory errors.

  For different types of page, soft-offline has different behaviors / costs.
  - For a raw error page, soft-offline migrates the in-use page's content to
    a new raw page.
  - For a page that is part of a transparent hugepage, soft-offline splits the
    transparent hugepage into raw pages, then migrates only the raw error page.
    As a result, user is transparently backed by 1 less hugepage, impacting
    memory access performance.
  - For a page that is part of a HugeTLB hugepage, soft-offline first migrates
    the entire HugeTLB hugepage, during which a free hugepage will be consumed
    as migration target. Then the original hugepage is dissolved into raw
    pages without compensation, reducing the capacity of the HugeTLB pool by 1.

  It is user's call to choose between reliability (staying away from fragile
  physical memory) vs performance / capacity implications in transparent and
  HugeTLB cases.

> difference in the splitting behavior, are hugepage users the only ones
> that should be concerned with this?

If the cost of migrating a raw page is negligible, then yes, only
hugepage users should be concerned and think about should they disable
soft offline.

>
> > +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,
>
> s/oringinal/original/

To fix in v3.

>
> > +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.
> >
>
> This behavior is the same for all architectures?
>

Yes, enable_soft_offline has the same behavior for all architectures,
and default=1.

It may be worth mentioning that setting enable_soft_offline to 0 means:
- If RAS Correctable Errors Collector is running, its request to soft
offline pages will be ignored.
- On ARM, the request to soft offline pages from GHES driver will be ignored.
- On PARISC, the request to soft offline pages from Page Deallocation
Table will be ignored.

I can add these clarifications in v3 if they are valuable.


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v2 1/3] mm/memory-failure: userspace controls soft-offlining pages
  2024-06-14 16:30     ` Jiaqi Yan
@ 2024-06-17  7:31       ` Miaohe Lin
  2024-06-17  7:51         ` Oscar Salvador
  0 siblings, 1 reply; 17+ messages in thread
From: Miaohe Lin @ 2024-06-17  7:31 UTC (permalink / raw)
  To: Jiaqi Yan
  Cc: nao.horiguchi, jane.chu, muchun.song, akpm, shuah, corbet,
	osalvador, rientjes, duenwen, fvdl, linux-mm, linux-kselftest,
	linux-doc, Lance Yang

On 2024/6/15 0:30, Jiaqi Yan wrote:
> On Fri, Jun 14, 2024 at 1:35 AM Lance Yang <ioworker0@gmail.com> wrote:
>>
>> Hi Jiaqi,
>>
>> On Wed, Jun 12, 2024 at 5:56 AM Jiaqi Yan <jiaqiyan@google.com> wrote:
>>>
>>> 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.
>>
>> s/enable_soft_line/enable_soft_offline
> 
> Will fix this typo in v3.
> 
>>
>>>
>>> Signed-off-by: Jiaqi Yan <jiaqiyan@google.com>
>>> ---
>>>  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;
>>
>> IMO, "-EPERM" might sound better ;)
>>
>> Using "-EPERM" indicates that the operation is not permitted due to
>> the OS-wide configuration.
> 
> Miaohe suggested -EOPNOTSUPP. I agree both EOPNOTSUPP and EPERM may be
> better than EINVAL. But I wonder how about EAGAIN? With EAGAIN plus
> showing "disabled by /proc/sys/vm/enable_soft_offline" in dmesg, users
> now should be clear that they can try again with
> /proc/sys/vm/enable_soft_offline=1.

IMHO, it might not be suitable to use EAGAIN. Because it means "Resource temporarily unavailable" and
this can be solved by simply retry later without any further actions taken. But I might be wrong.

Thanks.
.


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v2 1/3] mm/memory-failure: userspace controls soft-offlining pages
  2024-06-17  7:31       ` Miaohe Lin
@ 2024-06-17  7:51         ` Oscar Salvador
  2024-06-17  8:16           ` Miaohe Lin
  0 siblings, 1 reply; 17+ messages in thread
From: Oscar Salvador @ 2024-06-17  7:51 UTC (permalink / raw)
  To: Miaohe Lin
  Cc: Jiaqi Yan, nao.horiguchi, jane.chu, muchun.song, akpm, shuah,
	corbet, rientjes, duenwen, fvdl, linux-mm, linux-kselftest,
	linux-doc, Lance Yang

On 2024-06-17 09:31, Miaohe Lin wrote:

> IMHO, it might not be suitable to use EAGAIN. Because it means
> "Resource temporarily unavailable" and
> this can be solved by simply retry later without any further actions
> taken. But I might be wrong.

We usually use 'EOPNOTSUPP' when we fail due to a setting not being set.
EPERM is more for a capability matter.


-- 
Oscar Salvador
SUSE Labs


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v2 1/3] mm/memory-failure: userspace controls soft-offlining pages
  2024-06-17  7:51         ` Oscar Salvador
@ 2024-06-17  8:16           ` Miaohe Lin
  2024-06-17 10:34             ` Lance Yang
  0 siblings, 1 reply; 17+ messages in thread
From: Miaohe Lin @ 2024-06-17  8:16 UTC (permalink / raw)
  To: Oscar Salvador, Jiaqi Yan, Lance Yang
  Cc: nao.horiguchi, jane.chu, muchun.song, akpm, shuah, corbet,
	rientjes, duenwen, fvdl, linux-mm, linux-kselftest, linux-doc

On 2024/6/17 15:51, Oscar Salvador wrote:
> On 2024-06-17 09:31, Miaohe Lin wrote:
> 
>> IMHO, it might not be suitable to use EAGAIN. Because it means
>> "Resource temporarily unavailable" and
>> this can be solved by simply retry later without any further actions
>> taken. But I might be wrong.
> 
> We usually use 'EOPNOTSUPP' when we fail due to a setting not being set.
> EPERM is more for a capability matter.

Thanks Oscar. So we should return EOPNOTSUPP here.

Thanks.
.


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v2 1/3] mm/memory-failure: userspace controls soft-offlining pages
  2024-06-17  8:16           ` Miaohe Lin
@ 2024-06-17 10:34             ` Lance Yang
  2024-06-17 15:42               ` Jiaqi Yan
  0 siblings, 1 reply; 17+ messages in thread
From: Lance Yang @ 2024-06-17 10:34 UTC (permalink / raw)
  To: Miaohe Lin
  Cc: Oscar Salvador, Jiaqi Yan, nao.horiguchi, jane.chu, muchun.song,
	akpm, shuah, corbet, rientjes, duenwen, fvdl, linux-mm,
	linux-kselftest, linux-doc

On Mon, Jun 17, 2024 at 4:16 PM Miaohe Lin <linmiaohe@huawei.com> wrote:
>
> On 2024/6/17 15:51, Oscar Salvador wrote:
> > On 2024-06-17 09:31, Miaohe Lin wrote:
> >
> >> IMHO, it might not be suitable to use EAGAIN. Because it means
> >> "Resource temporarily unavailable" and
> >> this can be solved by simply retry later without any further actions
> >> taken. But I might be wrong.
> >
> > We usually use 'EOPNOTSUPP' when we fail due to a setting not being set.
> > EPERM is more for a capability matter.

Agreed. Let's use 'EOPNOTSUPP' here ;)

Thanks,
Lance

>
> Thanks Oscar. So we should return EOPNOTSUPP here.
>
> Thanks.
> .


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v2 1/3] mm/memory-failure: userspace controls soft-offlining pages
  2024-06-17 10:34             ` Lance Yang
@ 2024-06-17 15:42               ` Jiaqi Yan
  0 siblings, 0 replies; 17+ messages in thread
From: Jiaqi Yan @ 2024-06-17 15:42 UTC (permalink / raw)
  To: Lance Yang
  Cc: Miaohe Lin, Oscar Salvador, nao.horiguchi, jane.chu, muchun.song,
	akpm, shuah, corbet, rientjes, duenwen, fvdl, linux-mm,
	linux-kselftest, linux-doc

On Mon, Jun 17, 2024 at 3:34 AM Lance Yang <ioworker0@gmail.com> wrote:
>
> On Mon, Jun 17, 2024 at 4:16 PM Miaohe Lin <linmiaohe@huawei.com> wrote:
> >
> > On 2024/6/17 15:51, Oscar Salvador wrote:
> > > On 2024-06-17 09:31, Miaohe Lin wrote:
> > >
> > >> IMHO, it might not be suitable to use EAGAIN. Because it means
> > >> "Resource temporarily unavailable" and
> > >> this can be solved by simply retry later without any further actions
> > >> taken. But I might be wrong.
> > >
> > > We usually use 'EOPNOTSUPP' when we fail due to a setting not being set.
> > > EPERM is more for a capability matter.
>
> Agreed. Let's use 'EOPNOTSUPP' here ;)

Thanks Oscar and Lance. Agreed, will do in v3.

>
> Thanks,
> Lance
>
> >
> > Thanks Oscar. So we should return EOPNOTSUPP here.
> >
> > Thanks.
> > .


^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2024-06-17 15:43 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-11 21:55 [PATCH v2 0/3] Userspace controls soft-offline pages Jiaqi Yan
2024-06-11 21:55 ` [PATCH v2 1/3] mm/memory-failure: userspace controls soft-offlining pages Jiaqi Yan
2024-06-14  3:28   ` Miaohe Lin
2024-06-14 16:40     ` Jiaqi Yan
2024-06-14  8:35   ` Lance Yang
2024-06-14 16:30     ` Jiaqi Yan
2024-06-17  7:31       ` Miaohe Lin
2024-06-17  7:51         ` Oscar Salvador
2024-06-17  8:16           ` Miaohe Lin
2024-06-17 10:34             ` Lance Yang
2024-06-17 15:42               ` Jiaqi Yan
2024-06-11 21:55 ` [PATCH v2 2/3] selftest/mm: test enable_soft_offline behaviors Jiaqi Yan
2024-06-14  3:50   ` Miaohe Lin
2024-06-14 19:36     ` Jiaqi Yan
2024-06-11 21:55 ` [PATCH v2 3/3] docs: mm: add enable_soft_offline sysctl Jiaqi Yan
2024-06-12  0:25   ` David Rientjes
2024-06-14 23:15     ` Jiaqi Yan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox