mbox series

[v5,0/4] Userspace controls soft-offline pages

Message ID 20240624163348.1751454-1-jiaqiyan@google.com
Headers show
Series Userspace controls soft-offline pages | expand

Message

Jiaqi Yan June 24, 2024, 4:33 p.m. UTC
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

v4 => v5:
* incorportate feedbacks from Muhammad Usama Anjum
  <usama.anjum@collabora.com>
* refactor selftest to use what available in kselftest.h.
* update a comment in soft_offline_page.

v3 => v4:
* incorporate feedbacks from Miaohe Lin <linmiaohe@huawei.com>,
  Andrew Morton <akpm@linux-foundation.org>, and
  Oscar Salvador <osalvador@suse.de>.
* insert a refactor commit to unify soft offline's logs to follow
  "Soft offline: 0x${pfn}: ${message}" format.
* some rewords in document: fail => will not perform.
* v4 is still based on commit 83a7eefedc9b ("Linux 6.10-rc3"),
  akpm/mm-stable.

v2 => v3:
* incorporate feedbacks from Miaohe Lin <linmiaohe@huawei.com>,
  Lance Yang <ioworker0@gmail.com>, Oscar Salvador <osalvador@suse.de>,
  and David Rientjes <rientjes@google.com>.
* release potential refcount if enable_soft_offline is 0.
* soft_offline_page() returns EOPNOTSUPP if enable_soft_offline is 0.
* refactor hugetlb-soft-offline.c, for example, introduce
  test_soft_offline_common to reduce repeated code.
* rewrite enable_soft_offline's documentation, adds more details about
  the cost of soft-offline for transparent and hugetlb hugepages, and
  components that are impacted when enable_soft_offline becomes 0.
* fix typos in commit messages.
* v3 is still based on commit 83a7eefedc9b ("Linux 6.10-rc3").

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 (4):
  mm/memory-failure: refactor log format in soft offline code
  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       |  32 +++
 mm/memory-failure.c                           |  38 ++-
 tools/testing/selftests/mm/.gitignore         |   1 +
 tools/testing/selftests/mm/Makefile           |   1 +
 .../selftests/mm/hugetlb-soft-offline.c       | 227 ++++++++++++++++++
 tools/testing/selftests/mm/run_vmtests.sh     |   4 +
 6 files changed, 295 insertions(+), 8 deletions(-)
 create mode 100644 tools/testing/selftests/mm/hugetlb-soft-offline.c

Comments

Miaohe Lin June 25, 2024, 7:05 a.m. UTC | #1
On 2024/6/25 0:33, Jiaqi Yan wrote:
> Add regression and new tests when hugepage has correctable memory
...
> diff --git a/tools/testing/selftests/mm/hugetlb-soft-offline.c b/tools/testing/selftests/mm/hugetlb-soft-offline.c
> new file mode 100644
> index 000000000000..16fe52f972e2
> --- /dev/null
> +++ b/tools/testing/selftests/mm/hugetlb-soft-offline.c
> @@ -0,0 +1,227 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Test soft offline behavior for HugeTLB pages:
> + * - if enable_soft_offline = 0, hugepages should stay intact and soft
> + *   offlining failed with EINVAL.

s/failed with EINVAL/failed with EOPNOTSUPP/g

