ext4: Fix deadlock during page writeback
Jan Kara
reviewercover ext4: Fix deadlock during page writeback
Hello,
the first patch in the series fixes the deadlock during page writeback spotted by Eryu. Other three patches somewhat improve the lockdep instrumentations of transactions in JBD2. Originally I though we would be able to catch the writeback deadlock with them but sadly that is not the case as page locks / page writeback bits are not tracked by lockdep. But still the result looks nicer and has more coverage so I think it is worth it.
Honza
Jan Kara
reviewerPATCH 1/4 ext4: Fix deadlock during page writeback
Commit 06bd3c36a733 (ext4: fix data exposure after a crash) uncovered a deadlock in ext4_writepages() which was previously much harder to hit. After this commit xfstest generic/130 reproduces the deadlock on small filesystems.
The problem happens when ext4_do_update_inode() sets LARGE_FILE feature and marks current inode handle as synchronous. That subsequently results in ext4_journal_stop() called from ext4_writepages() to block waiting for transaction commit while still holding page locks, reference to io_end, and some prepared bio in mpd structure each of which can possibly block transaction commit from completing and thus results in deadlock.
Fix the problem by releasing page locks, io_end reference, and submitting prepared bio before calling ext4_journal_stop().
--- fs/ext4/inode.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-)
fs/ext4/inode.c +17 -3
@@ -2748,13 +2748,27 @@ retry: done = true; } } -ext4_journal_stop (handle);/* Submit prepared bio */ ext4_io_submit (&mpd.io_submit);/* Unlock pages we didn't use */ mpage_release_unused_pages (&mpd, give_up_on_write); -/* Drop our io_end reference we got from init */ -ext4_put_io_end (mpd.io_submit.io_end); +/* +* Drop our io_end reference we got from init. We have to be +* careful and use deferred io_end finishing as we can release +* the last reference to io_end which may end up doing unwritten +* extent conversion which we cannot do while holding +* transaction handle. +*/ +ext4_put_io_end_defer (mpd.io_submit.io_end); +/* +* Caution: ext4_journal_stop() can wait for transaction commit +* to finish which may depend on writeback of pages to complete +* or on page lock to be released. So we can call it only +* after we have submitted all the IO, released page locks +* we hold, and dropped io_end reference (for extent conversion +* to be able to complete). +*/ +ext4_journal_stop (handle);if (ret == -ENOSPC && sbi->s_journal) {/*
Signature · 1 line
2.6.6
Theodore Ts'o
maintainerQuoted 11 lines · Jan Kara
Commit
06bd3c36a733(ext4: fix data exposure after a crash) uncovered a deadlock inext4_writepages()which was previously much harder to hit. After this commit xfstest generic/130 reproduces the deadlock on small filesystems.The problem happens when
ext4_do_update_inode()setsLARGE_FILEfeature and marks current inode handle as synchronous. That subsequently results inext4_journal_stop()called fromext4_writepages()to block waiting for transaction commit while still holding page locks, reference toio_end, and some prepared bio in mpd structure each of which can possibly block transaction commit from completing and thus results in deadlock.
Would it be safe to submit the bio *after* calling ext4_journal_stop()? It looks like that would be safe, and I'd prefer to minimize the time that a handle is open since that can really impact performance when trying to close all existing handles when we are starting commit processing. It looks to me like this would be safe in terms of avoiding deadlocks, yes?
- Ted
Jan Kara
reviewerQuoted 13 lines · Ted Tso
Quoted 11 lines · Jan Kara
Commit
06bd3c36a733(ext4: fix data exposure after a crash) uncovered a deadlock inext4_writepages()which was previously much harder to hit. After this commit xfstest generic/130 reproduces the deadlock on small filesystems.The problem happens when
ext4_do_update_inode()setsLARGE_FILEfeature and marks current inode handle as synchronous. That subsequently results inext4_journal_stop()called fromext4_writepages()to block waiting for transaction commit while still holding page locks, reference toio_end, and some prepared bio in mpd structure each of which can possibly block transaction commit from completing and thus results in deadlock.
Quoted 6 lines
Would it be safe to submit the bio *after* calling
ext4_journal_stop()? It looks like that would be safe, and I'd prefer to minimize the time that a handle is open since that can really impact performance when trying to close all existing handles when we are starting commit processing. It looks to me like this would be safe in terms of avoiding deadlocks, yes?
But it is not safe - the bio contains pages, those pages have PageWriteback set and if the inode is part of the running transaction, ext4_journal_stop() will wait for transaction commit which will wait for all outstanding writeback on the inode, which will deadlock on those pages which are part of our unsubmitted bio. So the ordering really has to be the way it is...
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Theodore Ts'o
maintainerQuoted 6 lines · Jan Kara
But it is not safe - the bio contains pages, those pages have PageWriteback set and if the inode is part of the running transaction,
ext4_journal_stop()will wait for transaction commit which will wait for all outstanding writeback on the inode, which will deadlock on those pages which are part of our unsubmitted bio. So the ordering really has to be the way it is...
So to be clear. the issue is that PageWriteback won't get cleared until we potentially do a uninit->init conversion, and this is what requires taking a transaction handle leading to the other half of the deadlock?
... and it's probably not safe to clear the PageWriteback early in the bio completion callback function, isn't it. Hmm....
- Ted
Jan Kara
reviewerQuoted 8 lines · Ted Tso
Quoted 6 lines · Jan Kara
But it is not safe - the bio contains pages, those pages have PageWriteback set and if the inode is part of the running transaction,
ext4_journal_stop()will wait for transaction commit which will wait for all outstanding writeback on the inode, which will deadlock on those pages which are part of our unsubmitted bio. So the ordering really has to be the way it is...
Quoted 4 lines
So to be clear. the issue is that PageWriteback won't get cleared until we potentially do a
uninit->initconversion, and this is what requires taking a transaction handle leading to the other half of the deadlock?
No. It is even simpler:
ext4_writepages(inode == "foobar")
prepares pages to write, sets PageWriteback
...
mpage_map_and_submit_extent()
// Writing data past i_size
if (disksize > EXT4_I(inode)->i_disksize) {
...
err2 = ext4_mark_inode_dirty(handle, inode);
ext4_mark_iloc_dirty(handle, inode, &iloc);
ext4_do_update_inode(handle, inode, iloc);
// First file beyond 2 GB
if (ei->i_disksize > 0x7fffffffULL) {
if (!ext4_has_feature_large_file(sb) || ...)
set_large_file = 1;
}
...
if (set_large_file) {
...
ext4_handle_sync(handle);
...
}
ext4_journal_stop()
jbd2_journal_stop(handle);
...
if (handle->h_sync || ... ) {
if (handle->h_sync && !(current->flags & PF_MEMALLOC))
wait_for_commit = 1;
if (wait_for_commit)
err = jbd2_log_wait_commit(journal, tid);So we are waiting for transaction commit to finish with unsubmitted pages that already have PageWriteback set (and also potentially other pages that are locked and we didn't prepare them for writing because the block mapping we got was too short). Now JBD2 goes on trying to do the transaction commit:
jbd2_journal_commit_transaction()
...
journal_finish_inode_data_buffers()
list_for_each_entry(jinode, &commit_transaction->t_inode_list, i_list) {
...
err = filemap_fdatawait(jinode->i_vfs_inode->i_mapping);
// And when inode "foobar" is part of this transaction's inode list, this
// call is going to wait for PageWriteback bits on all the pages of
// the inode to get cleared - which never happens because the IO was
// not even submitted for them. The bio is just sitting prepared in
// mpd.io_submit in ext4_writepages() and would be submitted once
// ext4_journal_stop() completes.Hope it is clearer now.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Theodore Ts'o
maintainerQuoted 6 lines · Jan Kara
So we are waiting for transaction commit to finish with unsubmitted pages that already have PageWriteback set (and also potentially other pages that are locked and we didn't prepare them for writing because the block mapping we got was too short). Now JBD2 goes on trying to do the transaction commit:
Ah, I see, so this is only an issue in those cases where the handle is synchronous. Is this the only case where there is a concern? (e.g., could we test handle->h_sync and stop the handle early if h_sync is not set?) This would put the uninit->init conversion into potentially a separate transaction, but that should be OK.
The reason why I'm pushing so hard here is that long running handles is a major contributor to ext4 haveing poor CPU scalability numbers, since we can end up having lots of threads waiting on the last transaction to complete. So keeping transactions small and fast is a big deal.
- Ted
Jan Kara
reviewerQuoted 8 lines · Ted Tso
Quoted 6 lines · Jan Kara
So we are waiting for transaction commit to finish with unsubmitted pages that already have PageWriteback set (and also potentially other pages that are locked and we didn't prepare them for writing because the block mapping we got was too short). Now JBD2 goes on trying to do the transaction commit:
Quoted 5 lines
Ah, I see, so this is only an issue in those cases where the handle is synchronous. Is this the only case where there is a concern? (e.g., could we test
handle->h_syncand stop the handle early ifh_syncis not set?) This would put theuninit->initconversion into potentially a separate transaction, but that should be OK.
So checking handle->h_sync is possible and it would handle the problem as well AFAICS. However I find it rather hacky to rely on the fact that ext4_journal_stop() can block only when handle->h_sync is set.
With uninit->init conversion changes you likely mean ext4_put_io_end_defer() is run while the handle is still running - that is true but any real work is done from a workqueue so my patch doesn't really change in which transaction uninit->init conversion happens.
Quoted 5 lines
The reason why I'm pushing so hard here is that long running handles is a major contributor to ext4 haveing poor CPU scalability numbers, since we can end up having lots of threads waiting on the last transaction to complete. So keeping transactions small and fast is a big deal.
OK, but we do all the block mappings, page locking etc. while the handle is started so it is not exactly a really short lived handle. The patch adds there a submission of a bio (we have the IO plugged so it will just add the bio to the list of submitted bios), unlock locked pages, drop refcount to ioend (unless IO is already completed, only refcount update is done, if IO is completed we defer any real work to workqueue anyway). So although we add some work which is done while the handle is still running, it is not that much.
If you have some tests which show how the transaction wait time increased, I could be convinced the hack is worth it. But so far I don't think that messing with handle->h_sync is warranted.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Theodore Ts'o
maintainerQuoted 8 lines · Jan Kara
OK, but we do all the block mappings, page locking etc. while the handle is started so it is not exactly a really short lived handle. The patch adds there a submission of a bio (we have the IO plugged so it will just add the bio to the list of submitted bios), unlock locked pages, drop refcount to ioend (unless IO is already completed, only refcount update is done, if IO is completed we defer any real work to workqueue anyway). So although we add some work which is done while the handle is still running, it is not that much.
Good point that the block device is plugged. Ultimately I suspect the way to fix the scalability problem will be move to dioread nolock as the default, and use separate transaction to map the blocks using the uninitialized flags, and then do a separate transaction to convert them afterwards.
- Ted
Jan Kara
reviewerQuoted 10 lines · Ted Tso
Quoted 8 lines · Jan Kara
OK, but we do all the block mappings, page locking etc. while the handle is started so it is not exactly a really short lived handle. The patch adds there a submission of a bio (we have the IO plugged so it will just add the bio to the list of submitted bios), unlock locked pages, drop refcount to ioend (unless IO is already completed, only refcount update is done, if IO is completed we defer any real work to workqueue anyway). So although we add some work which is done while the handle is still running, it is not that much.
Quoted 5 lines
Good point that the block device is plugged. Ultimately I suspect the way to fix the scalability problem will be move to dioread nolock as the default, and use separate transaction to map the blocks using the uninitialized flags, and then do a separate transaction to convert them afterwards.
This is what already happens currently - we only reserve a handle for conversion during writeback but that reservation fluently moves between running transactions until a point where the reserved handle is started - then the handle is pinned to the currently running transaction - and this happens only in the completion handler after IO is completed.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Theodore Ts'o
maintainerQuoted 11 lines · Jan Kara
Quoted 5 lines
Good point that the block device is plugged. Ultimately I suspect the way to fix the scalability problem will be move to dioread nolock as the default, and use separate transaction to map the blocks using the uninitialized flags, and then do a separate transaction to convert them afterwards.
This is what already happens currently - we only reserve a handle for conversion during writeback but that reservation fluently moves between running transactions until a point where the reserved handle is started - then the handle is pinned to the currently running transaction - and this happens only in the completion handler after IO is completed.
Yes, but dioread_nolock only works with block size == page size. What we need to do is to make it work for all block sizes, then make dioread_nolock the default, and then remove the old direct I/O write path....
- Ted
Jan Kara
reviewerQuoted 13 lines · Ted Tso
Quoted 11 lines · Jan Kara
Quoted 5 lines
Good point that the block device is plugged. Ultimately I suspect the way to fix the scalability problem will be move to dioread nolock as the default, and use separate transaction to map the blocks using the uninitialized flags, and then do a separate transaction to convert them afterwards.
This is what already happens currently - we only reserve a handle for conversion during writeback but that reservation fluently moves between running transactions until a point where the reserved handle is started - then the handle is pinned to the currently running transaction - and this happens only in the completion handler after IO is completed.
Quoted 4 lines
Yes, but
dioread_nolockonly works with block size == page size. What we need to do is to make it work for all block sizes, then makedioread_nolockthe default, and then remove the old direct I/O write path....
Yes, I completely agree.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Theodore Ts'o
maintainerThis is what I'm currently testing; do you have objections to this?
It will make it harder if we ever want to teach lockdep how to notice problems like this, but we don't at the moment, and it will make a scalability difference in the common case...
- Ted
Quoted 1 line
From
646caa9c8e196880b41cd3e3d33a2ebc752bdb85Mon Sep 17 00:00:00 2001
From: Jan Kara <[email protected]>
Date: Mon, 4 Jul 2016 10:14:01 -0400
Subject: [PATCH] ext4: fix deadlock during page writeback
Commit 06bd3c36a733 (ext4: fix data exposure after a crash) uncovered a deadlock in ext4_writepages() which was previously much harder to hit. After this commit xfstest generic/130 reproduces the deadlock on small filesystems.
The problem happens when ext4_do_update_inode() sets LARGE_FILE feature and marks current inode handle as synchronous. That subsequently results in ext4_journal_stop() called from ext4_writepages() to block waiting for transaction commit while still holding page locks, reference to io_end, and some prepared bio in mpd structure each of which can possibly block transaction commit from completing and thus results in deadlock.
Fix the problem by releasing page locks, io_end reference, and submitting prepared bio before calling ext4_journal_stop().
[ Changed to defer the call to ext4_journal_stop() only if the handle
is synchronous. --tytso ]
--- fs/ext4/inode.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-)
fs/ext4/inode.c +26 -3
@@ -2754,13 +2754,36 @@ retry: done = true; } } -ext4_journal_stop (handle); +/* +* Caution: If the handle is synchronous, +* ext4_journal_stop() can wait for transaction commit +* to finish which may depend on writeback of pages to +* complete or on page lock to be released. In that +* case, we have to wait until after after we have +* submitted all the IO, released page locks we hold, +* and dropped io_end reference (for extent conversion +* to be able to complete) before stopping the handle. +*/ +if (!ext4_handle_valid (handle) || handle->h_sync ==0 ) { +ext4_journal_stop (handle); + handle =NULL ; + }/* Submit prepared bio */ ext4_io_submit (&mpd.io_submit);/* Unlock pages we didn't use */ mpage_release_unused_pages (&mpd, give_up_on_write); -/* Drop our io_end reference we got from init */ -ext4_put_io_end (mpd.io_submit.io_end); +/* +* Drop our io_end reference we got from init. We have +* to be careful and use deferred io_end finishing if +* we are still holding the transaction as we can +* release the last reference to io_end which may end +* up doing unwritten extent conversion. +*/ +if (handle) { +ext4_put_io_end_defer (mpd.io_submit.io_end); +ext4_journal_stop (handle); + }else +ext4_put_io_end (mpd.io_submit.io_end);if (ret == -ENOSPC && sbi->s_journal) {/*
Signature · 1 line
2.5.0
Jan Kara
reviewerQuoted 1 line · Ted Tso
This is what I'm currently testing; do you have objections to this?
Meh, I don't like it but it should work... Did you see any improvement with your change or are you just operating on the assumption that you want as few code while the handle is running as possible?
Can you maybe ajust dd a comment that we stop !h_sync handles early to make them run as shortly as possible to improve scalability?
Honza
Quoted 84 lines
It will make it harder if we ever want to teach lockdep how to notice problems like this, but we don't at the moment, and it will make a scalability difference in the common case...
- Ted
From
646caa9c8e196880b41cd3e3d33a2ebc752bdb85Mon Sep 17 00:00:00 2001
From: Jan Kara <[email protected]>
Date: Mon, 4 Jul 2016 10:14:01 -0400
Subject: [PATCH] ext4: fix deadlock during page writebackCommit
06bd3c36a733(ext4: fix data exposure after a crash) uncovered a deadlock inext4_writepages()which was previously much harder to hit. After this commit xfstest generic/130 reproduces the deadlock on small filesystems.The problem happens when
ext4_do_update_inode()setsLARGE_FILEfeature and marks current inode handle as synchronous. That subsequently results inext4_journal_stop()called fromext4_writepages()to block waiting for transaction commit while still holding page locks, reference toio_end, and some prepared bio in mpd structure each of which can possibly block transaction commit from completing and thus results in deadlock.Fix the problem by releasing page locks,
io_endreference, and submitting prepared bio before callingext4_journal_stop().[ Changed to defer the call to
ext4_journal_stop()only if the handle
is synchronous. --tytso ]Reported-and-tested-by Eryu Guan <[email protected]>Signed-off-by Theodore Ts'o <[email protected]>Signed-off-by Jan Kara <[email protected]>--- fs/ext4/inode.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-)fs/ext4/inode.c +26
-3@@ -2754,13 +2754,36 @@ retry: done = true; } } -ext4_journal_stop (handle); +/* +* Caution: If the handle is synchronous, +* ext4_journal_stop() can wait for transaction commit +* to finish which may depend on writeback of pages to +* complete or on page lock to be released. In that +* case, we have to wait until after after we have +* submitted all the IO, released page locks we hold, +* and dropped io_end reference (for extent conversion +* to be able to complete) before stopping the handle. +*/ +if (!ext4_handle_valid (handle) || handle->h_sync ==0 ) { +ext4_journal_stop (handle); + handle =NULL ; + }/* Submit prepared bio */ ext4_io_submit (&mpd.io_submit);/* Unlock pages we didn't use */ mpage_release_unused_pages (&mpd, give_up_on_write); -/* Drop our io_end reference we got from init */ -ext4_put_io_end (mpd.io_submit.io_end); +/* +* Drop our io_end reference we got from init. We have +* to be careful and use deferred io_end finishing if +* we are still holding the transaction as we can +* release the last reference to io_end which may end +* up doing unwritten extent conversion. +*/ +if (handle) { +ext4_put_io_end_defer (mpd.io_submit.io_end); +ext4_journal_stop (handle); + }else +ext4_put_io_end (mpd.io_submit.io_end);if (ret == -ENOSPC && sbi->s_journal) {/* Signature · 1 line
2.5.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Theodore Ts'o
maintainerQuoted 6 lines · Jan Kara
Quoted 1 line · Ted Tso
This is what I'm currently testing; do you have objections to this?
Meh, I don't like it but it should work... Did you see any improvement with your change or are you just operating on the assumption that you want as few code while the handle is running as possible?
I haven't had a chance to try to benchmark it yet. I've working at home over the long (US) holiday weekend, and the high core-count servers I need are on the internal work network, and it's pain to access them from home.
I've just been tired of seeing the sort of analysis that can be found at papers like:
https://www.usenix.org/system/files/conference/fast14/fast14-paper_kang.pdf
(And there's a ATC 2016 paper which shows that things haven't gotten any better as well.)
Given that our massive lock bottlenecks come from the j_list_lock and j_state_lock, and that most of the contention happens when we are closing down a transaction for a commit, there is a pretty direct correlation between handle lifetimes and the contention statistics on the journal spinlocks. Enough so that I've instrumented the handle type and handle line number in the jbd2_handle_stats tracepoint, and work to push down on the handle hold times have definitely helped our contention numbers.
So I do have experimental evidence that reducing code while the handle is running does matter in general. I just don't have it for this specific case yet....
Cheers,
- Ted
Jan Kara
reviewerQuoted 13 lines · Ted Tso
Quoted 6 lines · Jan Kara
Quoted 1 line · Ted Tso
This is what I'm currently testing; do you have objections to this?
Meh, I don't like it but it should work... Did you see any improvement with your change or are you just operating on the assumption that you want as few code while the handle is running as possible?
I haven't had a chance to try to benchmark it yet. I've working at home over the long (US) holiday weekend, and the high core-count servers I need are on the internal work network, and it's pain to access them from home.
Quoted 4 lines
I've just been tired of seeing the sort of analysis that can be found at papers like:
https://www.usenix.org/system/files/conference/fast14/fast14-paper_kang.pdf
So the biggest gap shown in this paper is for buffered write where I suspect ext4 suffers because it starts a handle for each write. There is some low-hanging fruit though - we just need to start it when we may be updating i_size. I'll try to look into this when I have time to setup proper benchmark.
Quoted 11 lines
(And there's a ATC 2016 paper which shows that things haven't gotten any better as well.)
Given that our massive lock bottlenecks come from the
j_list_lockandj_state_lock, and that most of the contention happens when we are closing down a transaction for a commit, there is a pretty direct correlation between handle lifetimes and the contention statistics on the journal spinlocks. Enough so that I've instrumented the handle type and handle line number in thejbd2_handle_statstracepoint, and work to push down on the handle hold times have definitely helped our contention numbers.
Yeah, JBD2 scalability sucks. I suspect you are conflating two issues here though. One issue is j_list_lock and j_state_lock contention - that is exposed by starting handles often, doing lots of operations with buffers etc. This is what the above paper shows. Another issue is that while a transaction is preparing for commit, we have to wait for all outstanding handles against that transaction and while we do that, we have no running transaction and the whole journalling machinery is stalled. For this problem, the time each handle runs is essential. This is what you've likely seen in your testing.
Reducing j_list_lock and j_state_lock contention is IMO doable, although the low hanging fruit is probably eaten these days ;). Fixing the second problem is harder as that is inherent problem with block-level journalling. I suspect we could allow starting another transaction while the previous one is in "preparing for commit" phase but that would lead to two transactions getting updates at one point in time which JBD2 currently does not expect.
Quoted 3 lines
So I do have experimental evidence that reducing code while the handle is running does matter in general. I just don't have it for this specific case yet....
OK.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Theodore Ts'o
maintainerQuoted 10 lines · Jan Kara
Yeah, JBD2 scalability sucks. I suspect you are conflating two issues here though. One issue is
j_list_lockandj_state_lockcontention - that is exposed by starting handles often, doing lots of operations with buffers etc. This is what the above paper shows. Another issue is that while a transaction is preparing for commit, we have to wait for all outstanding handles against that transaction and while we do that, we have no running transaction and the whole journalling machinery is stalled. For this problem, the time each handle runs is essential. This is what you've likely seen in your testing.
You're right, I'm conflating two separate issues. The j_{list,state}_lock contention is the more obvious of the two, but it's separate from the issue of the journalling machinery being stalled on j_wait_transaction_locked.
Quoted 3 lines
Reducing
j_list_lockandj_state_lockcontention is IMO doable, although the low hanging fruit is probably eaten these days ;).
Yeah, most of the low hanging fruit has been grabbed already, which is why I tend to focus more on the 2nd issue these days. The main thing which is left would be splitting j_list_lock so there are separate locks for each of the different lists (t_buffers, t_forget, t_shadow_list, t_reserved_list, etc.) What makes this tricky is that when we are moving blocks from one list to another, we need to have both lists locked, and so this means rearchitecting a large amount of the locking in fs/jbd2/transaction.c, and of course, worrying about lock rank issues.
Quoted 5 lines
Fixing the second problem is harder as that is inherent problem with block-level journalling. I suspect we could allow starting another transaction while the previous one is in "preparing for commit" phase but that would lead to two transactions getting updates at one point in time which JBD2 currently does not expect.
Starting another transaction while we are waiting for earlier transaction to lock down is going to be problematic, since while there are still handles active on the first transaction, they could still be modifying metadata blocks. And while that's happening, we can't allow any new handles associated with the second transaction to start modifying metadata blocks.
If there was some way for all of the currently open handles to guarantee that they won't call get_write_access() on any new blocks, maybe. But if you look at truncate for example, that gets messy --- and we could get most of the benefit by simply making truncate be a two part operation, where it identifies all of the blocks it needs to modify and makes sure they are in memory *before* it calls start_this_handle. And then this falls into the general design principle of keeping the run time of handles as short as possible.
- Ted
Jan Kara
reviewerQuoted 7 lines · Ted Tso
Quoted 5 lines · Jan Kara
Fixing the second problem is harder as that is inherent problem with block-level journalling. I suspect we could allow starting another transaction while the previous one is in "preparing for commit" phase but that would lead to two transactions getting updates at one point in time which JBD2 currently does not expect.
Quoted 6 lines
Starting another transaction while we are waiting for earlier transaction to lock down is going to be problematic, since while there are still handles active on the first transaction, they could still be modifying metadata blocks. And while that's happening, we can't allow any new handles associated with the second transaction to start modifying metadata blocks.
Well, we can. We just have to make sure we snapshot the contents that should be committed before we modify it from the new transaction. We already do this when we are committing block and need to modify it in the running transaction at the same time. Obviously allowing this logic to trigger earlier will lead to higher memory overhead and allocation, copying, and freeing of block snapshots isn't free either so it will need careful benchmarking.
Quoted 8 lines
If there was some way for all of the currently open handles to guarantee that they won't call
get_write_access()on any new blocks, maybe. But if you look at truncate for example, that gets messy --- and we could get most of the benefit by simply making truncate be a two part operation, where it identifies all of the blocks it needs to modify and makes sure they are in memory *before* it callsstart_this_handle. And then this falls into the general design principle of keeping the run time of handles as short as possible.
Yeah, I'm afraid the complexity of this will be rather high...
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Theodore Ts'o
maintainerQuoted 7 lines · Jan Kara
Quoted 6 lines
Starting another transaction while we are waiting for earlier transaction to lock down is going to be problematic, since while there are still handles active on the first transaction, they could still be modifying metadata blocks. And while that's happening, we can't allow any new handles associated with the second transaction to start modifying metadata blocks.
Quoted 7 lines
Well, we can. We just have to make sure we snapshot the contents that should be committed before we modify it from the new transaction. We already do this when we are committing block and need to modify it in the running transaction at the same time. Obviously allowing this logic to trigger earlier will lead to higher memory overhead and allocation, copying, and freeing of block snapshots isn't free either so it will need careful benchmarking.
Consider the following sequence:
Start handle A attached to txn #42
<Start Commiting transaction #42>
Start handle B attached to tnx #43 Call get_write_access on block bitmap #100 Modify block bitmap #100 journal_dirty_metadata for #100
Call get_write_access on block bitmap #100
Modify block bitmap #100journal_dirty_metadata for #100
Snapshotting the block bitmap at when handle B calls get_write_access() won't help, because if handle B starts modifying the block bitmap, and *then* handle A starts trying to modify the same block bitmap, what do we do?
You could make handle A make the same logical modification in both the copy of metadata block associated with first transaction (#42) as well as the copy of the metadata block associated with the second transaction (#43), and for an allocation bitmap maybe it's even doable.
But consider the even more hairy case where handle A and handle B are both modifying an inline xattr, and handle B has to convert spill some of the extended attribute contents to an external xattr block. Now when handle A makes some other xattr change, the change it needs to make for transaction #42 might be very different from the one for transaction #43.
The complexity for handling this would be extremely high, and I suspect doing a two-pass truncate would actually be simpler....
- Ted
Quoted 16 lines
Quoted 8 lines
If there was some way for all of the currently open handles to guarantee that they won't call
get_write_access()on any new blocks, maybe. But if you look at truncate for example, that gets messy --- and we could get most of the benefit by simply making truncate be a two part operation, where it identifies all of the blocks it needs to modify and makes sure they are in memory *before* it callsstart_this_handle. And then this falls into the general design principle of keeping the run time of handles as short as possible.Yeah, I'm afraid the complexity of this will be rather high...
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Jan Kara
reviewerQuoted 43 lines · Ted Tso
Quoted 7 lines · Jan Kara
Quoted 6 lines
Starting another transaction while we are waiting for earlier transaction to lock down is going to be problematic, since while there are still handles active on the first transaction, they could still be modifying metadata blocks. And while that's happening, we can't allow any new handles associated with the second transaction to start modifying metadata blocks.
Quoted 7 lines
Well, we can. We just have to make sure we snapshot the contents that should be committed before we modify it from the new transaction. We already do this when we are committing block and need to modify it in the running transaction at the same time. Obviously allowing this logic to trigger earlier will lead to higher memory overhead and allocation, copying, and freeing of block snapshots isn't free either so it will need careful benchmarking.
Consider the following sequence:
Start handle A attached to txn #42
<Start Commiting transaction #42>
Start handle B attached to tnx #43 Call
get_write_accesson block bitmap #100 Modify block bitmap #100journal_dirty_metadatafor #100Call
get_write_accesson block bitmap #100
Modify block bitmap #100journal_dirty_metadatafor #100Snapshotting the block bitmap at when handle B calls
get_write_access()won't help, because if handle B starts modifying the block bitmap, and *then* handle A starts trying to modify the same block bitmap, what do we do?You could make handle A make the same logical modification in both the copy of metadata block associated with first transaction (#42) as well as the copy of the metadata block associated with the second transaction (#43), and for an allocation bitmap maybe it's even doable.
Quoted 6 lines
But consider the even more hairy case where handle A and handle B are both modifying an inline xattr, and handle B has to convert spill some of the extended attribute contents to an external xattr block. Now when handle A makes some other xattr change, the change it needs to make for transaction #42 might be very different from the one for transaction #43.
Yup, good point.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Jan Kara
reviewerPATCH 2/4 jbd2: Move lockdep instrumentation for jbd2 handles
In reply to Jan KaraThe transaction the handle references is free to commit once we've decremented t_updates counter. Move the lockdep instrumentation to that place. Currently it was a bit later which did not really matter but subsequent improvements to lockdep instrumentation would cause false positives with it.
--- fs/jbd2/transaction.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/jbd2/transaction.c +2 -1
@@ -1750,11 +1750,11 @@ int jbd2_journal_stop(handle_t *handle)wake_up (&journal->j_wait_transaction_locked); } +lock_map_release (&handle->h_lockdep_map); +if (wait_for_commit) err =jbd2_log_wait_commit (journal, tid); -lock_map_release (&handle->h_lockdep_map);
Re: [PATCH 2/4] jbd2: Move lockdep instrumentation for jbd2 handles
Quoted 7 lines · Jan Kara
The transaction the handle references is free to commit once we've decremented
t_updatescounter. Move the lockdep instrumentation to that place. Currently it was a bit later which did not really matter but subsequent improvements to lockdep instrumentation would cause false positives with it.Signed-off-by Jan Kara <[email protected]>
Applied, thanks.
- Ted
Jan Kara
reviewerPATCH 3/4 jbd2: Move lockdep tracking to journal_s
In reply to Jan KaraCurrently lockdep map is tracked in each journal handle. To be able to expand lockdep support to cover also other cases where we depend on transaction commit and where handle is not available, move lockdep map into struct journal_s. Since this makes the lockdep map shared for all handles, we have to use rwsem_acquire_read() for acquisitions now.
--- fs/jbd2/journal.c | 4 ++++ fs/jbd2/transaction.c | 11 +++-------- include/linux/jbd2.h | 16 ++++++++++++---- 3 files changed, 19 insertions(+), 12 deletions(-)
fs/jbd2/journal.c +4 -0
@@ -1091,6 +1091,7 @@ static void jbd2_stats_proc_exit(journal_t *journal)static journal_t *journal_init_common (void ) { +static struct lock_class_key jbd2_trans_commit_key;journal_t *journal;int err;
@@ -1126,6 +1127,9 @@ static journal_t * journal_init_common (void)spin_lock_init (&journal->j_history_lock); +lockdep_init_map (&journal->j_trans_commit_map,"jbd2_handle" , + &jbd2_trans_commit_key,0 ); +return journal; }
fs/jbd2/transaction.c +3 -8
@@ -382,13 +382,11 @@ repeat:read_unlock (&journal->j_state_lock); current->journal_info = handle; -lock_map_acquire (&handle->h_lockdep_map); +rwsem_acquire_read (&journal->j_trans_commit_map,0 ,0 , _THIS_IP_);jbd2_journal_free_transaction (new_transaction);return 0 ; } -static struct lock_class_key jbd2_handle_key; -/* Allocate a new handle. This should probably be in a slab... */ static handle_t *new_handle (int nblocks) {
@@ -398,9 +396,6 @@ static handle_t *new_handle(int nblocks) handle->h_buffer_credits = nblocks; handle->h_ref =1 ; -lockdep_init_map (&handle->h_lockdep_map,"jbd2_handle" , - &jbd2_handle_key,0 ); -return handle; }
@@ -672,7 +667,7 @@ int jbd2__journal_restart(handle_t *handle, int nblocks, gfp_t gfp_mask)if (need_to_start)jbd2_log_start_commit (journal, tid); -lock_map_release (&handle->h_lockdep_map); +rwsem_release (&journal->j_trans_commit_map,1 , _THIS_IP_); handle->h_buffer_credits = nblocks; ret =start_this_handle (journal, handle, gfp_mask);return ret;
@@ -1750,7 +1745,7 @@ int jbd2_journal_stop(handle_t *handle)wake_up (&journal->j_wait_transaction_locked); } -lock_map_release (&handle->h_lockdep_map); +rwsem_release (&journal->j_trans_commit_map,1 , _THIS_IP_);if (wait_for_commit) err =jbd2_log_wait_commit (journal, tid);
include/linux/jbd2.h +12 -4
@@ -491,10 +491,6 @@ struct jbd2_journal_handleunsigned long h_start_jiffies;unsigned int h_requested_credits; - -#ifdef CONFIG_DEBUG_LOCK_ALLOC -struct lockdep_map h_lockdep_map; -#endif };
@@ -1035,6 +1031,18 @@ struct journal_s/* Precomputed journal UUID checksum for seeding other checksums */ __u32 j_csum_seed; + +#ifdef CONFIG_DEBUG_LOCK_ALLOC +/* +* Lockdep entity to track transaction commit dependencies. Handles +* hold this "lock" for read, when we wait for commit, we acquire the +* "lock" for writing. This matches the properties of jbd2 journalling +* where the running transaction has to wait for all handles to be +* dropped to commit that transaction and also acquiring a handle may +* require transaction commit to finish. +*/ +struct lockdep_map j_trans_commit_map; +#endif };/* journal feature predicate functions */
Signature · 1 line
2.6.6
kbuild test robot
build botRe: [PATCH 3/4] jbd2: Move lockdep tracking to journal_s
All warnings (new ones prefixed by >>):
Hi,
[auto build test WARNING on ext4/dev]
[also build test WARNING on v4.7-rc3 next-20160616]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Jan-Kara/ext4-Fix-deadlock-during-page-writeback/20160616-184851 base: https://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4.git dev reproduce: make htmldocs
All warnings (new ones prefixed by >>):
include/linux/jbd2.h:442: warning: No description found for parameter 'i_transaction' include/linux/jbd2.h:442: warning: No description found for parameter 'i_next_transaction' include/linux/jbd2.h:442: warning: No description found for parameter 'i_list' include/linux/jbd2.h:442: warning: No description found for parameter 'i_vfs_inode' include/linux/jbd2.h:442: warning: No description found for parameter 'i_flags' include/linux/jbd2.h:494: warning: No description found for parameter 'h_rsv_handle' include/linux/jbd2.h:494: warning: No description found for parameter 'h_reserved' include/linux/jbd2.h:494: warning: No description found for parameter 'h_type' include/linux/jbd2.h:494: warning: No description found for parameter 'h_line_no' include/linux/jbd2.h:494: warning: No description found for parameter 'h_start_jiffies' include/linux/jbd2.h:494: warning: No description found for parameter 'h_requested_credits' include/linux/jbd2.h:1046: warning: No description found for parameter 'j_chkpt_bhs[JBD2_NR_BATCH]' include/linux/jbd2.h:1046: warning: No description found for parameter 'j_devname[BDEVNAME_SIZE+24]' include/linux/jbd2.h:1046: warning: No description found for parameter 'j_average_commit_time' include/linux/jbd2.h:1046: warning: No description found for parameter 'j_min_batch_time' include/linux/jbd2.h:1046: warning: No description found for parameter 'j_max_batch_time' include/linux/jbd2.h:1046: warning: No description found for parameter 'j_commit_callback' include/linux/jbd2.h:1046: warning: No description found for parameter 'j_failed_commit' include/linux/jbd2.h:1046: warning: No description found for parameter 'j_chksum_driver' include/linux/jbd2.h:1046: warning: No description found for parameter 'j_csum_seed'
>> include/linux/jbd2.h:1046: warning: No description found for parameter 'j_trans_commit_map'
fs/jbd2/transaction.c:424: warning: No description found for parameter 'rsv_blocks' fs/jbd2/transaction.c:424: warning: No description found for parameter 'gfp_mask' fs/jbd2/transaction.c:424: warning: No description found for parameter 'type' fs/jbd2/transaction.c:424: warning: No description found for parameter 'line_no' fs/jbd2/transaction.c:500: warning: No description found for parameter 'type' fs/jbd2/transaction.c:500: warning: No description found for parameter 'line_no' fs/jbd2/transaction.c:630: warning: No description found for parameter 'gfp_mask'
vim +/j_trans_commit_map +1046 include/linux/jbd2.h
01b5adce Darrick J. Wong 2012-05-27 1030 struct crypto_shash *j_chksum_driver;
4fd5ea43 Darrick J. Wong 2012-05-27 1031
4fd5ea43 Darrick J. Wong 2012-05-27 1032 /* Precomputed journal UUID checksum for seeding other checksums */
4fd5ea43 Darrick J. Wong 2012-05-27 1033 __u32 j_csum_seed;
ef50ddbd Jan Kara 2016-06-16 1034
ef50ddbd Jan Kara 2016-06-16 1035 #ifdef CONFIG_DEBUG_LOCK_ALLOC
ef50ddbd Jan Kara 2016-06-16 1036 /*
ef50ddbd Jan Kara 2016-06-16 1037 * Lockdep entity to track transaction commit dependencies. Handles
ef50ddbd Jan Kara 2016-06-16 1038 * hold this "lock" for read, when we wait for commit, we acquire the
ef50ddbd Jan Kara 2016-06-16 1039 * "lock" for writing. This matches the properties of jbd2 journalling
ef50ddbd Jan Kara 2016-06-16 1040 * where the running transaction has to wait for all handles to be
ef50ddbd Jan Kara 2016-06-16 1041 * dropped to commit that transaction and also acquiring a handle may
ef50ddbd Jan Kara 2016-06-16 1042 * require transaction commit to finish.
ef50ddbd Jan Kara 2016-06-16 1043 */
ef50ddbd Jan Kara 2016-06-16 1044 struct lockdep_map j_trans_commit_map;
ef50ddbd Jan Kara 2016-06-16 1045 #endif
470decc6 Dave Kleikamp 2006-10-11 @1046 };
470decc6 Dave Kleikamp 2006-10-11 1047
56316a0d Darrick J. Wong 2015-10-17 1048 /* journal feature predicate functions */
56316a0d Darrick J. Wong 2015-10-17 1049 #define JBD2_FEATURE_COMPAT_FUNCS(name, flagname) \
56316a0d Darrick J. Wong 2015-10-17 1050 static inline bool jbd2_has_feature_##name(journal_t *j) \
56316a0d Darrick J. Wong 2015-10-17 1051 { \
56316a0d Darrick J. Wong 2015-10-17 1052 return ((j)->j_format_version >= 2 && \
56316a0d Darrick J. Wong 2015-10-17 1053 ((j)->j_superblock->s_feature_compat & \
56316a0d Darrick J. Wong 2015-10-17 1054 cpu_to_be32(JBD2_FEATURE_COMPAT_##flagname)) != 0); \:::::: The code at line 1046 was first introduced by commit
:::::: 470decc613ab2048b619a01028072d932d9086ee [PATCH] jbd2: initial copy of files from jbd
:::::: TO: Dave Kleikamp <[email protected]>
:::::: CC: Linus Torvalds <[email protected]>
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Re: [PATCH 3/4] jbd2: Move lockdep tracking to journal_s
In reply to Jan KaraQuoted 7 lines · Jan Kara
Currently lockdep map is tracked in each journal handle. To be able to expand lockdep support to cover also other cases where we depend on transaction commit and where handle is not available, move lockdep map into struct
journal_s. Since this makes the lockdep map shared for all handles, we have to userwsem_acquire_read()for acquisitions now.Signed-off-by Jan Kara <[email protected]>
Thanks, applied. I added documentation for j_trans_commit_map to keep the kbuild checker happy.
- Ted
Jan Kara
reviewerPATCH 4/4 jbd2: Track more dependencies on transaction commit
In reply to Jan KaraSo far we were tracking only dependency on transaction commit due to starting a new handle (which may require commit to start a new transaction). Now add tracking also for other cases where we wait for transaction commit. This way lockdep can catch deadlocks e. g. because we call jbd2_journal_stop() for a synchronous handle with some locks held which rank below transaction start.
--- fs/jbd2/journal.c | 1 + fs/jbd2/transaction.c | 4 ++++ include/linux/jbd2.h | 6 ++++++ 3 files changed, 11 insertions(+)
fs/jbd2/journal.c +1 -0
@@ -691,6 +691,7 @@ int jbd2_log_wait_commit(journal_t *journal, tid_t tid) {int err =0 ; +jbd2_might_wait_for_commit (journal);read_lock (&journal->j_state_lock);#ifdef CONFIG_JBD2_DEBUG if (!tid_geq (journal->j_commit_request, tid)) {
fs/jbd2/transaction.c +4 -0
@@ -182,6 +182,8 @@ static int add_transaction_credits(journal_t *journal, int blocks,int needed;int total = blocks + rsv_blocks; +jbd2_might_wait_for_commit (journal); +/* * If the current transaction is locked down for commit, wait * for the lock to be released.
@@ -695,6 +697,8 @@ void jbd2_journal_lock_updates(journal_t *journal) {DEFINE_WAIT (wait); +jbd2_might_wait_for_commit (journal); +write_lock (&journal->j_state_lock); ++journal->j_barrier_count;
include/linux/jbd2.h +6 -0
@@ -1045,6 +1045,12 @@ struct journal_s#endif }; +#define jbd2_might_wait_for_commit (j) \ + do { \ + rwsem_acquire(&j->j_trans_commit_map, 0, 0, _THIS_IP_); \ + rwsem_release(&j->j_trans_commit_map, 1, _THIS_IP_); \ + } while (0) +/* journal feature predicate functions */ #define JBD2_FEATURE_COMPAT_FUNCS (name, flagname) \ static inline bool jbd2_has_feature_##name(journal_t *j) \
Signature · 1 line
2.6.6
Re: [PATCH 4/4] jbd2: Track more dependencies on transaction commit
Quoted 8 lines · Jan Kara
So far we were tracking only dependency on transaction commit due to starting a new handle (which may require commit to start a new transaction). Now add tracking also for other cases where we wait for transaction commit. This way lockdep can catch deadlocks e. g. because we call
jbd2_journal_stop()for a synchronous handle with some locks held which rank below transaction start.Signed-off-by Jan Kara <[email protected]>
Thanks, applied.
- Ted