From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B01FED3942E for ; Thu, 2 Apr 2026 14:36:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0F77D6B0088; Thu, 2 Apr 2026 10:36:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0CF846B0089; Thu, 2 Apr 2026 10:36:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EFFF36B008A; Thu, 2 Apr 2026 10:36:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id DCDCB6B0088 for ; Thu, 2 Apr 2026 10:36:09 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 7530C14070D for ; Thu, 2 Apr 2026 14:36:09 +0000 (UTC) X-FDA: 84613865658.05.83A84DC Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by imf26.hostedemail.com (Postfix) with ESMTP id B6C75140016 for ; Thu, 2 Apr 2026 14:36:07 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=gZkI1qKF; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf26.hostedemail.com: domain of david@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=david@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1775140567; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=M4lioeZV/Qgvpz2qdwy0tvOL0tbHRvWqrRW18fGnoQk=; b=4d0q5msPiz4EYWm5Qjw3MEORMNhefXVNZaW7/YvVpfETWwh0SzuedOWDD0mPbVIHCZItkP ox+ohD9wH/kfi+5Q0JdQWufxNb/46PlQwpW5ZR7/hDBeBNkq+7wgBBXb+9EMxQbI8gRCD9 8PLLtjH+x27qCI+Ded4AuQVf7cxQVn0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1775140567; a=rsa-sha256; cv=none; b=q7pFLDsR0ce8fpKwKNhpXxbYTmooJijP23AymZROPTJdjuGthVLSv7YG9AJ6a/v7tZL48T a0LLiKZaFr3m+KEi1gUNQM15FYpY44Fgu3VnG5DISla+ztyPAaTWzfZ+205C/dYBs30hCp 7xBGS6aKwrYXSYoqr0f0d17KlqbnkbA= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=gZkI1qKF; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf26.hostedemail.com: domain of david@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=david@kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by tor.source.kernel.org (Postfix) with ESMTP id A5B5F60121; Thu, 2 Apr 2026 14:36:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 26644C116C6; Thu, 2 Apr 2026 14:35:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1775140566; bh=7SCfbCoJtq5q0RD/JJGIsXRe60Kg7wtGU58y09PXT+4=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=gZkI1qKFjAV9FAxkoNjUA/dEdVQSCBB10/ocV6e+opRkjrRtkhI+Lk9iAIFfL6/PU f56d2pMSIHuxBL39mkIEw5Q/9Eri0du3hh7BNqeoAp9NgH1bO9WisKrkpdNLfXjOXS EMiROD7uRd1E2Zt5GXe94xzEl0snqaADC3hl57Dly5KeZsEzSjDudfeplFfjFAX6Ui ABWg7ae/I4DwKF/Vuope1cyerZbH/ZP+T16UtJyrhTcBxbgABci2BDTXv/DgkH9uDt aEkG6BpCQ0am7WfrWTBvR6XFtj/CuYsb89Tzj/VN+Y1++Ao6a1uKCvko07pbhhWiHF R2SDTi52RGyjQ== Message-ID: Date: Thu, 2 Apr 2026 16:35:57 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v1 03/10] mm: fs: remove filemap_nr_thps*() functions and their users To: Zi Yan Cc: "Lorenzo Stoakes (Oracle)" , "Matthew Wilcox (Oracle)" , Song Liu , Chris Mason , David Sterba , Alexander Viro , Christian Brauner , Jan Kara , Andrew Morton , Baolin Wang , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Shuah Khan , linux-btrfs@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org References: <20260327014255.2058916-1-ziy@nvidia.com> <20260327014255.2058916-4-ziy@nvidia.com> <53cf6157-58b1-4539-a276-2486e8796c57@lucifer.local> <25899F67-955D-44DC-935E-D7F234BD335A@nvidia.com> <44DEB48D-589B-493D-A278-4896BDB58564@nvidia.com> From: "David Hildenbrand (Arm)" Content-Language: en-US Autocrypt: addr=david@kernel.org; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RM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e EPcA/4NaBQJonNqrBQkmWAihAAoJEE3eEPcA/4NaKtMQALAJ8PzprBEXbXcEXwDKQu+P/vts IfUb1UNMfMV76BicGa5NCZnJNQASDP/+bFg6O3gx5NbhHHPeaWz/VxlOmYHokHodOvtL0WCC 8A5PEP8tOk6029Z+J+xUcMrJClNVFpzVvOpb1lCbhjwAV465Hy+NUSbbUiRxdzNQtLtgZzOV Zw7jxUCs4UUZLQTCuBpFgb15bBxYZ/BL9MbzxPxvfUQIPbnzQMcqtpUs21CMK2PdfCh5c4gS sDci6D5/ZIBw94UQWmGpM/O1ilGXde2ZzzGYl64glmccD8e87OnEgKnH3FbnJnT4iJchtSvx yJNi1+t0+qDti4m88+/9IuPqCKb6Stl+s2dnLtJNrjXBGJtsQG/sRpqsJz5x1/2nPJSRMsx9 5YfqbdrJSOFXDzZ8/r82HgQEtUvlSXNaXCa95ez0UkOG7+bDm2b3s0XahBQeLVCH0mw3RAQg r7xDAYKIrAwfHHmMTnBQDPJwVqxJjVNr7yBic4yfzVWGCGNE4DnOW0vcIeoyhy9vnIa3w1uZ 3iyY2Nsd7JxfKu1PRhCGwXzRw5TlfEsoRI7V9A8isUCoqE2Dzh3FvYHVeX4Us+bRL/oqareJ CIFqgYMyvHj7Q06kTKmauOe4Nf0l0qEkIuIzfoLJ3qr5UyXc2hLtWyT9Ir+lYlX9efqh7mOY qIws/H2t In-Reply-To: <44DEB48D-589B-493D-A278-4896BDB58564@nvidia.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: B6C75140016 X-Stat-Signature: pyfbq9417qhqfga1n9mk3ajbzx1ggswb X-Rspam-User: X-Rspamd-Server: rspam02 X-HE-Tag: 1775140567-330000 X-HE-Meta: U2FsdGVkX19vECElMK6JiCo6KCOBd7mFgWw6pzx55hA0YOkDxOBs1iVlwMqPSZUpJGuc00FMRPFEdQVSqnYS7w9zvPbXN5QZvUcMEKRsM2a4DO5L65Jbz25up1LO1t4v42Fx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2r8noXy92KQAQ+s2XBSJVy2qJ7y6IFg6Hib8kUJAEVoDzKCpyoPc15BhzD5r5/HWh2m+DXNzFQJfUL0w0ezrKA8o16kGW/Wgi9l/XpdmlbwCU1s71o= Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On 4/1/26 22:33, Zi Yan wrote: > On 1 Apr 2026, at 15:15, David Hildenbrand (Arm) wrote: > >> On 4/1/26 17:32, Zi Yan wrote: >>> >>> >>> Let me think. >>> >>> do_dentry_open() -> file_get_write_access() -> get_write_access() bumps >>> inode->i_writecount atomically and it turns inode_is_open_for_write() >>> to true. Then, do_dentry_open() also truncates all pages >>> if filemap_nr_thps() is not zero. This pairs with khugepaged’s first >>> filemap_nr_thps_inc() then inode_is_open_for_write() to prevent opening >>> a fd with write when there is a read-only THP. >>> >>> After removing READ_ONLY_THP_FOR_FS, khugepaged only creates read-only THPs >>> on FSes with large folio support (to be precise THP support). If a fd >>> is opened for write before inode_is_open_for_write() check, khugepaged >>> will stop. It is fine. But if a fd is opened for write after >>> inode_is_open_for_write() check, khugepaged will try to collapse a read-only >>> THP and the fd can be written at the same time. >> >> Exactly, that's the race I mean. >> >>> >>> I notice that fd write requires locking the to-be-written folio first >>> (I see it from f_ops->write_iter() -> write_begin_get_folio() and assume >>> f_ops->write() has the same locking requirement) and khugepaged has already >>> locked the to-be-collapsed folio before inode_is_open_for_write(). So if the >>> fd is opened for write after inode_is_open_for_write() check, its write >>> will wait for khugepaged collapse and see a new THP. Since the FS >>> supports THP, writing to the new THP should be fine. >>> >>> Let me know if my analysis above makes sense. If yes, I will add it >>> to the commit message and add a succinct comment about it before >>> inode_is_open_for_write(). >> >> khugepaged code is the only code that replaces folios in the pagecache >> by other folios. So my main concern is if that is problematic on >> concurrent write access. > > folio_split() does it too, although it replaces a large folio with > a bunch of after-split folios. It is a kinda reverse process of > collapse_file(). Right. You won't start looking at a small folio and suddenly there is something larger. > > >> >> You argue that the folio lock is sufficient. That's certainly true for >> individual folios, but I am more concerned about the replacement part. > > For the replacement part, both old and new folios are locked during > the process. A parallel writer uses filemap_get_entry() to get the folio > from mapping, but all of them check folio->mapping after acquiring the > folio lock, except mincore_page() which is a reader. A writer can see > either old folio or new folio during the process, but > > 1. if it sees the old one, it waits on the old folio lock. After > it acquires the lock, it sees old_folio->mapping is NULL, no longer > matches the original mapping. The writer will try again. > > 2. if it sees the new one, it waits on the new folio lock. After > it acquires the lock, it sees new_folio->mapping matches the > original mapping and proceeds to its writes. > > 3. if khugepaged needs to do a rollback, the old folio will stay > the same and the writer will see the old one after it gets the old > folio lock. I am primarily wondering about what would happen if someone traverses the pageache, and found+processed 3 small folios. Suddenly there is a large folio that covers the 3 small folios processes before. I suspect that is fine, because the code likely had to deal with concurrent truncation+population if relevant locks are dropped already. Just raising it. > >> >> I don't have anything concrete, primarily just pointing out that this is >> a change that might unlock some code paths that could not have been >> triggered before. > > Yes, the concern makes sense. > > BTW, Claude is trying to convince me that even inode_is_open_for_write() > is unecessary since 1) folio_test_dirty() before it has > made sure the folio is clean, 2) try_to_unmap() and the locked folio prevents > further writes. > > But then we find a hole between folio_test_dirty() and > try_to_unmap() where a write via a writable mmap PTE can dirty the folio > after folio_test_dirty() and try_to_unmap(). To remove that hole, > the “if (!is_shmem && (folio_test_dirty(...) || folio_test_writeback(...))” > needs to be moved after try_to_unmap(). With that, all to-be-collapsed > folios will be clean, unmapped, and locked, where unmapped means > writes via mmap need to fault and take the folio lock, locked means > writes via mmap and write() need to wait until the folio is unlocked. > > Let me know if my reasoning makes sense. It is definitely worth the time > and effort to ensure this patchset does not introduce any unexpected race > condition or issue. Makes sense. Please clearly spell out that there is a slight change now, where we might be collapsing after the file has been opened for write. Then you can document that the folio locks should be protecting us from that. Implying that collapsing in writable files could likely "easily" done in the future. -- Cheers, David