Support for RWF_UNCACHED
Subsystems (9)
Jens Axboe
maintainercover Support for RWF_UNCACHED
Recently someone asked me how io_uring buffered IO compares to mmaped IO in terms of performance. So I ran some tests with buffered IO, and found the experience to be somewhat painful. The test case is pretty basic, random reads over a dataset that's 10x the size of RAM. Performance starts out fine, and then the page cache fills up and we hit a throughput cliff. CPU usage of the IO threads go up, and we have kswapd spending 100% of a core trying to keep up. Seeing that, I was reminded of the many complaints I here about buffered IO, and the fact that most of the folks complaining will ultimately bite the bullet and move to O_DIRECT to just get the kernel out of the way.
But I don't think it needs to be like that. Switching to O_DIRECT isn't always easily doable. The buffers have different life times, size and alignment constraints, etc. On top of that, mixing buffered and O_DIRECT can be painful.
Seems to me that we have an opportunity to provide something that sits somewhere in between buffered and O_DIRECT, and this is where RWF_UNCACHED enters the picture. If this flag is set on IO, we get the following behavior:
- If the data is in cache, it remains in cache and the copy (in or out) is served to/from that.
- If the data is NOT in cache, we add it while performing the IO. When the IO is done, we remove it again.
With this, I can do 100% smooth buffered reads or writes without pushing the kernel to the state where kswapd is sweating bullets. In fact it doesn't even register.
Comments appreciated! Patches are against current git (ish), and can also be found here:
https://git.kernel.dk/cgit/linux-block/log/?h=buffered-uncached
fs/ceph/file.c | 2 +- fs/dax.c | 2 +- fs/ext4/file.c | 2 +- fs/iomap/apply.c | 2 +- fs/iomap/buffered-io.c | 75 +++++++++++++++++------ fs/iomap/direct-io.c | 3 +- fs/iomap/fiemap.c | 5 +- fs/iomap/seek.c | 6 +- fs/iomap/swapfile.c | 2 +- fs/nfs/file.c | 2 +- include/linux/fs.h | 9 ++- include/linux/iomap.h | 6 +- include/uapi/linux/fs.h | 5 +- mm/filemap.c | 132 ++++++++++++++++++++++++++++++++++++---- 14 files changed, 208 insertions(+), 45 deletions(-)
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerPATCH 1/5 fs: add read support for RWF_UNCACHED
If RWF_UNCACHED is set for io_uring (or preadv2(2)), we'll drop the cache for buffered reads if we are the ones instantiating it. If the data is already cached, we leave it cached.
--- include/linux/fs.h | 3 +++ include/uapi/linux/fs.h | 5 ++++- mm/filemap.c | 46 ++++++++++++++++++++++++++++++++++++----- 3 files changed, 48 insertions(+), 6 deletions(-)
include/linux/fs.h +3 -0
@@ -314,6 +314,7 @@ enum rw_hint {#define IOCB_SYNC (1 << 5)#define IOCB_WRITE (1 << 6)#define IOCB_NOWAIT (1 << 7) +#define IOCB_UNCACHED (1 << 8)struct kiocb {struct file *ki_filp;
@@ -3418,6 +3419,8 @@ static inline int kiocb_set_rw_flags(struct kiocb *ki, rwf_t flags) ki->ki_flags |= (IOCB_DSYNC |IOCB_SYNC );if (flags &RWF_APPEND ) ki->ki_flags |=IOCB_APPEND ; +if (flags &RWF_UNCACHED ) + ki->ki_flags |=IOCB_UNCACHED ;return 0 ; }
include/uapi/linux/fs.h +4 -1
@@ -299,8 +299,11 @@ typedef int __bitwise __kernel_rwf_t;/* per-IO O_APPEND */ #define RWF_APPEND ((__force __kernel_rwf_t)0x00000010) +/* drop cache after reading or writing data */ +#define RWF_UNCACHED ((__force __kernel_rwf_t)0x00000040) +/* mask of flags supported by the kernel */ #define RWF_SUPPORTED (RWF_HIPRI | RWF_DSYNC | RWF_SYNC | RWF_NOWAIT |\ - RWF_APPEND) + RWF_APPEND | RWF_UNCACHED)#endif /* _UAPI_LINUX_FS_H */
mm/filemap.c +41 -5
84 lines not on this page · raw message
Signature · 1 line
2.24.0
Jens Axboe
maintainerPATCH 2/5 mm: make generic_perform_write() take a struct kiocb
In reply to Jens AxboeRight now all callers pass in iocb->ki_pos, just pass in the iocb. This is in preparation for using the iocb flags in generic_perform_write().
--- fs/ceph/file.c | 2 +- fs/ext4/file.c | 2 +- fs/nfs/file.c | 2 +- include/linux/fs.h | 3 ++- mm/filemap.c | 8 +++++--- 5 files changed, 10 insertions(+), 7 deletions(-)
fs/ceph/file.c +1 -1
@@ -1538,7 +1538,7 @@ static ssize_t ceph_write_iter(struct kiocb *iocb, struct iov_iter *from)* are pending vmtruncate. So write and vmtruncate * can not run at the same time */ - written =generic_perform_write (file, from, pos); + written =generic_perform_write (file, from, iocb);if (likely (written >=0 )) iocb->ki_pos = pos + written;ceph_end_io_write (inode);
fs/ext4/file.c +1 -1
@@ -249,7 +249,7 @@ static ssize_t ext4_buffered_write_iter(struct kiocb *iocb, goto out; current->backing_dev_info =inode_to_bdi (inode); - ret =generic_perform_write (iocb->ki_filp, from, iocb->ki_pos); + ret =generic_perform_write (iocb->ki_filp, from, iocb); current->backing_dev_info =NULL ; out:
fs/nfs/file.c +1 -1
@@ -624,7 +624,7 @@ ssize_t nfs_file_write(struct kiocb *iocb, struct iov_iter *from) result =generic_write_checks (iocb, from);if (result >0 ) { current->backing_dev_info =inode_to_bdi (inode); - result =generic_perform_write (file, from, iocb->ki_pos); + result =generic_perform_write (file, from, iocb); current->backing_dev_info =NULL ; }nfs_end_io_write (inode);
include/linux/fs.h +2 -1
@@ -3103,7 +3103,8 @@ extern ssize_t generic_file_read_iter(struct kiocb *, struct iov_iter *);extern ssize_t __generic_file_write_iter (struct kiocb *,struct iov_iter *);extern ssize_t generic_file_write_iter (struct kiocb *,struct iov_iter *);extern ssize_t generic_file_direct_write (struct kiocb *,struct iov_iter *); -extern ssize_t generic_perform_write (struct file *,struct iov_iter *,loff_t ); +extern ssize_t generic_perform_write (struct file *,struct iov_iter *, +struct kiocb *);ssize_t vfs_iter_read (struct file *file,struct iov_iter *iter,loff_t *ppos,rwf_t flags);
mm/filemap.c +5 -3
@@ -3302,10 +3302,11 @@ struct page *grab_cache_page_write_begin(struct address_space *mapping,EXPORT_SYMBOL (grab_cache_page_write_begin);ssize_t generic_perform_write (struct file *file, -struct iov_iter *i,loff_t pos) +struct iov_iter *i,struct kiocb *iocb) {struct address_space *mapping = file->f_mapping;const struct address_space_operations *a_ops = mapping->a_ops; +loff_t pos = iocb->ki_pos;long status =0 ;ssize_t written =0 ;unsigned int flags =0 ;
@@ -3439,7 +3440,8 @@ ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)if (written <0 || !iov_iter_count (from) ||IS_DAX (inode)) goto out; - status =generic_perform_write (file, from, pos = iocb->ki_pos); + pos = iocb->ki_pos; + status =generic_perform_write (file, from, iocb);/* * If generic_perform_write() returned a synchronous error * then we want to return the number of bytes which were
@@ -3471,7 +3473,7 @@ ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from)*/ } }else { - written =generic_perform_write (file, from, iocb->ki_pos); + written =generic_perform_write (file, from, iocb);if (likely (written >0 )) iocb->ki_pos += written; }
Signature · 1 line
2.24.0
Jens Axboe
maintainerPATCH 3/5 mm: make buffered writes work with RWF_UNCACHED
In reply to Jens AxboeIf RWF_UNCACHED is set for io_uring (or pwritev2(2)), we'll drop the cache instantiated for buffered writes. If new pages aren't instantiated, we leave them alone. This provides similar semantics to reads with RWF_UNCACHED set.
--- include/linux/fs.h | 3 ++ mm/filemap.c | 78 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 76 insertions(+), 5 deletions(-)
include/linux/fs.h +3 -0
@@ -285,6 +285,7 @@ enum positive_aop_returns {#define AOP_FLAG_NOFS 0x0002/* used by filesystem to direct * helper code (eg buffer layer) * to clear GFP_FS from alloc */ +#define AOP_FLAG_UNCACHED 0x0004/* * oh the beauties of C type declarations.
@@ -3105,6 +3106,8 @@ extern ssize_t generic_file_write_iter(struct kiocb *, struct iov_iter *);extern ssize_t generic_file_direct_write (struct kiocb *,struct iov_iter *);extern ssize_t generic_perform_write (struct file *,struct iov_iter *,struct kiocb *); +extern void write_drop_cached_pages (struct page **, +struct address_space *mapping,unsigned *);ssize_t vfs_iter_read (struct file *file,struct iov_iter *iter,loff_t *ppos,rwf_t flags);
mm/filemap.c +73 -5
132 lines not on this page · raw message
Signature · 1 line
2.24.0
Jens Axboe
maintainerPATCH 4/5 iomap: pass in the write_begin/write_end flags to iomap_actor
In reply to Jens AxboeThis is in preparation for passing in a flag to the iomap_actor, which currently doesn't support that.
No functional changes in this patch.
--- fs/dax.c | 2 +- fs/iomap/apply.c | 2 +- fs/iomap/buffered-io.c | 17 ++++++++++------- fs/iomap/direct-io.c | 3 ++- fs/iomap/fiemap.c | 5 +++-- fs/iomap/seek.c | 6 ++++-- fs/iomap/swapfile.c | 2 +- include/linux/iomap.h | 5 +++-- 8 files changed, 25 insertions(+), 17 deletions(-)
fs/dax.c +1 -1
@@ -1091,7 +1091,7 @@ EXPORT_SYMBOL_GPL(__dax_zero_page_range);static loff_t dax_iomap_actor (struct inode *inode,loff_t pos,loff_t length,void *data, -struct iomap *iomap,struct iomap *srcmap) +unsigned flags,struct iomap *iomap,struct iomap *srcmap) {struct block_device *bdev = iomap->bdev;struct dax_device *dax_dev = iomap->dax_dev;
fs/iomap/apply.c +1 -1
@@ -77,7 +77,7 @@ iomap_apply(struct inode *inode, loff_t pos, loff_t length, unsigned flags,* iomap into the actors so that they don't need to have special * handling for the two cases. */ - written =actor (inode, pos, length, data, &iomap, + written =actor (inode, pos, length, data, flags, &iomap, srcmap.type !=IOMAP_HOLE ? &srcmap : &iomap);/*
fs/iomap/buffered-io.c +10 -7
@@ -249,7 +249,7 @@ static inline bool iomap_block_needs_zeroing(struct inode *inode,static loff_t iomap_readpage_actor (struct inode *inode,loff_t pos,loff_t length,void *data, -struct iomap *iomap,struct iomap *srcmap) +unsigned flags,struct iomap *iomap,struct iomap *srcmap) {struct iomap_readpage_ctx *ctx = data;struct page *page = ctx->cur_page;
@@ -397,7 +397,8 @@ iomap_next_page(struct inode *inode, struct list_head *pages, loff_t pos,static loff_t iomap_readpages_actor (struct inode *inode,loff_t pos,loff_t length, -void *data,struct iomap *iomap,struct iomap *srcmap) +void *data,unsigned flags,struct iomap *iomap, +struct iomap *srcmap) {struct iomap_readpage_ctx *ctx = data;loff_t done, ret;
@@ -417,7 +418,7 @@ iomap_readpages_actor(struct inode *inode, loff_t pos, loff_t length, ctx->cur_page_in_bio = false; } ret =iomap_readpage_actor (inode, pos + done, length - done, - ctx, iomap, srcmap); + ctx,0 , iomap, srcmap); }return done;
@@ -797,7 +798,7 @@ iomap_write_end(struct inode *inode, loff_t pos, unsigned len, unsigned copied,static loff_t iomap_write_actor (struct inode *inode,loff_t pos,loff_t length,void *data, -struct iomap *iomap,struct iomap *srcmap) +unsigned flags,struct iomap *iomap,struct iomap *srcmap) {struct iov_iter *i = data;long status =0 ;
@@ -897,7 +898,7 @@ EXPORT_SYMBOL_GPL(iomap_file_buffered_write);static loff_t iomap_unshare_actor (struct inode *inode,loff_t pos,loff_t length,void *data, -struct iomap *iomap,struct iomap *srcmap) +unsigned flags,struct iomap *iomap,struct iomap *srcmap) {long status =0 ;ssize_t written =0 ;
@@ -983,7 +984,8 @@ static int iomap_dax_zero(loff_t pos, unsigned offset, unsigned bytes,static loff_t iomap_zero_range_actor (struct inode *inode,loff_t pos,loff_t count, -void *data,struct iomap *iomap,struct iomap *srcmap) +void *data,unsigned flags,struct iomap *iomap, +struct iomap *srcmap) {bool *did_zero = data;loff_t written =0 ;
@@ -1053,7 +1055,8 @@ EXPORT_SYMBOL_GPL(iomap_truncate_page);static loff_t iomap_page_mkwrite_actor (struct inode *inode,loff_t pos,loff_t length, -void *data,struct iomap *iomap,struct iomap *srcmap) +void *data,unsigned flags,struct iomap *iomap, +struct iomap *srcmap) {struct page *page = data;int ret;
fs/iomap/direct-io.c +2 -1
@@ -365,7 +365,8 @@ iomap_dio_inline_actor(struct inode *inode, loff_t pos, loff_t length,static loff_t iomap_dio_actor (struct inode *inode,loff_t pos,loff_t length, -void *data,struct iomap *iomap,struct iomap *srcmap) +void *data,unsigned flags,struct iomap *iomap, +struct iomap *srcmap) {struct iomap_dio *dio = data;
fs/iomap/fiemap.c +3 -2
@@ -44,7 +44,7 @@ static int iomap_to_fiemap(struct fiemap_extent_info *fi,static loff_t iomap_fiemap_actor (struct inode *inode,loff_t pos,loff_t length,void *data, -struct iomap *iomap,struct iomap *srcmap) +unsigned flags,struct iomap *iomap,struct iomap *srcmap) {struct fiemap_ctx *ctx = data;loff_t ret = length;
@@ -111,7 +111,8 @@ EXPORT_SYMBOL_GPL(iomap_fiemap);static loff_t iomap_bmap_actor (struct inode *inode,loff_t pos,loff_t length, -void *data,struct iomap *iomap,struct iomap *srcmap) +void *data,unsigned flags,struct iomap *iomap, +struct iomap *srcmap) {sector_t *bno = data, addr;
fs/iomap/seek.c +4 -2
@@ -119,7 +119,8 @@ page_cache_seek_hole_data(struct inode *inode, loff_t offset, loff_t length,static loff_t iomap_seek_hole_actor (struct inode *inode,loff_t offset,loff_t length, -void *data,struct iomap *iomap,struct iomap *srcmap) +void *data,unsigned flags,struct iomap *iomap, +struct iomap *srcmap) {switch (iomap->type) {case IOMAP_UNWRITTEN :
@@ -165,7 +166,8 @@ EXPORT_SYMBOL_GPL(iomap_seek_hole);static loff_t iomap_seek_data_actor (struct inode *inode,loff_t offset,loff_t length, -void *data,struct iomap *iomap,struct iomap *srcmap) +void *data,unsigned flags,struct iomap *iomap, +struct iomap *srcmap) {switch (iomap->type) {case IOMAP_HOLE :
fs/iomap/swapfile.c +1 -1
@@ -76,7 +76,7 @@ static int iomap_swapfile_add_extent(struct iomap_swapfile_info *isi)* distinction between written and unwritten extents. */ static loff_t iomap_swapfile_activate_actor (struct inode *inode,loff_t pos, -loff_t count,void *data,struct iomap *iomap, +loff_t count,void *data,unsigned flags,struct iomap *iomap,struct iomap *srcmap) {struct iomap_swapfile_info *isi = data;
include/linux/iomap.h +3 -2
@@ -113,7 +113,7 @@ struct iomap_page_ops { };/* -* Flags for iomap_begin / iomap_end. No flag implies a read. +* Flags for iomap_begin / iomap_end / factor. No flag implies a read. */ #define IOMAP_WRITE (1 << 0)/* writing, must allocate blocks */ #define IOMAP_ZERO (1 << 1)/* zeroing operation, may skip holes */
@@ -146,7 +146,8 @@ struct iomap_ops {* Main iomap iterator function. */ typedef loff_t (*iomap_actor_t )(struct inode *inode,loff_t pos,loff_t len, -void *data,struct iomap *iomap,struct iomap *srcmap); +void *data,unsigned flags,struct iomap *iomap, +struct iomap *srcmap); loff_t iomap_apply(struct inode *inode, loff_t pos, loff_t length, unsigned flags, const struct iomap_ops *ops, void *data,
Signature · 1 line
2.24.0
Jens Axboe
maintainerPATCH 5/5 iomap: support RWF_UNCACHED for buffered writes
In reply to Jens AxboeThis adds support for RWF_UNCACHED for file systems using iomap to perform buffered writes. We use the generic infrastructure for this, by tracking pages we created and calling write_drop_cached_pages() to issue writeback and prune those pages.
--- fs/iomap/buffered-io.c | 58 ++++++++++++++++++++++++++++++++++-------- include/linux/iomap.h | 1 + 2 files changed, 49 insertions(+), 10 deletions(-)
fs/iomap/buffered-io.c +48 -10
@@ -566,6 +566,7 @@ EXPORT_SYMBOL_GPL(iomap_migrate_page);enum {IOMAP_WRITE_F_UNSHARE = (1 <<0 ), +IOMAP_WRITE_F_UNCACHED = (1 <<1 ), }; static void
@@ -643,6 +644,7 @@ iomap_write_begin(struct inode *inode, loff_t pos, unsigned len, unsigned flags, struct page **pagep, struct iomap *iomap, struct iomap *srcmap) { const struct iomap_page_ops *page_ops = iomap->page_ops; +unsigned aop_flags;struct page *page;int status =0 ;
@@ -659,8 +661,11 @@ iomap_write_begin(struct inode *inode, loff_t pos, unsigned len, unsigned flags,return status; } + aop_flags =AOP_FLAG_NOFS ; +if (flags &IOMAP_UNCACHED ) + aop_flags |=AOP_FLAG_UNCACHED ; page =grab_cache_page_write_begin (inode->i_mapping, pos >>PAGE_SHIFT , -AOP_FLAG_NOFS ); + aop_flags);if (!page) { status = -ENOMEM ; goto out_no_page;
@@ -670,9 +675,14 @@ iomap_write_begin(struct inode *inode, loff_t pos, unsigned len, unsigned flags,iomap_read_inline_data (inode, page, srcmap); else if (iomap->flags & IOMAP_F_BUFFER_HEAD) status =__block_write_begin_int (page, pos, len,NULL , srcmap); - else - status =__iomap_write_begin (inode, pos, len, flags, page, + else { +unsigned wb_flags =0 ; + +if (flags &IOMAP_UNCACHED ) + wb_flags =IOMAP_WRITE_F_UNCACHED ; + status =__iomap_write_begin (inode, pos, len, wb_flags, page, srcmap); + }if (unlikely (status)) goto out_unlock;
@@ -796,19 +806,25 @@ iomap_write_end(struct inode *inode, loff_t pos, unsigned len, unsigned copied,return ret; } +#define GPW_PAGE_BATCH 16 + static loff_t iomap_write_actor(struct inode *inode, loff_t pos, loff_t length, void *data, unsigned flags, struct iomap *iomap, struct iomap *srcmap) { + struct address_space *mapping = inode->i_mapping; + struct page *drop_pages[GPW_PAGE_BATCH]; struct iov_iter *i = data; long status = 0; ssize_t written = 0; + unsigned drop_nr = 0; do { struct page *page; unsigned long offset; /* Offset into pagecache page */ unsigned long bytes; /* Bytes to write to page */ size_t copied; /* Bytes copied from user */ + bool drop_page = false; /* drop page after IO */ offset = offset_in_page(pos); bytes = min_t(unsigned long, PAGE_SIZE - offset,
@@ -832,10 +848,17 @@ iomap_write_actor(struct inode *inode, loff_t pos, loff_t length, void *data,break ; } - status =iomap_write_begin (inode, pos, bytes,0 , &page, iomap, - srcmap); -if (unlikely (status)) +retry: + status =iomap_write_begin (inode, pos, bytes, flags, &page, + iomap, srcmap); +if (unlikely (status)) { +if (status == -ENOMEM && (flags &IOMAP_UNCACHED )) { + drop_page = true; + flags &= ~IOMAP_UNCACHED ; + goto retry; + }break ; + }if (mapping_writably_mapped (inode->i_mapping))flush_dcache_page (page);
@@ -866,13 +889,24 @@ iomap_write_actor(struct inode *inode, loff_t pos, loff_t length, void *data, iov_iter_single_seg_count(i)); goto again; } + +if (drop_page && + ((pos >>PAGE_SHIFT ) != ((pos + copied) >>PAGE_SHIFT ))) { + drop_pages[drop_nr] = page; +if (++drop_nr ==GPW_PAGE_BATCH ) +write_drop_cached_pages (drop_pages, mapping, + &drop_nr); + }else +balance_dirty_pages_ratelimited (inode->i_mapping); + pos += copied; written += copied; length -= copied; - -balance_dirty_pages_ratelimited (inode->i_mapping); }while (iov_iter_count (i) && length); +if (drop_nr) +write_drop_cached_pages (drop_pages, mapping, &drop_nr); +return written ? written : status; }
@@ -882,10 +916,14 @@ iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *iter, {struct inode *inode = iocb->ki_filp->f_mapping->host;loff_t pos = iocb->ki_pos, ret =0 , written =0 ; +unsigned flags =IOMAP_WRITE ; + +if (iocb->ki_flags &IOCB_UNCACHED ) + flags |=IOMAP_UNCACHED ;while (iov_iter_count (iter)) { - ret =iomap_apply (inode, pos,iov_iter_count (iter), -IOMAP_WRITE , ops, iter, iomap_write_actor); + ret =iomap_apply (inode, pos,iov_iter_count (iter), flags, + ops, iter, iomap_write_actor);if (ret <=0 )break ; pos += ret;
include/linux/iomap.h +1 -0
@@ -121,6 +121,7 @@ struct iomap_page_ops {#define IOMAP_FAULT (1 << 3)/* mapping for page fault */ #define IOMAP_DIRECT (1 << 4)/* direct I/O */ #define IOMAP_NOWAIT (1 << 5)/* do not block */ +#define IOMAP_UNCACHED (1 << 6)struct iomap_ops {/*
Signature · 1 line
2.24.0
Matthew Wilcox
maintainerRe: [PATCH 3/5] mm: make buffered writes work with RWF_UNCACHED
In reply to Jens Axboemm/filemap.c struct page *grab_cache_page_write_begin(struct address_space *mapping, · line 3311
Quoted 6 lines · Jens Axboe
mm/filemap.c +6
-0+/* +* Start writeback on the pages in pgs[], and then try and remove those pages +* from the page cached. Used with RWF_UNCACHED. +*/ +void write_drop_cached_pages (struct page **pgs,struct address_space *mapping, +unsigned *nr)
It would seem more natural to use a pagevec instead of pgs/nr.
mm/filemap.c struct page *grab_cache_page_write_begin(struct address_space *mapping, · line 3322
Quoted 11 lines
mm/filemap.c +11
-0+{ +loff_t start, end; +int i; + + end =0 ; + start =LLONG_MAX ; +for (i =0 ; i < *nr; i++) { +struct page *page = pgs[i]; +loff_t off; + + off = (loff_t )page_to_index (page) <<PAGE_SHIFT ;
Isn't that page_offset()?
mm/filemap.c struct page *grab_cache_page_write_begin(struct address_space *mapping, · line 3336
Quoted 7 lines
mm/filemap.c +7
-0+__filemap_fdatawrite_range (mapping, start, end,WB_SYNC_NONE ); + +for (i =0 ; i < *nr; i++) { +struct page *page = pgs[i]; + +lock_page (page); +if (page->mapping == mapping) {
So you're protecting against the page being freed and reallocated to a different file, but not against the page being freed and reallocated to a location in the same file which is outside (start, end)?
Jens Axboe
maintainerRe: [PATCH 3/5] mm: make buffered writes work with RWF_UNCACHED
Quoted 9 lines · Matthew Wilcox
mm/filemap.c struct page *grab_cache_page_write_begin(struct address_space *mapping, · line 3311
Quoted 6 lines · Jens Axboe
mm/filemap.c +6
-0+/* +* Start writeback on the pages in pgs[], and then try and remove those pages +* from the page cached. Used with RWF_UNCACHED. +*/ +void write_drop_cached_pages (struct page **pgs,struct address_space *mapping, +unsigned *nr)It would seem more natural to use a pagevec instead of pgs/nr.
I did look into that, but they are intertwined with LRU etc. I deliberately avoided the LRU on the read side, as it adds noticeable overhead and gains us nothing since the pages will be dropped agian.
Quoted 5 lines
Quoted 5 lines
mm/filemap.c +5
-0+{ +loff_t start, end; +int i; + + end =0 ;
Quoted 8 lines
mm/filemap.c struct page *grab_cache_page_write_begin(struct address_space *mapping, · line 3322
Quoted 6 lines
mm/filemap.c +6
-0+ start =LLONG_MAX ; +for (i =0 ; i < *nr; i++) { +struct page *page = pgs[i]; +loff_t off; + + off = (loff_t )page_to_index (page) <<PAGE_SHIFT ;Isn't that
page_offset()?
I guess it is! I'll make that change.
Quoted 11 lines
mm/filemap.c struct page *grab_cache_page_write_begin(struct address_space *mapping, · line 3336
Quoted 7 lines
mm/filemap.c +7
-0+__filemap_fdatawrite_range (mapping, start, end,WB_SYNC_NONE ); + +for (i =0 ; i < *nr; i++) { +struct page *page = pgs[i]; + +lock_page (page); +if (page->mapping == mapping) {So you're protecting against the page being freed and reallocated to a different file, but not against the page being freed and reallocated to a location in the same file which is outside (start, end)?
I guess so, we can add that too, probably just check if the index is still the same. More of a behavioral thing, shouldn't be any correctness issues there.
Signature · 1 line
Jens Axboe
Re: [PATCH 3/5] mm: make buffered writes work with RWF_UNCACHED
Quoted 46 lines · Jens Axboe
Quoted 11 lines · Matthew Wilcox
mm/filemap.c struct page *grab_cache_page_write_begin(struct address_space *mapping, · line 3311
Quoted 8 lines · Jens Axboe
+/*
+ * Start writeback on the pages in pgs[], and then try and remove those pages
+ * from the page cached. Used withRWF_UNCACHED.
+ */
+void write_drop_cached_pages(struct page **pgs, structaddress_space*mapping,mm/filemap.c +1
-0+ unsigned *nr)It would seem more natural to use a pagevec instead of pgs/nr.
I did look into that, but they are intertwined with LRU etc. I deliberately avoided the LRU on the read side, as it adds noticeable overhead and gains us nothing since the pages will be dropped agian.
Quoted 5 lines
Quoted 5 lines
mm/filemap.c +5
-0+{ +loff_t start, end; +int i; + + end =0 ;Quoted 8 lines
mm/filemap.c struct page *grab_cache_page_write_begin(struct address_space *mapping, · line 3322
Quoted 6 lines
mm/filemap.c +6
-0+ start =LLONG_MAX ; +for (i =0 ; i < *nr; i++) { +struct page *page = pgs[i]; +loff_t off; + + off = (loff_t )page_to_index (page) <<PAGE_SHIFT ;Isn't that
page_offset()?I guess it is! I'll make that change.
Quoted 4 lines
Quoted 4 lines
mm/filemap.c +4
-0+__filemap_fdatawrite_range (mapping, start, end,WB_SYNC_NONE ); + +for (i =0 ; i < *nr; i++) { +struct page *page = pgs[i];Quoted 8 lines
mm/filemap.c struct page *grab_cache_page_write_begin(struct address_space *mapping, · line 3336
Quoted 3 lines
mm/filemap.c +3
-0+ +lock_page (page); +if (page->mapping == mapping) {So you're protecting against the page being freed and reallocated to a
different file, but not against the page being freed and reallocated to a location in the same file which is outside (start, end)?
Quoted 3 lines
I guess so, we can add that too, probably just check if the index is still the same. More of a behavioral thing, shouldn't be any correctness issues there.
Since we have a reference on the page, the mapping can go to NULL but otherwise it should stay in the same mapping at the same offset.
But, Jens and I both just realized he needs to take the reference on the page before write_end is called.
-chris
Matthew Wilcox
maintainerRe: [PATCH 3/5] mm: make buffered writes work with RWF_UNCACHED
In reply to Jens AxboeQuoted 11 lines · Jens Axboe
Quoted 9 lines · Matthew Wilcox
mm/filemap.c struct page *grab_cache_page_write_begin(struct address_space *mapping, · line 3311
Quoted 6 lines · Jens Axboe
mm/filemap.c +6
-0+/* +* Start writeback on the pages in pgs[], and then try and remove those pages +* from the page cached. Used with RWF_UNCACHED. +*/ +void write_drop_cached_pages (struct page **pgs,struct address_space *mapping, +unsigned *nr)It would seem more natural to use a pagevec instead of pgs/nr.
Quoted 3 lines
I did look into that, but they are intertwined with LRU etc. I deliberately avoided the LRU on the read side, as it adds noticeable overhead and gains us nothing since the pages will be dropped agian.
I agree the LRU uses them, but they're used in all kinds of places where we need to batch pages, eg truncate, munlock.
Jens Axboe
maintainerRe: [PATCH 3/5] mm: make buffered writes work with RWF_UNCACHED
Quoted 12 lines · Matthew Wilcox
Quoted 11 lines · Jens Axboe
Quoted 9 lines · Matthew Wilcox
mm/filemap.c struct page *grab_cache_page_write_begin(struct address_space *mapping, · line 3311
Quoted 6 lines · Jens Axboe
mm/filemap.c +6
-0+/* +* Start writeback on the pages in pgs[], and then try and remove those pages +* from the page cached. Used with RWF_UNCACHED. +*/ +void write_drop_cached_pages (struct page **pgs,struct address_space *mapping, +unsigned *nr)It would seem more natural to use a pagevec instead of pgs/nr.
Quoted 6 lines
Quoted 3 lines
I did look into that, but they are intertwined with LRU etc. I deliberately avoided the LRU on the read side, as it adds noticeable overhead and gains us nothing since the pages will be dropped agian.
I agree the LRU uses them, but they're used in all kinds of places where we need to batch pages, eg truncate, munlock.
I think I just had to convince myself that the release business works the way it should for my use case, and I think it does. I'll test it. release_pages() isn't exactly the prettiest thing out there.
Signature · 1 line
Jens Axboe
Quoted 25 lines · Jens Axboe
Recently someone asked me how
io_uringbuffered IO compares to mmaped IO in terms of performance. So I ran some tests with buffered IO, and found the experience to be somewhat painful. The test case is pretty basic, random reads over a dataset that's 10x the size of RAM. Performance starts out fine, and then the page cache fills up and we hit a throughput cliff. CPU usage of the IO threads go up, and we have kswapd spending 100% of a core trying to keep up. Seeing that, I was reminded of the many complaints I here about buffered IO, and the fact that most of the folks complaining will ultimately bite the bullet and move toO_DIRECTto just get the kernel out of the way.But I don't think it needs to be like that. Switching to
O_DIRECTisn't always easily doable. The buffers have different life times, size and alignment constraints, etc. On top of that, mixing buffered andO_DIRECTcan be painful.Seems to me that we have an opportunity to provide something that sits somewhere in between buffered and
O_DIRECT, and this is whereRWF_UNCACHEDenters the picture. If this flag is set on IO, we get the following behavior:- If the data is in cache, it remains in cache and the copy (in or out) is served to/from that.
Quoted 8 lines
- If the data is NOT in cache, we add it while performing the IO. When the IO is done, we remove it again.
With this, I can do 100% smooth buffered reads or writes without pushing the kernel to the state where kswapd is sweating bullets. In fact it doesn't even register.
Comments appreciated!
I think this is a definite win for e.g. NVMe/Optane devices where the underlying storage is fast enough to avoid the need for page cache.
In our testing of Lustre on NVMe, it was faster to avoid the page cache entirely - just inserting and removing the pages from cache took a considerable amount of CPU for workloads where we knew it was not beneficial (e.g. IO that was large enough that the storage was as fast as the network).
This also makes it easier to keep other data in cache (e.g. filesystem metadata, small IOs, etc.).
Cheers, Andreas
Quoted 20 lines
Patches are against current git (ish), and can also be found here:
https://git.kernel.dk/cgit/linux-block/log/?h=buffered-uncached
fs/ceph/file.c | 2 +- fs/dax.c | 2 +- fs/ext4/file.c | 2 +- fs/iomap/apply.c | 2 +- fs/iomap/buffered-io.c | 75 +++++++++++++++++------ fs/iomap/direct-io.c | 3 +- fs/iomap/fiemap.c | 5 +- fs/iomap/seek.c | 6 +- fs/iomap/swapfile.c | 2 +- fs/nfs/file.c | 2 +- include/linux/fs.h | 9 ++- include/linux/iomap.h | 6 +- include/uapi/linux/fs.h | 5 +- mm/filemap.c | 132 ++++++++++++++++++++++++++++++++++++---- 14 files changed, 208 insertions(+), 45 deletions(-)
Quoted 4 lines
--
Jens Axboe
Cheers, Andreas
Re: [PATCH 3/5] mm: make buffered writes work with RWF_UNCACHED
In reply to Jens AxboeQuoted 4 lines · Jens Axboe
If
RWF_UNCACHEDis set forio_uring(or pwritev2(2)), we'll drop the cache instantiated for buffered writes. If new pages aren't instantiated, we leave them alone. This provides similar semantics to reads withRWF_UNCACHEDset.
So what about filesystems that don't use generic_perform_write()? i.e. Anything that uses the iomap infrastructure (i.e. iomap_file_buffered_write()) instead of generic_file_write_iter()) will currently ignore RWF_UNCACHED. That's XFS and gfs2 right now, but there are likely to be more in the near future as more filesystems are ported to the iomap infrastructure.
I'd also really like to see extensive fsx and fstress testing of this new IO mode before it is committed - this is going to exercise page cache coherency across different operations in new and unique ways. that means we need patches to fstests to detect and use this functionality when available, and new tests that explicitly exercise combinations of buffered, mmap, dio and uncached for a range of different IO size and alignments (e.g. mixing sector sized uncached IO with page sized buffered/mmap/dio and vice versa).
We are not going to have a repeat of the copy_file_range() data corruption fuckups because no testing was done and no test infrastructure was written before the new API was committed.
Quoted 29 lines
mm/filemap.c +29
-0+void write_drop_cached_pages (struct page **pgs,struct address_space *mapping, +unsigned *nr) +{ +loff_t start, end; +int i; + + end =0 ; + start =LLONG_MAX ; +for (i =0 ; i < *nr; i++) { +struct page *page = pgs[i]; +loff_t off; + + off = (loff_t )page_to_index (page) <<PAGE_SHIFT ; +if (off < start) + start = off; +if (off > end) + end = off; +get_page (page); + } + +__filemap_fdatawrite_range (mapping, start, end,WB_SYNC_NONE ); + +for (i =0 ; i < *nr; i++) { +struct page *page = pgs[i]; + +lock_page (page); +if (page->mapping == mapping) { +wait_on_page_writeback (page); + if (!page_has_private(page) ||
mm/filemap.c struct page *grab_cache_page_write_begin(struct address_space *mapping, · line 3348
Quoted 10 lines
mm/filemap.c +10
-0+ try_to_release_page(page, 0)) +remove_mapping (mapping, page); + } +unlock_page (page); + } + *nr =0 ; +} +EXPORT_SYMBOL_GPL (write_drop_cached_pages); + +#define GPW_PAGE_BATCH 16
In terms of performance, file fragmentation and premature filesystem aging, this is also going to suck *really badly* for filesystems that use delayed allocation because it is going to force conversion of delayed allocation extents during the write() call. IOWs, it adds all the overheads of doing delayed allocation, but it reaps none of the benefits because it doesn't allow large contiguous extents to build up in memory before physical allocation occurs. i.e. there is no "delayed" in this allocation....
So it might work fine on a pristine, empty filesystem where it is easy to find contiguous free space accross multiple allocations, but it's going to suck after a few months of production usage has fragmented all the free space into tiny pieces...
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Re: [PATCH 3/5] mm: make buffered writes work with RWF_UNCACHED
Quoted 6 lines · Dave Chinner
Quoted 4 lines · Jens Axboe
If
RWF_UNCACHEDis set forio_uring(or pwritev2(2)), we'll drop the cache instantiated for buffered writes. If new pages aren't instantiated, we leave them alone. This provides similar semantics to reads withRWF_UNCACHEDset.
Quoted 6 lines
So what about filesystems that don't use
generic_perform_write()? i.e. Anything that uses the iomap infrastructure (i.e.iomap_file_buffered_write()) instead ofgeneric_file_write_iter()) will currently ignoreRWF_UNCACHED. That's XFS and gfs2 right now, but there are likely to be more in the near future as more filesystems are ported to the iomap infrastructure.
Hmmm - I'm missing part of this patchset, and I see a second version has been posted that has iomap stuff in it. I'll go look at that now...
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Jens Axboe
maintainerRe: [PATCH 3/5] mm: make buffered writes work with RWF_UNCACHED
In reply to Dave ChinnerQuoted 6 lines · Dave Chinner
Quoted 4 lines · Jens Axboe
If
RWF_UNCACHEDis set forio_uring(or pwritev2(2)), we'll drop the cache instantiated for buffered writes. If new pages aren't instantiated, we leave them alone. This provides similar semantics to reads withRWF_UNCACHEDset.
Quoted 6 lines
So what about filesystems that don't use
generic_perform_write()? i.e. Anything that uses the iomap infrastructure (i.e.iomap_file_buffered_write()) instead ofgeneric_file_write_iter()) will currently ignoreRWF_UNCACHED. That's XFS and gfs2 right now, but there are likely to be more in the near future as more filesystems are ported to the iomap infrastructure.
I'll skip this one as you found it.
Quoted 9 lines
I'd also really like to see extensive fsx and fstress testing of this new IO mode before it is committed - this is going to exercise page cache coherency across different operations in new and unique ways. that means we need patches to fstests to detect and use this functionality when available, and new tests that explicitly exercise combinations of buffered, mmap, dio and uncached for a range of different IO size and alignments (e.g. mixing sector sized uncached IO with page sized buffered/mmap/dio and vice versa).
Quoted 3 lines
We are not going to have a repeat of the
copy_file_range()data corruption fuckups because no testing was done and no test infrastructure was written before the new API was committed.
Oh I totally agree, and there's no push from my end on this. I just think it's a cool feature and could be very useful, but it obviously needs a healthy dose of testing and test cases written. I'll be doing that as well.
Quoted 49 lines
Quoted 29 lines
mm/filemap.c +29
-0+void write_drop_cached_pages (struct page **pgs,struct address_space *mapping, +unsigned *nr) +{ +loff_t start, end; +int i; + + end =0 ; + start =LLONG_MAX ; +for (i =0 ; i < *nr; i++) { +struct page *page = pgs[i]; +loff_t off; + + off = (loff_t )page_to_index (page) <<PAGE_SHIFT ; +if (off < start) + start = off; +if (off > end) + end = off; +get_page (page); + } + +__filemap_fdatawrite_range (mapping, start, end,WB_SYNC_NONE ); + +for (i =0 ; i < *nr; i++) { +struct page *page = pgs[i]; + +lock_page (page); +if (page->mapping == mapping) { +wait_on_page_writeback (page); + if (!page_has_private(page) ||mm/filemap.c struct page *grab_cache_page_write_begin(struct address_space *mapping, · line 3348
Quoted 10 lines
mm/filemap.c +10
-0+ try_to_release_page(page, 0)) +remove_mapping (mapping, page); + } +unlock_page (page); + } + *nr =0 ; +} +EXPORT_SYMBOL_GPL (write_drop_cached_pages); + +#define GPW_PAGE_BATCH 16In terms of performance, file fragmentation and premature filesystem aging, this is also going to suck *really badly* for filesystems that use delayed allocation because it is going to force conversion of delayed allocation extents during the
write()call. IOWs, it adds all the overheads of doing delayed allocation, but it reaps none of the benefits because it doesn't allow large contiguous extents to build up in memory before physical allocation occurs. i.e. there is no "delayed" in this allocation....
Quoted 4 lines
So it might work fine on a pristine, empty filesystem where it is easy to find contiguous free space accross multiple allocations, but it's going to suck after a few months of production usage has fragmented all the free space into tiny pieces...
I totally agree on this one, and I'm not a huge fan of it. But considering your suggestion in the other email, I think we just need to move this up a notch and do it per-write instead. If we can pass back information about the state of the page cache for the range we care about, then there's no reason to do it per-page for the write case. Reads are still best done that way, and we can avoid the LRU overhead by doing it that way.
Signature · 1 line
Jens Axboe
Quoted 11 lines · Jens Axboe
Seems to me that we have an opportunity to provide something that sits somewhere in between buffered and
O_DIRECT, and this is whereRWF_UNCACHEDenters the picture. If this flag is set on IO, we get the following behavior:- If the data is in cache, it remains in cache and the copy (in or out) is served to/from that.
- If the data is NOT in cache, we add it while performing the IO. When the IO is done, we remove it again.
Quoted 6 lines
With this, I can do 100% smooth buffered reads or writes without pushing the kernel to the state where kswapd is sweating bullets. In fact it doesn't even register.
Comments appreciated! Patches are against current git (ish), and can also be found here:
I can't say I particularly like the model, as it still has all the page cache overhead. Direct I/O with bounce buffers for unaligned I/O sounds simpler and faster to me.
Jens Axboe
maintainerQuoted 16 lines · Christoph Hellwig
Quoted 15 lines · Jens Axboe
Seems to me that we have an opportunity to provide something that sits somewhere in between buffered and
O_DIRECT, and this is whereRWF_UNCACHEDenters the picture. If this flag is set on IO, we get the following behavior:- If the data is in cache, it remains in cache and the copy (in or out) is served to/from that.
- If the data is NOT in cache, we add it while performing the IO. When the IO is done, we remove it again.
With this, I can do 100% smooth buffered reads or writes without pushing the kernel to the state where kswapd is sweating bullets. In fact it doesn't even register.
Quoted 6 lines
Quoted 2 lines
Comments appreciated! Patches are against current git (ish), and can also be found here:
I can't say I particularly like the model, as it still has all the page cache overhead. Direct I/O with bounce buffers for unaligned I/O sounds simpler and faster to me.
The current patchset read side does not, hopefully the same can be done on the write side. No page cache usage for reads, because it did indeed turn out to have too much overhead.
Signature · 1 line
Jens Axboe