bcachefs: Introduce bch2_splice_read
PATCH 1/3 bcachefs: Introduce bch2_splice_read
This provides our own splice read, which locks ei_pagecache_lock around filemap_splice_read.
--- fs/bcachefs/fs-io-buffered.c | 12 ++++++++++++ fs/bcachefs/fs-io-buffered.h | 4 ++++ 2 files changed, 16 insertions(+)
fs/bcachefs/fs-io-buffered.c +12 -0
@@ -1100,6 +1100,18 @@ ssize_t bch2_write_iter(struct kiocb *iocb, struct iov_iter *from)return bch2_err_class (ret); } +ssize_t bch2_splice_read (struct file *in,loff_t *ppos, +struct pipe_inode_info *pipe, +size_t len,unsigned int flags) +{ +ssize_t ret; +struct bch_inode_info *inode =to_bch_ei (in->f_mapping->host); +bch2_pagecache_add_get (inode); + ret =filemap_splice_read (in, ppos, pipe, len, flags); +bch2_pagecache_add_put (inode); +return ret; +} +void bch2_fs_fs_io_buffered_exit (struct bch_fs *c) {bioset_exit (&c->writepage_bioset);
fs/bcachefs/fs-io-buffered.h +4 -0
@@ -17,6 +17,10 @@ int bch2_write_end(struct file *, struct address_space *, loff_t,ssize_t bch2_write_iter (struct kiocb *,struct iov_iter *); +ssize_t bch2_splice_read (struct file *in,loff_t *ppos, +struct pipe_inode_info *pipe, +size_t len,unsigned int flags); +void bch2_fs_fs_io_buffered_exit (struct bch_fs *);int bch2_fs_fs_io_buffered_init (struct bch_fs *);#else
Signature · 1 line
2.49.0
PATCH 2/3 bcachefs: Use our own splice_read implementation
splice_read will read file data to the page cache, to prevent cache coherency issure from happening, we need to lock ei_pagecache_lock around it. Therefore, use our own splice_read implementation.
This also solves one deadlock issue, which locks the folio first when bch2_readahead is invoked from splice_read path, but fail to lock ei_pagecache_lock, which was already locked by the direct I/O path or fallocate path, when the DIO path tries to invalidate the page cache, a deadlock happens.
--- fs/bcachefs/fs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/bcachefs/fs.c +1 -1
@@ -1716,7 +1716,7 @@ static const struct file_operations bch_file_operations = { .mmap = bch2_mmap, .get_unmapped_area = thp_get_unmapped_area, .fsync = bch2_fsync, - .splice_read = filemap_splice_read, + .splice_read = bch2_splice_read, .splice_write = iter_file_splice_write, .fallocate = bch2_fallocate_dispatch, .unlocked_ioctl = bch2_fs_file_ioctl,
Signature · 1 line
2.49.0
PATCH 3/3 bcachefs: Don't lock ei_pagecache_lock in bch2_readahead
In reply to Alan HuangThe lock should already be acquired from all code path.
--- fs/bcachefs/fs-io-buffered.c | 15 --------------- 1 file changed, 15 deletions(-)
fs/bcachefs/fs-io-buffered.c +0 -15
@@ -70,15 +70,6 @@ static int readpages_iter_init(struct readpages_iter *iter,return 0 ; } -static void readpages_iter_exit (struct readpages_iter *iter, -struct readahead_control *ractl) -{ -darray_for_each_reverse (iter->folios, folio) { -readpages_iter_folio_revert (ractl, *folio); -folio_get (*folio); - } -} -static inline struct folio *readpage_iter_peek (struct readpages_iter *iter) {if (iter->idx >= iter->folios.nr)
@@ -305,10 +296,6 @@ void bch2_readahead(struct readahead_control *ractl)* scheduling. */ blk_start_plug (&plug); -if (!bch2_pagecache_add_tryget (inode)) { -readpages_iter_exit (&readpages_iter, ractl); - goto out; - }struct btree_trans *trans =bch2_trans_get (c);while ((folio =readpage_iter_peek (&readpages_iter))) {
@@ -334,8 +321,6 @@ void bch2_readahead(struct readahead_control *ractl) }bch2_trans_put (trans); -bch2_pagecache_add_put (inode); -out:blk_finish_plug (&plug);darray_exit (&readpages_iter.folios); }
Signature · 1 line
2.49.0
Quoted 18 lines · Alan Huang
This provides our own splice read, which locks
ei_pagecache_lockaroundfilemap_splice_read.Signed-off-by Alan Huang <[email protected]>--- fs/bcachefs/fs-io-buffered.c | 12 ++++++++++++ fs/bcachefs/fs-io-buffered.h | 4 ++++ 2 files changed, 16 insertions(+)fs/bcachefs/fs-io-buffered.c +1
-0@@ -1100,6 +1100,18 @@ ssize_t bch2_write_iter(struct kiocb *iocb, struct iov_iter *from)return bch2_err_class (ret); } +ssize_t bch2_splice_read(struct file *in, loff_t *ppos,
fs/bcachefs/fs-io-buffered.c ssize_t bch2_write_iter(struct kiocb *iocb, struct iov_iter *from) · line 1113
Quoted 10 lines
fs/bcachefs/fs-io-buffered.c +10
-0+struct pipe_inode_info *pipe, + size_t len, unsigned int flags) +{ + ssize_t ret; +struct bch_inode_info *inode =to_bch_ei (in->f_mapping->host); +bch2_pagecache_add_get (inode); + ret =filemap_splice_read (in, ppos, pipe, len, flags); +bch2_pagecache_add_put (inode); +return ret; +}
Taking the lock here, around the entire splice_read() call sucks - most buffered reads (and this is the fastpath, which we care about) will be reading from cache, i.e. they aren't adding new folios to the pagecache and they don't need pagecache_add lock.
Meaning, we really don't want to add two new atomic ops on the inode to every buffered read.
Does this help with lockdep at all? The main issue is still the fault path vs. mmap_lock.