Support large folios buffered write for regular files
cover Support large folios buffered write for regular files
This RFC PATCH series adds support for large folio buffered writes to regular readable and writable files in f2fs with exception of atomic/compressed/inlined files.
Since adapting page writeback to support large folios is relatively complex, it is not included in this series.
I ran simple dd tests on a Pixel 6 device.
During the tests, dirty page writeback was temporarily disabled.
---------------------------------------------------------------------------------
Test:
echo 100 > /proc/sys/vm/dirty_ratio
echo 100 > /proc/sys/vm/dirty_background_ratio
echo 0 > /proc/sys/vm/dirty_writeback_centisecs
echo 8640000 > /proc/sys/vm/dirty_expire_centisecs
Before large folio enable:
oriole:/data # dd if=/dev/zero of=lf.c bs=2M count=500 500+0 records in
500+0 records out
1048576000 bytes (0.9 G) copied, 0.815 s, 1.1 G/s
After large folio enable:
oriole:/data # dd if=/dev/zero of=lf.c bs=2M count=500
500+0 records in
500+0 records out
1048576000 bytes (0.9 G) copied, 0.330 s, 2.9 G/s
---------------------------------------------------------------------------------
Note:
- bs=2M means always allocating the largest folio order in buffered write, so it represents a theoretical maximum write performance.
- Because large folio support can only be enabled for files via f2fs_iget, it may be more convenient for testing to create a large file first and then immediately reboot the device so that the inode is reloaded.
**Discussion:**
I would like to add fault injection to f2fs_write_end to simulate short writes (copied != len).
However, I am concerned that directly adding time_to_inject inside f2fs_write_end and returning 0 may cause generic_perform_write to loop forever during testing. Better ideas are appreciated.
Thanks.
---
Nanzhe Zhao (5):
f2fs: Move f2fs_folio_state to f2fs.h
f2fs: Introduce f2fs_inode_may_use_large_folio
f2fs: Preallocate partial-write blocks for large-folio write
f2fs: Support large folios buffered write
f2fs: Make folio->private flags work with folio state
fs/f2fs/data.c | 226 ++++++++++++++++++++++++++++++++++++++++++++---- fs/f2fs/f2fs.h | 58 ++++++++++--- fs/f2fs/file.c | 21 +++-- fs/f2fs/inode.c | 3 +- 4 files changed, 272 insertions(+), 36 deletions(-)
base-commit: bc367775f60214312befa33f101b31fe74bba48a
Signature · 8 lines
2.34.1 _______________________________________________ Linux-f2fs-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
RFC PATCH 1/5 f2fs: Move f2fs_folio_state to f2fs.h
Move the definition of f2fs_folio_state to f2fs.h
for upcoming changes to PAGE_PRIVATE functions.
No logical change.
--- fs/f2fs/data.c | 4 ---- fs/f2fs/f2fs.h | 5 +++++ 2 files changed, 5 insertions(+), 4 deletions(-)
fs/f2fs/data.c +0 -4
@@ -35,10 +35,6 @@ static struct kmem_cache *ffs_entry_slab;static mempool_t *bio_post_read_ctx_pool;static struct bio_set f2fs_bioset; -struct f2fs_folio_state { -spinlock_t state_lock; -unsigned int read_pages_pending; -};#define F2FS_BIO_POOL_SIZE NR_CURSEG_TYPE
fs/f2fs/f2fs.h +5 -0
@@ -2042,6 +2042,11 @@ struct f2fs_sb_info {#endif }; +struct f2fs_folio_state { +spinlock_t state_lock; +unsigned int read_pages_pending; +}; +/* Definitions to access f2fs_sb_info */ #define SEGS_TO_BLKS (sbi, segs) \ ((segs) << (sbi)->log_blocks_per_seg)
Signature · 8 lines
2.34.1 _______________________________________________ Linux-f2fs-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
RFC PATCH 2/5 f2fs: Introduce f2fs_inode_may_use_large_folio
In reply to Nanzhe ZhaoIntroudce a helper function to check whether an inode may use large folio for scalability.
--- fs/f2fs/f2fs.h | 16 ++++++++++++++++ fs/f2fs/inode.c | 3 +-- 2 files changed, 17 insertions(+), 2 deletions(-)
fs/f2fs/f2fs.h +16 -0
@@ -5035,6 +5035,22 @@ static inline bool f2fs_quota_file(struct f2fs_sb_info *sbi, nid_t ino)return false; } +static inline bool f2fs_inode_may_use_large_folio (struct inode *inode) +{ +if (f2fs_has_inline_data (inode)) +return false; + +if (f2fs_compressed_file (inode)) +return false; + +if (f2fs_is_atomic_file (inode)) +return false; + +if (f2fs_quota_file (F2FS_I_SB (inode), inode->i_ino)) +return false; +return true; +} +static inline bool f2fs_block_unit_discard (struct f2fs_sb_info *sbi) {return F2FS_OPTION (sbi).discard_unit ==DISCARD_UNIT_BLOCK ;
fs/f2fs/inode.c +1 -2
@@ -620,8 +620,7 @@ struct inode *f2fs_iget(struct super_block *sb, unsigned long ino) inode->i_op = &f2fs_file_inode_operations; inode->i_fop = &f2fs_file_operations; inode->i_mapping->a_ops = &f2fs_dblock_aops; -if (IS_IMMUTABLE (inode) && !f2fs_compressed_file (inode) && - !f2fs_quota_file (sbi, inode->i_ino)) +if (f2fs_inode_may_use_large_folio (inode))mapping_set_folio_min_order (inode->i_mapping,0 ); }else if (S_ISDIR (inode->i_mode)) { inode->i_op = &f2fs_dir_inode_operations;
Signature · 8 lines
2.34.1 _______________________________________________ Linux-f2fs-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
RFC PATCH 3/5 f2fs: Preallocate partial-write blocks for large-folio write
In reply to Nanzhe ZhaoIn the case of large folio buffered write, if we pre-allocate partial blocks after acquiring the folio lock, it may call f2fs_balance_fs and need to unlock the folio during write.
Re-locking after unlocking can be troublesome because a large folio carries additional f2fs_folio_state metadata, and partial truncate can cause the large folio to be split.
At this point, the large folio is still in the inode->mapping.
Prellocate partial blocks in advance to avoid handling this.
--- fs/f2fs/file.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-)
fs/f2fs/file.c +14 -3
@@ -4995,9 +4995,20 @@ static int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *iter,return ret; } -/* Do not preallocate blocks that will be written partially in 4KB. */ - map.m_lblk =F2FS_BLK_ALIGN (pos); - map.m_len =F2FS_BYTES_TO_BLK (pos + count); +if (mapping_large_folio_support (inode->i_mapping)) { +/* preallocate all blocks for large folio inode before write to avoid +* unlocking large folios by balance_fs during write. Rechecking the +* large-folio state is unreliable since partial truncation may split +* the folio. +*/ + map.m_lblk =F2FS_BYTES_TO_BLK (pos); + map.m_len =F2FS_BLK_ALIGN (pos + count); + }else { +/* Do not preallocate blocks that will be written partially in 4KB. */ + map.m_lblk =F2FS_BLK_ALIGN (pos); + map.m_len =F2FS_BYTES_TO_BLK (pos + count); + } +if (map.m_len > map.m_lblk) map.m_len -= map.m_lblk;else
Signature · 8 lines
2.34.1 _______________________________________________ Linux-f2fs-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
RFC PATCH 4/5 f2fs: Support large folios buffered write
In reply to Nanzhe ZhaoThis patch adds support for large folios buffered write for regular files with exception of atomic files, inline files and compressed files.
We introducte state bitmap to track the uptodate/dirty state of each sub -page within a folio. And for allocating multiple size f2fs_folio_state, we use kzalloc for now. Better ideas are appreciated.
--- fs/f2fs/data.c | 222 ++++++++++++++++++++++++++++++++++++++++++++++--- fs/f2fs/f2fs.h | 1 + fs/f2fs/file.c | 4 - 3 files changed, 210 insertions(+), 17 deletions(-)
fs/f2fs/data.c +209 -13
@@ -2414,14 +2414,26 @@ int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,static struct f2fs_folio_state *ffs_find_or_alloc (struct folio *folio) {struct f2fs_folio_state *ffs = folio->private; +unsigned int nr_subpages;if (ffs)return ffs; - ffs =f2fs_kmem_cache_alloc (ffs_entry_slab, -GFP_NOIO | __GFP_ZERO, true,NULL ); + nr_subpages =folio_size (folio) >>PAGE_SHIFT ; +if (nr_subpages <=1 ) +return NULL ; + + ffs =kzalloc (struct_size (ffs, state,BITS_TO_LONGS (2 * nr_subpages)), +GFP_NOIO ); +if (!ffs) +return NULL ;spin_lock_init (&ffs->state_lock); +if (folio_test_uptodate (folio)) +bitmap_set (ffs->state,0 , nr_subpages); +if (folio_test_dirty (folio)) +bitmap_set (ffs->state, nr_subpages, nr_subpages); +folio_attach_private (folio, ffs);return ffs; }
@@ -2440,7 +2452,177 @@ static void ffs_detach_free(struct folio *folio)return ;WARN_ON_ONCE (ffs->read_pages_pending !=0 ); -kmem_cache_free (ffs_entry_slab, ffs); +kfree (ffs); +} + +static inline unsigned int ffs_nr_subpages (const struct folio *folio) +{ +return folio_size (folio) >>PAGE_SHIFT ; +} + +static inline bool ffs_subpage_is_uptodate (struct f2fs_folio_state *ffs, +const struct folio *folio,size_t offset) +{ +unsigned int idx = offset >>PAGE_SHIFT ; + +if (!ffs) +return false; + +if (idx >=ffs_nr_subpages (folio)) +return false; + +return test_bit (idx, ffs->state); +} + +/* Must be called while holding folio lock */ +static inline void ffs_mark_subrange_uptodate (struct folio *folio,size_t offset,size_t len) +{ +struct f2fs_folio_state *ffs = folio->private; +unsigned int nr_subpages =ffs_nr_subpages (folio); +unsigned int start, end; + +if (!ffs) { +folio_mark_uptodate (folio); +return ; + } + + start = offset >>PAGE_SHIFT ; + end = (offset + len +PAGE_SIZE -1 ) >>PAGE_SHIFT ; + end =min (end, nr_subpages); + +bitmap_set (ffs->state, start, end - start); +if (bitmap_full (ffs->state, nr_subpages)) +folio_mark_uptodate (folio); +} + +/* +* During buffered writes, we may only need to read the first and last +* page of a folio. +*/ +static bool f2fs_find_next_need_read_block (struct f2fs_folio_state *ffs, +const struct folio *folio, +size_t orig_off,size_t *need_off, +size_t len) +{ +size_t start = orig_off; +size_t end = start + len; +size_t head, tail; + +if (start & (PAGE_SIZE -1 )) { + head =round_down (start,PAGE_SIZE ); +if (!ffs_subpage_is_uptodate (ffs, folio, head)) { + *need_off = head; +return true; + } + } + +if (end & (PAGE_SIZE -1 )) { + tail =round_down (end -1 ,PAGE_SIZE ); +if (!ffs_subpage_is_uptodate (ffs, folio, tail)) { + *need_off = tail; +return true; + } + } + +return false; +} + +static int prepare_large_folio_write_begin (struct inode *inode, +struct address_space *mapping, +struct folio *folio,loff_t pos, +unsigned int len) +{ +struct f2fs_sb_info *sbi =F2FS_I_SB (inode); +struct f2fs_folio_state *ffs; +struct dnode_of_data dn; +size_t ori_off =offset_in_folio (folio, pos); +size_t need_off = ori_off; +pgoff_t index; +bool get_dn; +int err =0 ; +sector_t sector; +struct block_device *bdev; +struct bio *bio; + + len = min_t(unsigned int, len, folio_size(folio) - ori_off); +if (folio_test_uptodate (folio) || len ==folio_size (folio)) +return 0 ; + + ffs =ffs_find_or_alloc (folio); +if (!ffs) +return 0 ; + +/* Inline data must have been converted before reaching here. */ +if (WARN_ON_ONCE (f2fs_has_inline_data (inode))) +return -EINVAL ; + +while (f2fs_find_next_need_read_block (ffs, folio, ori_off, &need_off, len)) { +size_t off; + + index = folio->index + (ori_off >>PAGE_SHIFT ); + get_dn = false; + +if (!f2fs_lookup_read_extent_cache_block (inode, index, + &dn.data_blkaddr)) { +if (IS_DEVICE_ALIASING (inode)) +return -ENODATA ; + +set_new_dnode (&dn, inode,NULL ,NULL ,0 ); + err =f2fs_get_dnode_of_data (&dn, index,LOOKUP_NODE ); + get_dn = true; +if (err) + goto out; + } + +if (dn.data_blkaddr ==NULL_ADDR ) { + err = -EFSCORRUPTED ; + goto out; + } + + off =offset_in_folio (folio, index <<PAGE_SHIFT ); + +if (dn.data_blkaddr ==NEW_ADDR ) { +folio_zero_segment (folio, off, off +PAGE_SIZE ); +ffs_mark_subrange_uptodate (folio, off,PAGE_SIZE ); +continue ; + } + +if (!f2fs_is_valid_blkaddr (sbi, dn.data_blkaddr, +DATA_GENERIC_ENHANCE_READ )) { + err = -EFSCORRUPTED ; + goto out; + } + +/* Submit a synchronous read for this subpage. */ +f2fs_wait_on_block_writeback (inode, dn.data_blkaddr); + bdev =f2fs_target_device (sbi, dn.data_blkaddr, §or); + + bio =bio_alloc_bioset (bdev,1 ,REQ_OP_READ |REQ_SYNC , +GFP_NOIO , &f2fs_bioset); + bio->bi_iter.bi_sector = sector; +f2fs_set_bio_crypt_ctx (bio, inode, index,NULL ,GFP_NOFS ); + +if (!bio_add_folio (bio, folio,PAGE_SIZE , off)) { +bio_put (bio); + err = -EIO ; + goto out; + } + + err =submit_bio_wait (bio); +bio_put (bio); +if (err) + goto out; + +ffs_mark_subrange_uptodate (folio, off,PAGE_SIZE ); + +out: +if (get_dn) +f2fs_put_dnode (&dn); +if (err) +return err; + } + +return 0 ; } static int f2fs_read_data_large_folio(struct inode *inode,
@@ -2457,7 +2639,7 @@ static int f2fs_read_data_large_folio(struct inode *inode,int ret =0 ;bool folio_in_bio; -if (!IS_IMMUTABLE (inode) ||f2fs_compressed_file (inode)) { +if (f2fs_compressed_file (inode)) {if (folio)folio_unlock (folio);return -EOPNOTSUPP ;
@@ -3828,6 +4010,7 @@ static int f2fs_write_begin(const struct kiocb *iocb,bool need_balance = false;bool use_cow = false;block_t blkaddr =NULL_ADDR ; +fgf_t fgp =FGP_LOCK |FGP_WRITE |FGP_CREAT ;int err =0 ;trace_f2fs_write_begin (inode, pos, len);
@@ -3875,9 +4058,8 @@ static int f2fs_write_begin(const struct kiocb *iocb,* Do not use FGP_STABLE to avoid deadlock. * Will wait that below with our IO control. */ - folio =f2fs_filemap_get_folio (mapping, index, -FGP_LOCK |FGP_WRITE |FGP_CREAT |FGP_NOFS , -mapping_gfp_mask (mapping)); + fgp |=fgf_set_order (len); + folio =f2fs_filemap_get_folio (mapping, index, fgp,mapping_gfp_mask (mapping));if (IS_ERR (folio)) { err =PTR_ERR (folio); goto fail;
@@ -3890,7 +4072,7 @@ static int f2fs_write_begin(const struct kiocb *iocb,if (f2fs_is_atomic_file (inode)) err =prepare_atomic_write_begin (sbi, folio, pos, len, &blkaddr, &need_balance, &use_cow); -else +else if (!folio_test_large (folio)) err =prepare_write_begin (sbi, folio, pos, len, &blkaddr, &need_balance);if (err)
@@ -3911,6 +4093,13 @@ static int f2fs_write_begin(const struct kiocb *iocb,f2fs_folio_wait_writeback (folio,DATA , false, true); +if (folio_test_large (folio)) { + err =prepare_large_folio_write_begin (inode, mapping, folio, pos, len); +if (!err) +return 0 ; + goto put_folio; + } +if (len ==folio_size (folio) ||folio_test_uptodate (folio))return 0 ;
@@ -3963,15 +4152,22 @@ static int f2fs_write_end(const struct kiocb *iocb,trace_f2fs_write_end (inode, pos, len, copied);/* -* This should be come from len == PAGE_SIZE, and we expect copied -* should be PAGE_SIZE. Otherwise, we treat it with zero copied and -* let generic_perform_write() try to copy data again through copied=0. +* If a short copy happens on a folio that isn't uptodate, we treat it +* with zero copied and let generic_perform_write() try to copy data again +* through copied=0. */ if (!folio_test_uptodate (folio)) { -if (unlikely (copied != len)) +if (unlikely (copied != len)) { copied =0 ; -else + }else if (folio_test_large (folio)) { +ffs_mark_subrange_uptodate (folio, +offset_in_folio (folio, pos), len); + }else { +/* +* For order-0 folios, this should be come from len == PAGE_SIZE +*/ folio_mark_uptodate (folio); + } }#ifdef CONFIG_F2FS_FS_COMPRESSION
fs/f2fs/f2fs.h +1 -0
@@ -2045,6 +2045,7 @@ struct f2fs_sb_info {struct f2fs_folio_state {spinlock_t state_lock;unsigned int read_pages_pending; +unsigned long state[]; };/* Definitions to access f2fs_sb_info */
fs/f2fs/file.c +0 -4
@@ -624,10 +624,6 @@ static int f2fs_file_open(struct inode *inode, struct file *filp)if (!f2fs_is_compress_backend_ready (inode))return -EOPNOTSUPP ; -if (mapping_large_folio_support (inode->i_mapping) && - filp->f_mode &FMODE_WRITE ) -return -EOPNOTSUPP ; - err =fsverity_file_open (inode, filp);if (err)return err;
Signature · 8 lines
2.34.1 _______________________________________________ Linux-f2fs-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
RFC PATCH 5/5 f2fs: Make folio->private flags work with folio state
In reply to Nanzhe ZhaoLarge folios store a f2fs_folio_state pointer in folio->private, so page-private bits must be stored separately.
Route page-private flags to a extra private_flags in f2fs_folio_state for large folios while preserving the existing encoding for order-0 folios.We also make PAGE_PRIVATE funcs aware of f2fs_folio_state.
Note there's and edge case when folio_set_f2fs_#name is called on a large folio before any f2fs_folio_state is attached. We may should alloc a f2fs_folio_state in that case, but it requires to move ffs_find_or_alloc to f2fs.h. For now I just let this case sliently return.
Need more disscusion here.
--- fs/f2fs/f2fs.h | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-)
fs/f2fs/f2fs.h +26 -10
@@ -2045,6 +2045,7 @@ struct f2fs_sb_info {struct f2fs_folio_state {spinlock_t state_lock;unsigned int read_pages_pending; +unsigned long private_flags;unsigned long state[]; };
@@ -2618,9 +2619,15 @@ static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,#define PAGE_PRIVATE_GET_FUNC (name, flagname) \ static inline bool folio_test_f2fs_##name(const struct folio *folio) \ { \ - unsigned long priv = (unsigned long)folio->private; \ + unsigned long priv; \ unsigned long v = (1UL << PAGE_PRIVATE_NOT_POINTER) | \ (1UL << PAGE_PRIVATE_##flagname); \ + if (folio_test_large(folio) && folio->private) { \ + struct f2fs_folio_state *ffs = folio->private; \ + priv = ffs->private_flags; \ + } else { \ + priv = (unsigned long)folio->private; \ + } \ return (priv & v) == v; \ } \ static inline bool page_private_##name(struct page *page) \
@@ -2635,9 +2642,14 @@ static inline void folio_set_f2fs_##name(struct folio *folio) \ { \unsigned long v = (1UL <<PAGE_PRIVATE_NOT_POINTER ) | \ (1UL << PAGE_PRIVATE_##flagname); \ -if (!folio->private) \ +if (!folio->private && !folio_test_large (folio)) \folio_attach_private (folio, (void *)v); \ -else { \ +else if (folio_test_large (folio)) { \ +struct f2fs_folio_state *ffs = folio->private; \ +if (!ffs) \ +return ; \ + ffs->private_flags |= v; \ + }else { \ v |= (unsigned long )folio->private; \ folio->private = (void *)v; \ } \
@@ -2653,13 +2665,17 @@ static inline void set_page_private_##name(struct page *page) \#define PAGE_PRIVATE_CLEAR_FUNC (name, flagname) \ static inline void folio_clear_f2fs_##name(struct folio *folio) \ { \ - unsigned long v = (unsigned long)folio->private; \ - \ - v &= ~(1UL << PAGE_PRIVATE_##flagname); \ - if (v == (1UL << PAGE_PRIVATE_NOT_POINTER)) \ - folio_detach_private(folio); \ - else \ - folio->private = (void *)v; \ + if (folio_test_large(folio) && folio->private) { \ + struct f2fs_folio_state *ffs = folio->private; \ + ffs->private_flags &= ~(1UL << PAGE_PRIVATE_##flagname); \ + } else { \ + unsigned long v = (unsigned long)folio->private; \ + v &= ~(1UL << PAGE_PRIVATE_##flagname); \ + if (v == (1UL << PAGE_PRIVATE_NOT_POINTER)) \ + folio_detach_private(folio); \ + else \ + folio->private = (void *)v; \ + } \ } \ static inline void clear_page_private_##name(struct page *page) \ { \
Signature · 8 lines
2.34.1 _______________________________________________ Linux-f2fs-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
Quoted 4 lines · Nanzhe Zhao
This RFC PATCH series adds support for large folio buffered writes to regular readable and writable files in f2fs with exception of atomic/compressed/inlined files.
You are actually extending large folio support to non-immutable files, so for reads you would also get large folios, right?
If so, I feel the patchset name might be misleading. Perhaps something like "f2fs: enable large folios for regular files"?
Quoted 6 lines
Since adapting page writeback to support large folios is relatively complex, it is not included in this series.
I ran simple
ddtests on a Pixel 6 device.
During the tests, dirty page writeback was temporarily disabled.
---------------------------------------------------------------------------------
What happens if dirty_writeback is present? Will it cause a crash? I feel that dirty page writeback is actually more important—imagine we read some content using large folios and end up dirtying them.
Thanks
Barry
_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel