* [BUG] fs: btrfs: several possible ABBA deadlocks
@ 2021-11-27 8:23 Jia-Ju Bai
2021-11-29 0:34 ` Josef Bacik
0 siblings, 1 reply; 4+ messages in thread
From: Jia-Ju Bai @ 2021-11-27 8:23 UTC (permalink / raw)
To: clm, josef, dsterba; +Cc: linux-btrfs, linux-kernel
Hello,
My static analysis tool reports several possible ABBA deadlocks in the
btrfs module in Linux 5.10:
# DEADLOCK 1:
__clear_extent_bit()
spin_lock(&tree->lock); --> Line 733 (Lock A)
split_state()
btrfs_split_delalloc_extent()
spin_lock(&BTRFS_I(inode)->lock); --> Line 1870 (Lock B)
btrfs_inode_safe_disk_i_size_write()
spin_lock(&BTRFS_I(inode)->lock); --> Line 53 (Lock B)
find_contiguous_extent_bit()
spin_lock(&tree->lock); --> Line 1620 (Lock A)
When __clear_extent_bit() and btrfs_inode_safe_disk_i_size_write() are
concurrently executed, the deadlock can occur.
# DEADLOCK 2:
__set_extent_bit()
spin_lock(&tree->lock); --> Line 995 (Lock A)
set_state_bits()
btrfs_set_delalloc_extent()
spin_lock(&BTRFS_I(inode)->lock); --> Line 2007 or 2017 or 2029
(Lock B)
btrfs_inode_safe_disk_i_size_write()
spin_lock(&BTRFS_I(inode)->lock); --> Line 53 (Lock B)
find_contiguous_extent_bit()
spin_lock(&tree->lock); --> Line 1620 (Lock A)
When __set_extent_bit() and btrfs_inode_safe_disk_i_size_write() are
concurrently executed, the deadlock can occur.
# DEADLOCK 3:
convert_extent_bit()
spin_lock(&tree->lock); --> Line 1241 (Lock A)
set_state_bits()
btrfs_set_delalloc_extent()
spin_lock(&BTRFS_I(inode)->lock); --> Line 2007 or 2017 or 2029
(Lock B)
btrfs_inode_safe_disk_i_size_write()
spin_lock(&BTRFS_I(inode)->lock); --> Line 53 (Lock B)
find_contiguous_extent_bit()
spin_lock(&tree->lock); --> Line 1620 (Lock A)
When convert_extent_bit() and btrfs_inode_safe_disk_i_size_write() are
concurrently executed, the deadlock can occur.
I am not quite sure whether these possible deadlocks are real and how to
fix them if they are real.
Any feedback would be appreciated, thanks :)
Reported-by: TOTE Robot <oslab@tsinghua.edu.cn>
Best wishes,
Jia-Ju Bai
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [BUG] fs: btrfs: several possible ABBA deadlocks
2021-11-27 8:23 [BUG] fs: btrfs: several possible ABBA deadlocks Jia-Ju Bai
@ 2021-11-29 0:34 ` Josef Bacik
2021-11-29 1:48 ` Jia-Ju Bai
2021-11-29 14:37 ` Nikolay Borisov
0 siblings, 2 replies; 4+ messages in thread
From: Josef Bacik @ 2021-11-29 0:34 UTC (permalink / raw)
To: Jia-Ju Bai; +Cc: clm, dsterba, linux-btrfs, linux-kernel
On Sat, Nov 27, 2021 at 04:23:37PM +0800, Jia-Ju Bai wrote:
> Hello,
>
> My static analysis tool reports several possible ABBA deadlocks in the btrfs
> module in Linux 5.10:
>
> # DEADLOCK 1:
> __clear_extent_bit()
> spin_lock(&tree->lock); --> Line 733 (Lock A)
> split_state()
> btrfs_split_delalloc_extent()
> spin_lock(&BTRFS_I(inode)->lock); --> Line 1870 (Lock B)
>
> btrfs_inode_safe_disk_i_size_write()
> spin_lock(&BTRFS_I(inode)->lock); --> Line 53 (Lock B)
> find_contiguous_extent_bit()
> spin_lock(&tree->lock); --> Line 1620 (Lock A)
>
> When __clear_extent_bit() and btrfs_inode_safe_disk_i_size_write() are
> concurrently executed, the deadlock can occur.
>
> # DEADLOCK 2:
> __set_extent_bit()
> spin_lock(&tree->lock); --> Line 995 (Lock A)
> set_state_bits()
> btrfs_set_delalloc_extent()
> spin_lock(&BTRFS_I(inode)->lock); --> Line 2007 or 2017 or 2029 (Lock
> B)
>
> btrfs_inode_safe_disk_i_size_write()
> spin_lock(&BTRFS_I(inode)->lock); --> Line 53 (Lock B)
> find_contiguous_extent_bit()
> spin_lock(&tree->lock); --> Line 1620 (Lock A)
>
> When __set_extent_bit() and btrfs_inode_safe_disk_i_size_write() are
> concurrently executed, the deadlock can occur.
>
> # DEADLOCK 3:
> convert_extent_bit()
> spin_lock(&tree->lock); --> Line 1241 (Lock A)
> set_state_bits()
> btrfs_set_delalloc_extent()
> spin_lock(&BTRFS_I(inode)->lock); --> Line 2007 or 2017 or 2029 (Lock
> B)
>
> btrfs_inode_safe_disk_i_size_write()
> spin_lock(&BTRFS_I(inode)->lock); --> Line 53 (Lock B)
> find_contiguous_extent_bit()
> spin_lock(&tree->lock); --> Line 1620 (Lock A)
>
> When convert_extent_bit() and btrfs_inode_safe_disk_i_size_write() are
> concurrently executed, the deadlock can occur.
>
> I am not quite sure whether these possible deadlocks are real and how to fix
> them if they are real.
> Any feedback would be appreciated, thanks :)
>
Hey Jia-Ju,
This is pretty good work, unfortunately it's wrong but it's in a subtle way that
a tool wouldn't be able to catch. The btrfs_inode_safe_disk_i_size_write()
helper only messes with BTRFS_I(inode)->file_extent_tree, which is separate from
the BTRFS_I(inode)->io_tree. io_tree gets the btrfs_set_delalloc_extent() stuff
called on it, but the file_extent_tree does not. The file_extent_tree has
inode->lock -> tree->lock as the locking order, whereas the file_extent_tree has
inode->lock -> tree->lock as the locking order. Thanks,
Josef
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [BUG] fs: btrfs: several possible ABBA deadlocks
2021-11-29 0:34 ` Josef Bacik
@ 2021-11-29 1:48 ` Jia-Ju Bai
2021-11-29 14:37 ` Nikolay Borisov
1 sibling, 0 replies; 4+ messages in thread
From: Jia-Ju Bai @ 2021-11-29 1:48 UTC (permalink / raw)
To: Josef Bacik; +Cc: clm, dsterba, linux-btrfs, linux-kernel
On 2021/11/29 8:34, Josef Bacik wrote:
> On Sat, Nov 27, 2021 at 04:23:37PM +0800, Jia-Ju Bai wrote:
>> Hello,
>>
>> My static analysis tool reports several possible ABBA deadlocks in the btrfs
>> module in Linux 5.10:
>>
>> # DEADLOCK 1:
>> __clear_extent_bit()
>> spin_lock(&tree->lock); --> Line 733 (Lock A)
>> split_state()
>> btrfs_split_delalloc_extent()
>> spin_lock(&BTRFS_I(inode)->lock); --> Line 1870 (Lock B)
>>
>> btrfs_inode_safe_disk_i_size_write()
>> spin_lock(&BTRFS_I(inode)->lock); --> Line 53 (Lock B)
>> find_contiguous_extent_bit()
>> spin_lock(&tree->lock); --> Line 1620 (Lock A)
>>
>> When __clear_extent_bit() and btrfs_inode_safe_disk_i_size_write() are
>> concurrently executed, the deadlock can occur.
>>
>> # DEADLOCK 2:
>> __set_extent_bit()
>> spin_lock(&tree->lock); --> Line 995 (Lock A)
>> set_state_bits()
>> btrfs_set_delalloc_extent()
>> spin_lock(&BTRFS_I(inode)->lock); --> Line 2007 or 2017 or 2029 (Lock
>> B)
>>
>> btrfs_inode_safe_disk_i_size_write()
>> spin_lock(&BTRFS_I(inode)->lock); --> Line 53 (Lock B)
>> find_contiguous_extent_bit()
>> spin_lock(&tree->lock); --> Line 1620 (Lock A)
>>
>> When __set_extent_bit() and btrfs_inode_safe_disk_i_size_write() are
>> concurrently executed, the deadlock can occur.
>>
>> # DEADLOCK 3:
>> convert_extent_bit()
>> spin_lock(&tree->lock); --> Line 1241 (Lock A)
>> set_state_bits()
>> btrfs_set_delalloc_extent()
>> spin_lock(&BTRFS_I(inode)->lock); --> Line 2007 or 2017 or 2029 (Lock
>> B)
>>
>> btrfs_inode_safe_disk_i_size_write()
>> spin_lock(&BTRFS_I(inode)->lock); --> Line 53 (Lock B)
>> find_contiguous_extent_bit()
>> spin_lock(&tree->lock); --> Line 1620 (Lock A)
>>
>> When convert_extent_bit() and btrfs_inode_safe_disk_i_size_write() are
>> concurrently executed, the deadlock can occur.
>>
>> I am not quite sure whether these possible deadlocks are real and how to fix
>> them if they are real.
>> Any feedback would be appreciated, thanks :)
>>
> Hey Jia-Ju,
>
> This is pretty good work, unfortunately it's wrong but it's in a subtle way that
> a tool wouldn't be able to catch. The btrfs_inode_safe_disk_i_size_write()
> helper only messes with BTRFS_I(inode)->file_extent_tree, which is separate from
> the BTRFS_I(inode)->io_tree. io_tree gets the btrfs_set_delalloc_extent() stuff
> called on it, but the file_extent_tree does not. The file_extent_tree has
> inode->lock -> tree->lock as the locking order, whereas the file_extent_tree has
> inode->lock -> tree->lock as the locking order.
Okay, thanks for your explanation :)
Best wishes,
Jia-Ju Bai
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [BUG] fs: btrfs: several possible ABBA deadlocks
2021-11-29 0:34 ` Josef Bacik
2021-11-29 1:48 ` Jia-Ju Bai
@ 2021-11-29 14:37 ` Nikolay Borisov
1 sibling, 0 replies; 4+ messages in thread
From: Nikolay Borisov @ 2021-11-29 14:37 UTC (permalink / raw)
To: Josef Bacik, Jia-Ju Bai; +Cc: clm, dsterba, linux-btrfs, linux-kernel
On 29.11.21 г. 2:34, Josef Bacik wrote:
> On Sat, Nov 27, 2021 at 04:23:37PM +0800, Jia-Ju Bai wrote:
>> Hello,
>>
>> My static analysis tool reports several possible ABBA deadlocks in the btrfs
>> module in Linux 5.10:
>>
>> # DEADLOCK 1:
>> __clear_extent_bit()
>> spin_lock(&tree->lock); --> Line 733 (Lock A)
>> split_state()
>> btrfs_split_delalloc_extent()
>> spin_lock(&BTRFS_I(inode)->lock); --> Line 1870 (Lock B)
>>
>> btrfs_inode_safe_disk_i_size_write()
>> spin_lock(&BTRFS_I(inode)->lock); --> Line 53 (Lock B)
>> find_contiguous_extent_bit()
>> spin_lock(&tree->lock); --> Line 1620 (Lock A)
>>
>> When __clear_extent_bit() and btrfs_inode_safe_disk_i_size_write() are
>> concurrently executed, the deadlock can occur.
>>
>> # DEADLOCK 2:
>> __set_extent_bit()
>> spin_lock(&tree->lock); --> Line 995 (Lock A)
>> set_state_bits()
>> btrfs_set_delalloc_extent()
>> spin_lock(&BTRFS_I(inode)->lock); --> Line 2007 or 2017 or 2029 (Lock
>> B)
>>
>> btrfs_inode_safe_disk_i_size_write()
>> spin_lock(&BTRFS_I(inode)->lock); --> Line 53 (Lock B)
>> find_contiguous_extent_bit()
>> spin_lock(&tree->lock); --> Line 1620 (Lock A)
>>
>> When __set_extent_bit() and btrfs_inode_safe_disk_i_size_write() are
>> concurrently executed, the deadlock can occur.
>>
>> # DEADLOCK 3:
>> convert_extent_bit()
>> spin_lock(&tree->lock); --> Line 1241 (Lock A)
>> set_state_bits()
>> btrfs_set_delalloc_extent()
>> spin_lock(&BTRFS_I(inode)->lock); --> Line 2007 or 2017 or 2029 (Lock
>> B)
>>
>> btrfs_inode_safe_disk_i_size_write()
>> spin_lock(&BTRFS_I(inode)->lock); --> Line 53 (Lock B)
>> find_contiguous_extent_bit()
>> spin_lock(&tree->lock); --> Line 1620 (Lock A)
>>
>> When convert_extent_bit() and btrfs_inode_safe_disk_i_size_write() are
>> concurrently executed, the deadlock can occur.
>>
>> I am not quite sure whether these possible deadlocks are real and how to fix
>> them if they are real.
>> Any feedback would be appreciated, thanks :)
>>
>
> Hey Jia-Ju,
>
> This is pretty good work, unfortunately it's wrong but it's in a subtle way that
> a tool wouldn't be able to catch. The btrfs_inode_safe_disk_i_size_write()
> helper only messes with BTRFS_I(inode)->file_extent_tree, which is separate from
> the BTRFS_I(inode)->io_tree. io_tree gets the btrfs_set_delalloc_extent() stuff
> called on it, but the file_extent_tree does not. The file_extent_tree has
> inode->lock -> tree->lock as the locking order, whereas the file_extent_tree has
> inode->lock -> tree->lock as the locking order. Thanks,
nit: did you mean to reverse tree->lock ->inode->lock for the
file_extent_tree?
>
> Josef
>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2021-11-29 14:39 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-27 8:23 [BUG] fs: btrfs: several possible ABBA deadlocks Jia-Ju Bai
2021-11-29 0:34 ` Josef Bacik
2021-11-29 1:48 ` Jia-Ju Bai
2021-11-29 14:37 ` Nikolay Borisov
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.