From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=115.124.30.130 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796204554; b=I182pNstSV0QSBUJlQRvwRHBWo/QT1wK/MtwNdATYHWJRnRHDkK8YfavVn0zn1fQp4zx vi75SOeh8NFeVBHlv34kX5OxJiv1otVsCGzIFLyMOox5ODFC7DIT6XP1vM/w/OhfWFg8V EG18PMnm/51j2nPotUAx0Da9mLsyFHs1dB+FZIXQEW5Ax8NYse/f6AUUTpnqo0FeJtqwm AfX7PAC4JyBdcbtCiwVgvuQMNgCKNuoiKO07OGDJiaGM61Lpzy9ZIHdp1Gjokhf2AW34U NYdyiZRI/hBJRMgKzpye342UcuYF8oS7hS1aghQ9LzPN9xZP/7+qNmEx+3f0lZl+slQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796204554; h=DMARC-Filter:DKIM-Signature:X-Alimail-AntiSpam:Received:Message-ID: Date:MIME-Version:User-Agent:Subject:To:Cc:References:From: In-Reply-To:Content-Type:Content-Transfer-Encoding; bh=6x1YPe8o5YcqKC9fFHwZdJR0V2GcLuVx+7FtSHovUnM=; b=xIAiQfYPvm4aHxP8YHx+8DK8ECTmqIuQYavv4BE3w0ikvMhPpsQ/CEoq+0GpwDJTCJVc ueVAiuKv35/wQRIBkSlz9PlQ/0NyhGGNOoiItEJk01zY0pCnaZqsrMIli8HsCnpnZlFmg JACOntGJqprS5p4qPw2WAx3vswqclSGm/Edu+1C0kVIXFs6HcQMMiSwVuIEyT6knxnkaP aX8OpCR2Ll56kH9/5OrURkA0Jnty2x4QpOk8B0lmwVYyOzQsvCC3E9IVH+bj3R8XL1O3P OHUeX2RDOk2fPGbscBG0HjDjrkRO4YSJNn6gFPNrCdIapvj/nRkp03hdkzcVUjoMVyg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass header.d=linux.alibaba.com header.i=@linux.alibaba.com header.a=rsa-sha256 header.s=default header.b=ZoPm2Q2v; arc=none smtp.remote-ip=115.124.30.130 Received: from out30-130.freemail.mail.aliyun.com (out30-130.freemail.mail.aliyun.com [115.124.30.130]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A6893606A31A; Wed, 2 Dec 2026 09:42:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A6893606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.a=rsa-sha256 header.s=default header.b=ZoPm2Q2v DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1796204549; h=Message-ID:Date:MIME-Version:Subject:To:From:Content-Type; bh=6x1YPe8o5YcqKC9fFHwZdJR0V2GcLuVx+7FtSHovUnM=; b=ZoPm2Q2vhW+sPF+keu1sRiM6smRKDRR5RYt3bU574og48XD/Jh7SZ9/R7LpJp4p3ce6U94IRN7MXnaivypmz1MzElNOUrvBh29sy/0YACraTzuKSifa16FfmS5Zmn/Yl3TnP2mrOIy/RfVxZPfZ02k708wZOB8m6qZq2+Vwb9ig= X-Alimail-AntiSpam:AC=PASS;BC=-1|-1;BR=01201311R311e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=maildocker-contentspam033037026112;MF=baolin.wang@linux.alibaba.com;NM=1;PH=DS;RN=81;SR=0;TI=SMTPD_---0XG9R0E._1796204540; Received: from 30.74.144.133(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0XG9R0E._1796204540 cluster:ay36) by smtp.aliyun-inc.com; Wed, 02 Dec 2026 17:42:24 +0800 Message-ID: Date: Wed, 2 Dec 2026 17:42:20 +0800 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 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 12/32] mm/khugepaged: check PMD support with the folios locked To: "David Hildenbrand (Arm)" , Hugh Dickins , 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 , Chris Mason , stable@vger.kernel.org References: <20261129-fixes_core_3-v1-0-f66094116801@kernel.org> <20261129-fixes_core_3-v1-12-f66094116801@kernel.org> From: Baolin Wang In-Reply-To: <20261129-fixes_core_3-v1-12-f66094116801@kernel.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 11/29/26 6:06 PM, David Hildenbrand (Arm) wrote: > 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; I sent a patch[1] earlier to clean up the shmem comments. While you are touching this part, could you remove those obsolete comments and check for shmem? [1] https://lore.kernel.org/all/12bac4614aa4b37490e92051d484f4f2cb215076.1789701677.git.baolin.wang@linux.alibaba.com/