> + * - if enable_soft_offline = 1, a hugepage should be dissolved and
> + *   nr_hugepages/free_hugepages should be reduced by 1.
> + *
> + * Before running, make sure more than 2 hugepages of default_hugepagesz
> + * are allocated. For example, if /proc/meminfo/Hugepagesize is 2048kB:
> + *   echo 8 > /sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
> + */
> +
...
> +static void test_soft_offline_common(int enable_soft_offline)
> +{
> +	int fd;
> +	int expect_errno = enable_soft_offline ? 0 : EOPNOTSUPP;
> +	struct statfs file_stat;
> +	unsigned long hugepagesize_kb = 0;
> +	unsigned long nr_hugepages_before = 0;
> +	unsigned long nr_hugepages_after = 0;
> +	int ret;
> +
> +	ksft_print_msg("Test soft-offline when enabled_soft_offline=%d\n",
> +		       enable_soft_offline);
> +
> +	fd = create_hugetlbfs_file(&file_stat);
> +	if (fd < 0) {
> +		ksft_exit_fail_msg("Failed to create hugetlbfs file\n");
> +		return;
> +	}
> +
> +	hugepagesize_kb = file_stat.f_bsize / 1024;
> +	ksft_print_msg("Hugepagesize is %ldkB\n", hugepagesize_kb);
> +
> +	if (set_enable_soft_offline(enable_soft_offline)) {
> +		ksft_exit_fail_msg("Failed to set enable_soft_offline\n");

Call destroy_hugetlbfs_file() in error path?

> +		return;
> +	}
> +
> +	if (read_nr_hugepages(hugepagesize_kb, &nr_hugepages_before) != 0) {
> +		ksft_exit_fail_msg("Failed to read nr_hugepages\n");
> +		return;
> +	}
> +
> +	ksft_print_msg("Before MADV_SOFT_OFFLINE nr_hugepages=%ld\n",
> +		       nr_hugepages_before);
> +
> +	ret = do_soft_offline(fd, 2 * file_stat.f_bsize, expect_errno);
> +
> +	if (read_nr_hugepages(hugepagesize_kb, &nr_hugepages_after) != 0) {
> +		ksft_exit_fail_msg("Failed to read nr_hugepages\n");
> +		return;
> +	}
> +
> +	ksft_print_msg("After MADV_SOFT_OFFLINE nr_hugepages=%ld\n",
> +		nr_hugepages_after);
> +
> +	if (enable_soft_offline) {
> +		if (nr_hugepages_before != nr_hugepages_after + 1) {
> +			ksft_test_result_fail("MADV_SOFT_OFFLINE should reduced 1 hugepage\n");
> +			return;
> +		}
> +	} else {
> +		if (nr_hugepages_before != nr_hugepages_after) {
> +			ksft_test_result_fail("MADV_SOFT_OFFLINE reduced %lu hugepages\n",
> +				nr_hugepages_before - nr_hugepages_after);
> +			return;
> +		}
> +	}
> +
> +	ksft_test_result(ret == 0,
> +			 "Test soft-offline when enabled_soft_offline=%d\n",
> +			 enable_soft_offline);

Call destroy_hugetlbfs_file() when test finished ?

Thanks.
.
Jiaqi Yan June 25, 2024, 3:48 p.m. UTC | #2
On Mon, Jun 24, 2024 at 11:41 PM Miaohe Lin <linmiaohe@huawei.com> wrote:
>
> On 2024/6/25 0:33, Jiaqi Yan wrote:
> > Logs from soft_offline_page and soft_offline_in_use_page have
> > different formats than majority of the memory failure code:
> >
> >   "Memory failure: 0x${pfn}: ${lower_case_message}"
> >
> > Convert them to the following format:
> >
> >   "Soft offline: 0x${pfn}: ${lower_case_message}"
> >
> > No functional change in this commit.
> >
> > Signed-off-by: Jiaqi Yan <jiaqiyan@google.com>
> > ---
> >  mm/memory-failure.c | 15 +++++++++------
> >  1 file changed, 9 insertions(+), 6 deletions(-)
> >
> > diff --git a/mm/memory-failure.c b/mm/memory-failure.c
> > index d3c830e817e3..2a097af7da0e 100644
> > --- a/mm/memory-failure.c
> > +++ b/mm/memory-failure.c
> > @@ -2631,6 +2631,9 @@ int unpoison_memory(unsigned long pfn)
> >  }
> >  EXPORT_SYMBOL(unpoison_memory);
> >
> > +#undef pr_fmt
> > +#define pr_fmt(fmt) "Soft offline: " fmt
> > +
> >  static bool mf_isolate_folio(struct folio *folio, struct list_head *pagelist)
> >  {
> >       bool isolated = false;
> > @@ -2686,7 +2689,7 @@ static int soft_offline_in_use_page(struct page *page)
> >
> >       if (!huge && folio_test_large(folio)) {
> >               if (try_to_split_thp_page(page)) {
> > -                     pr_info("soft offline: %#lx: thp split failed\n", pfn);
> > +                     pr_info("%#lx: thp split failed\n", pfn);
> >                       return -EBUSY;
> >               }
> >               folio = page_folio(page);
> > @@ -2698,7 +2701,7 @@ static int soft_offline_in_use_page(struct page *page)
> >       if (PageHWPoison(page)) {
> >               folio_unlock(folio);
> >               folio_put(folio);
> > -             pr_info("soft offline: %#lx page already poisoned\n", pfn);
> > +             pr_info("%#lx page already poisoned\n", pfn);
>
> Again, it's better to be "%#lx: page" to make log format consistent.

Ah, I missed a ":", thanks for catching this!

> Thanks.
> .