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, 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 --- mm/fadvise.c | 60 +++++++++++++++++++++++++++++++++++++++++---- mm/internal.h | 1 + mm/page-writeback.c | 23 +++++++++++++++++ 3 files changed, 79 insertions(+), 5 deletions(-) diff --git a/mm/fadvise.c b/mm/fadvise.c index b63fe21416ff21..6e7b86f072fe84 100644 --- a/mm/fadvise.c +++ b/mm/fadvise.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -28,6 +29,43 @@ * deactivate the pages and clear PG_Referenced. */ +/* + * 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); + + 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); + /* * It's common to FADV_DONTNEED right after * the read or write that instantiates the diff --git a/mm/internal.h b/mm/internal.h index 8942e57744a96e..7a64b5c27f3064 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -626,6 +626,7 @@ static inline void hugetlb_folio_restore_rsv_on_unmap(struct vm_area_struct *vma #endif int folio_unmap_invalidate(struct address_space *mapping, struct folio *folio, gfp_t gfp); +void folio_mark_dropbehind(struct folio *folio); void page_cache_ra_order(struct readahead_control *, struct file_ra_state *); void force_page_cache_ra(struct readahead_control *, unsigned long nr); diff --git a/mm/page-writeback.c b/mm/page-writeback.c index 5939f781fc5402..13f4408f068d5e 100644 --- a/mm/page-writeback.c +++ b/mm/page-writeback.c @@ -2654,6 +2654,29 @@ void folio_account_cleaned(struct folio *folio, struct bdi_writeback *wb) task_io_account_cancelled_write(nr * PAGE_SIZE); } +/* + * 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); +} + /* * Mark the folio dirty, and set it dirty in the page cache. * -- 2.53.1-Meta