Skip to contentSkip to conversation

hfsplus: remove mutex_lock check in hfsplus_free_extents

PATCH In review Yangtao Li 5 participants 10 messages May 29, 2025 – Jun 6, 2025

Subsystems (2)
lore
Share & tools
lore

Yangtao Li

author maintainer
#0 lore raw

PATCH v2 1/1 hfsplus: remove mutex_lock check in hfsplus_free_extents

Syzbot reported an issue in hfsplus filesystem:

------------[ cut here ]------------
WARNING: CPU: 0 PID: 4400 at fs/hfsplus/extents.c:346
	hfsplus_free_extents+0x700/0xad0
Call Trace:
<TASK>
hfsplus_file_truncate+0x768/0xbb0 fs/hfsplus/extents.c:606
hfsplus_write_begin+0xc2/0xd0 fs/hfsplus/inode.c:56
cont_expand_zero fs/buffer.c:2383 [inline]
cont_write_begin+0x2cf/0x860 fs/buffer.c:2446
hfsplus_write_begin+0x86/0xd0 fs/hfsplus/inode.c:52
generic_cont_expand_simple+0x151/0x250 fs/buffer.c:2347
hfsplus_setattr+0x168/0x280 fs/hfsplus/inode.c:263
notify_change+0xe38/0x10f0 fs/attr.c:420
do_truncate+0x1fb/0x2e0 fs/open.c:65
do_sys_ftruncate+0x2eb/0x380 fs/open.c:193
do_syscall_x64 arch/x86/entry/common.c:50 [inline]
do_syscall_64+0x3d/0xb0 arch/x86/entry/common.c:80
entry_SYSCALL_64_after_hwframe+0x63/0xcd

To avoid deadlock, Commit 31651c607151 ("hfsplus: avoid deadlock on file truncation") unlock extree before hfsplus_free_extents(), and add check wheather extree is locked in hfsplus_free_extents().

However, when operations such as hfsplus_file_release, hfsplus_setattr, hfsplus_unlink, and hfsplus_get_block are executed concurrently in different files, it is very likely to trigger the WARN_ON, which will lead syzbot and xfstest to consider it as an abnormality.

The comment above this warning also describes one of the easy triggering situations, which can easily trigger and cause xfstest&syzbot to report errors.

[task A] [task B]
->hfsplus_file_release
->hfsplus_file_truncate

    ->hfs_find_init
      ->mutex_lock
    ->mutex_unlock
				->hfsplus_write_begin
				  ->hfsplus_get_block
				    ->hfsplus_file_extend
				      ->hfsplus_ext_read_extent
				        ->hfs_find_init
					  ->mutex_lock
    ->hfsplus_free_extents
      WARN_ON(mutex_is_locked) !!!

Several threads could try to lock the shared extents tree. And warning can be triggered in one thread when another thread has locked the tree. This is the wrong behavior of the code and we need to remove the warning.

Fixes 31651c607151f ("hfsplus: avoid deadlock on file truncation")
Reported-by [email protected]
---
 fs/hfsplus/extents.c | 3 ---
 1 file changed, 3 deletions(-)
