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=1795947027; b=Tuh1SJvijZyc/lVVDkpTYdiJQ3cafELnlX4T0omOvzm17SfUR/3DsEVfBpnQuWLteSxB lNfMjd0/HZNve89Ymp0oTO7s3ZoDh7p3fJkvCZmS55Iq6qrnEg+jAO8dd2/tg35OaMxkl qd2lz6EMnYEBbMjP+ztDU0fIEo7KKYCtzbheb7/nqK3/uiCiIgxKUCXd/Mi5JqnE/uSvF 52wEmb5o5v6Bkn5TfZg8ZObGerOPuW0+5dWwH9pndWuOjwubjwp7yt97rcovwelK47+r6 7lEyuWS6MqFxlehFruIjsDwHCCd8862jhSP2e2e5A37PySgvO9Vklt9VnCqxFS8mCKw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795947027; 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=sKQ2WyZwDFgb1nh3LN0l98bh7qT7addXljv/+2AHrNU=; b=hXWTk8KQOOeNasZXgoOvhtEJb15WY86PeYaVMm0mWkrAFyV0T3qm48ohWGW3R30OSdk5 r8oVRyBmYoIng3qf44lelGtIpGXgk5R0z9lQgXf2X688aVjC3CcQS3FGSLHISgL2dimyj XGFu5VZ82WO0Xn87GERvDJC0hITi0WGS2TWtha4r2xFEmlSMWpcSaSpP9F/nZmZkU6fpX pmnTgOqlOqZ1O6hfLVK9zpUEwqDqkmhHKswOSU+cgz2Ty7jqV1v7JQBNj8f+4Bf5vtZM2 +NH767Tq/ZXm/9FcvVCv2XjaQkVoE/lTpnwBqNW5jMu2AsDTKjTX+iBSkZ0nSbHrrwQ== 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=hLQZyY1H; 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 6C4DC606A31C; Sun, 29 Nov 2026 10:10:26 +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=hLQZyY1H Received: by smtp.kernel.org (Postfix) with ESMTPSA id CCBC51F00899; Sun, 29 Nov 2026 10:10:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795947026; bh=sKQ2WyZwDFgb1nh3LN0l98bh7qT7addXljv/+2AHrNU=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=hLQZyY1HR6qeL8T1nZf+0ZO5fVU6AYOJ7ri/wvpcbKzPno9DH7sGgdpKhtLFt8Mgn 3x0vNcvrr27vqxrs44Xm+bZWw+7xN8HFWILl3Q9hIaoDXv5h7IhIxZ+m1pKEJ0Pn9k n+ZNYnhIITVbxYF+hn2tl5Co/NUVTXZC32WPLfRjy/L2ValujGECK+u3/9k6xup8qP WVml+lnUaJrr7b8+UgTocdsmcJlX9GqrH3SV3L0w4xAPVh85YKBlbMI6JHuORZVhfC Q/qhrSgXpxMH6VqczD5SXwXwjksT70zEz6jUgm0t0Ar/szvrU/aP95eXDlGM+zuetz TpzQhp076feFw== From: "David Hildenbrand (Arm)" Date: Sun, 29 Nov 2026 11:06:42 +0100 Subject: [PATCH 11/32] mm/khugepaged: store new folio before adding it to LRU X-Mailing-List: linux-kernel@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: <20261129-fixes_core_3-v1-11-f66094116801@kernel.org> References: <20261129-fixes_core_3-v1-0-f66094116801@kernel.org> In-Reply-To: <20261129-fixes_core_3-v1-0-f66094116801@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 X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=2598; i=david@kernel.org; h=from:subject:message-id; bh=cF4GsH7tQ+SNnPAtRL7jLTYA4fbXoT3PMPVyB6vVk1g=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLK5f+pMe95amfhqnYxdkO7UeQXTskoXnjHW+jl7vgDjX qF99SGiHaUsDGIcDLJiiixBwfcNXr5cV1vtxscMM4eVCWQIAxenAEzkPQcjw+N7VY2LXYrlf1uu /rSM//vCDys8TT9fU7gqe1Y9Uenp+yyGf2aWffzXl62PW/ek4GLqXNO1DY+Wb7hvE3Wgddtzo6i TUXwA 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 ] 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 708a73f45dd81..c25f732f2bf84 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -2647,7 +2647,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); @@ -2655,6 +2654,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