From: Baolin Wang <baolin.wang@linux.alibaba.com>
To: akpm@linux-foundation.org, david@kernel.org, ljs@kernel.org
Cc: ziy@nvidia.com, liam@infradead.org, nico.pache@linux.dev,
dev.jain@arm.com, ryan.roberts@arm.com, baohua@kernel.org,
lance.yang@linux.dev, usama.arif@linux.dev, linux-mm@kvack.org,
linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 4/4] selftests: mm: add mTHP collapse test cases
Date: Sat, 1 Aug 2026 13:37:36 +0800 [thread overview]
Message-ID: <4280f431-ce88-4f09-9de6-328414c98411@linux.alibaba.com> (raw)
In-Reply-To: <bea2086517b69a1af8813dac5774a1409aafd262.1785224928.git.baolin.wang@linux.alibaba.com>
On 7/28/26 4:13 PM, Baolin Wang wrote:
> Added a new command 'mthp_khugepaged' for mTHP collapse, along with the '-c'
> parameter to specify the collapse order. Additionally, added mTHP collapse
> test cases for 'collapse_full', 'collapse_empty', and 'collapse_single_mthp'
> for anonymous folios. All khugepaged test cases passed.
>
> Signed-off-by: Baolin Wang <baolin.wang@linux.alibaba.com>
> ---
> tools/testing/selftests/mm/khugepaged.c | 130 ++++++++++++++++++----
> tools/testing/selftests/mm/run_vmtests.sh | 2 +
> 2 files changed, 113 insertions(+), 19 deletions(-)
>
> diff --git a/tools/testing/selftests/mm/khugepaged.c b/tools/testing/selftests/mm/khugepaged.c
> index f69be6be0ecd..e21a6ec38363 100644
> --- a/tools/testing/selftests/mm/khugepaged.c
> +++ b/tools/testing/selftests/mm/khugepaged.c
> @@ -26,9 +26,11 @@
>
> #define BASE_ADDR ((void *)(1UL << 30))
> static unsigned long hpage_pmd_size;
> +static int hpage_pmd_order;
> static unsigned long page_size;
> static int hpage_pmd_nr;
> static int anon_order;
> +static int collapse_order;
>
> #define PID_SMAPS "/proc/self/smaps"
> #define TEST_FILE "collapse_test_file"
> @@ -69,6 +71,7 @@ struct collapse_context {
> };
>
> static struct collapse_context *khugepaged_context;
> +static struct collapse_context *mthp_khugepaged_context;
> static struct collapse_context *madvise_context;
>
> struct file_info {
> @@ -554,25 +557,25 @@ static void madvise_collapse(const char *msg, char *p, int nr_hpages,
> }
>
> #define TICK 500000
> -static bool wait_for_scan(const char *msg, char *p, int nr_hpages,
> - struct mem_ops *ops)
> +static bool wait_for_scan(const char *msg, char *p, unsigned long size,
> + int nr_hpages, int collap_order, struct mem_ops *ops)
> {
> - unsigned long size = nr_hpages * hpage_pmd_size;
> + unsigned long hpage_size = page_size << collap_order;
> int full_scans;
> int timeout = 6; /* 3 seconds */
>
> /* Sanity check */
> - if (!ops->check_huge(p, size, 0, hpage_pmd_size))
> + if (!ops->check_huge(p, size, 0, hpage_size))
> ksft_exit_fail_msg("Unexpected huge page\n");
>
> - madvise(p, nr_hpages * hpage_pmd_size, MADV_HUGEPAGE);
> + madvise(p, size, MADV_HUGEPAGE);
>
> /* Wait until the second full_scan completed */
> full_scans = thp_read_num("khugepaged/full_scans") + 2;
>
> ksft_print_msg("%s...", msg);
> while (timeout--) {
> - if (ops->check_huge(p, size, nr_hpages, hpage_pmd_size))
> + if (ops->check_huge(p, size, nr_hpages, hpage_size))
> break;
> if (thp_read_num("khugepaged/full_scans") >= full_scans)
> break;
> @@ -595,7 +598,7 @@ static void khugepaged_collapse(const char *msg, char *p, int nr_hpages,
> if (!is_tmpfs(ops) && ops == &__read_write_file_write_ops)
> expect = false;
>
> - if (wait_for_scan(msg, p, nr_hpages, ops)) {
> + if (wait_for_scan(msg, p, size, nr_hpages, hpage_pmd_order, ops)) {
> if (expect)
> fail("Timeout");
> else
> @@ -617,12 +620,62 @@ static void khugepaged_collapse(const char *msg, char *p, int nr_hpages,
> fail("Fail");
> }
>
> +static void mthp_khugepaged_collapse(const char *msg, char *p, int nr_hpages,
> + struct mem_ops *ops, bool expect)
> +{
> + unsigned long hpage_size = page_size << collapse_order;
> + struct thp_settings settings = *thp_current_settings();
> + /* mTHP collpase only allocates PMD sized memory */
> + unsigned long size = hpage_pmd_size;
> +
> + /* Set mTHP setting for mTHP collapse */
> + if (ops == &__anon_ops) {
> + settings.thp_enabled = THP_NEVER;
> + settings.hugepages[collapse_order].enabled = THP_ALWAYS;
> + }
> +
> + thp_push_settings(&settings);
> +
> + if (wait_for_scan(msg, p, size, nr_hpages, collapse_order, ops)) {
sashiko said:
"Could this setup create a race condition that leads to spurious test
failures?
In mthp_khugepaged_collapse(), thp_push_settings() is called to set
hugepages[collapse_order].enabled = THP_ALWAYS before wait_for_scan() is
invoked. The khugepaged thread is then immediately allowed to scan and
collapse the faulted memory.
The very first action in wait_for_scan() is a sanity check to verify
that there are currently zero huge pages:
tools/testing/selftests/mm/khugepaged.c:wait_for_scan() {
...
/* Sanity check */
if (!ops->check_huge(p, size, 0, hpage_size))
ksft_exit_fail_msg("Unexpected huge page\n");
...
}
If khugepaged collapses the memory in the brief window between
thp_push_settings() and this sanity check, will the test falsely fail
with an unexpected huge page error?
For configurations relying on THP_MADVISE, this race doesn't appear to
occur because the madvise(..., MADV_HUGEPAGE) call happens after the
sanity check in wait_for_scan()."
Although this race is unlikely to happen (from my testing, it has never
occurred), setting it to THP_MADVISE is indeed more reasonable. Will do
in the next version.
prev parent reply other threads:[~2026-08-01 5:37 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-28 8:13 [PATCH 0/4] add anon " Baolin Wang
2026-07-28 8:13 ` [PATCH 1/4] selftests: mm: extend the check_huge() to support mTHP check Baolin Wang
2026-07-28 8:13 ` [PATCH 2/4] selftests: mm: move gather_after_split_folio_orders() into vm_util.c file Baolin Wang
2026-07-28 8:13 ` [PATCH 3/4] selftests: mm: implement the mTHP-sized hugepage check helpers Baolin Wang
2026-08-01 5:08 ` Baolin Wang
2026-07-28 8:13 ` [PATCH 4/4] selftests: mm: add mTHP collapse test cases Baolin Wang
2026-08-01 5:37 ` Baolin Wang [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4280f431-ce88-4f09-9de6-328414c98411@linux.alibaba.com \
--to=baolin.wang@linux.alibaba.com \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=david@kernel.org \
--cc=dev.jain@arm.com \
--cc=lance.yang@linux.dev \
--cc=liam@infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=nico.pache@linux.dev \
--cc=ryan.roberts@arm.com \
--cc=usama.arif@linux.dev \
--cc=ziy@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®