From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796384791; b=X6rzVAFeXzVxpgCXHyObtplaYUKZ0nvBw8WHSaW+2OhHDjVpDdR/kD7QZ2Z6QU6w6pdu MlTuIBkv7mzelUCzJB2PsC9SJQiyDc7QN3FNLLQCSbIuHfAlrMzEAlAWWHekCFLBQ89Ld Qgk49TaGsN5OrO1++HP8KggTEsNPI38XCxYC3yT/84K8CiKzs5mByWArrwUsUv+ghkd5Y zkmUxkSqhzfj0y2OZR679VxdQ80CWytpmPS5VvYkESYTVoPh01y4pnG8/GdlSFwGBqd0H 71UAWiQaf9TjuxHFjM//bM9KNWC/a0zQO6OlXNOn6CFj7xtC+d7ruKc7jxRYvoNaLoA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796384791; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=kMRgYubC5vQP39KaZhLB1gMu54v8dhL+xo/c2Mbm5dQ=; b=s6MdzIJW3N78zsHuBnaOnZHQfMUZEL5YTF7g4LeDZMKqrls7+FEguvRktVkOi98i9sUX 27G1LGX7KM3u7z6Uq6bxQUbn0jnjBY7ARDznNGyf1y1Q6iq6aRuSDkTCQixtFIHmvZtnr eLEeqzXOul/7yo1tMMjs/OqGAX3yDBBVbtQOwhbyZK7HMDTFWUNzaGtRTH9pvfGEIHWBx v2u1bp6om6dBOQWmncAENKyOEkkIGgH4OOgwY5DyAxVeev0mTRbj7RXASFfAXNlg+cK8Q hgvuvXSMcq208jGTY0FNNhbEMc5AeZIRV7ETd8cqGVkzB8NkkFZSUlX9s+FsukR9DWA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=07GCxNbn; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 589C6606A32F; Fri, 4 Dec 2026 11:46:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=07GCxNbn Received: by smtp.kernel.org (Postfix) with ESMTPSA id CE5061F0093B; Fri, 4 Dec 2026 11:46:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796384791; bh=kMRgYubC5vQP39KaZhLB1gMu54v8dhL+xo/c2Mbm5dQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=07GCxNbnO2D5VXFHwSQ41EqJJ5cV879zCpjpvDzg+Ii/veKtImLxn5mG2LNA6J+wt pBJuo6P070Y2u/672OR6xZpbHlxQxbZx/KIEND9BLzNZAFH8Rw3qrcWmRhC9901rlj ol/Lu59orV1gLhsvYVk+3xdN/9QCTpwE+RgACgYk= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, syzbot+16bf7cd0ebeb1de93aa5@syzkaller.appspotmail.com, Lance Yang , Pedro Falcato , "David Hildenbrand (Arm)" , Zi Yan , Baolin Wang , Sasha Levin Subject: [PATCH 6.6 0296/1167] khugepaged: fix readahead lock inversion in collapse_file() Date: Fri, 4 Dec 2026 18:55:23 +0900 Message-ID: <20261204100709.742427001@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100702.047403948@linuxfoundation.org> References: <20261204100702.047403948@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore 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 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Lance Yang [ Upstream commit eadce52a9ef657d561b3e76c9b5f178bd99735f5 ] 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) [ sashal: adapted collapse_file() to the older page-based code (struct page, int result, find_lock_page) instead of folios and enum scan_result, keeping the same readahead-after-rollback logic. ] Signed-off-by: Sasha Levin --- mm/khugepaged.c | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 4933bafe61760..2eae83f34c76c 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1802,6 +1802,8 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, 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; VM_BUG_ON(!IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && !is_shmem); VM_BUG_ON(start & (HPAGE_PMD_NR - 1)); @@ -1813,6 +1815,10 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, mapping_set_update(&xas, mapping); +retry: + result = SCAN_SUCCEED; + do_readahead = false; + xas_set_order(&xas, start, HPAGE_PMD_ORDER); __folio_set_locked(new_folio); if (is_shmem) __folio_set_swapbacked(new_folio); @@ -1877,17 +1883,9 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, } } 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 lru cache 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, @@ -2218,6 +2216,30 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, new_folio->mapping = NULL; folio_unlock(new_folio); + + /* Readahead may take invalidate_lock; source folios are unlocked. */ + if (do_readahead) { + for (index = start; index < end;) { + folio = filemap_lock_folio(mapping, index); + if (IS_ERR(folio)) { + page_cache_sync_readahead(mapping, &file->f_ra, + file, index, end - index); + folio = filemap_lock_folio(mapping, index); + if (IS_ERR(folio)) { + result = SCAN_FAIL; + goto out_put; + } + } + index = folio_next_index(folio); + folio_unlock(folio); + folio_put(folio); + } + /* Drain the local LRU batch before retrying isolation. */ + lru_add_drain(); + readahead_done = true; + goto retry; + } +out_put: folio_put(new_folio); out: VM_BUG_ON(!list_empty(&pagelist)); -- 2.53.0