Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Lance Yang <lance.yang@linux.dev>
To: Zi Yan <ziy@nvidia.com>, Sasha Levin <sashal@kernel.org>
Cc: stable@vger.kernel.org,
	syzbot+16bf7cd0ebeb1de93aa5@syzkaller.appspotmail.com,
	Pedro Falcato <pfalcato@suse.de>,
	"David Hildenbrand (Arm)" <david@kernel.org>,
	Baolin Wang <baolin.wang@linux.alibaba.com>
Subject: Re: [PATCH 6.12.y] khugepaged: fix readahead lock inversion in collapse_file()
Date: Sat, 14 Nov 2026 00:34:27 +0800	[thread overview]
Message-ID: <a849043e-8421-473f-a7de-8030482bd5b5@linux.dev> (raw)
In-Reply-To: <59D0C605-0343-4CA6-8529-C399AE8BBA9F@nvidia.com>



On 2026/11/14 00:27, Zi Yan wrote:
> On 11 Nov 2026, at 8:26, Lance Yang wrote:
> 
>> collapse_file() can call page_cache_sync_readahead() while still holding
>> source folios locked. If truncate holds mapping->invalidate_lock
>> exclusively and waits for one of those folios, readahead waits for
>> invalidate_lock and neither side can make progress.
>>
>> We need to release those folio locks before starting readahead. On a page
>> cache miss, roll back first, populate the collapse range, and retry once.
>> Doing readahead after rollback avoids an extra pass when all folios are
>> already present. If another miss occurs on retry, give up rather than keep
>> retrying while truncate or reclaim removes folios.
>>
>> Keep page_cache_sync_readahead() and wait for read I/O with
>> filemap_lock_folio(), releasing each folio before moving to the next. The
>> existing uptodate check during collapse still handles read errors.
>>
>> Shmem uses shmem_get_folio() for swapin and fallocated folios without
>> taking mapping->invalidate_lock, so it is unaffected.
>>
>> Reported-by: syzbot+16bf7cd0ebeb1de93aa5@syzkaller.appspotmail.com
>> Closes: https://syzkaller.appspot.com/bug?extid=16bf7cd0ebeb1de93aa5
>> Fixes: 730633f0b7f9 ("mm: Protect operations adding pages to page cache with invalidate_lock")
>> Cc: stable@vger.kernel.org
>> Signed-off-by: Lance Yang <lance.yang@linux.dev>
>> Reviewed-by: Pedro Falcato <pfalcato@suse.de>
>> Acked-by: David Hildenbrand (Arm) <david@kernel.org>
>> Acked-by: Zi Yan <ziy@nvidia.com>
>> Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
>> Link: https://patch.msgid.link/20261026152013.10511-1-lance.yang@linux.dev
>> Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
>>
>> (cherry picked from commit eadce52a9ef657d561b3e76c9b5f178bd99735f5)
>> ---
>> Backport to 6.12:
>> - 6.12 still takes invalidate_lock in collapse_file() after locking the
>>    source folios. Use filemap_invalidate_trylock_shared() and roll back
>>    on contention, otherwise the same deadlock is possible even after
>>    moving readahead.
>>
>>   mm/khugepaged.c | 49 +++++++++++++++++++++++++++++++++++++------------
>>   1 file changed, 37 insertions(+), 12 deletions(-)
>>
> <snip>
> 
>> @@ -2040,7 +2038,10 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr,
>>   		 * in do_dentry_open() truncating the page cache. This is
>>   		 * particularly important if there are dirty folios in transit.
>>   		 */
>> -		filemap_invalidate_lock_shared(mapping);
>> +		if (!filemap_invalidate_trylock_shared(mapping)) {
>> +			result = SCAN_FAIL;
>> +			goto rollback;
>> +		}
>>   		filemap_nr_thps_inc(mapping);
>>   		/*
>>   		 * Paired with the fence in do_dentry_open() -> get_write_access()
> 
> This additional change to address the same deadlock makes sense to me.
> 
> Acked-by: Zi Yan <ziy@nvidia.com>

Much appreciated, thanks!

      reply	other threads:[~2026-11-13 16:34 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-10  1:09 FAILED: patch "[PATCH] khugepaged: fix readahead lock inversion in collapse_file()" failed to apply to 6.12-stable tree Sasha Levin
2026-11-11 13:26 ` [PATCH 6.12.y] khugepaged: fix readahead lock inversion in collapse_file() Lance Yang
2026-11-13  2:33   ` Sasha Levin
2026-11-13 16:27   ` Zi Yan
2026-11-13 16:34     ` Lance Yang [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=a849043e-8421-473f-a7de-8030482bd5b5@linux.dev \
    --to=lance.yang@linux.dev \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=david@kernel.org \
    --cc=pfalcato@suse.de \
    --cc=sashal@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=syzbot+16bf7cd0ebeb1de93aa5@syzkaller.appspotmail.com \
    --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