fs/hfsplus/extents.c +0 -3
index a6d61685ae79..b1699b3c246a 100644
@@ -342,9 +342,6 @@ static int hfsplus_free_extents(struct super_block *sb,
 	int i;
 	int err = 0;
 
-	/* Mapping the allocation file may lock the extent tree */
-	WARN_ON(mutex_is_locked(&HFSPLUS_SB(sb)->ext_tree->tree_lock));
-
 	hfsplus_dump_extent(extent);
 	for (i = 0; i < 8; extent++, i++) {
 		count = be32_to_cpu(extent->block_count);
Signature · 1 line
2.48.1
#1 lore raw
Quoted 38 lines · Yangtao Li

Syzbot reported an issue in hfsplus filesystem:

------------[ cut here ]------------
WARNING: CPU: 0 PID: 4400 at fs/hfsplus/extents.c:346
	hfsplus_free_extents+0x700/0xad0
Call Trace:
<TASK>
hfsplus_file_truncate+0x768/0xbb0 fs/hfsplus/extents.c:606
hfsplus_write_begin+0xc2/0xd0 fs/hfsplus/inode.c:56
cont_expand_zero fs/buffer.c:2383 [inline]
cont_write_begin+0x2cf/0x860 fs/buffer.c:2446
hfsplus_write_begin+0x86/0xd0 fs/hfsplus/inode.c:52
generic_cont_expand_simple+0x151/0x250 fs/buffer.c:2347
hfsplus_setattr+0x168/0x280 fs/hfsplus/inode.c:263
notify_change+0xe38/0x10f0 fs/attr.c:420
do_truncate+0x1fb/0x2e0 fs/open.c:65
do_sys_ftruncate+0x2eb/0x380 fs/open.c:193
do_syscall_x64 arch/x86/entry/common.c:50 [inline]
do_syscall_64+0x3d/0xb0 arch/x86/entry/common.c:80
entry_SYSCALL_64_after_hwframe+0x63/0xcd

To avoid deadlock, Commit 31651c607151 ("hfsplus: avoid deadlock on file truncation") unlock extree before hfsplus_free_extents(), and add check wheather extree is locked in hfsplus_free_extents().

However, when operations such as hfsplus_file_release, hfsplus_setattr, hfsplus_unlink, and hfsplus_get_block are executed concurrently in different files, it is very likely to trigger the WARN_ON, which will lead syzbot and xfstest to consider it as an abnormality.

The comment above this warning also describes one of the easy triggering situations, which can easily trigger and cause xfstest&syzbot to report errors.

[task A] [task B]
->hfsplus_file_release
->hfsplus_file_truncate

Quoted 11 lines
     ->hfs_find_init
       ->mutex_lock
     ->mutex_unlock
				->hfsplus_write_begin
				  ->hfsplus_get_block
				    ->hfsplus_file_extend
				      ->hfsplus_ext_read_extent
				        ->hfs_find_init
					  ->mutex_lock
     ->hfsplus_free_extents
       WARN_ON(mutex_is_locked) !!!

I am not familiar with hfsplus, but hfsplus_file_release calls hfsplus_file_truncate with inode lock, and hfsplus_write_begin can be called from hfsplus_file_truncate and buffer write, which should also grab inode lock, so that I think task B should be writeback process, which call hfsplus_get_block.

And ->opencnt seems serves as something like link count of other fs, may be we can move hfsplus_file_truncate to hfsplus_evict_inode, which can only be called when all users of this inode disappear and writeback process should also finished for this inode.

Quoted 28 lines

Several threads could try to lock the shared extents tree. And warning can be triggered in one thread when another thread has locked the tree. This is the wrong behavior of the code and we need to remove the warning.

Fixes 31651c607151f ("hfsplus: avoid deadlock on file truncation")
Reported-by [email protected]
---
  fs/hfsplus/extents.c | 3 ---
  1 file changed, 3 deletions(-)
fs/hfsplus/extents.c +0 -3
index a6d61685ae79..b1699b3c246a 100644
@@ -342,9 +342,6 @@ static int hfsplus_free_extents(struct super_block *sb,
  	int i;
  	int err = 0;
 
-	/* Mapping the allocation file may lock the extent tree */
-	WARN_ON(mutex_is_locked(&HFSPLUS_SB(sb)->ext_tree->tree_lock));
-
  	hfsplus_dump_extent(extent);
  	for (i = 0; i < 8; extent++, i++) {
  		count = be32_to_cpu(extent->block_count);
Signature · 1 line
Regards
author maintainer
#2 lore raw

Hi,

Quoted 52 lines · wangjianjian (C)

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Quoted 35 lines · Yangtao Li

Syzbot reported an issue in hfsplus filesystem:

------------[ cut here ]------------
WARNING: CPU: 0 PID: 4400 at fs/hfsplus/extents.c:346
      hfsplus_free_extents+0x700/0xad0
Call Trace:
<TASK>
hfsplus_file_truncate+0x768/0xbb0 fs/hfsplus/extents.c:606
hfsplus_write_begin+0xc2/0xd0 fs/hfsplus/inode.c:56
cont_expand_zero fs/buffer.c:2383 [inline]
cont_write_begin+0x2cf/0x860 fs/buffer.c:2446
hfsplus_write_begin+0x86/0xd0 fs/hfsplus/inode.c:52
generic_cont_expand_simple+0x151/0x250 fs/buffer.c:2347
hfsplus_setattr+0x168/0x280 fs/hfsplus/inode.c:263
notify_change+0xe38/0x10f0 fs/attr.c:420
do_truncate+0x1fb/0x2e0 fs/open.c:65
do_sys_ftruncate+0x2eb/0x380 fs/open.c:193
do_syscall_x64 arch/x86/entry/common.c:50 [inline]
do_syscall_64+0x3d/0xb0 arch/x86/entry/common.c:80
entry_SYSCALL_64_after_hwframe+0x63/0xcd

To avoid deadlock, Commit 31651c607151 ("hfsplus: avoid deadlock on file truncation") unlock extree before hfsplus_free_extents(), and add check wheather extree is locked in hfsplus_free_extents().

However, when operations such as hfsplus_file_release, hfsplus_setattr, hfsplus_unlink, and hfsplus_get_block are executed concurrently in different files, it is very likely to trigger the WARN_ON, which will lead syzbot and xfstest to consider it as an abnormality.

The comment above this warning also describes one of the easy triggering situations, which can easily trigger and cause xfstest&syzbot to report errors.

Quoted 14 lines

[task A]                      [task B] ->hfsplus_file_release
   ->hfsplus_file_truncate
     ->hfs_find_init
       ->mutex_lock
     ->mutex_unlock
                              ->hfsplus_write_begin ->hfsplus_get_block ->hfsplus_file_extend ->hfsplus_ext_read_extent ->hfs_find_init ->mutex_lock ->hfsplus_free_extents WARN_ON(mutex_is_locked) !!!

Quoted 5 lines

I am not familiar with hfsplus, but hfsplus_file_release calls hfsplus_file_truncate with inode lock, and hfsplus_write_begin can be called from hfsplus_file_truncate and buffer write, which should also grab inode lock, so that I think task B should be writeback process, which call hfsplus_get_block.

Did you read the commit log carefully? I mentioned different files.

Quoted 28 lines

And ->opencnt seems serves as something like link count of other fs, may be we can move hfsplus_file_truncate to hfsplus_evict_inode, which can only be called when all users of this inode disappear and writeback process should also finished for this inode.

Quoted 23 lines

Several threads could try to lock the shared extents tree. And warning can be triggered in one thread when another thread has locked the tree. This is the wrong behavior of the code and we need to remove the warning.

Fixes 31651c607151f ("hfsplus: avoid deadlock on file truncation")
Reported-by [email protected]
Closes https://apc01.safelinks.protection.outlook.com/? url=https%3A%2F%2Flore.kernel.org%2Fall%2F00000000000057fa4605ef101c4c%40google.com%2F&data=05%7C02%7Cfrank.li%40vivo.com%7C54c2b4030d4c4a98c6c908dd9e9c71b1%7C923e42dc48d54cbeb5821a797a6412ed%7C0%7C0%7C638841116945048579%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=4snXozMFZN%2FsmsL9CP5VJb4mf2p0ReNhQIEuA%2B3tD3A%3D&reserved=0

---
  fs/hfsplus/extents.c | 3 ---

  1 file changed, 3 deletions(-)
fs/hfsplus/extents.c +0 -0
index a6d61685ae79..b1699b3c246a 100644
@@ -342,9 +342,6 @@ static int hfsplus_free_extents(struct super_block 
 *sb,
       int i;
       int err = 0;
 
Quoted 9 lines
Quoted 6 lines
fs/hfsplus/extents.c +0 -3
-     /* Mapping the allocation file may lock the extent tree */
-     WARN_ON(mutex_is_locked(&HFSPLUS_SB(sb)->ext_tree->tree_lock));
-
       hfsplus_dump_extent(extent);
       for (i = 0; i < 8; extent++, i++) {

              count = be32_to_cpu(extent->block_count);

Signature · 1 line
Regards

Thx,
Yangtao

#3 lore raw
Quoted 55 lines · Yangtao Li

Hi,

Quoted 52 lines · wangjianjian (C)

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Quoted 49 lines · Yangtao Li

Syzbot reported an issue in hfsplus filesystem:

------------[ cut here ]------------
WARNING: CPU: 0 PID: 4400 at fs/hfsplus/extents.c:346
      hfsplus_free_extents+0x700/0xad0
Call Trace:
<TASK>
hfsplus_file_truncate+0x768/0xbb0 fs/hfsplus/extents.c:606
hfsplus_write_begin+0xc2/0xd0 fs/hfsplus/inode.c:56
cont_expand_zero fs/buffer.c:2383 [inline]
cont_write_begin+0x2cf/0x860 fs/buffer.c:2446
hfsplus_write_begin+0x86/0xd0 fs/hfsplus/inode.c:52
generic_cont_expand_simple+0x151/0x250 fs/buffer.c:2347
hfsplus_setattr+0x168/0x280 fs/hfsplus/inode.c:263
notify_change+0xe38/0x10f0 fs/attr.c:420
do_truncate+0x1fb/0x2e0 fs/open.c:65
do_sys_ftruncate+0x2eb/0x380 fs/open.c:193
do_syscall_x64 arch/x86/entry/common.c:50 [inline]
do_syscall_64+0x3d/0xb0 arch/x86/entry/common.c:80
entry_SYSCALL_64_after_hwframe+0x63/0xcd

To avoid deadlock, Commit 31651c607151 ("hfsplus: avoid deadlock on file truncation") unlock extree before hfsplus_free_extents(), and add check wheather extree is locked in hfsplus_free_extents().

However, when operations such as hfsplus_file_release, hfsplus_setattr, hfsplus_unlink, and hfsplus_get_block are executed concurrently in different files, it is very likely to trigger the WARN_ON, which will lead syzbot and xfstest to consider it as an abnormality.

The comment above this warning also describes one of the easy triggering situations, which can easily trigger and cause xfstest&syzbot to report errors.

[task A]                      [task B] ->hfsplus_file_release
   ->hfsplus_file_truncate
     ->hfs_find_init
       ->mutex_lock
     ->mutex_unlock
                              ->hfsplus_write_begin ->hfsplus_get_block ->hfsplus_file_extend ->hfsplus_ext_read_extent ->hfs_find_init ->mutex_lock ->hfsplus_free_extents WARN_ON(mutex_is_locked) !!!

Quoted 8 lines
Quoted 5 lines

I am not familiar with hfsplus, but hfsplus_file_release calls hfsplus_file_truncate with inode lock, and hfsplus_write_begin can be called from hfsplus_file_truncate and buffer write, which should also grab inode lock, so that I think task B should be writeback process, which call hfsplus_get_block.

Did you read the commit log carefully? I mentioned different files.

sorry, didn't not notice that.

Quoted 41 lines
Quoted 28 lines

And ->opencnt seems serves as something like link count of other fs, may be we can move hfsplus_file_truncate to hfsplus_evict_inode, which can only be called when all users of this inode disappear and writeback process should also finished for this inode.

Quoted 23 lines

Several threads could try to lock the shared extents tree. And warning can be triggered in one thread when another thread has locked the tree. This is the wrong behavior of the code and we need to remove the warning.

Fixes 31651c607151f ("hfsplus: avoid deadlock on file truncation")
Reported-by [email protected]
Closes https://apc01.safelinks.protection.outlook.com/? url=https%3A%2F%2Flore.kernel.org%2Fall%2F00000000000057fa4605ef101c4c%40google.com%2F&data=05%7C02%7Cfrank.li%40vivo.com%7C54c2b4030d4c4a98c6c908dd9e9c71b1%7C923e42dc48d54cbeb5821a797a6412ed%7C0%7C0%7C638841116945048579%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=4snXozMFZN%2FsmsL9CP5VJb4mf2p0ReNhQIEuA%2B3tD3A%3D&reserved=0

---
  fs/hfsplus/extents.c | 3 ---

  1 file changed, 3 deletions(-)
fs/hfsplus/extents.c +0 -0
index a6d61685ae79..b1699b3c246a 100644
@@ -342,9 +342,6 @@ static int hfsplus_free_extents(struct 
 super_block *sb,
       int i;
       int err = 0;
 
Quoted 9 lines
Quoted 6 lines
fs/hfsplus/extents.c +0 -3
-     /* Mapping the allocation file may lock the extent tree */
-     WARN_ON(mutex_is_locked(&HFSPLUS_SB(sb)->ext_tree->tree_lock));
-
       hfsplus_dump_extent(extent);
       for (i = 0; i < 8; extent++, i++) {

              count = be32_to_cpu(extent->block_count);

Signature · 1 line
Regards

Thx,
Yangtao

Signature · 1 line
Regards
Reviewed-by on v2 1/1
#4 lore raw
In reply to Yangtao Li
Quoted 68 lines · Yangtao Li

Syzbot reported an issue in hfsplus filesystem:

------------[ cut here ]------------
WARNING: CPU: 0 PID: 4400 at fs/hfsplus/extents.c:346
	hfsplus_free_extents+0x700/0xad0
Call Trace:
<TASK>
hfsplus_file_truncate+0x768/0xbb0 fs/hfsplus/extents.c:606
hfsplus_write_begin+0xc2/0xd0 fs/hfsplus/inode.c:56
cont_expand_zero fs/buffer.c:2383 [inline]
cont_write_begin+0x2cf/0x860 fs/buffer.c:2446
hfsplus_write_begin+0x86/0xd0 fs/hfsplus/inode.c:52
generic_cont_expand_simple+0x151/0x250 fs/buffer.c:2347
hfsplus_setattr+0x168/0x280 fs/hfsplus/inode.c:263
notify_change+0xe38/0x10f0 fs/attr.c:420
do_truncate+0x1fb/0x2e0 fs/open.c:65
do_sys_ftruncate+0x2eb/0x380 fs/open.c:193
do_syscall_x64 arch/x86/entry/common.c:50 [inline]
do_syscall_64+0x3d/0xb0 arch/x86/entry/common.c:80
entry_SYSCALL_64_after_hwframe+0x63/0xcd

To avoid deadlock, Commit 31651c607151 ("hfsplus: avoid deadlock on file truncation") unlock extree before hfsplus_free_extents(), and add check wheather extree is locked in hfsplus_free_extents().

However, when operations such as hfsplus_file_release, hfsplus_setattr, hfsplus_unlink, and hfsplus_get_block are executed concurrently in different files, it is very likely to trigger the WARN_ON, which will lead syzbot and xfstest to consider it as an abnormality.

The comment above this warning also describes one of the easy triggering situations, which can easily trigger and cause xfstest&syzbot to report errors.

[task A] [task B]
->hfsplus_file_release
->hfsplus_file_truncate

    ->hfs_find_init
      ->mutex_lock
    ->mutex_unlock
				->hfsplus_write_begin
				  ->hfsplus_get_block
				    ->hfsplus_file_extend
				      ->hfsplus_ext_read_extent
				        ->hfs_find_init
					  ->mutex_lock
    ->hfsplus_free_extents
      WARN_ON(mutex_is_locked) !!!

Several threads could try to lock the shared extents tree. And warning can be triggered in one thread when another thread has locked the tree. This is the wrong behavior of the code and we need to remove the warning.

Fixes 31651c607151f ("hfsplus: avoid deadlock on file truncation")
Reported-by [email protected]
---
 fs/hfsplus/extents.c | 3 ---
 1 file changed, 3 deletions(-)
fs/hfsplus/extents.c +0 -0
index a6d61685ae79..b1699b3c246a 100644

fs/hfsplus/extents.c static int hfsplus_free_extents(struct super_block *sb, · line 347

Quoted 6 lines
fs/hfsplus/extents.c +0 -3
@@ -342,9 +342,6 @@ static int hfsplus_free_extents(struct super_block *sb,
 	int i;
 	int err = 0;
 
-	/* Mapping the allocation file may lock the extent tree */
-	WARN_ON(mutex_is_locked(&HFSPLUS_SB(sb)->ext_tree->tree_lock));
-

Makes sense to me. Looks good.

But I really like your mentioning of reproducing the issue in generic/013 and really nice analysis of the issue there. Sadly, we haven't it in the comment. :)

Thanks,
Slava.

Quoted 3 lines
fs/hfsplus/extents.c +0 -0
 	hfsplus_dump_extent(extent);
 	for (i = 0; i < 8; extent++, i++) {
 		count = be32_to_cpu(extent->block_count);
maintainer
#5 lore raw
Quoted 8 lines · Viacheslav Dubeyko
Quoted 8 lines
fs/hfsplus/extents.c +0 -0
index a6d61685ae79..b1699b3c246a 100644
@@ -342,9 +342,6 @@ static int hfsplus_free_extents(struct super_block *sb,
 	int i;
 	int err = 0;
 
Quoted 8 lines

fs/hfsplus/extents.c static int hfsplus_free_extents(struct super_block *sb, · line 347

Quoted 3 lines
fs/hfsplus/extents.c +0 -3
-	/* Mapping the allocation file may lock the extent tree */
-	WARN_ON(mutex_is_locked(&HFSPLUS_SB(sb)->ext_tree->tree_lock));
-

Makes sense to me. Looks good.

But I really like your mentioning of reproducing the issue in generic/013 and really nice analysis of the issue there. Sadly, we haven't it in the comment. :)

Umm... *Is* that thing safe to call without that lock?

#6 lore raw
Quoted 15 lines · Al Viro
Quoted 14 lines · Viacheslav Dubeyko

fs/hfsplus/extents.c static int hfsplus_free_extents(struct super_block *sb, · line 347

Quoted 11 lines
fs/hfsplus/extents.c +0 -3
index a6d61685ae79..b1699b3c246a 100644
@@ -342,9 +342,6 @@ static int hfsplus_free_extents(struct super_block *sb,
 	int i;
 	int err = 0;
 
-	/* Mapping the allocation file may lock the extent tree */
-	WARN_ON(mutex_is_locked(&HFSPLUS_SB(sb)->ext_tree->tree_lock));
-

Makes sense to me. Looks good.

Quoted 4 lines
Quoted 2 lines

But I really like your mentioning of reproducing the issue in generic/013 and really nice analysis of the issue there. Sadly, we haven't it in the comment. :)

Umm... *Is* that thing safe to call without that lock?

As far as I can see, hfsplus_free_fork() works under ext_tree->tree_lock mutex lock.
And hfsplus_free_extents() calls hfsplus_block_free(). This guy uses sbi->alloc_mutex to protect free blocks operation. So, operation should be mostly safe.

Thanks,
Slava.

author maintainer
#8 lore raw
In reply to Viacheslav Dubeyko

Hi Slava,

Quoted 72 lines · Viacheslav Dubeyko
Quoted 71 lines · Yangtao Li

Syzbot reported an issue in hfsplus filesystem:

------------[ cut here ]------------
WARNING: CPU: 0 PID: 4400 at fs/hfsplus/extents.c:346
	hfsplus_free_extents+0x700/0xad0
Call Trace:
<TASK>
hfsplus_file_truncate+0x768/0xbb0 fs/hfsplus/extents.c:606
hfsplus_write_begin+0xc2/0xd0 fs/hfsplus/inode.c:56
cont_expand_zero fs/buffer.c:2383 [inline]
cont_write_begin+0x2cf/0x860 fs/buffer.c:2446
hfsplus_write_begin+0x86/0xd0 fs/hfsplus/inode.c:52
generic_cont_expand_simple+0x151/0x250 fs/buffer.c:2347
hfsplus_setattr+0x168/0x280 fs/hfsplus/inode.c:263
notify_change+0xe38/0x10f0 fs/attr.c:420
do_truncate+0x1fb/0x2e0 fs/open.c:65
do_sys_ftruncate+0x2eb/0x380 fs/open.c:193
do_syscall_x64 arch/x86/entry/common.c:50 [inline]
do_syscall_64+0x3d/0xb0 arch/x86/entry/common.c:80
entry_SYSCALL_64_after_hwframe+0x63/0xcd

To avoid deadlock, Commit 31651c607151 ("hfsplus: avoid deadlock on file truncation") unlock extree before hfsplus_free_extents(), and add check wheather extree is locked in hfsplus_free_extents().

However, when operations such as hfsplus_file_release, hfsplus_setattr, hfsplus_unlink, and hfsplus_get_block are executed concurrently in different files, it is very likely to trigger the WARN_ON, which will lead syzbot and xfstest to consider it as an abnormality.

The comment above this warning also describes one of the easy triggering situations, which can easily trigger and cause xfstest&syzbot to report errors.

[task A] [task B]
->hfsplus_file_release
->hfsplus_file_truncate

     ->hfs_find_init
       ->mutex_lock
     ->mutex_unlock
				->hfsplus_write_begin
				  ->hfsplus_get_block
				    ->hfsplus_file_extend
				      ->hfsplus_ext_read_extent
				        ->hfs_find_init
					  ->mutex_lock
     ->hfsplus_free_extents
       WARN_ON(mutex_is_locked) !!!

Several threads could try to lock the shared extents tree. And warning can be triggered in one thread when another thread has locked the tree. This is the wrong behavior of the code and we need to remove the warning.

Fixes 31651c607151f ("hfsplus: avoid deadlock on file truncation")
Reported-by [email protected]
---
  fs/hfsplus/extents.c | 3 ---
  1 file changed, 3 deletions(-)
fs/hfsplus/extents.c +0 -0
index a6d61685ae79..b1699b3c246a 100644
@@ -342,9 +342,6 @@ static int hfsplus_free_extents(struct super_block *sb,
  	int i;
  	int err = 0;
 
Quoted 8 lines

fs/hfsplus/extents.c static int hfsplus_free_extents(struct super_block *sb, · line 347

Quoted 3 lines
fs/hfsplus/extents.c +0 -3
-	/* Mapping the allocation file may lock the extent tree */
-	WARN_ON(mutex_is_locked(&HFSPLUS_SB(sb)->ext_tree->tree_lock));
-

Makes sense to me. Looks good.

But I really like your mentioning of reproducing the issue in generic/013 and really nice analysis of the issue there. Sadly, we haven't it in the comment. :)

Oh, so this is what you mean, but the description is already very long. I think this may be enough...

Quoted 10 lines

Thanks,
Slava.

Quoted 3 lines
fs/hfsplus/extents.c +0 -0
  	hfsplus_dump_extent(extent);
  	for (i = 0; i < 8; extent++, i++) {
  		count = be32_to_cpu(extent->block_count);

Thx,
Yangtao

#9 lore raw
In reply to Viacheslav Dubeyko

Hi Christian,

Could you please pick up the patch?

Thanks,
Slava.

Quoted 95 lines · Viacheslav Dubeyko
Quoted 77 lines · Yangtao Li

Syzbot reported an issue in hfsplus filesystem:

------------[ cut here ]------------
WARNING: CPU: 0 PID: 4400 at fs/hfsplus/extents.c:346
	hfsplus_free_extents+0x700/0xad0
Call Trace:
<TASK>
hfsplus_file_truncate+0x768/0xbb0 fs/hfsplus/extents.c:606
hfsplus_write_begin+0xc2/0xd0 fs/hfsplus/inode.c:56
cont_expand_zero fs/buffer.c:2383 [inline]
cont_write_begin+0x2cf/0x860 fs/buffer.c:2446
hfsplus_write_begin+0x86/0xd0 fs/hfsplus/inode.c:52
generic_cont_expand_simple+0x151/0x250 fs/buffer.c:2347
hfsplus_setattr+0x168/0x280 fs/hfsplus/inode.c:263
notify_change+0xe38/0x10f0 fs/attr.c:420
do_truncate+0x1fb/0x2e0 fs/open.c:65
do_sys_ftruncate+0x2eb/0x380 fs/open.c:193
do_syscall_x64 arch/x86/entry/common.c:50 [inline]
do_syscall_64+0x3d/0xb0 arch/x86/entry/common.c:80
entry_SYSCALL_64_after_hwframe+0x63/0xcd

To avoid deadlock, Commit 31651c607151 ("hfsplus: avoid deadlock on file truncation") unlock extree before hfsplus_free_extents(), and add check wheather extree is locked in hfsplus_free_extents().

However, when operations such as hfsplus_file_release, hfsplus_setattr, hfsplus_unlink, and hfsplus_get_block are executed concurrently in different files, it is very likely to trigger the WARN_ON, which will lead syzbot and xfstest to consider it as an abnormality.

The comment above this warning also describes one of the easy triggering situations, which can easily trigger and cause xfstest&syzbot to report errors.

[task A] [task B]
->hfsplus_file_release
  ->hfsplus_file_truncate
    ->hfs_find_init
      ->mutex_lock
    ->mutex_unlock

				->hfsplus_write_begin
				  ->hfsplus_get_block
				    ->hfsplus_file_extend
				      ->hfsplus_ext_read_extent
				        ->hfs_find_init
					  ->mutex_lock

    ->hfsplus_free_extents
      WARN_ON(mutex_is_locked) !!!

Several threads could try to lock the shared extents tree. And warning can be triggered in one thread when another thread has locked the tree. This is the wrong behavior of the code and we need to remove the warning.

Fixes 31651c607151f ("hfsplus: avoid deadlock on file truncation")
Reported-by [email protected]

Closes:
https://lore.kernel.org/all/[email protected]/

---
 fs/hfsplus/extents.c | 3 ---

 1 file changed, 3 deletions(-)
fs/hfsplus/extents.c +0 -0
index a6d61685ae79..b1699b3c246a 100644
@@ -342,9 +342,6 @@ static int hfsplus_free_extents(struct
 super_block *sb,
  	int i;
  	int err = 0;

fs/hfsplus/extents.c +0 -2
-	/* Mapping the allocation file may lock the extent tree */
-	WARN_ON(mutex_is_locked(&HFSPLUS_SB(sb)->ext_tree-
Quoted 1 line

tree_lock));

Quoted 1 line

-

Makes sense to me. Looks good.

But I really like your mentioning of reproducing the issue in generic/013 and
really nice analysis of the issue there. Sadly, we haven't it in the comment. :)

Thanks,
Slava.

Quoted 3 lines

  hfsplus_dump_extent(extent);
  for (i = 0; i < 8; extent++, i++) { count = be32_to_cpu(extent->block_count);