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=1795947047; b=odf7qqQjx0Z99UB0gyE7keo9nikcTv2UtG7Xxonq3exaTiipO91ZFQZxrTatV7bL/yLt SxEpG0oDklHZk4zrDm28uOrL21SRELqvahglnnODPmbg1l+Sp2whnswsQgMO0Eqj9rhLF ah7BmRCfw9/K+vv3a1PWXrehJlyokUcSdkfRD1kKh6PTNwSotA2CpMNe17AQWFuvUDM8u o2+yEeNC1GN5BAx+6B62Koqmu2VmAOrHqQ1W/sjjcLKKr9s/Ih6ZrXrn+ZLD6fgl45Rln zuA0UckQ+G3x1M63z9KF2inwTbp1Ci1DhdaH46ebXAkHAIQRCYJUBW+aTXHxkdUS3VA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795947047; 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=29VUeZuPzoXnU3Txbi/Efip5l6DmprOiuXwJtG/S2dg=; b=SpjV4Y2wvMdffvv9jWhh0Ic1idxajOiLnCO/eRGTSrF9i4RVCWft+8ccdVzfTnv5MR7V GAmYOX0vk+jrz8vbxzo/cy3yh+UyXfHq6yT6y4TYpYyctkCM90P9wSlFG3PpMB4Q+jcyu C/nGRmqb6Qxg799GJ4Xug7kOe68cFnQ7s8GKHP+1OeXGr5TICDvEbdxQgYm2S4lQtVsWZ r6MApQ4zfsUVePk0C+6hUJKDHL8QDO+Mv2kQvrIFtuEDStHx7dJFEVCB+W3ow8Shc70T7 vrhqgvvmLQPNNIAXv3DwJVN524Za+L8rTtspeo3+LfPILUP0o3D3LYnB1ZQJiaWCqMA== 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=Wlm3JFwm; 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 59B2C606A325; Sun, 29 Nov 2026 10:10:47 +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=Wlm3JFwm Received: by smtp.kernel.org (Postfix) with ESMTPSA id 290391F000C1; Sun, 29 Nov 2026 10:10:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795947046; bh=29VUeZuPzoXnU3Txbi/Efip5l6DmprOiuXwJtG/S2dg=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=Wlm3JFwm7ymjF361Y05cQWSssAJXrNQ+Ni794uP2d/J5/4nWEY5PS8jEaT8zXjsRN luXNXPqVSv5ijW31UfvAy7qcx319Ai1sMncjVyYptUm06GcB7ueSx3SGUrRXRQLSLx Kbjv6N0V1pqK7UjE47gEAptamV9zt3bpNlMyH2wNN6wWGs2HJPM9Dgs0x3eXAV/Sp3 /3Ub7WRm0hw3/cWiMsVhroqYtUiSTjhbbfshsgScttr3R8Fa78c3hQ5Mt2jGqcUbVN iZ8odvm843ICX09Uhv8oQE5eu0TM6F26WVSefCpoYpqdsoOJV2SZeLkv9JhmFoUcax IVxqimyLv/MeA== From: "David Hildenbrand (Arm)" Date: Sun, 29 Nov 2026 11:06:43 +0100 Subject: [PATCH 12/32] mm/khugepaged: check PMD support with the folios locked 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-12-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 , stable@vger.kernel.org X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3540; i=david@kernel.org; h=from:subject:message-id; bh=/+3g0ryI+u4SfjulfwG5rOkoVgmf91+xGLCyFx+/EgQ=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLK5f+rstjvwWjD3OU/l8YdbPCvTp/xctD7fIN+WV8Ri+ +eLy2//7yhlYRDjYJAVU2QJCr5v8PLlutpqNz5mmDmsTCBDGLg4BWAiwR6MDFdOMPXKipTIHF88 a/OVeRM0jl/cIr10V3T2zY3zMj2iGfYy/M97ar2Qn09k+5c5HYse8p9cEn1a6gdHmG9pwc21Jjs eR3MBAA== X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A From: Chris Mason A collapse that races with chattr +j on an ext4 file can store a PMD folio in a mapping whose maximum folio order no longer allows it, as the maximum folio order can change once the page cache is empty. ext4_change_inode_journal_flag() empties the page cache and then lowers the maximum folio order under the invalidate_lock. But collapsing only checks early, in file_thp_enabled() under the mmap_lock, and collapse_file() merely has a VM_WARN_ON_ONCE() that the same race triggers: madvise_collapse() ext4_change_inode_journal_flag() ================== ================================ mmap_read_lock() file_thp_enabled() mmap_read_unlock() filemap_invalidate_lock() truncate_pagecache() ext4_set_inode_mapping_order() filemap_invalidate_unlock() collapse_file() page_cache_sync_readahead() xas_store(new_folio) Consequently, on a nodelalloc mount, a write fault on that PMD folio runs out of journal credits, resulting in SIGBUS and an ext4 file system error. Let's fix it by checking again once we have the page cache folios locked, stabilizing the maximum supported folio order. A MADV_COLLAPSE that loses the race now fails with -EINVAL, like one that starts after the chattr. This has low urgency, as chattr +j requires CAP_SYS_RESOURCE. Fixes: 8a088263a097 ("mm/huge_memory: remove READ_ONLY_THP_FOR_FS from file_thp_enabled()") Cc: stable@vger.kernel.org Signed-off-by: Chris Mason [ david: rework code; adjust comment and description ] Signed-off-by: David Hildenbrand (Arm) --- mm/khugepaged.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index c25f732f2bf84..091e4136ba037 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -2300,13 +2300,6 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr, bool readahead_done = false; bool any_non_shmem_dirty = false; - /* - * MADV_COLLAPSE ignores shmem huge config, so do not check shmem - * - * TODO: once shmem always calls mapping_set_large_folios() on its - * mapping, the shmem check can be removed. - */ - VM_WARN_ON_ONCE(!is_shmem && !mapping_pmd_folio_support(mapping)); VM_WARN_ON_ONCE(start & (HPAGE_PMD_NR - 1)); result = alloc_charge_folio(&new_folio, mm, cc, HPAGE_PMD_ORDER); @@ -2542,9 +2535,22 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr, */ try_to_unmap_flush(); - if (result == SCAN_SUCCEED && nr_none && - !shmem_charge(mapping->host, nr_none)) - result = SCAN_FAIL; + if (result == SCAN_SUCCEED) { + /* + * A filesystem can lower the maximum folio order only once the + * page cache is empty. Recheck now that all old folios are + * locked and still in the page cache. + * + * MADV_COLLAPSE ignores shmem huge config, so do not check shmem. + * + * TODO: once shmem always calls mapping_set_large_folios() on its + * mapping, the shmem check can be removed. + */ + if (!is_shmem && !mapping_pmd_folio_support(mapping)) + result = SCAN_FAIL; + else if (nr_none && !shmem_charge(mapping->host, nr_none)) + result = SCAN_FAIL; + } if (result != SCAN_SUCCEED) { nr_none = 0; goto rollback; -- 2.56.1