From: Baolin Wang <baolin.wang@linux.alibaba.com>
To: Jan Kara <jack@suse.cz>, Qiliang Yuan <odys.yuan@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
David Hildenbrand <david@kernel.org>,
Lorenzo Stoakes <ljs@kernel.org>,
"Liam R. Howlett" <liam@infradead.org>,
Vlastimil Babka <vbabka@kernel.org>,
Mike Rapoport <rppt@kernel.org>,
Suren Baghdasaryan <surenb@google.com>,
Michal Hocko <mhocko@suse.com>, Hugh Dickins <hughd@google.com>,
"Matthew Wilcox (Oracle)" <willy@infradead.org>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
linux-fsdevel@vger.kernel.org
Subject: Re: [PATCH] mm/shmem: remove truncated folios from the page cache in batches
Date: Thu, 5 Nov 2026 17:43:55 +0800 [thread overview]
Message-ID: <c064d145-e085-4380-aa95-286a4bc396a8@linux.alibaba.com> (raw)
In-Reply-To: <dru32b7k62tbur7kmk6vv52zd6jnglnzzz4pvhlsqez6smcjip@gjuh6fud4nir>
On 11/3/26 5:46 AM, Jan Kara wrote:
> On Thu 29-10-26 23:44:57, Qiliang Yuan wrote:
>> truncate_inode_pages_range() takes the folios it finds out of the page
>> cache with delete_from_page_cache_batch(), which takes the inode's
>> i_lock and the i_pages lock once for each batch of up to
>> FOLIO_BATCH_SIZE folios.
>>
>> shmem_undo_range() calls truncate_inode_folio() on each folio instead,
>> so filemap_remove_folio() takes and drops both locks once per folio.
>> Truncating, punching or freeing a tmpfs file or memfd made of small
>> folios, such as a large /dev/shm segment, pays for that on every page.
>>
>> Clean each folio up as truncate_inode_folio() does, collect the ones to
>> remove and take them out with delete_from_page_cache_batch(), which
>> needs truncate_cleanup_folio() outside mm/truncate.c. Swap entries, and
>> the folios kept when undoing a failed fallocate(), stay out of the
>> batch. Dropping an 8 GiB file of 4K folios, median of five runs,
>> averaged over two boots of each kernel, in a 32 vCPU VM:
>>
>> before after
>> ftruncate() to 0 1163 ms 913 ms
>> hole punch 1155 ms 914 ms
>> close() of a memfd 1161 ms 914 ms
>> unlink() 1162 ms 916 ms
>>
>> Files of PMD-sized folios take 20-25 ms with or without this change.
>>
>> Signed-off-by: Qiliang Yuan <odys.yuan@gmail.com>
>> ---
>> To see it, fill and remove a large /dev/shm file:
>>
>> fallocate -l 8G /dev/shm/f && time rm /dev/shm/f
>
> Looks sensible. Just one nit below. Otherwise feel free to add:
>
> Reviewed-by: Jan Kara <jack@suse.cz>
>
>> @@ -1352,6 +1352,9 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, uoff_t lend,
>> index = start;
>> while (index < end && find_lock_entries(mapping, &index, end - 1,
>> &fbatch, indices)) {
>> + struct folio_batch remove;
>> +
>> + folio_batch_init(&remove);
>> for (i = 0; i < folio_batch_count(&fbatch); i++) {
>> folio = fbatch.folios[i];
>>
>> @@ -1363,9 +1366,16 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, uoff_t lend,
>> continue;
>> }
>>
>> - if (!unfalloc || !folio_test_uptodate(folio))
>> - truncate_inode_folio(mapping, folio);
>> - folio_unlock(folio);
>> + if (!unfalloc || !folio_test_uptodate(folio)) {
>> + truncate_cleanup_folio(folio);
>> + folio_batch_add(&remove, folio);
>> + }
>> + }
>> + delete_from_page_cache_batch(mapping, &remove);
>> + for (i = 0; i < folio_batch_count(&fbatch); i++) {
>> + folio = fbatch.folios[i];
>> + if (!xa_is_value(folio))
>
> You can move this loop after folio_batch_remove_exceptionals() and drop
> this condition.
+1. With this addressed,
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
next prev parent reply other threads:[~2026-11-05 9:43 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-30 3:44 Qiliang Yuan
2026-11-02 21:46 ` Jan Kara
2026-11-05 9:41 ` David Hildenbrand (Arm)
2026-11-05 9:43 ` Baolin Wang [this message]
2026-11-03 8:28 ` David Hildenbrand (Arm)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=c064d145-e085-4380-aa95-286a4bc396a8@linux.alibaba.com \
--to=baolin.wang@linux.alibaba.com \
--cc=akpm@linux-foundation.org \
--cc=david@kernel.org \
--cc=hughd@google.com \
--cc=jack@suse.cz \
--cc=liam@infradead.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=mhocko@suse.com \
--cc=odys.yuan@gmail.com \
--cc=rppt@kernel.org \
--cc=surenb@google.com \
--cc=vbabka@kernel.org \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®