iomap: small block problems
Subsystems (2)
cover iomap: small block problems
Hi Christoph and all,
the recent gfs2 switch from buffer heads to iomap for managing the block-to-page mappings in commit 2164f9b91869 ("gfs2: use iomap for buffered I/O in ordered and writeback mode") broke filesystems with a block size smaller than the page size. This ha??ens when iomap_page objects aren't attached to pages in all the right circumstances, or the iop->upodate bitmap isn't kept in sync with the PageUptodata flag. There are three instances of this problem:
(1) In iomap_readpage_actor, an iomap_page is attached to the page even for inline inodes. This is unnecessary because inline inodes don't need iomap_page objects. That alone wouldn't cause any real issues, but when iomap_read_inline_data copies the inline data into the page, it sets the PageUptodate flag without setting iop->uptodate, causing an inconsistency between the two. This will trigger a WARN_ON in iomap_page_release. The fix should be not to allocate iomap_page objects when reading from inline inodes (patch 1).
(2) When un-inlining an inode, we must allocate a page with an attached iomap_page object (iomap_page_create) and initialize the iop->uptodate bitmap (iomap_set_range_uptodate). We can't currently do that because iomap_page_create and iomap_set_range_uptodate are not exported. That could be fixed by exporting those functions, or by implementing an additional helper as in patch 2. Which of the two would you prefer?
(3) We're not yet using iomap_page_mkwrite, so iomap_page objects don't get created on .page_mkwrite, either. Part of the reason is that iomap_page_mkwrite locks the page and then calls into the filesystem for uninlining and for allocating backing blocks. This conflicts with the gfs2 locking order: on gfs2, transactions must be started before locking any pages. We can fix that by calling iomap_page_create from gfs2_page_mkwrite, or by doing the uninlining and allocations before calling iomap_page_mkwrite. I've implemented option 2 for now; see here:
Any thoughts?
Thanks,
Andreas
Andreas Gruenbacher (1):
iomap: Don't create iomap_page objects for inline files
Bob Peterson (1):
iomap: Add helper for un-inlining an inline inode
fs/iomap/buffered-io.c | 28 +++++++++++++++++++++++++++- include/linux/iomap.h | 2 ++ 2 files changed, 29 insertions(+), 1 deletion(-)
Signature · 1 line
2.26.3
PATCH 1/2 iomap: Don't create iomap_page objects for inline files
In iomap_readpage_actor, don't create iop objects for inline inodes. Otherwise, iomap_read_inline_data will set PageUptodate without setting iop->uptodate, and iomap_page_release will eventually complain.
To prevent this kind of bug from occurring in the future, make sure the page doesn't have private data attached in iomap_read_inline_data.
--- fs/iomap/buffered-io.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
fs/iomap/buffered-io.c +3 -1
@@ -215,6 +215,7 @@ iomap_read_inline_data(struct inode *inode, struct page *page,if (PageUptodate (page))return ; +BUG_ON (page_has_private (page));BUG_ON (page->index);BUG_ON (size >PAGE_SIZE -offset_in_page (iomap->inline_data));
@@ -239,7 +240,7 @@ iomap_readpage_actor(struct inode *inode, loff_t pos, loff_t length, void *data, {struct iomap_readpage_ctx *ctx = data;struct page *page = ctx->cur_page; -struct iomap_page *iop =iomap_page_create (inode, page); +struct iomap_page *iop;bool same_page = false, is_contig = false;loff_t orig_pos = pos;unsigned poff, plen;
@@ -252,6 +253,7 @@ iomap_readpage_actor(struct inode *inode, loff_t pos, loff_t length, void *data, }/* zero post-eof blocks as the page may be mapped */ + iop =iomap_page_create (inode, page);iomap_adjust_read_range (inode, iop, &pos, length, &poff, &plen);if (plen ==0 ) goto done;
Signature · 1 line
2.26.3
Re: [PATCH 1/2] iomap: Don't create iomap_page objects for inline files
Quoted 8 lines · Andreas Gruenbacher
In
iomap_readpage_actor, don't create iop objects for inline inodes. Otherwise,iomap_read_inline_datawill set PageUptodate without settingiop->uptodate, andiomap_page_releasewill eventually complain.To prevent this kind of bug from occurring in the future, make sure the page doesn't have private data attached in
iomap_read_inline_data.Signed-off-by Andreas Gruenbacher <[email protected]>
PATCH 2/2 iomap: Add helper for un-inlining an inline inode
In reply to Andreas GruenbacherFrom: Bob Peterson <[email protected]>
Add function iomap_uninline_inode for converting an inline inode into a non-inline inode. This takes care of attaching a new iomap_page object to page->private if the block size is smaller than the page size.
--- fs/iomap/buffered-io.c | 24 ++++++++++++++++++++++++ include/linux/iomap.h | 2 ++ 2 files changed, 26 insertions(+)
fs/iomap/buffered-io.c +24 -0
@@ -226,6 +226,30 @@ iomap_read_inline_data(struct inode *inode, struct page *page,SetPageUptodate (page); } +int iomap_uninline_inode (struct inode *inode, +int (*uninline)(struct inode *,struct page *)) +{ +struct page *page =NULL ; +int ret; + +if (i_size_read (inode)) { + page =find_or_create_page (inode->i_mapping,0 ,GFP_NOFS ); +if (!page) +return -ENOMEM ; + } + ret =uninline (inode, page); +if (page) { +if (PageUptodate (page)) { +iomap_page_create (inode, page); +iomap_set_range_uptodate (page,0 ,PAGE_SIZE ); + } +unlock_page (page); +put_page (page); + } +return ret; +} +EXPORT_SYMBOL_GPL (iomap_uninline_inode); +static inline bool iomap_block_needs_zeroing (struct inode *inode,struct iomap *iomap,loff_t pos) {
include/linux/iomap.h +2 -0
@@ -157,6 +157,8 @@ loff_t iomap_apply(struct inode *inode, loff_t pos, loff_t length,ssize_t iomap_file_buffered_write (struct kiocb *iocb,struct iov_iter *from,const struct iomap_ops *ops); +int iomap_uninline_inode (struct inode *inode, +int (*uninline)(struct inode *,struct page *));int iomap_readpage (struct page *page,const struct iomap_ops *ops);void iomap_readahead (struct readahead_control *,const struct iomap_ops *ops);int iomap_set_page_dirty (struct page *page);
Signature · 1 line
2.26.3
Quoted 8 lines · Andreas Gruenbacher
(1) In
iomap_readpage_actor, aniomap_pageis attached to the page even for inline inodes. This is unnecessary because inline inodes don't neediomap_pageobjects. That alone wouldn't cause any real issues, but wheniomap_read_inline_datacopies the inline data into the page, it sets the PageUptodate flag without settingiop->uptodate, causing an inconsistency between the two. This will trigger aWARN_ONiniomap_page_release. The fix should be not to allocateiomap_pageobjects when reading from inline inodes (patch 1).
I don't have a problem with this patch.
Quoted 6 lines
(2) When un-inlining an inode, we must allocate a page with an attached
iomap_pageobject (iomap_page_create) and initialize theiop->uptodatebitmap (iomap_set_range_uptodate). We can't currently do that becauseiomap_page_createandiomap_set_range_uptodateare not exported. That could be fixed by exporting those functions, or by implementing an additional helper as in patch 2. Which of the two would you prefer?
Not hugely happy with either of these options, tbh. I'd rather we apply a patch akin to this one (plucked from the folio tree), so won't apply:
@@ -1305,7 +1311,7 @@ iomap_writepage_map(struct iomap_writepage_ctx *wpc,
struct writeback_control *wbc, struct inode *inode, struct folio *folio, loff_t end_pos)
{
- struct iomap_page *iop = to_iomap_page(folio);
+ struct iomap_page *iop = iomap_page_create(inode, folio);
struct iomap_ioend *ioend, *next;
unsigned len = i_blocksize(inode);
unsigned nblocks = i_blocks_per_folio(inode, folio);
@@ -1313,7 +1319,6 @@ iomap_writepage_map(struct iomap_writepage_ctx *wpc,
int error = 0, count = 0, i;
LIST_HEAD(submit_list);- WARN_ON_ONCE(nblocks > 1 && !iop);
WARN_ON_ONCE(iop && atomic_read(&iop->write_bytes_pending) != 0);
/*so permit pages without an iop to enter writeback and create an iop *then*. Would that solve your problem?
Quoted 9 lines
(3) We're not yet using
iomap_page_mkwrite, soiomap_pageobjects don't get created on .page_mkwrite, either. Part of the reason is thatiomap_page_mkwritelocks the page and then calls into the filesystem for uninlining and for allocating backing blocks. This conflicts with the gfs2 locking order: on gfs2, transactions must be started before locking any pages. We can fix that by callingiomap_page_createfromgfs2_page_mkwrite, or by doing the uninlining and allocations before callingiomap_page_mkwrite. I've implemented option 2 for now; see here:
I think this might also solve this problem?
Quoted 5 lines · Matthew Wilcox
Not hugely happy with either of these options, tbh. I'd rather we apply a patch akin to this one (plucked from the folio tree), so won't apply:
so permit pages without an iop to enter writeback and create an iop *then*. Would that solve your problem?
It is the right thing to do, especially when combined with a feature patch to not bother to create the iomap_page structure on small block size file systems when the extent covers the whole page.
Quoted 11 lines
Quoted 10 lines
(3) We're not yet using
iomap_page_mkwrite, soiomap_pageobjects don't get created on .page_mkwrite, either. Part of the reason is thatiomap_page_mkwritelocks the page and then calls into the filesystem for uninlining and for allocating backing blocks. This conflicts with the gfs2 locking order: on gfs2, transactions must be started before locking any pages. We can fix that by callingiomap_page_createfromgfs2_page_mkwrite, or by doing the uninlining and allocations before callingiomap_page_mkwrite. I've implemented option 2 for now; see here:
Quoted 1 line
I think this might also solve this problem?
We'll still need to create the iomap_page structure for page_mkwrite if there is an extent boundary inside the page.
Quoted 22 lines · Christoph Hellwig
Quoted 5 lines · Matthew Wilcox
Not hugely happy with either of these options, tbh. I'd rather we apply a patch akin to this one (plucked from the folio tree), so won't apply:
so permit pages without an iop to enter writeback and create an iop *then*. Would that solve your problem?
It is the right thing to do, especially when combined with a feature patch to not bother to create the
iomap_pagestructure on small block size file systems when the extent covers the whole page.Quoted 11 lines
Quoted 10 lines
(3) We're not yet using
iomap_page_mkwrite, soiomap_pageobjects don't get created on .page_mkwrite, either. Part of the reason is thatiomap_page_mkwritelocks the page and then calls into the filesystem for uninlining and for allocating backing blocks. This conflicts with the gfs2 locking order: on gfs2, transactions must be started before locking any pages. We can fix that by callingiomap_page_createfromgfs2_page_mkwrite, or by doing the uninlining and allocations before callingiomap_page_mkwrite. I've implemented option 2 for now; see here:
Quoted 4 lines
Quoted 1 line
I think this might also solve this problem?
We'll still need to create the
iomap_pagestructure forpage_mkwriteif there is an extent boundary inside the page.
Yes, but the iop wouldn't need to be allocated in page_mkwrite; that would be taken care of by iomap_writepage / iomap_writepages as in the patch suggested by Matthew, right?
Thanks,
Andreas
Quoted 10 lines · Christoph Hellwig
Quoted 5 lines · Matthew Wilcox
Not hugely happy with either of these options, tbh. I'd rather we apply a patch akin to this one (plucked from the folio tree), so won't apply:
so permit pages without an iop to enter writeback and create an iop *then*. Would that solve your problem?
It is the right thing to do, especially when combined with a feature patch to not bother to create the
iomap_pagestructure on small block size file systems when the extent covers the whole page.
We don't know the extent layout at the point where *this* patch creates iomap_pages during writeback. I imagine we can delay creating one until we find out what our destination layout will be?
Quoted 10 lines · Matthew Wilcox
Quoted 6 lines
Quoted 2 lines
so permit pages without an iop to enter writeback and create an iop *then*. Would that solve your problem?
It is the right thing to do, especially when combined with a feature patch to not bother to create the
iomap_pagestructure on small block size file systems when the extent covers the whole page.We don't know the extent layout at the point where *this* patch creates
iomap_pagesduring writeback. I imagine we can delay creating one until we find out what our destination layout will be?
Hmm. Actually ->page_mkwrite is always is always called on an uptodate page and we even assert that. I should have remembered the whole page fault path better.
So yeah, I think we should take patch 1 from Andreas, then a non-folio version of your patch as a start. The next steps then would be in approximate order:
1. remove the iomap_page_create in iomap_page_mkwrite_actor as it clearly is not needed at that point
2. don't bother to create an iomap_page in iomap_readpage_actor when the iomap spans the whole page
3. don't create the iomap_page in __iomap_write_begin when the page is marked uptodate or the write covers the whole page
delaying the creation further in iomap_writepage_map will be harder as the loop around iomap_add_to_ioend is still fundamentally block based.
Quoted 8 lines · Christoph Hellwig
Hmm. Actually ->page_mkwrite is always is always called on an uptodate page and we even assert that. I should have remembered the whole page fault path better.
So yeah, I think we should take patch 1 from Andreas, then a non-folio version of your patch as a start. The next steps then would be in approximate order:
Quoted 6 lines
1. remove the
iomap_page_createiniomap_page_mkwrite_actoras it clearly is not needed at that point
2. don't bother to create aniomap_pageiniomap_readpage_actorwhen the iomap spans the whole page
3. don't create theiomap_pagein__iomap_write_beginwhen the page is marked uptodate or the write covers the whole page
Further thoughts for a better series:
1. create iomap_page if needed in iomap_writepage_map
2. do not create the iomap_page at all in iomap_readpage_actor. ->readahead is always called on newly allocated pages, and ->readpage either on a clean !uptodate page or on one that has seen a write leading to a partial uptodate state. That is for the case that cares about the iomap_page it is present already
3. don't create the iomap_page in iomap_page_mkwrite_actor
I think this is the simple initial series that should solve Andreas' problem. Then we can look into optimizing __iomap_write_begin and iomap_writepage_map further as needed.
Darrick,
Quoted 12 lines · Christoph Hellwig
Quoted 10 lines · Matthew Wilcox
Quoted 6 lines
Quoted 2 lines
so permit pages without an iop to enter writeback and create an iop *then*. Would that solve your problem?
It is the right thing to do, especially when combined with a feature patch to not bother to create the
iomap_pagestructure on small block size file systems when the extent covers the whole page.We don't know the extent layout at the point where *this* patch creates
iomap_pagesduring writeback. I imagine we can delay creating one until we find out what our destination layout will be?
Quoted 6 lines
Hmm. Actually ->page_mkwrite is always is always called on an uptodate page and we even assert that. I should have remembered the whole page fault path better.
So yeah, I think we should take patch 1 from Andreas, then a non-folio version of your patch as a start.
will you pick up those two patches and push them to Linus? They both seem pretty safe.
Thanks,
Andreas
Quoted 4 lines · Andreas Gruenbacher
Darrick,
will you pick up those two patches and push them to Linus? They both seem pretty safe.
Hello, is there anybody out there?
I've put the two patches here with the sign-offs they've received:
https://git.kernel.org/pub/scm/linux/kernel/git/gfs2/linux-gfs2.git/log/?h=for-next.iomap
Thanks,
Andreas
Quoted 9 lines · Andreas Gruenbacher
Quoted 4 lines · Andreas Gruenbacher
Darrick,
will you pick up those two patches and push them to Linus? They both seem pretty safe.
Hello, is there anybody out there?
I've put the two patches here with the sign-offs they've received:
Please send out an updated series likey everyone else.
Quoted 40 lines · Matthew Wilcox
Quoted 8 lines · Andreas Gruenbacher
(1) In
iomap_readpage_actor, aniomap_pageis attached to the page even for inline inodes. This is unnecessary because inline inodes don't neediomap_pageobjects. That alone wouldn't cause any real issues, but wheniomap_read_inline_datacopies the inline data into the page, it sets the PageUptodate flag without settingiop->uptodate, causing an inconsistency between the two. This will trigger aWARN_ONiniomap_page_release. The fix should be not to allocateiomap_pageobjects when reading from inline inodes (patch 1).I don't have a problem with this patch.
Quoted 6 lines
(2) When un-inlining an inode, we must allocate a page with an attached
iomap_pageobject (iomap_page_create) and initialize theiop->uptodatebitmap (iomap_set_range_uptodate). We can't currently do that becauseiomap_page_createandiomap_set_range_uptodateare not exported. That could be fixed by exporting those functions, or by implementing an additional helper as in patch 2. Which of the two would you prefer?Not hugely happy with either of these options, tbh. I'd rather we apply a patch akin to this one (plucked from the folio tree), so won't apply:
(quoted diff) +1
-2@@ -1305,7 +1311,7 @@ iomap_writepage_map(struct iomap_writepage_ctx *wpc, struct writeback_control *wbc, struct inode *inode, struct folio *folio, loff_t end_pos) { - struct iomap_page *iop = to_iomap_page(folio); + struct iomap_page *iop = iomap_page_create(inode, folio); struct iomap_ioend *ioend, *next; unsigned len = i_blocksize(inode); unsigned nblocks = i_blocks_per_folio(inode, folio);@@ -1313,7 +1319,6 @@ iomap_writepage_map(struct iomap_writepage_ctx *wpc, int error = 0, count = 0, i; LIST_HEAD(submit_list); - WARN_ON_ONCE(nblocks > 1 && !iop); WARN_ON_ONCE(iop && atomic_read(&iop->write_bytes_pending) != 0); /*
Quoted 2 lines
so permit pages without an iop to enter writeback and create an iop *then*. Would that solve your problem?
It probably would. Let me do some testing based on that.
Quoted 11 lines
Quoted 9 lines
(3) We're not yet using
iomap_page_mkwrite, soiomap_pageobjects don't get created on .page_mkwrite, either. Part of the reason is thatiomap_page_mkwritelocks the page and then calls into the filesystem for uninlining and for allocating backing blocks. This conflicts with the gfs2 locking order: on gfs2, transactions must be started before locking any pages. We can fix that by callingiomap_page_createfromgfs2_page_mkwrite, or by doing the uninlining and allocations before callingiomap_page_mkwrite. I've implemented option 2 for now; see here:I think this might also solve this problem?
Probably yes.
Thanks,
Andreas
Below is a version of your patch on top of v5.13 which has passed some local testing here.
Thanks,
Andreas
--
iomap: Permit pages without an iop to enter writeback
Permit pages without an iop to enter writeback and create an iop *then*. This allows filesystems to mark pages dirty without having to worry about how the iop block tracking is implemented.
--- fs/iomap/buffered-io.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
fs/iomap/buffered-io.c +1 -2
@@ -1336,14 +1336,13 @@ iomap_writepage_map(struct iomap_writepage_ctx *wpc, struct writeback_control *wbc, struct inode *inode, struct page *page, u64 end_offset) { - struct iomap_page *iop = to_iomap_page(page); + struct iomap_page *iop = iomap_page_create(inode, page);struct iomap_ioend *ioend, *next;unsigned len =i_blocksize (inode);u64 file_offset;/* file offset of page */ int error =0 , count =0 , i;LIST_HEAD (submit_list); -WARN_ON_ONCE (i_blocks_per_page (inode, page) >1 && !iop);WARN_ON_ONCE (iop &&atomic_read (&iop->write_bytes_pending) !=0 );/*
Signature · 1 line
2.26.3
Quoted 6 lines · Andreas Gruenbacher
Below is a version of your patch on top of v5.13 which has passed some local testing here.
Thanks,
Andreas
Quoted 7 lines
--
iomap: Permit pages without an iop to enter writeback
Permit pages without an iop to enter writeback and create an iop *then*. This allows filesystems to mark pages dirty without having to worry about how the iop block tracking is implemented.
How about ...
Create an iop in the writeback path if one doesn't exist. This allows us to avoid creating the iop in some cases. The only current case we do that for is pages with inline data, but it can be extended to pages which are entirely within an extent. It also allows for an iop to be removed from pages in the future (eg page split).
Quoted 2 lines
Suggested-by Matthew Wilcox <[email protected]>Signed-off-by Andreas Gruenbacher <[email protected]>
Quoted 23 lines · Matthew Wilcox
Quoted 6 lines · Andreas Gruenbacher
Below is a version of your patch on top of v5.13 which has passed some local testing here.
Thanks,
AndreasQuoted 7 lines
--
iomap: Permit pages without an iop to enter writeback
Permit pages without an iop to enter writeback and create an iop *then*. This allows filesystems to mark pages dirty without having to worry about how the iop block tracking is implemented.
How about ...
Create an iop in the writeback path if one doesn't exist. This allows us to avoid creating the iop in some cases. The only current case we do that for is pages with inline data, but it can be extended to pages which are entirely within an extent. It also allows for an iop to be removed from pages in the future (eg page split).
Quoted 5 lines
Quoted 2 lines
Suggested-by Matthew Wilcox <[email protected]>Signed-off-by Andreas Gruenbacher <[email protected]>Co-developed-by Matthew Wilcox (Oracle) <[email protected]>Signed-off-by Matthew Wilcox (Oracle) <[email protected]>
Sure, that works for me.
Thanks,
Andreas