From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3FE18606A322 for ; Fri, 4 Dec 2026 17:49:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=uEP7NQ37 Received: by kanga.kvack.org (Postfix) id 136986B05B1; Fri, 4 Dec 2026 12:49:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0E80D6B05B9; Fri, 4 Dec 2026 12:49:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F18BA6B05BA; Fri, 4 Dec 2026 12:49:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 71B256B05B1 for ; Fri, 4 Dec 2026 12:49:00 -0500 (EST) Received: from smtpin14.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 03D19A045C for ; Fri, 4 Dec 2026 17:48:59 +0000 (UTC) X-FDA: 85507036440.14.F861D0F Received: from mta1.migadu.com (out-231.mta1.migadu.com [95.215.58.231]) by imf04.hostedemail.com (Postfix) with ESMTP id A2DC740005 for ; Fri, 4 Dec 2026 17:48:57 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=uEP7NQ37; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf04.hostedemail.com: domain of usama.arif@linux.dev designates 95.215.58.231 as permitted sender) smtp.mailfrom=usama.arif@linux.dev ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796406538; b=cxNcPVlgOa8E4MBtvDaGItaVpHG8sEvwm7w6KdFLxf0Aas1buf8pitqg1Obo5YtBLNNKYX TjwiajjloiXBM2XLIvKWdEjANjIM6PqYYPWiLj2Mb12lLA+BOGbSbU/QmZuck0xkrrHJyu Z3NEVyUpXXxuPK5akuqr/AFx4eoGWXY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796406538; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=VlnuAaLH8tDHyQ9nX8ver4u6QkNEUI0kLSFlbVvX5v8=; b=GiY8MP/yGT+cIiuSdvMoIvioMB0HfWZj1lYULTSetE3IMp6gPi3cGIZq43NO7lkLakCwX0 lHWD1O1KfqNfU3XTg+ZnvHmdnGfBYTCXbhbaDL2++SQkNLHS2JQVwIVOwrYS1KbVKy6tST VKgH0fwxGbyO5Tdld8+Z8sU2YyQIXuU= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=uEP7NQ37; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf04.hostedemail.com: domain of usama.arif@linux.dev designates 95.215.58.231 as permitted sender) smtp.mailfrom=usama.arif@linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=mQKHSVVm0OB2VsTaejPEcQRNwCsusqKSvHG/RITvQFk=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796406535; v=1; x=1797011335; b=uEP7NQ374TPBweDIIc2hZB49/tL7skSQVTrhb2t7OUzpp2+YT3kBMPMRlCSKA5zChBVOdMNl QUgKtM91MqNgFAwOCP3+3vk4hnyRhCr6kFjyypjSh34bdORByDT55R/NPq6JWVOInKGQBsdBetZ bhHafZfMW/x7So81Z5Fy4GKA= X-Envelope-To: linux-mm@kvack.org Received: by mta10.migadu.com with ESMTPS id dc4080be9e74c69a; Fri, 04 Dec 2026 17:48:55 +0000 X-Mizu-Trace-ID: dc4080be9e74c69a X-Migadu-Flow: FLOW_OUT From: Usama Arif To: Jan Kara Cc: Usama Arif , Andrew Morton , willy@infradead.org, david@kernel.org, axboe@kernel.dk, Christoph Hellwig , 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 Message-ID: <20261204174848.955248-1-usama.arif@linux.dev> X-Mailer: git-send-email 2.53.1 In-Reply-To: References: X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Stat-Signature: 9dz9abd5uqbcd71dzcx3x7cicu5885us X-Spam-Status: No, score=-19.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Rspam-User: X-Rspamd-Queue-Id: A2DC740005 X-Rspamd-Server: rspam07 X-HE-Tag: 1796406537-100988 X-HE-Meta: U2FsdGVkX1/o7+sbtKePVjCUa80JcWCNJIYCA3bl2jcIslH96mY/eTaTaCAUzcx5l8VOgIXiX4e+cTB3HpTbRQJZkKboUmiV+F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3fHy5MvS32X+/8tbEI7/fGNgtZnI6USagPIOe0ZOwcPyZXuNKVw7nvq8TTaRU7sJ3VPxqFMgfEziZbo33XmDJW3SuJ4tZccg081YoXWbTQKfw+s4g2CPA4s671fKQqktja8ianlVVw81u6LGJ3M0JREw+tr8PYnKRIqebpekCwnEnLNKRM0g2RoKFYrTDwhmeg3B6nwbYfNvO2AFYa/JRmjLNWUWz46qx4TH9fO0v1RWTxzfc0GSVbJexHTuq+drgHjlYZzSUChdNE4zzcN70GlUIzNG6gwPR5LR+adllE8qKkVEKhIEvBIgAxKzRFCVqJ1wtTTHSdH0eUU5ZsLmmI1X9WCCXcwRiEFkIBzEl/E8FpsDNt9u3ietHXBVxHO34at/EqCLkOxgwXpzM6w4mkThp0sbQIvpkfwInLFTogWsK0VZPFn7b4wKYQKf11KxmcP5A== Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On Thu, 3 Dec 2026 10:58:15 +0100 Jan Kara 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 > > ... > > > +/* > > + * 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 > SUSE Labs, CR >