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=1796388829; b=AzEthzdBlOBQq6lQ1wRmfKu/Ou7cSLDAo5WuzUWdSMCgQqPoXBAwN7Nbugaxp3UoUmgY D+brWL6wm8enxylMm2ITD/6ZKt8hSqLvTb60iiM5orSoqfcvjEeDX347ZxjZe9PUg9non HM+y2xuOWW6swBEWWGeti16rX8QwQuG2MAuMBTXrFY8jIBuD//c3GA6r4gmFMl7Mp+EIM 8I4uEgqNA15KPExhkuKY51grD0bONVVDff8UylvZ8cB4Tv3d5LCrPwmryNGQqNmi9EvM+ /FG5Q4kHwBCLxmqLUcChz/maixdh2KP/nqBfSRQ5rGWjND+BTh0FNPS+uolh5lS88nQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796388829; 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=njV7T8HBFyiA43Q+21GXydbu2dny9gs8tAc4ITjEC/0=; b=Dg+YZeo/Ji+KQdFl27UiFm0dYYFiKjAagSnNWPKIaMKM6T65P5isJeuFDhFe+U2Im757 m7rlJAEuCwq3KhJjrw+LXsjFU8MBWriUgE40g3XJEdPD3meCuO+gA8vL1IyC/6q3Z/gbg J+girDgygiyj9/23xdU4aJBr6Z/HWS4oUJopBkxeIUZYF3BzLycA9c2CduzvmQbCERS9D fSxbpU922AGH+sZo44WMtKb/9uSweenta0yOXQf2XisEY2aWO2CJiwh5eszXMsEB3ix7w n5gvulGHFH8f6+AM8xtGahMArAxuQniX9vcF8dSER/Ts80IfR13Zqn0fYKMFi9JFy8Q== 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=oos0QTgF; 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 2A2A1606A32C; Fri, 4 Dec 2026 12:53:49 +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=oos0QTgF Received: by smtp.kernel.org (Postfix) with ESMTPSA id C73FE1F00A08; Fri, 4 Dec 2026 12:53:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796388828; bh=njV7T8HBFyiA43Q+21GXydbu2dny9gs8tAc4ITjEC/0=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=oos0QTgF69XAhwyjJa/wlR9kjgMJ8iO40f5mtx9ObgZ641GlCC4wfTAqZJ3UU61iq MrIBJ/QNcyLpC5Tali++B/tWfiZsuePyi3Fd5tG9Mf6xrurSJdV1IoJaUQWQLBevFf q50pucsr+kjLgh8+h7fn9ODenGP3dN0pBJNRhGSS7As6t7kY+ZEObvuzjoH/Fo7PoC tMX/CT2MVVeymxHydiGejnzQtDiRV+/B5h9LKzK/qVpIDd2KJHAlyAoUN6VWMHBLHj cbHpUgagFku+uWLcLIcCff769oWsE1adm1qTLi5ff0isjL+yZaofIKks7Z6ZAoEhNp zwj1b3olZvemA== From: "David Hildenbrand (Arm)" Date: Fri, 04 Dec 2026 13:50:02 +0100 Subject: [PATCH v2 11/31] mm/khugepaged: store new folio before adding it to LRU X-Mailing-List: linux-rt-devel@lists.linux.dev 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-11-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 , "Jose A. Perez de Azpillaga" X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2809; i=david@kernel.org; h=from:subject:message-id; bh=skp0GcgSGxJle9G8EBapdzX+hrgqDmQmyk2aEt9Zgps=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLKFtv0/Pu/AuZV/9kjd1ptz/LblT4X6oNwl3p4HNoTI3 pzHvK1qcUcpC4MYB4OsmCJLUPB9g5cv19VWu/Exw8xhZQIZwsDFKQATeZPM8N/lxyXBuQLW4lvV t6+6FhlQltr385yP7NlfpZ9vrKjao/WYkeFyevK9pKcpzbFlZgl53tPMnY/Zps6Tu5rxaLqNhuE WVT4A X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A From: Chris Mason collapse_file() adds the new folio to the LRU before adding it to the page cache. If SHM_LOCKed, the folio will be added to the unevictable LRU. A concurrent SHM_UNLOCK walks the page cache to make all folios evictable again, and can miss the new folio: collapse_file() SHM_UNLOCK =============== ========== folio_add_lru(new_folio) /* unevictable */ shmem_unlock_mapping() /* misses new_folio */ xas_store(&xas, new_folio) Consequently, if SHM_UNLOCK races with a collapse in a SHM_LOCKed SysV shared memory segment, the new folio can get stranded on the unevictable LRU. Let's improve it by adding the new folio to the LRU only after adding it to the page cache. There is a more fundamental race that must be sorted out to fix this completely: check_move_unevictable_folios() skips folios that are not on the LRU without taking the lruvec lock, while lru_add() checks evictability before marking the folio as being on the LRU. That affects all folios that are added to the LRU, and requires a more generic fix. This has low urgency, as it only strands a folio on the unevictable LRU. No need to CC stable. Fixes: a2e17cc2efc7 ("mm/khugepaged: maintain page cache uptodate flag") Signed-off-by: Chris Mason [ david: simplify description and comment ] Reviewed-by: Jose A. Perez de Azpillaga Reviewed-by: Zi Yan Reviewed-by: Baolin Wang Reviewed-by: Gregory Price (Meta) Signed-off-by: David Hildenbrand (Arm) --- mm/khugepaged.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 2e8e5b94ce8fc..a2d13d989a558 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -2648,7 +2648,6 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr, if (is_shmem) folio_mark_dirty(new_folio); - folio_add_lru(new_folio); /* Join all the small entries into a single multi-index entry. */ xas_set_order(&xas, start, HPAGE_PMD_ORDER); @@ -2656,6 +2655,13 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr, WARN_ON_ONCE(xas_error(&xas)); xas_unlock_irq(&xas); + /* + * If SHM_LOCKed, the folio will be added to the unevictable LRU. Do + * that after adding it to the page cache, so a concurrent SHM_UNLOCK + * reliably finds it to make it evictable again. + */ + folio_add_lru(new_folio); + /* * Remove pte page tables, so we can re-fault the page as huge. A * caller that wants the PMD mapped now is told to go and do that. -- 2.56.1