From: Nico Pache <npache@redhat.com>
To: Sergey Senozhatsky <senozhatsky@chromium.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
David Hildenbrand <david@kernel.org>,
Lorenzo Stoakes <lorenzo.stoakes@oracle.com>,
Zi Yan <ziy@nvidia.com>,
Baolin Wang <baolin.wang@linux.alibaba.com>,
"Liam R. Howlett" <Liam.Howlett@oracle.com>,
Ryan Roberts <ryan.roberts@arm.com>, Dev Jain <dev.jain@arm.com>,
Barry Song <baohua@kernel.org>,
Lance Yang <lance.yang@linux.dev>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCHv2] mm: khugepaged: make scan loops suspend aware
Date: Tue, 10 Feb 2026 23:15:52 -0700 [thread overview]
Message-ID: <CAA1CXcA-V7FtK-QkyRHak1GBtepiFiTHFtF7g93P7mM41_2LMw@mail.gmail.com> (raw)
In-Reply-To: <20260211031512.261127-1-senozhatsky@chromium.org>
On Tue, Feb 10, 2026 at 8:15 PM Sergey Senozhatsky
<senozhatsky@chromium.org> wrote:
>
> A number of khugepaaged's loops, e.g. khugepaged_scan_mm_slot(),
> are time unbound, which can become problematic during system
> suspend:
>
> PM: suspend entry (s2idle)
> Filesystems sync: 0.003 seconds
> Freezing user space processes
> Freezing user space processes completed (elapsed 0.003 seconds)
> OOM killer disabled.
> Freezing remaining freezable tasks
> Freezing remaining freezable tasks failed after 20.004 seconds (1 tasks refusing to freeze, wq_busy=0):
> task:khugepaged state:D stack:0 pid:1345 ppid:2 flags:0x00004000
> Call Trace:
> <TASK>
> schedule+0x523/0x16a0
> schedule_timeout+0x23b/0x6e0
> io_schedule_timeout+0x3f/0x80
> wait_for_completion_io_timeout+0xe4/0x170
> submit_bio_wait+0x79/0xc0
> swap_readpage+0x150/0x2d0
> swap_cluster_readahead+0x3be/0x750
> shmem_swapin+0xa7/0x100
> shmem_swapin_folio+0xcd/0x2e0
> shmem_get_folio+0x237/0x580
> collapse_file+0x247/0x1280
> hpage_collapse_scan_file+0x26e/0x380
> khugepaged+0x43b/0x810
> kthread+0xfb/0x120
> </TASK>
>
> Make hpage_collapse_test_exit_or_disable() suspend aware so
> that khugepaaged's scan loops can terminate in a timely manner
> and let system enter the sleep state.
>
> Co-developed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
> Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org>
Hi Sergey!
Thank you for reporting this and taking the time to investigate a fix.
Here are some simple review points then I'll comment on the code below.
- We usually send "To:" the mailing lists and "CC:" to all other people.
- Your subject contains "PATCHv2" there should be a space there
- It would be worth noting the "HOW" in the commit message
> ---
>
> v1->v2: Actually pass "cc" to hpage_collapse_test_exit_or_disable()
>
> mm/khugepaged.c | 22 +++++++++++++++-------
> 1 file changed, 15 insertions(+), 7 deletions(-)
>
> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> index eff9e3061925..d32a5ad27097 100644
> --- a/mm/khugepaged.c
> +++ b/mm/khugepaged.c
> @@ -392,10 +392,18 @@ static inline int hpage_collapse_test_exit(struct mm_struct *mm)
> return atomic_read(&mm->mm_users) == 0;
> }
>
> -static inline int hpage_collapse_test_exit_or_disable(struct mm_struct *mm)
> +static inline int hpage_collapse_test_exit_or_disable(struct mm_struct *mm,
> + struct collapse_control *cc)
> {
> + bool was_frozen = false;
> +
> + if (cc->is_khugepaged &&
> + unlikely(kthread_freezable_should_stop(&was_frozen)))
> + return 1;
> +
> return hpage_collapse_test_exit(mm) ||
> - mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm);
> + mm_flags_test(MMF_DISABLE_THP_COMPLETELY, mm) ||
> + was_frozen;
I dont really understand the freezer code, and there are few examples
of this. But given how other callers do it, this seems correct.
> }
>
> static bool hugepage_pmd_enabled(void)
> @@ -895,7 +903,7 @@ static enum scan_result hugepage_vma_revalidate(struct mm_struct *mm, unsigned l
> enum tva_type type = cc->is_khugepaged ? TVA_KHUGEPAGED :
> TVA_FORCED_COLLAPSE;
>
> - if (unlikely(hpage_collapse_test_exit_or_disable(mm)))
> + if (unlikely(hpage_collapse_test_exit_or_disable(mm, cc)))
> return SCAN_ANY_PROCESS;
>
> *vmap = vma = find_vma(mm, address);
> @@ -2420,7 +2428,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
> goto breakouterloop_mmap_lock;
>
> progress++;
> - if (unlikely(hpage_collapse_test_exit_or_disable(mm)))
> + if (unlikely(hpage_collapse_test_exit_or_disable(mm, cc)))
> goto breakouterloop;
>
> vma_iter_init(&vmi, mm, khugepaged_scan.address);
> @@ -2428,7 +2436,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
> unsigned long hstart, hend;
>
> cond_resched();
> - if (unlikely(hpage_collapse_test_exit_or_disable(mm))) {
> + if (unlikely(hpage_collapse_test_exit_or_disable(mm, cc))) {
> progress++;
> break;
> }
> @@ -2450,7 +2458,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
> bool mmap_locked = true;
>
> cond_resched();
> - if (unlikely(hpage_collapse_test_exit_or_disable(mm)))
> + if (unlikely(hpage_collapse_test_exit_or_disable(mm, cc)))
> goto breakouterloop;
>
> VM_BUG_ON(khugepaged_scan.address < hstart ||
> @@ -2468,7 +2476,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned int pages, enum scan_result
> fput(file);
> if (*result == SCAN_PTE_MAPPED_HUGEPAGE) {
> mmap_read_lock(mm);
> - if (hpage_collapse_test_exit_or_disable(mm))
> + if (hpage_collapse_test_exit_or_disable(mm, cc))
> goto breakouterloop;
> *result = try_collapse_pte_mapped_thp(mm,
> khugepaged_scan.address, false);
> --
> 2.53.0.239.g8d8fc8a987-goog
>
next prev parent reply other threads:[~2026-02-11 6:16 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-11 3:15 Sergey Senozhatsky
2026-02-11 6:15 ` Nico Pache [this message]
2026-02-12 1:51 ` Sergey Senozhatsky
2026-02-11 9:50 ` David Hildenbrand (Arm)
2026-02-12 1:50 ` Sergey Senozhatsky
2026-02-12 8:30 ` David Hildenbrand (Arm)
2026-02-12 8:42 ` Sergey Senozhatsky
2026-02-12 6:32 ` Sergey Senozhatsky
2026-02-12 8:44 ` David Hildenbrand (Arm)
2026-02-12 9:05 ` Sergey Senozhatsky
2026-02-12 9:10 ` David Hildenbrand (Arm)
2026-02-12 9:24 ` Sergey Senozhatsky
2026-02-14 6:35 ` Lance Yang
2026-02-16 9:24 ` David Hildenbrand (Arm)
2026-02-16 9:50 ` Sergey Senozhatsky
2026-02-16 10:05 ` Sergey Senozhatsky
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=CAA1CXcA-V7FtK-QkyRHak1GBtepiFiTHFtF7g93P7mM41_2LMw@mail.gmail.com \
--to=npache@redhat.com \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=baohua@kernel.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=david@kernel.org \
--cc=dev.jain@arm.com \
--cc=lance.yang@linux.dev \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=ryan.roberts@arm.com \
--cc=senozhatsky@chromium.org \
--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