From: Usama Arif <usama.arif@linux.dev>
To: Jan Kara <jack@suse.cz>
Cc: Usama Arif <usama.arif@linux.dev>,
Andrew Morton <akpm@linux-foundation.org>,
willy@infradead.org, david@kernel.org, axboe@kernel.dk,
Christoph Hellwig <hch@lst.de>,
hannes@cmpxchg.org, jlayton@kernel.org, brauner@kernel.org,
tz2294@columbia.edu, ritesh.list@gmail.com,
chentaotao@didiglobal.com, ziy@nvidia.com, bigeasy@linutronix.de,
liam@infradead.org, vbabka@kernel.org, rppt@kernel.org,
mhocko@suse.com, apopple@nvidia.com, byungchul@sk.com,
gourry@gourry.net, joshua.hahnjy@gmail.com,
matthew.brost@intel.com, ying.huang@linux.alibaba.com,
clrkwllms@kernel.org, rostedt@goodmis.org, linux-mm@kvack.org,
linux-fsdevel@vger.kernel.org, linux-rt-devel@lists.linux.dev,
kas@kernel.org, riel@surriel.com, shakeel.butt@linux.dev,
kernel-team@meta.com
Subject: Re: [PATCH 4/4] mm/fadvise: drop folios under writeback on POSIX_FADV_DONTNEED
Date: Fri, 4 Dec 2026 09:48:47 -0800 [thread overview]
Message-ID: <20261204174848.955248-1-usama.arif@linux.dev> (raw)
In-Reply-To: <whn4cxqhnpqrmctzikfqjmjckt4elijgma3ulj7smxyd6ta5u6@pws3o7sb3bey>
On Thu, 3 Dec 2026 10:58:15 +0100 Jan Kara <jack@suse.cz> wrote:
> On Wed 02-12-26 06:24:39, Usama Arif wrote:
> > POSIX_FADV_DONTNEED starts writeback on dirty folios but skips folios
> > under writeback when invalidating. With large folios one writeback bio
> > can hold hundreds of megabytes, none of which leaves writeback until all
> > of it completes, so on XFS DONTNEED right after writing a new 256 MiB
> > file left all of it cached. Syncing first avoids this but blocks the
> > caller, and RWF_DONTCACHE does not help writers using a shared mapping.
> >
> > Mark the unmapped dirty folios in the range dropbehind before starting
> > writeback, and the unmapped writeback folios after it, so that writeback
> > completion drops them as it does for RWF_DONTCACHE writes. Marking the
> > dirty ones before submission lets their writeback complete in task
> > context, where they can be dropped. Folios that were already under
> > writeback and complete in interrupt context stay cached.
> >
> > Skip the folio extending past EOF, which an appender calling DONTNEED
> > after each write would otherwise have to read back. Only do this for
> > regular files on filesystems that support RWF_DONTCACHE: those are
> > prepared to drop folios at writeback completion, and unlike block
> > devices, regular files rarely dirty an unmapped folio without its lock,
> ^^^^^^^^
> This doesn't put peace into my mind. So we "sometimes" corrupt the
> WB_DONTCACHE_DIRTY counter. Not good and I'm not sure why you thought this
> is good enough. See below for more details.
>
> > which would race with the WB_DONTCACHE_DIRTY accounting.
> >
> > On XFS and ext4, new files and overwrites, written with write() or
> > through a shared mapping, were then dropped within a second, apart from
> > the EOF folio; before, most stayed cached.
> >
> > Signed-off-by: Usama Arif <usama.arif@linux.dev>
>
> ...
>
> > +/*
> > + * Folios under writeback cannot be invalidated. Mark the unmapped dirty and
> > + * writeback ones dropbehind so that writeback completion drops them, as for
> > + * RWF_DONTCACHE writes. Only filesystems that support those can do that.
> > + */
> > +static void fadvise_mark_dropbehind(struct address_space *mapping,
> > + pgoff_t start, pgoff_t end, xa_mark_t tag)
> > +{
> > + loff_t isize = i_size_read(mapping->host);
> > + struct folio_batch fbatch;
> > + pgoff_t index = start;
> > + unsigned int i;
> > +
> > + folio_batch_init(&fbatch);
> > + while (filemap_get_folios_tag(mapping, &index, end, tag, &fbatch)) {
> > + for (i = 0; i < folio_batch_count(&fbatch); i++) {
> > + struct folio *folio = fbatch.folios[i];
> > +
> > + /* Don't mark the EOF folio, appenders write it again */
> > + if (folio->index < start ||
> > + folio_next_index(folio) - 1 > end ||
> > + folio_next_pos(folio) > isize)
> > + continue;
> > + if (folio_test_dropbehind(folio) ||
> > + !folio_trylock(folio))
> > + continue;
> > + if (folio->mapping == mapping && !folio_mapped(folio) &&
> > + (folio_test_dirty(folio) ||
> > + folio_test_writeback(folio)))
> > + folio_mark_dropbehind(folio);
> > + folio_unlock(folio);
> > + }
> > + folio_batch_release(&fbatch);
> > + cond_resched();
> > + }
> > +}
> > +
> > int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
> > {
> > struct inode *inode;
> > @@ -37,6 +75,7 @@ int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
> > pgoff_t start_index;
> > pgoff_t end_index;
> > unsigned long nrpages;
> > + bool dropbehind;
> >
> > inode = file_inode(file);
> > if (S_ISFIFO(inode->i_mode))
> > @@ -111,8 +150,6 @@ int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
> > spin_unlock(&file->f_lock);
> > break;
> > case POSIX_FADV_DONTNEED:
> > - filemap_flush_range(mapping, offset, endbyte);
> > -
> > /*
> > * First and last FULL page! Partial pages are deliberately
> > * preserved on the expectation that it is better to preserve
> > @@ -135,14 +172,27 @@ int generic_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
> > * file cache which is not what was asked.
> > */
> > if (end_index == 0)
> > - break;
> > -
> > - end_index--;
> > + start_index = 1;
> > + else
> > + end_index--;
> > }
> >
> > + dropbehind = end_index >= start_index &&
> > + S_ISREG(inode->i_mode) &&
> > + (file->f_op->fop_flags & FOP_DONTCACHE);
> > + if (dropbehind)
> > + fadvise_mark_dropbehind(mapping, start_index, end_index,
> > + PAGECACHE_TAG_DIRTY);
>
> Why do you mark dirty folios as dropbehind here? Isn't that unnecessarily
> expensive? We could just filemap_flush_range(), then mark dropbehind folios
> under writeback and then invalidate whatever is already clean and that
> should be good enough, shouldn't it?
It was for overwrites: BIO_COMPLETE_IN_TASK is set from PG_dropbehind
when the bio is built, so overwrites marked only after the flush
complete in interrupt context and aren't dropped. It wasn't measurably
slower (median DONTNEED 1.9 ms vs 1.8 ms without), but as dirty folios
can't be marked safely as you said above, I will drop this in the next
revision . New files, the case that hit us, are still dropped, as XFS
and ext4 complete them from their workqueues.
>
> > +
> > + filemap_flush_range(mapping, offset, endbyte);
> > +
> > if (end_index >= start_index) {
> > unsigned long nr_failed = 0;
> >
> > + if (dropbehind)
> > + fadvise_mark_dropbehind(mapping, start_index,
> > + end_index, PAGECACHE_TAG_WRITEBACK);
> > +
>
> ...
>
> > +/*
> > + * Mark a locked page cache folio dropbehind, and account it in
> > + * WB_DONTCACHE_DIRTY if it is dirty.
> > + */
> > +void folio_mark_dropbehind(struct folio *folio)
> > +{
> > + struct address_space *mapping = folio->mapping;
> > + struct inode *inode = mapping->host;
> > + struct wb_lock_cookie cookie = {};
> > + struct bdi_writeback *wb;
> > +
> > + VM_WARN_ON_ONCE_FOLIO(!folio_test_locked(folio), folio);
> > +
> > + if (folio_test_dropbehind(folio))
> > + return;
> > +
> > + wb = unlocked_inode_to_wb_begin(inode, &cookie);
> > + folio_set_dropbehind(folio);
> > + if (folio_test_dirty(folio) && mapping_can_writeback(mapping))
> > + wb_stat_mod(wb, WB_DONTCACHE_DIRTY, folio_nr_pages(folio));
> > + unlocked_inode_to_wb_end(inode, &cookie);
> > +}
>
> So as you glossed over in the changelog, this can race with
> folio_account_dirtied() which isn't necessarily called under the folio
> lock. Both places will see dirty & dropbehind set and increase
> WB_DONTCACHE_DIRTY. You need to hold xa_lock for &mapping->i_pages to make
> this safe, which luckily shouldn't be that hard to do...
>
It looks like xa_lock alone isn't enough for a dirty folio, as
filemap_dirty_folio(), block_dirty_folio() and mark_buffer_dirty()
set PG_dirty before taking it. So the next revision will mark folio
under xa_lock only if it is clean, and never touches the counter. The
lookup clear in this revision patch 2 also has the same problem,
so next revision applies the same rule there.
> Honza
> --
> Jan Kara <jack@suse.com>
> SUSE Labs, CR
>
next prev parent reply other threads:[~2026-12-04 17:49 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 14:24 [PATCH 0/4] mm: drop folios under writeback on POSIX_FADV_DONTNEED Usama Arif
2026-12-02 14:24 ` [PATCH 1/4] mm/migrate: preserve PG_dropbehind across folio migration Usama Arif
2026-12-02 14:41 ` Jan Kara
2026-12-02 14:56 ` Jeff Layton
2026-12-02 15:03 ` Zi Yan
2026-12-02 15:08 ` David Hildenbrand (Arm)
2026-12-02 15:18 ` Usama Arif
2026-12-02 14:24 ` [PATCH 2/4] mm/filemap: clear PG_dropbehind on lookup under the folio lock Usama Arif
2026-12-02 14:42 ` Jan Kara
2026-12-02 15:04 ` Jeff Layton
2026-12-02 16:20 ` Christoph Hellwig
2026-12-03 14:26 ` sashiko-bot
2026-12-02 14:24 ` [PATCH 3/4] mm/filemap: don't drop dropbehind folios with interrupts disabled Usama Arif
2026-12-02 14:51 ` Jan Kara
2026-12-02 15:04 ` Jeff Layton
2026-12-02 16:21 ` Christoph Hellwig
2026-12-02 17:50 ` Usama Arif
2026-12-03 14:26 ` sashiko-bot
2026-12-02 14:24 ` [PATCH 4/4] mm/fadvise: drop folios under writeback on POSIX_FADV_DONTNEED Usama Arif
2026-12-02 15:18 ` Jeff Layton
2026-12-02 16:26 ` Christoph Hellwig
2026-12-02 17:52 ` Usama Arif
2026-12-03 9:58 ` Jan Kara
2026-12-04 17:48 ` Usama Arif [this message]
2026-12-02 15:40 ` (subset) [PATCH 0/4] mm: " David Hildenbrand
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=20261204174848.955248-1-usama.arif@linux.dev \
--to=usama.arif@linux.dev \
--cc=akpm@linux-foundation.org \
--cc=apopple@nvidia.com \
--cc=axboe@kernel.dk \
--cc=bigeasy@linutronix.de \
--cc=brauner@kernel.org \
--cc=byungchul@sk.com \
--cc=chentaotao@didiglobal.com \
--cc=clrkwllms@kernel.org \
--cc=david@kernel.org \
--cc=gourry@gourry.net \
--cc=hannes@cmpxchg.org \
--cc=hch@lst.de \
--cc=jack@suse.cz \
--cc=jlayton@kernel.org \
--cc=joshua.hahnjy@gmail.com \
--cc=kas@kernel.org \
--cc=kernel-team@meta.com \
--cc=liam@infradead.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-rt-devel@lists.linux.dev \
--cc=matthew.brost@intel.com \
--cc=mhocko@suse.com \
--cc=riel@surriel.com \
--cc=ritesh.list@gmail.com \
--cc=rostedt@goodmis.org \
--cc=rppt@kernel.org \
--cc=shakeel.butt@linux.dev \
--cc=tz2294@columbia.edu \
--cc=vbabka@kernel.org \
--cc=willy@infradead.org \
--cc=ying.huang@linux.alibaba.com \
--cc=ziy@nvidia.com \
/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