From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out30-100.freemail.mail.aliyun.com (out30-100.freemail.mail.aliyun.com [115.124.30.100]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CCDE4314B95 for ; Fri, 6 Feb 2026 08:55:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.100 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770368142; cv=none; b=kuVl3dfUNFpi1moOPp9TJijA5vcLL+wgEZMKvuO0e4hYhT6iqBeSpWR8xIb9iWbZPo9KD+cJT+OdpjNzLU6CAHAmVEjlvUWhqydCe0vYLNcr0VkGvJPR3oidc4zIWu3krTGwhHHLNzmmzG5Bt2gZSD8uwoMLKJe/50ialGKpS/0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770368142; c=relaxed/simple; bh=wH1/zLNoz5Hfp6YwaODjW+RZVEfiUDwx/qWmzfHYkwI=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=nh0FJUm86u7xhUb4k0qOTipna4mqvuUdKZu6FN4Sr5BVYapDFfMnacTPiJr9FJbjqn5EErpgHM94fcUEPVCAg9Q7OYDEqRiViVCSLOK81QvQdb/+/Qq64DMNDopb1aR/HQ0lqEQlXiA6LIS2cxfBc3Ng63XbwWXyZidODcox6WA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=o/HW2eOX; arc=none smtp.client-ip=115.124.30.100 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="o/HW2eOX" DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1770368138; h=Message-ID:Date:MIME-Version:Subject:To:From:Content-Type; bh=Wp7Qazz8xwMa3br3OGB1GRZNlhrigjBZUeslfFenub0=; b=o/HW2eOX9N/XfMtTxZzVaXC2vYHfk8QEynZR/hpitDgs/wbjrc11EfxTtCMRMI36ECitgrkmKfzF+J4Xi01oZiKoqujGB095Q7cWKz1LUXuSBYVhicElgZlcAPr/dxMYW9yFbqH+IAmUFui2pKeBD3aBc3kJwWJsUCNi5+y4BmI= Received: from 30.74.144.131(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0Wye899q_1770368136 cluster:ay36) by smtp.aliyun-inc.com; Fri, 06 Feb 2026 16:55:37 +0800 Message-ID: <70ed2de7-3e66-4a23-85bb-e0a4c5b61088@linux.alibaba.com> Date: Fri, 6 Feb 2026 16:55:36 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [stable-6.6.y] mm: khugepaged refuses to freeze To: "David Hildenbrand (Arm)" , Sergey Senozhatsky Cc: Andrew Morton , Lorenzo Stoakes , Zi Yan , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <3d0f189b-faab-4452-b9cc-8f4e7a15025f@linux.alibaba.com> <1179320d-6bab-40a4-a7ac-dbcfbab24623@linux.alibaba.com> From: Baolin Wang In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 2/6/26 4:36 PM, David Hildenbrand (Arm) wrote: > On 2/6/26 06:12, Baolin Wang wrote: >> >> >> On 2/6/26 12:31 PM, Sergey Senozhatsky wrote: >>> On (26/02/06 12:38), Sergey Senozhatsky wrote: >>> [..] >>>> >>>> Right, I thought about it but wasn't sure.  Could the inner loop (e.g. >>>> collapse_file() in this particular case) loop long enough to fail >>>> suspend >>>> w/o ever giving the outer loop (khugepaged_do_scan()) a chance to >>>> freeze? >> >> Yes, that’s possible. However, if we add a try_to_freeze() check in >> the inner loop, we need to consider various scenarios (such as >> anonymous folio swap-in and other potential cases?), which feels too >> hacky to me. >> >>> For inner loops I wondered if cond_resched() could be an indicator of >>> where try_to_freeze() should be placed.  Those cond_resched() calls >>> are there for a reason, after all.   E.g. something like: >>> >>> --- >>> >>> diff --git a/mm/khugepaged.c b/mm/khugepaged.c >>> index fa6a018b20a8..cee08466a069 100644 >>> --- a/mm/khugepaged.c >>> +++ b/mm/khugepaged.c >>> @@ -2431,6 +2431,9 @@ static unsigned int >>> khugepaged_scan_mm_slot(unsigned int pages, enum scan_result >>>           unsigned long hstart, hend; >>>           cond_resched(); >>> +        if (try_to_freeze()) >>> +            break; >>> + >>>           if (unlikely(hpage_collapse_test_exit_or_disable(mm))) { >>>               progress++; >>>               break; >>> @@ -2453,6 +2456,9 @@ static unsigned int >>> khugepaged_scan_mm_slot(unsigned int pages, enum scan_result >>>               bool mmap_locked = true; >>>               cond_resched(); >>> +            if (try_to_freeze()) >>> +                goto breakouterloop; >>> + >>>               if (unlikely(hpage_collapse_test_exit_or_disable(mm))) >>>                   goto breakouterloop; >> >> This looks better than the previous version. Let’s also wait to see if >> others have any better suggestions. > > What prevents other callpaths (faults, read(), write(), etc) from > similarly triggering swapin? Usually it’s just a userspace process triggering one page fault to swap a page in, then will return to userspace. There aren’t other kernel threads like khugepaged continuously do swap-in in a loop. > I recall that there is a notifier when the system is preparing to sleep > (pm notifier or something). Could we simply hook into that to tell > khugepaged to suspend+resume? Do you mean “struct dev_pm_ops”, which is used to register PM callbacks for devices? However, I don’t know how to use it with a kernel thread. Also look at how kswapd does it, kswapd also uses kthread_freezable_should_stop() to check the freeze state. > Essentially, making hpage_collapse_test_exit_or_disable() break our for us. Ah, yes, even better:)