From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.118 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796041854; b=OQB5AfNmTLoraMHD2LDOUY4sB5qD192rMjCPVM4BVdsUrhPP7TgOOX+R2QLnZbkfJCPW NNVrUyqrwezIP7jLMs0g4bqtVkYR9pzblj/ULsj/l874oFhps8A22MkmjuPsj8Z9Mqvnw eF8vD12g7YW17HrzRk0crf0mmk1V0MyN9kZFjt0cibDUqZMvKUQMXCBvW6a9PTB+q2JHg kNQz4OXALtKiwpxuLsssAbNM9jHMq7b1O13kDt1fniXPT27iVsQYDkce9uASFIU4npoHR e/DalhSTNcKPlhrWrWahdxlmW0lSjFY1Zi7RULLx2WzYMQMVqrbDjb4CXoyeN6lOeuA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796041854; 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=GBpxaTntFrK+nwJI3YcmOyTuFbwbDMF0wHYXYiLfpfA=; b=yAaAJUzQl/6uxxbiRAoCK28+zXZq40r6tuabfRm0Y43s+uiTU7c96ZnDmxxHonHchukf xf0268Rf6RFM2Nf0u4kdZF/PAt6WVUiq0nRyWzNKeCTzxI2T+DXDLjacpWPyP/l3poGL4 bUz3CdZJZU6n2Co2bZUD+BFHfXTGmc/BPsHAwLq9I5ef89JZCr3mAdDJq8FR4aEPMNGPm zXtfjUca0tkt+G1IgqBdmGjpX2AeHTFh8sNAFQDFOW2MQ9nxfk5+RpymFfqQfWG1XAAwy vakeiEU7FgLO8sMlisHRxGyqlM+Gaj9YY3NfAq+7ZQsLvFhUuanwDfljjRaGVVHae/w== 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=BWdWYE3B; arc=none smtp.remote-ip=95.215.58.118 Received: from mta1.migadu.com (out-118.mta1.migadu.com [95.215.58.118]) (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 59DE5606A32A for ; Mon, 30 Nov 2026 12:30:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 59DE5606A32A 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=BWdWYE3B X-Envelope-To: stable@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=Hx4b3n3w89phbttTEYCZ6a1pXIqd4sKPRC+V8U5f1s4=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796041851; v=1; x=1796646651; b=BWdWYE3B/LLTh9mLot8WHdQj+BGt62w5PQVlNuRA6TyNBz9r9KQm2igzzQIG5tBg6IuRuaKC QNJP5SIBUcw7xs/qCjKphoGxnJUpLvdjAl03dxbbqlmNiKtbNwKwJUReJn7q8qSyMYOLulKtd5G H6j1jlIY1ph5o2lZlUJXUmTo= X-Envelope-To: stable@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 79ce9395dcf7f270; Mon, 30 Nov 2026 12:30:51 +0000 X-Mizu-Trace-ID: 79ce9395dcf7f270 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 6.1.y v3] khugepaged: fix readahead lock inversion in collapse_file() Date: Mon, 30 Nov 2026 20:30:40 +0800 Message-ID: <20261130123040.40795-1-lance.yang@linux.dev> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20261109201858-failed-6.1-eadce52a9ef6@kernel.org> References: <20261109201858-failed-6.1-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 6.1: - Use find_lock_page(), unlock_page() and put_page() for the existing page-based collapse code. - The scan puts the destination huge page in the page cache. A reader may still hold it after rollback. Retry from alloc_charge_hpage() to get a fresh page. v2 -> v3: - Allocate a fresh huge page for the retry (per Sasha) - thanks! - https://lore.kernel.org/all/20261127081556.31348-1-lance.yang@linux.dev/ v1 -> v2: - Refresh context after the shmem_mapping() update. - https://lore.kernel.org/all/20261111132506.88827-1-lance.yang@linux.dev/ mm/khugepaged.c | 45 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index c9ee735a2b1d..c97a6cf11aec 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1780,17 +1780,22 @@ 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; int nr; VM_BUG_ON(!IS_ENABLED(CONFIG_READ_ONLY_THP_FOR_FS) && !is_shmem); VM_BUG_ON(start & (HPAGE_PMD_NR - 1)); +retry: result = alloc_charge_hpage(&hpage, mm, cc); if (result != SCAN_SUCCEED) goto out; mapping_set_update(&xas, mapping); + do_readahead = false; + xas_set_order(&xas, start, HPAGE_PMD_ORDER); /* * Ensure we have slots for all the pages in the range. This is * almost certainly a no-op because most of the pages must be present @@ -1875,17 +1880,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 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, @@ -2151,6 +2148,32 @@ static int collapse_file(struct mm_struct *mm, unsigned long addr, if (hpage) unlock_page(hpage); + + /* Readahead may take invalidate_lock; source pages are unlocked. */ + if (do_readahead) { + put_page(hpage); + 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(); + readahead_done = true; + goto retry; + } out: VM_BUG_ON(!list_empty(&pagelist)); if (hpage) { -- 2.49.0