From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.150 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796041972; b=kGgwXx4mpJ1EnWWBOAQSvm1Dp7tltRRpxwwYVGZGTv/pY63BXDCbZC6NEkc+T0fRcSHO 28WlazXti+9e0hqa4CeFvKTa3UgpdyTuBt28q+mfX7KmwbWXuueljKcmJhb3g7fjNl8V8 Ngr64YgqExpOj4JWIjBzfwctZvD573CQ3m26y5YFg/7oaI++9/D6D6LtxdYyd5eBXL+WC uWA3/I7tMG5Iwr1Y9r1mIIqXAGAtr6Wi9qpUlkX/53H5CSko9GPO31ngqqbZMjCUhqZ9J vujZ2++8PHd0ElUm1XrYDrUG+/jBnuISIvgq+qvNPX7ZfC2jqin7QN3d+VOsoOtu5lg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796041972; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=RHVruziRsNaWODZ5SoGbFT6zRXjdMTTLXJU3rdGP0OI=; b=iS087uzOJ0gNYZCjWBGYf/BNFLQ7j9TTQ8adf/foESa++T2OsFtM2Vga5bMQBhlYjQVt ojs6B8srI83KTxAyYIqV5UoFChGSdWNAekIvIluWzlqbNmCPFTUCZgDefmaNhb6l+zPpf eBmeUIb/Udd03QuUy12omcszF/GdubR/XP8UZiGnfodNnyxWEgBHrME3iMS7RocqUJp+9 oUDCLRnIifXzqlupoaIeCNEbGW7nm+4pWsjfdR1r4hryHAvIZL2sbho1IvernGJ5Uv/pj EpgSAb5UZticrHXza8OuRU/d+EVhfsqJDwPsYS4FEQZLZwrrvGbEm2dxUymn6rhY2Ig== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=muZrKiXa; arc=none smtp.remote-ip=95.215.58.150 Received: from mta1.migadu.com (out-150.mta1.migadu.com [95.215.58.150]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 59FEE606A320 for ; Mon, 30 Nov 2026 12:32:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 59FEE606A320 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=muZrKiXa X-Envelope-To: stable@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=ImhJOWdrakDRGyOnjMjwvvUZelzHMg3+uCi9fLaK9+0=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796041969; v=1; x=1796646769; b=muZrKiXaDyjmwRH1/OLttJSmSDITiu1o8df2vnDEcvKWrwV0GIx5gpY1DvsGAPquzjZ+b6ap pOKGP5omcQA1R94I45saR4rje04oljYYGIOSwgdtFjWJJX7LkgzL1hpHN/pw1B6wXkhUHobwMqC Erhft4EfW7dFzGwMcTqvZxqU= X-Envelope-To: stable@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 4b9c15d651e6cb2c; Mon, 30 Nov 2026 12:32:49 +0000 X-Mizu-Trace-ID: 4b9c15d651e6cb2c X-Migadu-Flow: FLOW_OUT From: Lance Yang To: stable@vger.kernel.org Cc: syzbot+16bf7cd0ebeb1de93aa5@syzkaller.appspotmail.com, Lance Yang , Pedro Falcato , "David Hildenbrand (Arm)" , Zi Yan , Baolin Wang Subject: [PATCH 5.15.y v3] khugepaged: fix readahead lock inversion in collapse_file() Date: Mon, 30 Nov 2026 20:32:37 +0800 Message-ID: <20261130123237.41399-1-lance.yang@linux.dev> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20261109202404-failed-5.15-eadce52a9ef6@kernel.org> References: <20261109202404-failed-5.15-eadce52a9ef6@kernel.org> X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Pedro Falcato Acked-by: David Hildenbrand (Arm) Acked-by: Zi Yan Reviewed-by: Baolin Wang Link: https://patch.msgid.link/20261026152013.10511-1-lance.yang@linux.dev Signed-off-by: David Hildenbrand (Arm) (cherry picked from commit eadce52a9ef657d561b3e76c9b5f178bd99735f5) --- Backport to 5.15: - Use find_lock_page(), unlock_page() and put_page() for the existing page-based collapse code. - Drop our reference to the destination huge page before readahead, since a reader may still hold it after rollback. With CONFIG_NUMA disabled, khugepaged_alloc_page() expects a preallocated page. Call khugepaged_prealloc_page() before retrying. v2 -> v3: - Allocate a fresh huge page for the retry (per Sasha) - thanks! - https://lore.kernel.org/all/20261127081618.31491-1-lance.yang@linux.dev/ v1 -> v2: - Refresh context after the shmem_mapping() update. - https://lore.kernel.org/all/20261111132424.88744-1-lance.yang@linux.dev/ mm/khugepaged.c | 52 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index bd909aa4b8fe..2e2a0b2e1fa0 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1701,6 +1701,8 @@ static void collapse_file(struct mm_struct *mm, LIST_HEAD(pagelist); XA_STATE_ORDER(xas, &mapping->i_pages, start, HPAGE_PMD_ORDER); int nr_none = 0, result = SCAN_SUCCEED; + bool do_readahead = false; + bool readahead_done = false; int nr; VM_BUG_ON(!IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && !is_shmem); @@ -1709,6 +1711,7 @@ static void collapse_file(struct mm_struct *mm, /* Only allocate from the target node */ gfp = alloc_hugepage_khugepaged_gfpmask() | __GFP_THISNODE; +retry: new_page = khugepaged_alloc_page(hpage, gfp, node); if (!new_page) { result = SCAN_ALLOC_HUGE_PAGE_FAIL; @@ -1721,6 +1724,9 @@ static void collapse_file(struct mm_struct *mm, } count_memcg_page_event(new_page, THP_COLLAPSE_ALLOC); + result = SCAN_SUCCEED; + do_readahead = false; + xas_set_order(&xas, start, HPAGE_PMD_ORDER); /* This will be less messy when we use multi-index entries */ do { xas_lock_irq(&xas); @@ -1791,17 +1797,9 @@ static void collapse_file(struct mm_struct *mm, } } else { /* !is_shmem */ if (!page || xa_is_value(page)) { - xas_unlock_irq(&xas); - page_cache_sync_readahead(mapping, &file->f_ra, - file, index, - end - index); - /* drain pagevecs to help isolate_lru_page() */ - lru_add_drain(); - page = find_lock_page(mapping, index); - if (unlikely(page == NULL)) { - result = SCAN_FAIL; - goto xa_unlocked; - } + do_readahead = !readahead_done; + result = SCAN_FAIL; + goto xa_locked; } else if (PageDirty(page)) { /* * khugepaged only works on read-only fd, @@ -2034,6 +2032,38 @@ static void collapse_file(struct mm_struct *mm, } unlock_page(new_page); + + /* Readahead may take invalidate_lock; source pages are unlocked. */ + if (do_readahead) { + bool wait = false; + + put_page(new_page); + *hpage = NULL; + + for (index = start; index < end; index++) { + struct page *page = find_lock_page(mapping, index); + + if (!page) { + page_cache_sync_readahead(mapping, &file->f_ra, + file, index, end - index); + page = find_lock_page(mapping, index); + if (!page) { + result = SCAN_FAIL; + goto out; + } + } + unlock_page(page); + put_page(page); + } + /* Drain the local LRU batch before retrying isolation. */ + lru_add_drain(); + if (!khugepaged_prealloc_page(hpage, &wait)) { + result = SCAN_ALLOC_HUGE_PAGE_FAIL; + goto out; + } + readahead_done = true; + goto retry; + } out: VM_BUG_ON(!list_empty(&pagelist)); if (!IS_ERR_OR_NULL(*hpage)) -- 2.49.0