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=1796388773; b=GJ1014TgRiRRgDrs4VIqv34yFoNnZIOc6eIiWOpm99/wlnCBrLCDMbiX1RBJ70fCewso Iy+xMCA/nHG62EN6VQOxyMsvDb+Vmyn9O7/NERDWcEfCbzAgLcAp/ZEpAdwlCVThsjYZy D9yKOS8hdyQrd164GmNU2olASKzWzlEA/l/lyMW/QYxJC9nqdxbFkGTQJZeknvpXQfqv2 DyzAfGHpDvpY8qzPt096P1loRTIH7Obw0ukYIdqd4iqPJaBmlV9OSRbl/eVeuEzhK3kRU wAzClxejdtAxGqHopWLXSJ9OBr2cXPkEZ/E0QujG0ZeJ87RgSaLHQn1dLhsftnfdw+g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796388773; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=Wx2JKkYA1obOuW1cAE6pXfQ2P20myPFLjJGwEo3fzug=; b=hYN3YmdkEJJUXGvHnPLFDM4bXuHheiOaQoo3YoyWooW0bfnmJ0zsIsxEh6sVnllkf9PQ M3zUURbp1WzUXyhj7UXzIA8JQc1md3wRga+EIorlw1uRa6wPE3pRoulqx6q6+pNTiG+Zh G5J7gIqCQ7rBPhlpSH/Ai0WCMdLNONPbIUywLua+0x5C5guxZT3V/FhQOWTit+2WClI6b baj+UXrm0T3ABTv3DeN7HXCvoLImoTRbNU6m98v0b6eQbUa7SvW+rYK7r2L/VxcllrhKX MATJeM4PrrEJBb2txb8wL1Kl+ImkPtymBPcnZiGmbxcOq0voV+etV6xlDo4dguVAZ0w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=lfSazi6l; 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 3D3B5606A332; Fri, 4 Dec 2026 12:52:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=lfSazi6l Received: by smtp.kernel.org (Postfix) with ESMTPSA id 852861F00934; Fri, 4 Dec 2026 12:52:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796388772; bh=Wx2JKkYA1obOuW1cAE6pXfQ2P20myPFLjJGwEo3fzug=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=lfSazi6lqfBpM9I51buZ53ag3bR8Pqc1p7DKPZ6LX8u3MPx3+rRpHwkVrfNF3ifBE R/3jnxuF7s5Sox0MEXZMwfnahSIMYfY/VFwIqAKqmrkBr76yOrWFaLvToTkKYhMdLE cZXqdWpY1qry1OHgtvUwMGmDC6IyNqoq+KdKpXQyk9xWc3PTIz+AisJGITYFot54PE x1tqLn39vCd6rvbgG99/cPa/UghKRqNrMnjBje6qpeLmzpuTg093v3+h/zzVvAgcLq VhjR8rIs9efAR6lM6pKQYP8bwr+H25YlpQNeHQzB5s4qDcpgRA93KJ1/+zOYHCvqAC WUxM8He7JPdlQ== From: "David Hildenbrand (Arm)" Date: Fri, 04 Dec 2026 13:49:59 +0100 Subject: [PATCH v2 08/31] mm/khugepaged: flush file after unlocking folios in collapse_file() 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-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261204-fixes_core_3-v2-8-9aad65727632@kernel.org> References: <20261204-fixes_core_3-v2-0-9aad65727632@kernel.org> In-Reply-To: <20261204-fixes_core_3-v2-0-9aad65727632@kernel.org> To: Hugh Dickins , Baolin Wang , Andrew Morton , Mike Rapoport , Peter Xu , Michal Hocko , Kiryl Shutsemau , Manfred Spraul , Davidlohr Bueso , Cyrill Gorcunov , Zi Yan , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Lorenzo Stoakes , Rik van Riel , Vlastimil Babka , Harry Yoo , Jann Horn , Yang Shi , Zach O'Keefe , Song Liu , David Stevens , Kefeng Wang , Alex Williamson , Suren Baghdasaryan , Dave Jiang , Matthew Wilcox , Miaohe Lin , David Rientjes , Shakeel Butt , Jan Kara , Mel Gorman , Jason Gunthorpe , John Hubbard , Gerd Hoffmann , Vivek Kasireddy , Dave Airlie , Matthew Brost , Joshua Hahn , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Hagen Paul Pfeifer , James Bottomley , Felix Kuehling , Alex Sierra , Balbir Singh , Kairui Song , Qi Zheng , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baoquan He , Oscar Salvador , Muchun Song , Dan Williams , Rakie Kim , Hao Li , Christoph Lameter , Roman Gushchin , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt , Alexei Starovoitov , Xu Xin , Chengming Zhou , Shivank Garg Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-cxl@vger.kernel.org, linux-rt-devel@lists.linux.dev, SJ Park , "David Hildenbrand (Arm)" , Chris Mason , stable@vger.kernel.org, "Jose A. Perez de Azpillaga" X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2746; i=david@kernel.org; h=from:subject:message-id; bh=G8/3G/hXrHgz5vukewVAoc5oW33SP202/v3Xb8FKBJU=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLKFtv3TVmdZFnH5/M1ZLwWlPwYIshgsYan+++3C5G2Vs +7vE9hztaOUhUGMg0FWTJElKPi+wcuX62qr3fiYYeawMoEMYeDiFICJcL5g+B9/WPb5rqWvspJn VWhvP/39afmll5YlDW4xBtYXfks77dNi+J+7bJbVrFffL7gsLJ9s2WMdJcTS0OwW8mL7Vpm1O79 JhbIDAA== X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A From: Chris Mason collapse_file() calls filemap_flush() when it encounters a dirty folio in a file that is not open for writing. At that point, source folios earlier in the range are still locked. The writable-open check is racy: a writer can open the file after the check. On ext4, the flush may need a journal handle, while EXT4_IOC_MOVE_EXT takes a journal handle before locking folios, and a journal commit blocks new handles while waiting for existing handles to be released. Consequently, we can deadlock: collapse_file() waits for a handle, the commit waits for extent movement to finish, and extent movement waits for a source folio lock. Let's fix it by deferring filemap_flush() until rollback has unlocked all source folios. This has medium urgency, as it can deadlock ext4 in some scenarios. Fixes: 75f360696ce9 ("mm/thp: flush file for !is_shmem PageDirty() case in collapse_file()") Cc: stable@vger.kernel.org Signed-off-by: Chris Mason [ david: rework code; simplify comments and description ] Reviewed-by: Jose A. Perez de Azpillaga Reviewed-by: Zi Yan Reviewed-by: Baolin Wang Tested-by: Gregory Price (Meta) Reviewed-by: Gregory Price (Meta) Signed-off-by: David Hildenbrand (Arm) --- mm/khugepaged.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 6658c1da02903..83471396bd996 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -2273,6 +2273,7 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr, int nr_none = 0; bool do_readahead = false; bool readahead_done = false; + bool any_non_shmem_dirty = false; /* * MADV_COLLAPSE ignores shmem huge config, so do not check shmem @@ -2377,8 +2378,7 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr, * forcing writeback in loop. */ xas_unlock_irq(&xas); - if (!inode_is_open_for_write(mapping->host)) - filemap_flush(mapping); + any_non_shmem_dirty = true; result = SCAN_PAGE_DIRTY_OR_WRITEBACK; goto xa_unlocked; } else if (folio_test_writeback(folio)) { @@ -2712,6 +2712,10 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr, } out_put: folio_put(new_folio); + + /* Flush after unlocking all folios. */ + if (any_non_shmem_dirty && !inode_is_open_for_write(mapping->host)) + filemap_flush(mapping); out: VM_BUG_ON(!list_empty(&pagelist)); trace_mm_khugepaged_collapse_file(mm, new_pfn, index, addr, is_shmem, file, HPAGE_PMD_NR, result); -- 2.56.1