mm/filemap: don't initiate writeback if mapping has no dirty pages
Subsystems (3)
PATCH 1/2 mm/filemap: don't initiate writeback if mapping has no dirty pages
Functions like filemap_write_and_wait_range() should do nothing if inode has no dirty pages or pages currently under writeback. But they anyway construct struct writeback_control and this does some atomic operations if CONFIG_CGROUP_WRITEBACK=y - on fast path it locks inode->i_lock and updates state of writeback ownership, on slow path might be more work. Current this path is safely avoided only when inode mapping has no pages.
For example generic_file_read_iter() calls filemap_write_and_wait_range() at each O_DIRECT read - pretty hot path.
This patch skips starting new writeback if mapping has no dirty tags set. If writeback is already in progress filemap_write_and_wait_range() will wait for it.
--- mm/filemap.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
mm/filemap.c +2 -1
@@ -408,7 +408,8 @@ int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start, .range_end = end, }; -if (!mapping_cap_writeback_dirty (mapping)) +if (!mapping_cap_writeback_dirty (mapping) || + !mapping_tagged (mapping,PAGECACHE_TAG_DIRTY ))return 0 ;wbc_attach_fdatawrite_inode (&wbc, mapping->host);
PATCH 2/2 mm/filemap: rewrite mapping_needs_writeback in less fancy manner
This actually checks that writeback is needed or in progress.
--- mm/filemap.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
mm/filemap.c +5 -2
@@ -618,10 +618,13 @@ int filemap_fdatawait_keep_errors(struct address_space *mapping) }EXPORT_SYMBOL (filemap_fdatawait_keep_errors); +/* Returns true if writeback might be needed or already in progress. */ static bool mapping_needs_writeback (struct address_space *mapping) { -return (!dax_mapping (mapping) && mapping->nrpages) || - (dax_mapping (mapping) && mapping->nrexceptional); +if (dax_mapping (mapping)) +return mapping->nrexceptional; + +return mapping->nrpages; }int filemap_write_and_wait (struct address_space *mapping)
Andrew Morton
maintainer(cc linux-fsdevel and Jan)
Quoted 19 lines · On Mon, 22 Jul 2019 12:36:08 +0300 Konstantin Khlebnikov…
Functions like
filemap_write_and_wait_range()should do nothing if inode has no dirty pages or pages currently under writeback. But they anyway construct structwriteback_controland this does some atomic operations ifCONFIG_CGROUP_WRITEBACK=y- on fast path it locksinode->i_lockand updates state of writeback ownership, on slow path might be more work. Current this path is safely avoided only when inode mapping has no pages.For example
generic_file_read_iter()callsfilemap_write_and_wait_range()at eachO_DIRECTread - pretty hot path.This patch skips starting new writeback if mapping has no dirty tags set. If writeback is already in progress
filemap_write_and_wait_range()will wait for it....
mm/filemap.c int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start, · line 415
Quoted 9 lines
mm/filemap.c +2
-1@@ -408,7 +408,8 @@ int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start, .range_end = end, }; -if (!mapping_cap_writeback_dirty (mapping)) +if (!mapping_cap_writeback_dirty (mapping) || + !mapping_tagged (mapping,PAGECACHE_TAG_DIRTY ))return 0 ;wbc_attach_fdatawrite_inode (&wbc, mapping->host);
How does this play with tagged_writepages? We assume that no tagging has been performed by any __filemap_fdatawrite_range() caller?
Quoted 32 lines · Andrew Morton
(cc linux-fsdevel and Jan)
Quoted 27 lines · On Mon, 22 Jul 2019 12:36:08 +0300 Konstantin Khlebnikov…
Functions like
filemap_write_and_wait_range()should do nothing if inode has no dirty pages or pages currently under writeback. But they anyway construct structwriteback_controland this does some atomic operations ifCONFIG_CGROUP_WRITEBACK=y- on fast path it locksinode->i_lockand updates state of writeback ownership, on slow path might be more work. Current this path is safely avoided only when inode mapping has no pages.For example
generic_file_read_iter()callsfilemap_write_and_wait_range()at eachO_DIRECTread - pretty hot path.This patch skips starting new writeback if mapping has no dirty tags set. If writeback is already in progress
filemap_write_and_wait_range()will wait for it....
mm/filemap.c +2
-1@@ -408,7 +408,8 @@ int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start, .range_end = end, }; -if (!mapping_cap_writeback_dirty (mapping)) +if (!mapping_cap_writeback_dirty (mapping) || + !mapping_tagged (mapping,PAGECACHE_TAG_DIRTY ))return 0 ;
Quoted 5 lines
mm/filemap.c int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start, · line 415
Quoted 1 line
mm/filemap.c +0
-0wbc_attach_fdatawrite_inode (&wbc, mapping->host);How does this play with
tagged_writepages? We assume that no tagging has been performed by any__filemap_fdatawrite_range()caller?
Checking also PAGECACHE_TAG_TOWRITE is cheap but seems redundant.
To-write tags are supposed to be a subset of dirty tags:
to-write is set only when dirty is set and cleared after starting writeback.
Special case set_page_writeback_keepwrite() which does not clear to-write should be for dirty page thus dirty tag is not going to be cleared either. Ext4 calls it after redirty_page_for_writepage()
XFS even without clear_page_dirty_for_io()
Anyway to-write tag without dirty tag or at clear page is confusing.
Jan Kara
maintainerQuoted 3 lines · Konstantin Khlebnikov
Quoted 2 lines · Andrew Morton
(cc linux-fsdevel and Jan)
Thanks for CC Andrew.
Quoted 11 lines
Quoted 11 lines · On Mon, 22 Jul 2019 12:36:08 +0300 Konstantin Khlebnikov…
Quoted 10 lines
Functions like
filemap_write_and_wait_range()should do nothing if inode has no dirty pages or pages currently under writeback. But they anyway construct structwriteback_controland this does some atomic operations ifCONFIG_CGROUP_WRITEBACK=y- on fast path it locksinode->i_lockand updates state of writeback ownership, on slow path might be more work. Current this path is safely avoided only when inode mapping has no pages.For example
generic_file_read_iter()callsfilemap_write_and_wait_range()at eachO_DIRECTread - pretty hot path.
Yes, but in common case mapping_needs_writeback() is false for files you do direct IO to (exactly the case with no pages in the mapping). So you shouldn't see the overhead at all. So which case you really care about?
Quoted 26 lines
Quoted 6 lines
Quoted 6 lines
This patch skips starting new writeback if mapping has no dirty tags set. If writeback is already in progress
filemap_write_and_wait_range()will wait for it....
Quoted 14 lines
mm/filemap.c int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start, · line 413
Quoted 10 lines
mm/filemap.c +2
-1@@ -408,7 +408,8 @@ int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start, .range_end = end, }; -if (!mapping_cap_writeback_dirty (mapping)) +if (!mapping_cap_writeback_dirty (mapping) || + !mapping_tagged (mapping,PAGECACHE_TAG_DIRTY ))return 0 ;wbc_attach_fdatawrite_inode (&wbc, mapping->host);How does this play with
tagged_writepages? We assume that no tagging has been performed by any__filemap_fdatawrite_range()caller?Checking also
PAGECACHE_TAG_TOWRITEis cheap but seems redundant.To-write tags are supposed to be a subset of dirty tags:
to-write is set only when dirty is set and cleared after starting writeback.
Quoted 6 lines
Special case
set_page_writeback_keepwrite()which does not clear to-write should be for dirty page thus dirty tag is not going to be cleared either. Ext4 calls it afterredirty_page_for_writepage()
XFS even withoutclear_page_dirty_for_io()Anyway to-write tag without dirty tag or at clear page is confusing.
Yeah, TOWRITE tag is intended to be internal to writepages logic so your patch is fine in that regard. Overall the patch looks good to me so I'm just wondering a bit about the motivation...
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 54 lines · Jan Kara
Quoted 3 lines · Konstantin Khlebnikov
Quoted 2 lines · Andrew Morton
(cc linux-fsdevel and Jan)
Thanks for CC Andrew.
Quoted 11 lines
Quoted 11 lines · On Mon, 22 Jul 2019 12:36:08 +0300 Konstantin Khlebnikov…
Quoted 10 lines
Functions like
filemap_write_and_wait_range()should do nothing if inode has no dirty pages or pages currently under writeback. But they anyway construct structwriteback_controland this does some atomic operations ifCONFIG_CGROUP_WRITEBACK=y- on fast path it locksinode->i_lockand updates state of writeback ownership, on slow path might be more work. Current this path is safely avoided only when inode mapping has no pages.For example
generic_file_read_iter()callsfilemap_write_and_wait_range()at eachO_DIRECTread - pretty hot path.Yes, but in common case
mapping_needs_writeback()is false for files you do direct IO to (exactly the case with no pages in the mapping). So you shouldn't see the overhead at all. So which case you really care about?Quoted 31 lines
Quoted 6 lines
Quoted 6 lines
This patch skips starting new writeback if mapping has no dirty tags set. If writeback is already in progress
filemap_write_and_wait_range()will wait for it....
Quoted 14 lines
mm/filemap.c int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start, · line 413
Quoted 10 lines
mm/filemap.c +2
-1@@ -408,7 +408,8 @@ int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start, .range_end = end, }; -if (!mapping_cap_writeback_dirty (mapping)) +if (!mapping_cap_writeback_dirty (mapping) || + !mapping_tagged (mapping,PAGECACHE_TAG_DIRTY ))return 0 ;wbc_attach_fdatawrite_inode (&wbc, mapping->host);How does this play with
tagged_writepages? We assume that no tagging has been performed by any__filemap_fdatawrite_range()caller?Checking also
PAGECACHE_TAG_TOWRITEis cheap but seems redundant.To-write tags are supposed to be a subset of dirty tags:
to-write is set only when dirty is set and cleared after starting writeback.Special case
set_page_writeback_keepwrite()which does not clear to-write should be for dirty page thus dirty tag is not going to be cleared either. Ext4 calls it afterredirty_page_for_writepage()
XFS even withoutclear_page_dirty_for_io()
Quoted 5 lines
Quoted 1 line
Anyway to-write tag without dirty tag or at clear page is confusing.
Yeah, TOWRITE tag is intended to be internal to writepages logic so your patch is fine in that regard. Overall the patch looks good to me so I'm just wondering a bit about the motivation...
In our case file mixes cached pages and O_DIRECT read. Kind of database were index header is memory mapped while the rest data read via O_DIRECT. I suppose for sharing index between multiple instances.
On this path we also hit this bug:
https://lore.kernel.org/lkml/156355839560.2063.5265687291430814589.stgit@buzz/
so that's why I've started looking into this code.
Quoted 57 lines · Konstantin Khlebnikov
Quoted 56 lines · Jan Kara
Quoted 3 lines · Konstantin Khlebnikov
Quoted 2 lines · Andrew Morton
(cc linux-fsdevel and Jan)
Thanks for CC Andrew.
Quoted 11 lines
Quoted 11 lines · On Mon, 22 Jul 2019 12:36:08 +0300 Konstantin Khlebnikov…
Quoted 10 lines
Functions like
filemap_write_and_wait_range()should do nothing if inode has no dirty pages or pages currently under writeback. But they anyway construct structwriteback_controland this does some atomic operations ifCONFIG_CGROUP_WRITEBACK=y- on fast path it locksinode->i_lockand updates state of writeback ownership, on slow path might be more work. Current this path is safely avoided only when inode mapping has no pages.For example
generic_file_read_iter()callsfilemap_write_and_wait_range()at eachO_DIRECTread - pretty hot path.Yes, but in common case
mapping_needs_writeback()is false for files you do direct IO to (exactly the case with no pages in the mapping). So you shouldn't see the overhead at all. So which case you really care about?Quoted 32 lines
Quoted 6 lines
Quoted 6 lines
This patch skips starting new writeback if mapping has no dirty tags set. If writeback is already in progress
filemap_write_and_wait_range()will wait for it....
Quoted 14 lines
mm/filemap.c int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start, · line 413
Quoted 10 lines
mm/filemap.c +2
-1@@ -408,7 +408,8 @@ int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start, .range_end = end, }; -if (!mapping_cap_writeback_dirty (mapping)) +if (!mapping_cap_writeback_dirty (mapping) || + !mapping_tagged (mapping,PAGECACHE_TAG_DIRTY ))return 0 ;wbc_attach_fdatawrite_inode (&wbc, mapping->host);How does this play with
tagged_writepages? We assume that no tagging has been performed by any__filemap_fdatawrite_range()caller?Checking also
PAGECACHE_TAG_TOWRITEis cheap but seems redundant.To-write tags are supposed to be a subset of dirty tags:
to-write is set only when dirty is set and cleared after starting writeback.Special case
set_page_writeback_keepwrite()which does not clear to-write should be for dirty page thus dirty tag is not going to be cleared either. Ext4 calls it afterredirty_page_for_writepage()
XFS even withoutclear_page_dirty_for_io()Anyway to-write tag without dirty tag or at clear page is confusing.
Quoted 7 lines
Quoted 3 lines
Yeah, TOWRITE tag is intended to be internal to writepages logic so your patch is fine in that regard. Overall the patch looks good to me so I'm just wondering a bit about the motivation...
In our case file mixes cached pages and
O_DIRECTread. Kind of database were index header is memory mapped while the rest data read viaO_DIRECT. I suppose for sharing index between multiple instances.
OK, that has always been a bit problematic but you're not the first one to have such design ;). So feel free to add:
to your patch.
Quoted 3 lines
On this path we also hit this bug:
https://lore.kernel.org/lkml/156355839560.2063.5265687291430814589.stgit@buzz/
so that's why I've started looking into this code.
I see. OK.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 66 lines · Jan Kara
Quoted 57 lines · Konstantin Khlebnikov
Quoted 56 lines · Jan Kara
Quoted 3 lines · Konstantin Khlebnikov
Quoted 2 lines · Andrew Morton
(cc linux-fsdevel and Jan)
Thanks for CC Andrew.
Quoted 11 lines
Quoted 11 lines · On Mon, 22 Jul 2019 12:36:08 +0300 Konstantin Khlebnikov…
Quoted 10 lines
Functions like
filemap_write_and_wait_range()should do nothing if inode has no dirty pages or pages currently under writeback. But they anyway construct structwriteback_controland this does some atomic operations ifCONFIG_CGROUP_WRITEBACK=y- on fast path it locksinode->i_lockand updates state of writeback ownership, on slow path might be more work. Current this path is safely avoided only when inode mapping has no pages.For example
generic_file_read_iter()callsfilemap_write_and_wait_range()at eachO_DIRECTread - pretty hot path.Yes, but in common case
mapping_needs_writeback()is false for files you do direct IO to (exactly the case with no pages in the mapping). So you shouldn't see the overhead at all. So which case you really care about?Quoted 32 lines
Quoted 6 lines
Quoted 6 lines
This patch skips starting new writeback if mapping has no dirty tags set. If writeback is already in progress
filemap_write_and_wait_range()will wait for it....
Quoted 14 lines
mm/filemap.c int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start, · line 413
Quoted 10 lines
mm/filemap.c +2
-1@@ -408,7 +408,8 @@ int __filemap_fdatawrite_range(struct address_space *mapping, loff_t start, .range_end = end, }; -if (!mapping_cap_writeback_dirty (mapping)) +if (!mapping_cap_writeback_dirty (mapping) || + !mapping_tagged (mapping,PAGECACHE_TAG_DIRTY ))return 0 ;wbc_attach_fdatawrite_inode (&wbc, mapping->host);How does this play with
tagged_writepages? We assume that no tagging has been performed by any__filemap_fdatawrite_range()caller?Checking also
PAGECACHE_TAG_TOWRITEis cheap but seems redundant.To-write tags are supposed to be a subset of dirty tags:
to-write is set only when dirty is set and cleared after starting writeback.Special case
set_page_writeback_keepwrite()which does not clear to-write should be for dirty page thus dirty tag is not going to be cleared either. Ext4 calls it afterredirty_page_for_writepage()
XFS even withoutclear_page_dirty_for_io()Anyway to-write tag without dirty tag or at clear page is confusing.
Quoted 7 lines
Quoted 3 lines
Yeah, TOWRITE tag is intended to be internal to writepages logic so your patch is fine in that regard. Overall the patch looks good to me so I'm just wondering a bit about the motivation...
In our case file mixes cached pages and
O_DIRECTread. Kind of database were index header is memory mapped while the rest data read viaO_DIRECT. I suppose for sharing index between multiple instances.
Quoted 6 lines
OK, that has always been a bit problematic but you're not the first one to have such design ;). So feel free to add:
Reviewed-by Jan Kara <[email protected]>to your patch.
Thanks.
O_DIRECT has long history of misunderstandings =)
It looks some cases are still not documented.
My favourite: O_DIRECT write into hole goes into cache, at least for ext4.
Quoted 9 lines
Quoted 4 lines
On this path we also hit this bug:
https://lore.kernel.org/lkml/156355839560.2063.5265687291430814589.stgit@buzz/
so that's why I've started looking into this code.I see. OK.
Honza