Skip to contentSkip to conversation

btrfs: fix data race when accessing the block_group's used field

PATCH In review Hao-ran Zheng 4 participants 10 messages Feb 8, 2025 – Feb 26, 2025 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: fix data race when accessing the block_group's used field

A data race may occur when the function btrfs_discard_queue_work() and the function btrfs_update_block_group() is executed concurrently. Specifically, when the btrfs_update_block_group() function is executed to lines cache->used = old_val;, and btrfs_discard_queue_work() is accessing if(block_group->used == 0) will appear with data race, which may cause block_group to be placed unexpectedly in discard_list or discard_unused_list. The specific function call stack is as follows:

============DATA_RACE============

 btrfs_discard_queue_work+0x245/0x500 [btrfs]
 __btrfs_add_free_space+0x3066/0x32f0 [btrfs]
 btrfs_add_free_space+0x19a/0x200 [btrfs]
 unpin_extent_range+0x847/0x2120 [btrfs]
 btrfs_finish_extent_commit+0x9a3/0x1840 [btrfs]
 btrfs_commit_transaction+0x5f65/0xc0f0 [btrfs]
 transaction_kthread+0x764/0xc20 [btrfs]
 kthread+0x292/0x330
 ret_from_fork+0x4d/0x80
 ret_from_fork_asm+0x1a/0x30
============OTHER_INFO============
 btrfs_update_block_group+0xa9d/0x2430 [btrfs]
 __btrfs_free_extent+0x4f69/0x9920 [btrfs]
 __btrfs_run_delayed_refs+0x290e/0xd7d0 [btrfs]
 btrfs_run_delayed_refs+0x317/0x770 [btrfs]
 flush_space+0x388/0x1440 [btrfs]
 btrfs_preempt_reclaim_metadata_space+0xd65/0x14c0 [btrfs]
 process_scheduled_works+0x716/0xf10
 worker_thread+0xb6a/0x1190
 kthread+0x292/0x330
 ret_from_fork+0x4d/0x80
 ret_from_fork_asm+0x1a/0x30
=================================

Although the block_group->used was checked again in the use of the peek_discard_list function, considering that block_group->used is a 64-bit variable, we still think that the data race here is an unexpected behavior. It is recommended to add READ_ONCE and WRITE_ONCE to read and write.

---
 fs/btrfs/block-group.c | 4 ++--
 fs/btrfs/discard.c     | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/block-group.c +2 -2
index c0a8f7d92acc..c681b97f6835 100644
@@ -3678,7 +3678,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
 	old_val = cache->used;
 	if (alloc) {
 		old_val += num_bytes;
-		cache->used = old_val;
+		WRITE_ONCE(cache->used, old_val);
 		cache->reserved -= num_bytes;
 		cache->reclaim_mark = 0;
 		space_info->bytes_reserved -= num_bytes;
@@ -3690,7 +3690,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
 		spin_unlock(&space_info->lock);
 	} else {
 		old_val -= num_bytes;
-		cache->used = old_val;
+		WRITE_ONCE(cache->used, old_val);
 		cache->pinned += num_bytes;
 		btrfs_space_info_update_bytes_pinned(space_info, num_bytes);
 		space_info->bytes_used -= num_bytes;
fs/btrfs/discard.c +1 -1
index e815d165cccc..71c57b571d50 100644
@@ -363,7 +363,7 @@ void btrfs_discard_queue_work(struct btrfs_discard_ctl *discard_ctl,
 	if (!block_group || !btrfs_test_opt(block_group->fs_info, DISCARD_ASYNC))
 		return;
 
-	if (block_group->used == 0)
+	if (READ_ONCE(block_group->used) == 0)
 		add_to_discard_unused_list(discard_ctl, block_group);
 	else
 		add_to_discard_list(discard_ctl, block_group);
Signature · 1 line
2.34.1
#1 lore raw
Quoted 73 lines · Hao-ran Zheng

A data race may occur when the function btrfs_discard_queue_work() and the function btrfs_update_block_group() is executed concurrently. Specifically, when the btrfs_update_block_group() function is executed to lines cache->used = old_val;, and btrfs_discard_queue_work() is accessing if(block_group->used == 0) will appear with data race, which may cause block_group to be placed unexpectedly in discard_list or discard_unused_list. The specific function call stack is as follows:

============DATA_RACE============

 btrfs_discard_queue_work+0x245/0x500 [btrfs]
 __btrfs_add_free_space+0x3066/0x32f0 [btrfs]
 btrfs_add_free_space+0x19a/0x200 [btrfs]
 unpin_extent_range+0x847/0x2120 [btrfs]
 btrfs_finish_extent_commit+0x9a3/0x1840 [btrfs]
 btrfs_commit_transaction+0x5f65/0xc0f0 [btrfs]
 transaction_kthread+0x764/0xc20 [btrfs]
 kthread+0x292/0x330
 ret_from_fork+0x4d/0x80
 ret_from_fork_asm+0x1a/0x30
============OTHER_INFO============
 btrfs_update_block_group+0xa9d/0x2430 [btrfs]
 __btrfs_free_extent+0x4f69/0x9920 [btrfs]
 __btrfs_run_delayed_refs+0x290e/0xd7d0 [btrfs]
 btrfs_run_delayed_refs+0x317/0x770 [btrfs]
 flush_space+0x388/0x1440 [btrfs]
 btrfs_preempt_reclaim_metadata_space+0xd65/0x14c0 [btrfs]
 process_scheduled_works+0x716/0xf10
 worker_thread+0xb6a/0x1190
 kthread+0x292/0x330
 ret_from_fork+0x4d/0x80
 ret_from_fork_asm+0x1a/0x30
=================================

Although the block_group->used was checked again in the use of the peek_discard_list function, considering that block_group->used is a 64-bit variable, we still think that the data race here is an unexpected behavior. It is recommended to add READ_ONCE and WRITE_ONCE to read and write.

---
 fs/btrfs/block-group.c | 4 ++--
 fs/btrfs/discard.c     | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/block-group.c +2 -2
index c0a8f7d92acc..c681b97f6835 100644
@@ -3678,7 +3678,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
        old_val = cache->used;
        if (alloc) {
                old_val += num_bytes;
-               cache->used = old_val;
+               WRITE_ONCE(cache->used, old_val);
                cache->reserved -= num_bytes;
                cache->reclaim_mark = 0;
                space_info->bytes_reserved -= num_bytes;
@@ -3690,7 +3690,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
                spin_unlock(&space_info->lock);
        } else {
                old_val -= num_bytes;
-               cache->used = old_val;
+               WRITE_ONCE(cache->used, old_val);
                cache->pinned += num_bytes;
                btrfs_space_info_update_bytes_pinned(space_info, num_bytes);
                space_info->bytes_used -= num_bytes;
fs/btrfs/discard.c +0 -0
index e815d165cccc..71c57b571d50 100644

fs/btrfs/discard.c void btrfs_discard_queue_work(struct btrfs_discard_ctl *discard_ctl, · line 366

Quoted 5 lines
fs/btrfs/discard.c +1 -1
@@ -363,7 +363,7 @@ void btrfs_discard_queue_work(struct btrfs_discard_ctl *discard_ctl,
        if (!block_group || !btrfs_test_opt(block_group->fs_info, DISCARD_ASYNC))
                return;
 
-       if (block_group->used == 0)
+       if (READ_ONCE(block_group->used) == 0)

There are at least 3 more places in discard.c where we access ->used without being under the protection of the block group's spinlock. So let's fix this for all places and not just a single one...

Also, this is quite ugly to spread READ_ONCE/WRITE_ONCE all over the place. What we typically do in btrfs is to add helpers that hide them, see block-rsv.h for example.

Also, I don't think we need READ_ONCE/WRITE_ONCE.
We could use data_race(), though I think that could be subject to load/store tearing, or just take the lock.
So adding a helper like this to block-group.h:

static inline u64 btrfs_block_group_used(struct btrfs_block_group *bg) {
u64 ret;

   spin_lock(&bg->lock);
   ret = bg->used;
   spin_unlock(&bg->lock);
    return ret;
}

And then use btrfs_bock_group_used() everywhere in discard.c where we aren't holding a block group's lock.

Thanks.

Quoted 7 lines
fs/btrfs/discard.c +0 -1
                add_to_discard_unused_list(discard_ctl, block_group);
        else
                add_to_discard_list(discard_ctl, block_group);
--

2.34.1

#2 lore raw
Quoted 95 lines · Filipe Manana
Quoted 73 lines · Hao-ran Zheng

A data race may occur when the function btrfs_discard_queue_work() and the function btrfs_update_block_group() is executed concurrently. Specifically, when the btrfs_update_block_group() function is executed to lines cache->used = old_val;, and btrfs_discard_queue_work() is accessing if(block_group->used == 0) will appear with data race, which may cause block_group to be placed unexpectedly in discard_list or discard_unused_list. The specific function call stack is as follows:

============DATA_RACE============

 btrfs_discard_queue_work+0x245/0x500 [btrfs]
 __btrfs_add_free_space+0x3066/0x32f0 [btrfs]
 btrfs_add_free_space+0x19a/0x200 [btrfs]
 unpin_extent_range+0x847/0x2120 [btrfs]
 btrfs_finish_extent_commit+0x9a3/0x1840 [btrfs]
 btrfs_commit_transaction+0x5f65/0xc0f0 [btrfs]
 transaction_kthread+0x764/0xc20 [btrfs]
 kthread+0x292/0x330
 ret_from_fork+0x4d/0x80
 ret_from_fork_asm+0x1a/0x30
============OTHER_INFO============
 btrfs_update_block_group+0xa9d/0x2430 [btrfs]
 __btrfs_free_extent+0x4f69/0x9920 [btrfs]
 __btrfs_run_delayed_refs+0x290e/0xd7d0 [btrfs]
 btrfs_run_delayed_refs+0x317/0x770 [btrfs]
 flush_space+0x388/0x1440 [btrfs]
 btrfs_preempt_reclaim_metadata_space+0xd65/0x14c0 [btrfs]
 process_scheduled_works+0x716/0xf10
 worker_thread+0xb6a/0x1190
 kthread+0x292/0x330
 ret_from_fork+0x4d/0x80
 ret_from_fork_asm+0x1a/0x30
=================================

Although the block_group->used was checked again in the use of the peek_discard_list function, considering that block_group->used is a 64-bit variable, we still think that the data race here is an unexpected behavior. It is recommended to add READ_ONCE and WRITE_ONCE to read and write.

---
 fs/btrfs/block-group.c | 4 ++--
 fs/btrfs/discard.c     | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/block-group.c +2 -2
index c0a8f7d92acc..c681b97f6835 100644
@@ -3678,7 +3678,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
        old_val = cache->used;
        if (alloc) {
                old_val += num_bytes;
-               cache->used = old_val;
+               WRITE_ONCE(cache->used, old_val);
                cache->reserved -= num_bytes;
                cache->reclaim_mark = 0;
                space_info->bytes_reserved -= num_bytes;
@@ -3690,7 +3690,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
                spin_unlock(&space_info->lock);
        } else {
                old_val -= num_bytes;
-               cache->used = old_val;
+               WRITE_ONCE(cache->used, old_val);
                cache->pinned += num_bytes;
                btrfs_space_info_update_bytes_pinned(space_info, num_bytes);
                space_info->bytes_used -= num_bytes;
fs/btrfs/discard.c +0 -0
index e815d165cccc..71c57b571d50 100644

fs/btrfs/discard.c void btrfs_discard_queue_work(struct btrfs_discard_ctl *discard_ctl, · line 366

Quoted 5 lines
fs/btrfs/discard.c +1 -1
@@ -363,7 +363,7 @@ void btrfs_discard_queue_work(struct btrfs_discard_ctl *discard_ctl,
        if (!block_group || !btrfs_test_opt(block_group->fs_info, DISCARD_ASYNC))
                return;
 
-       if (block_group->used == 0)
+       if (READ_ONCE(block_group->used) == 0)

There are at least 3 more places in discard.c where we access ->used without being under the protection of the block group's spinlock. So let's fix this for all places and not just a single one...

Also, this is quite ugly to spread READ_ONCE/WRITE_ONCE all over the place. What we typically do in btrfs is to add helpers that hide them, see block-rsv.h for example.

Also, I don't think we need READ_ONCE/WRITE_ONCE.
We could use data_race(), though I think that could be subject to load/store tearing, or just take the lock.
So adding a helper like this to block-group.h:

static inline u64 btrfs_block_group_used(struct btrfs_block_group *bg)

Quoted 9 lines
{
   u64 ret;

   spin_lock(&bg->lock);
   ret = bg->used;
   spin_unlock(&bg->lock);

    return ret;
}

Would memory barriers be sufficient here? Taking a lock just for reading one member seems excessive...

Quoted 14 lines

And then use btrfs_bock_group_used() everywhere in discard.c where we aren't holding a block group's lock.

Thanks.

Quoted 7 lines
fs/btrfs/discard.c +0 -1
                add_to_discard_unused_list(discard_ctl, block_group);
        else
                add_to_discard_list(discard_ctl, block_group);
--

2.34.1

#3 lore raw
Quoted 104 lines · Daniel Vacek
Quoted 102 lines · Filipe Manana
Quoted 73 lines · Hao-ran Zheng

A data race may occur when the function btrfs_discard_queue_work() and the function btrfs_update_block_group() is executed concurrently. Specifically, when the btrfs_update_block_group() function is executed to lines cache->used = old_val;, and btrfs_discard_queue_work() is accessing if(block_group->used == 0) will appear with data race, which may cause block_group to be placed unexpectedly in discard_list or discard_unused_list. The specific function call stack is as follows:

============DATA_RACE============

 btrfs_discard_queue_work+0x245/0x500 [btrfs]
 __btrfs_add_free_space+0x3066/0x32f0 [btrfs]
 btrfs_add_free_space+0x19a/0x200 [btrfs]
 unpin_extent_range+0x847/0x2120 [btrfs]
 btrfs_finish_extent_commit+0x9a3/0x1840 [btrfs]
 btrfs_commit_transaction+0x5f65/0xc0f0 [btrfs]
 transaction_kthread+0x764/0xc20 [btrfs]
 kthread+0x292/0x330
 ret_from_fork+0x4d/0x80
 ret_from_fork_asm+0x1a/0x30
============OTHER_INFO============
 btrfs_update_block_group+0xa9d/0x2430 [btrfs]
 __btrfs_free_extent+0x4f69/0x9920 [btrfs]
 __btrfs_run_delayed_refs+0x290e/0xd7d0 [btrfs]
 btrfs_run_delayed_refs+0x317/0x770 [btrfs]
 flush_space+0x388/0x1440 [btrfs]
 btrfs_preempt_reclaim_metadata_space+0xd65/0x14c0 [btrfs]
 process_scheduled_works+0x716/0xf10
 worker_thread+0xb6a/0x1190
 kthread+0x292/0x330
 ret_from_fork+0x4d/0x80
 ret_from_fork_asm+0x1a/0x30
=================================

Although the block_group->used was checked again in the use of the peek_discard_list function, considering that block_group->used is a 64-bit variable, we still think that the data race here is an unexpected behavior. It is recommended to add READ_ONCE and WRITE_ONCE to read and write.

---
 fs/btrfs/block-group.c | 4 ++--
 fs/btrfs/discard.c     | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/block-group.c +2 -2
index c0a8f7d92acc..c681b97f6835 100644
@@ -3678,7 +3678,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
        old_val = cache->used;
        if (alloc) {
                old_val += num_bytes;
-               cache->used = old_val;
+               WRITE_ONCE(cache->used, old_val);
                cache->reserved -= num_bytes;
                cache->reclaim_mark = 0;
                space_info->bytes_reserved -= num_bytes;
@@ -3690,7 +3690,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
                spin_unlock(&space_info->lock);
        } else {
                old_val -= num_bytes;
-               cache->used = old_val;
+               WRITE_ONCE(cache->used, old_val);
                cache->pinned += num_bytes;
                btrfs_space_info_update_bytes_pinned(space_info, num_bytes);
                space_info->bytes_used -= num_bytes;
fs/btrfs/discard.c +0 -0
index e815d165cccc..71c57b571d50 100644

fs/btrfs/discard.c void btrfs_discard_queue_work(struct btrfs_discard_ctl *discard_ctl, · line 366

Quoted 5 lines
fs/btrfs/discard.c +1 -1
@@ -363,7 +363,7 @@ void btrfs_discard_queue_work(struct btrfs_discard_ctl *discard_ctl,
        if (!block_group || !btrfs_test_opt(block_group->fs_info, DISCARD_ASYNC))
                return;
 
-       if (block_group->used == 0)
+       if (READ_ONCE(block_group->used) == 0)

There are at least 3 more places in discard.c where we access ->used without being under the protection of the block group's spinlock. So let's fix this for all places and not just a single one...

Also, this is quite ugly to spread READ_ONCE/WRITE_ONCE all over the place. What we typically do in btrfs is to add helpers that hide them, see block-rsv.h for example.

Also, I don't think we need READ_ONCE/WRITE_ONCE.
We could use data_race(), though I think that could be subject to load/store tearing, or just take the lock.
So adding a helper like this to block-group.h:

static inline u64 btrfs_block_group_used(struct btrfs_block_group *bg)

{
   u64 ret;

   spin_lock(&bg->lock);
   ret = bg->used;
   spin_unlock(&bg->lock);
Quoted 5 lines
Quoted 2 lines
    return ret;
}

Would memory barriers be sufficient here? Taking a lock just for reading one member seems excessive...

Do you think there's heavy contention on this lock?

Usually I prefer to go the simplest way, and using locks is a lot more straightforward and easier to understand than memory barriers. Unless it's clear there's an observable performance penalty, keeping it simple is better.

data_race() may be ok here, at least for one of the unprotected accesses it's fine, but would have to analyse the other 3 cases.

Quoted 15 lines
Quoted 15 lines

And then use btrfs_bock_group_used() everywhere in discard.c where we aren't holding a block group's lock.

Thanks.

Quoted 7 lines
fs/btrfs/discard.c +0 -1
                add_to_discard_unused_list(discard_ctl, block_group);
        else
                add_to_discard_list(discard_ctl, block_group);
--

2.34.1

#4 lore raw
Quoted 106 lines · Filipe Manana
Quoted 104 lines · Daniel Vacek
Quoted 102 lines · Filipe Manana
Quoted 73 lines · Hao-ran Zheng

A data race may occur when the function btrfs_discard_queue_work() and the function btrfs_update_block_group() is executed concurrently. Specifically, when the btrfs_update_block_group() function is executed to lines cache->used = old_val;, and btrfs_discard_queue_work() is accessing if(block_group->used == 0) will appear with data race, which may cause block_group to be placed unexpectedly in discard_list or discard_unused_list. The specific function call stack is as follows:

============DATA_RACE============

 btrfs_discard_queue_work+0x245/0x500 [btrfs]
 __btrfs_add_free_space+0x3066/0x32f0 [btrfs]
 btrfs_add_free_space+0x19a/0x200 [btrfs]
 unpin_extent_range+0x847/0x2120 [btrfs]
 btrfs_finish_extent_commit+0x9a3/0x1840 [btrfs]
 btrfs_commit_transaction+0x5f65/0xc0f0 [btrfs]
 transaction_kthread+0x764/0xc20 [btrfs]
 kthread+0x292/0x330
 ret_from_fork+0x4d/0x80
 ret_from_fork_asm+0x1a/0x30
============OTHER_INFO============
 btrfs_update_block_group+0xa9d/0x2430 [btrfs]
 __btrfs_free_extent+0x4f69/0x9920 [btrfs]
 __btrfs_run_delayed_refs+0x290e/0xd7d0 [btrfs]
 btrfs_run_delayed_refs+0x317/0x770 [btrfs]
 flush_space+0x388/0x1440 [btrfs]
 btrfs_preempt_reclaim_metadata_space+0xd65/0x14c0 [btrfs]
 process_scheduled_works+0x716/0xf10
 worker_thread+0xb6a/0x1190
 kthread+0x292/0x330
 ret_from_fork+0x4d/0x80
 ret_from_fork_asm+0x1a/0x30
=================================

Although the block_group->used was checked again in the use of the peek_discard_list function, considering that block_group->used is a 64-bit variable, we still think that the data race here is an unexpected behavior. It is recommended to add READ_ONCE and WRITE_ONCE to read and write.

---
 fs/btrfs/block-group.c | 4 ++--
 fs/btrfs/discard.c     | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/block-group.c +2 -2
index c0a8f7d92acc..c681b97f6835 100644
@@ -3678,7 +3678,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
        old_val = cache->used;
        if (alloc) {
                old_val += num_bytes;
-               cache->used = old_val;
+               WRITE_ONCE(cache->used, old_val);
                cache->reserved -= num_bytes;
                cache->reclaim_mark = 0;
                space_info->bytes_reserved -= num_bytes;
@@ -3690,7 +3690,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
                spin_unlock(&space_info->lock);
        } else {
                old_val -= num_bytes;
-               cache->used = old_val;
+               WRITE_ONCE(cache->used, old_val);
                cache->pinned += num_bytes;
                btrfs_space_info_update_bytes_pinned(space_info, num_bytes);
                space_info->bytes_used -= num_bytes;
fs/btrfs/discard.c +0 -0
index e815d165cccc..71c57b571d50 100644

fs/btrfs/discard.c void btrfs_discard_queue_work(struct btrfs_discard_ctl *discard_ctl, · line 366

Quoted 5 lines
fs/btrfs/discard.c +1 -1
@@ -363,7 +363,7 @@ void btrfs_discard_queue_work(struct btrfs_discard_ctl *discard_ctl,
        if (!block_group || !btrfs_test_opt(block_group->fs_info, DISCARD_ASYNC))
                return;
 
-       if (block_group->used == 0)
+       if (READ_ONCE(block_group->used) == 0)

There are at least 3 more places in discard.c where we access ->used without being under the protection of the block group's spinlock. So let's fix this for all places and not just a single one...

Also, this is quite ugly to spread READ_ONCE/WRITE_ONCE all over the place. What we typically do in btrfs is to add helpers that hide them, see block-rsv.h for example.

Also, I don't think we need READ_ONCE/WRITE_ONCE.
We could use data_race(), though I think that could be subject to load/store tearing, or just take the lock.
So adding a helper like this to block-group.h:

static inline u64 btrfs_block_group_used(struct btrfs_block_group *bg)

{
   u64 ret;

   spin_lock(&bg->lock);
   ret = bg->used;
   spin_unlock(&bg->lock);
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
    return ret;
}

Would memory barriers be sufficient here? Taking a lock just for reading one member seems excessive...

Do you think there's heavy contention on this lock?

This is not about contention. Spin lock should just never be used this way. Or any lock actually. The critical section only contains a single fetch operation which does not justify using a lock.
Hence the only guarantee such lock usage provides are the implicit memory barriers (from which maybe only one of them is really needed depending on the context where this helper is going to be used).

Simply put, the lock is degraded into a memory barrier this way. So why not just use the barriers in the first place if only ordering guarantees are required? It only makes sense.

Quoted 2 lines

Usually I prefer to go the simplest way, and using locks is a lot more straightforward and easier to understand than memory barriers.

How so? Locks provide the same memory barriers implicitly.

Quoted 2 lines

Unless it's clear there's an observable performance penalty, keeping it simple is better.

Exactly. Here is where our opinions differ. For me 'simple' means without useless locking.
I mean especially with locks they should only be used when absolutely necessary. In a sense of 'use the right tool for the job'. There are more lightweight tools possible in this context. Locks provide additional guarantees which are not needed here.

On the other hand I understand that using a lock is a 'better safe than sorry' approach which should also work. Just keep in mind that spinlock may sleep on RT.

Quoted 2 lines

data_race() may be ok here, at least for one of the unprotected accesses it's fine, but would have to analyse the other 3 cases.

That's exactly my thoughts. Maybe not even the barriers are needed after all. This needs to be checked on a per-case basis.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

And then use btrfs_bock_group_used() everywhere in discard.c where we aren't holding a block group's lock.

Thanks.

Quoted 7 lines
fs/btrfs/discard.c +0 -1
                add_to_discard_unused_list(discard_ctl, block_group);
        else
                add_to_discard_list(discard_ctl, block_group);
--

2.34.1

#5 lore raw
Quoted 123 lines · Daniel Vacek
Quoted 106 lines · Filipe Manana
Quoted 104 lines · Daniel Vacek
Quoted 102 lines · Filipe Manana
Quoted 73 lines · Hao-ran Zheng

A data race may occur when the function btrfs_discard_queue_work() and the function btrfs_update_block_group() is executed concurrently. Specifically, when the btrfs_update_block_group() function is executed to lines cache->used = old_val;, and btrfs_discard_queue_work() is accessing if(block_group->used == 0) will appear with data race, which may cause block_group to be placed unexpectedly in discard_list or discard_unused_list. The specific function call stack is as follows:

============DATA_RACE============

 btrfs_discard_queue_work+0x245/0x500 [btrfs]
 __btrfs_add_free_space+0x3066/0x32f0 [btrfs]
 btrfs_add_free_space+0x19a/0x200 [btrfs]
 unpin_extent_range+0x847/0x2120 [btrfs]
 btrfs_finish_extent_commit+0x9a3/0x1840 [btrfs]
 btrfs_commit_transaction+0x5f65/0xc0f0 [btrfs]
 transaction_kthread+0x764/0xc20 [btrfs]
 kthread+0x292/0x330
 ret_from_fork+0x4d/0x80
 ret_from_fork_asm+0x1a/0x30
============OTHER_INFO============
 btrfs_update_block_group+0xa9d/0x2430 [btrfs]
 __btrfs_free_extent+0x4f69/0x9920 [btrfs]
 __btrfs_run_delayed_refs+0x290e/0xd7d0 [btrfs]
 btrfs_run_delayed_refs+0x317/0x770 [btrfs]
 flush_space+0x388/0x1440 [btrfs]
 btrfs_preempt_reclaim_metadata_space+0xd65/0x14c0 [btrfs]
 process_scheduled_works+0x716/0xf10
 worker_thread+0xb6a/0x1190
 kthread+0x292/0x330
 ret_from_fork+0x4d/0x80
 ret_from_fork_asm+0x1a/0x30
=================================

Although the block_group->used was checked again in the use of the peek_discard_list function, considering that block_group->used is a 64-bit variable, we still think that the data race here is an unexpected behavior. It is recommended to add READ_ONCE and WRITE_ONCE to read and write.

---
 fs/btrfs/block-group.c | 4 ++--
 fs/btrfs/discard.c     | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/block-group.c +2 -2
index c0a8f7d92acc..c681b97f6835 100644
@@ -3678,7 +3678,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
        old_val = cache->used;
        if (alloc) {
                old_val += num_bytes;
-               cache->used = old_val;
+               WRITE_ONCE(cache->used, old_val);
                cache->reserved -= num_bytes;
                cache->reclaim_mark = 0;
                space_info->bytes_reserved -= num_bytes;
@@ -3690,7 +3690,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
                spin_unlock(&space_info->lock);
        } else {
                old_val -= num_bytes;
-               cache->used = old_val;
+               WRITE_ONCE(cache->used, old_val);
                cache->pinned += num_bytes;
                btrfs_space_info_update_bytes_pinned(space_info, num_bytes);
                space_info->bytes_used -= num_bytes;
fs/btrfs/discard.c +0 -0
index e815d165cccc..71c57b571d50 100644

fs/btrfs/discard.c void btrfs_discard_queue_work(struct btrfs_discard_ctl *discard_ctl, · line 366

Quoted 5 lines
fs/btrfs/discard.c +1 -1
@@ -363,7 +363,7 @@ void btrfs_discard_queue_work(struct btrfs_discard_ctl *discard_ctl,
        if (!block_group || !btrfs_test_opt(block_group->fs_info, DISCARD_ASYNC))
                return;
 
-       if (block_group->used == 0)
+       if (READ_ONCE(block_group->used) == 0)

There are at least 3 more places in discard.c where we access ->used without being under the protection of the block group's spinlock. So let's fix this for all places and not just a single one...

Also, this is quite ugly to spread READ_ONCE/WRITE_ONCE all over the place. What we typically do in btrfs is to add helpers that hide them, see block-rsv.h for example.

Also, I don't think we need READ_ONCE/WRITE_ONCE.
We could use data_race(), though I think that could be subject to load/store tearing, or just take the lock.
So adding a helper like this to block-group.h:

static inline u64 btrfs_block_group_used(struct btrfs_block_group *bg)

{
   u64 ret;

   spin_lock(&bg->lock);
   ret = bg->used;
   spin_unlock(&bg->lock);
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
    return ret;
}

Would memory barriers be sufficient here? Taking a lock just for reading one member seems excessive...

Do you think there's heavy contention on this lock?

This is not about contention. Spin lock should just never be used this way. Or any lock actually. The critical section only contains a single fetch operation which does not justify using a lock.
Hence the only guarantee such lock usage provides are the implicit memory barriers (from which maybe only one of them is really needed depending on the context where this helper is going to be used).

Quoted 3 lines

Simply put, the lock is degraded into a memory barrier this way. So why not just use the barriers in the first place if only ordering guarantees are required? It only makes sense.

As said earlier, it's a lot easier to reason about lock() and unlock() calls rather than spreading memory barriers in the write and read sides.
Historically he had several mistakes with barriers, they're simply not as straightforward to reason as locks.

Plus spreading the barriers in the read and write sides makes the code not so easy to read, not to mention in case of any new sites updating the member, we'll have to not forget adding a barrier.

It's just easier to reason and maintain.

Quoted 5 lines
Quoted 3 lines

Usually I prefer to go the simplest way, and using locks is a lot more straightforward and easier to understand than memory barriers.

How so? Locks provide the same memory barriers implicitly.

I'm not saying they don't.
I'm talking about easier code to read and maintain.

Quoted 11 lines
Quoted 3 lines

Unless it's clear there's an observable performance penalty, keeping it simple is better.

Exactly. Here is where our opinions differ. For me 'simple' means without useless locking.
I mean especially with locks they should only be used when absolutely necessary. In a sense of 'use the right tool for the job'. There are more lightweight tools possible in this context. Locks provide additional guarantees which are not needed here.

Quoted 3 lines

On the other hand I understand that using a lock is a 'better safe than sorry' approach which should also work. Just keep in mind that spinlock may sleep on RT.

It's not about a better safe than sorry, but easier to read, reason and maintain.

Quoted 22 lines
Quoted 3 lines

data_race() may be ok here, at least for one of the unprotected accesses it's fine, but would have to analyse the other 3 cases.

That's exactly my thoughts. Maybe not even the barriers are needed after all. This needs to be checked on a per-case basis.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

And then use btrfs_bock_group_used() everywhere in discard.c where we aren't holding a block group's lock.

Thanks.

Quoted 7 lines
fs/btrfs/discard.c +0 -1
                add_to_discard_unused_list(discard_ctl, block_group);
        else
                add_to_discard_list(discard_ctl, block_group);
--

2.34.1

#6 lore raw
Quoted 162 lines · Filipe Manana
Quoted 123 lines · Daniel Vacek
Quoted 106 lines · Filipe Manana
Quoted 104 lines · Daniel Vacek
Quoted 102 lines · Filipe Manana
Quoted 73 lines · Hao-ran Zheng

A data race may occur when the function btrfs_discard_queue_work() and the function btrfs_update_block_group() is executed concurrently. Specifically, when the btrfs_update_block_group() function is executed to lines cache->used = old_val;, and btrfs_discard_queue_work() is accessing if(block_group->used == 0) will appear with data race, which may cause block_group to be placed unexpectedly in discard_list or discard_unused_list. The specific function call stack is as follows:

============DATA_RACE============

 btrfs_discard_queue_work+0x245/0x500 [btrfs]
 __btrfs_add_free_space+0x3066/0x32f0 [btrfs]
 btrfs_add_free_space+0x19a/0x200 [btrfs]
 unpin_extent_range+0x847/0x2120 [btrfs]
 btrfs_finish_extent_commit+0x9a3/0x1840 [btrfs]
 btrfs_commit_transaction+0x5f65/0xc0f0 [btrfs]
 transaction_kthread+0x764/0xc20 [btrfs]
 kthread+0x292/0x330
 ret_from_fork+0x4d/0x80
 ret_from_fork_asm+0x1a/0x30
============OTHER_INFO============
 btrfs_update_block_group+0xa9d/0x2430 [btrfs]
 __btrfs_free_extent+0x4f69/0x9920 [btrfs]
 __btrfs_run_delayed_refs+0x290e/0xd7d0 [btrfs]
 btrfs_run_delayed_refs+0x317/0x770 [btrfs]
 flush_space+0x388/0x1440 [btrfs]
 btrfs_preempt_reclaim_metadata_space+0xd65/0x14c0 [btrfs]
 process_scheduled_works+0x716/0xf10
 worker_thread+0xb6a/0x1190
 kthread+0x292/0x330
 ret_from_fork+0x4d/0x80
 ret_from_fork_asm+0x1a/0x30
=================================

Although the block_group->used was checked again in the use of the peek_discard_list function, considering that block_group->used is a 64-bit variable, we still think that the data race here is an unexpected behavior. It is recommended to add READ_ONCE and WRITE_ONCE to read and write.

---
 fs/btrfs/block-group.c | 4 ++--
 fs/btrfs/discard.c     | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/block-group.c +2 -2
index c0a8f7d92acc..c681b97f6835 100644
@@ -3678,7 +3678,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
        old_val = cache->used;
        if (alloc) {
                old_val += num_bytes;
-               cache->used = old_val;
+               WRITE_ONCE(cache->used, old_val);
                cache->reserved -= num_bytes;
                cache->reclaim_mark = 0;
                space_info->bytes_reserved -= num_bytes;
@@ -3690,7 +3690,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
                spin_unlock(&space_info->lock);
        } else {
                old_val -= num_bytes;
-               cache->used = old_val;
+               WRITE_ONCE(cache->used, old_val);
                cache->pinned += num_bytes;
                btrfs_space_info_update_bytes_pinned(space_info, num_bytes);
                space_info->bytes_used -= num_bytes;
fs/btrfs/discard.c +0 -0
index e815d165cccc..71c57b571d50 100644

fs/btrfs/discard.c void btrfs_discard_queue_work(struct btrfs_discard_ctl *discard_ctl, · line 366

Quoted 5 lines
fs/btrfs/discard.c +1 -1
@@ -363,7 +363,7 @@ void btrfs_discard_queue_work(struct btrfs_discard_ctl *discard_ctl,
        if (!block_group || !btrfs_test_opt(block_group->fs_info, DISCARD_ASYNC))
                return;
 
-       if (block_group->used == 0)
+       if (READ_ONCE(block_group->used) == 0)

There are at least 3 more places in discard.c where we access ->used without being under the protection of the block group's spinlock. So let's fix this for all places and not just a single one...

Also, this is quite ugly to spread READ_ONCE/WRITE_ONCE all over the place. What we typically do in btrfs is to add helpers that hide them, see block-rsv.h for example.

Also, I don't think we need READ_ONCE/WRITE_ONCE.
We could use data_race(), though I think that could be subject to load/store tearing, or just take the lock.
So adding a helper like this to block-group.h:

static inline u64 btrfs_block_group_used(struct btrfs_block_group *bg)

{
   u64 ret;

   spin_lock(&bg->lock);
   ret = bg->used;
   spin_unlock(&bg->lock);
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
    return ret;
}

Would memory barriers be sufficient here? Taking a lock just for reading one member seems excessive...

Do you think there's heavy contention on this lock?

This is not about contention. Spin lock should just never be used this way. Or any lock actually. The critical section only contains a single fetch operation which does not justify using a lock.
Hence the only guarantee such lock usage provides are the implicit memory barriers (from which maybe only one of them is really needed depending on the context where this helper is going to be used).

Quoted 3 lines

Simply put, the lock is degraded into a memory barrier this way. So why not just use the barriers in the first place if only ordering guarantees are required? It only makes sense.

As said earlier, it's a lot easier to reason about lock() and unlock() calls rather than spreading memory barriers in the write and read sides.
Historically he had several mistakes with barriers, they're simply not as straightforward to reason as locks.

Plus spreading the barriers in the read and write sides makes the code not so easy to read, not to mention in case of any new sites updating the member, we'll have to not forget adding a barrier.

It's just easier to reason and maintain.

Quoted 5 lines
Quoted 3 lines

Usually I prefer to go the simplest way, and using locks is a lot more straightforward and easier to understand than memory barriers.

How so? Locks provide the same memory barriers implicitly.

I'm not saying they don't.
I'm talking about easier code to read and maintain.

Quoted 11 lines
Quoted 3 lines

Unless it's clear there's an observable performance penalty, keeping it simple is better.

Exactly. Here is where our opinions differ. For me 'simple' means without useless locking.
I mean especially with locks they should only be used when absolutely necessary. In a sense of 'use the right tool for the job'. There are more lightweight tools possible in this context. Locks provide additional guarantees which are not needed here.

Quoted 6 lines
Quoted 3 lines

On the other hand I understand that using a lock is a 'better safe than sorry' approach which should also work. Just keep in mind that spinlock may sleep on RT.

It's not about a better safe than sorry, but easier to read, reason and maintain.

I see. Understood. I guess I'm just too micro-optimizing where it's not really needed and only adds complexity in a way.

Quoted 22 lines
Quoted 22 lines
Quoted 3 lines

data_race() may be ok here, at least for one of the unprotected accesses it's fine, but would have to analyse the other 3 cases.

That's exactly my thoughts. Maybe not even the barriers are needed after all. This needs to be checked on a per-case basis.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

And then use btrfs_bock_group_used() everywhere in discard.c where we aren't holding a block group's lock.

Thanks.

Quoted 7 lines
fs/btrfs/discard.c +0 -1
                add_to_discard_unused_list(discard_ctl, block_group);
        else
                add_to_discard_list(discard_ctl, block_group);
--

2.34.1

#7 lore raw
In reply to Filipe Manana
Quoted 106 lines · Filipe Manana
Quoted 104 lines · Daniel Vacek
Quoted 102 lines · Filipe Manana
Quoted 100 lines · Daniel Vacek
Quoted 98 lines · Filipe Manana
Quoted 73 lines · Hao-ran Zheng

A data race may occur when the function btrfs_discard_queue_work() and the function btrfs_update_block_group() is executed concurrently. Specifically, when the btrfs_update_block_group() function is executed to lines cache->used = old_val;, and btrfs_discard_queue_work() is accessing if(block_group->used == 0) will appear with data race, which may cause block_group to be placed unexpectedly in discard_list or discard_unused_list. The specific function call stack is as follows:

============DATA_RACE============

 btrfs_discard_queue_work+0x245/0x500 [btrfs]
 __btrfs_add_free_space+0x3066/0x32f0 [btrfs]
 btrfs_add_free_space+0x19a/0x200 [btrfs]
 unpin_extent_range+0x847/0x2120 [btrfs]
 btrfs_finish_extent_commit+0x9a3/0x1840 [btrfs]
 btrfs_commit_transaction+0x5f65/0xc0f0 [btrfs]
 transaction_kthread+0x764/0xc20 [btrfs]
 kthread+0x292/0x330
 ret_from_fork+0x4d/0x80
 ret_from_fork_asm+0x1a/0x30
============OTHER_INFO============
 btrfs_update_block_group+0xa9d/0x2430 [btrfs]
 __btrfs_free_extent+0x4f69/0x9920 [btrfs]
 __btrfs_run_delayed_refs+0x290e/0xd7d0 [btrfs]
 btrfs_run_delayed_refs+0x317/0x770 [btrfs]
 flush_space+0x388/0x1440 [btrfs]
 btrfs_preempt_reclaim_metadata_space+0xd65/0x14c0 [btrfs]
 process_scheduled_works+0x716/0xf10
 worker_thread+0xb6a/0x1190
 kthread+0x292/0x330
 ret_from_fork+0x4d/0x80
 ret_from_fork_asm+0x1a/0x30
=================================

Although the block_group->used was checked again in the use of the peek_discard_list function, considering that block_group->used is a 64-bit variable, we still think that the data race here is an unexpected behavior. It is recommended to add READ_ONCE and WRITE_ONCE to read and write.

---
 fs/btrfs/block-group.c | 4 ++--
 fs/btrfs/discard.c     | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/block-group.c +2 -2
index c0a8f7d92acc..c681b97f6835 100644
@@ -3678,7 +3678,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
        old_val = cache->used;
        if (alloc) {
                old_val += num_bytes;
-               cache->used = old_val;
+               WRITE_ONCE(cache->used, old_val);
                cache->reserved -= num_bytes;
                cache->reclaim_mark = 0;
                space_info->bytes_reserved -= num_bytes;
@@ -3690,7 +3690,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
                spin_unlock(&space_info->lock);
        } else {
                old_val -= num_bytes;
-               cache->used = old_val;
+               WRITE_ONCE(cache->used, old_val);
                cache->pinned += num_bytes;
                btrfs_space_info_update_bytes_pinned(space_info, num_bytes);
                space_info->bytes_used -= num_bytes;
fs/btrfs/discard.c +0 -0
index e815d165cccc..71c57b571d50 100644

fs/btrfs/discard.c void btrfs_discard_queue_work(struct btrfs_discard_ctl *discard_ctl, · line 366

Quoted 5 lines
fs/btrfs/discard.c +1 -1
@@ -363,7 +363,7 @@ void btrfs_discard_queue_work(struct btrfs_discard_ctl *discard_ctl,
        if (!block_group || !btrfs_test_opt(block_group->fs_info, DISCARD_ASYNC))
                return;
 
-       if (block_group->used == 0)
+       if (READ_ONCE(block_group->used) == 0)

There are at least 3 more places in discard.c where we access ->used without being under the protection of the block group's spinlock. So let's fix this for all places and not just a single one...

Also, this is quite ugly to spread READ_ONCE/WRITE_ONCE all over the place. What we typically do in btrfs is to add helpers that hide them, see block-rsv.h for example.

Also, I don't think we need READ_ONCE/WRITE_ONCE.
We could use data_race(), though I think that could be subject to load/store tearing, or just take the lock.
So adding a helper like this to block-group.h:

static inline u64 btrfs_block_group_used(struct btrfs_block_group *bg)

{
   u64 ret;
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
   spin_lock(&bg->lock);
   ret = bg->used;
   spin_unlock(&bg->lock);
    return ret;
}

I understand that using lock to protect block_group->used in discard.c file is feasible. In addition, I looked at the code of block-group.c and found that locks have been added in some places where block_group->used are used. , it seems that it is not appropriate to call btrfs_block_group_used again to obtain (because it will cause deadlock). I would like to ask other similar places where block_group->used needs to call btrfs_block_group_used function in addition to the four places in discard.c?

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

Would memory barriers be sufficient here? Taking a lock just for reading one member seems excessive...

Do I need to perform performance testing to ensure the impact if I lock it?

Quoted 76 lines
Quoted 10 lines
Quoted 2 lines

Do you think there's heavy contention on this lock?

This is not about contention. Spin lock should just never be used this way. Or any lock actually. The critical section only contains a single fetch operation which does not justify using a lock.
Hence the only guarantee such lock usage provides are the implicit memory barriers (from which maybe only one of them is really needed depending on the context where this helper is going to be used).

Quoted 3 lines

Simply put, the lock is degraded into a memory barrier this way. So why not just use the barriers in the first place if only ordering guarantees are required? It only makes sense.

As said earlier, it's a lot easier to reason about lock() and unlock() calls rather than spreading memory barriers in the write and read sides.
Historically he had several mistakes with barriers, they're simply not as straightforward to reason as locks.

Plus spreading the barriers in the read and write sides makes the code not so easy to read, not to mention in case of any new sites updating the member, we'll have to not forget adding a barrier.

It's just easier to reason and maintain.

Quoted 5 lines
Quoted 3 lines

Usually I prefer to go the simplest way, and using locks is a lot more straightforward and easier to understand than memory barriers.

How so? Locks provide the same memory barriers implicitly.

I'm not saying they don't.
I'm talking about easier code to read and maintain.

Quoted 11 lines
Quoted 3 lines

Unless it's clear there's an observable performance penalty, keeping it simple is better.

Exactly. Here is where our opinions differ. For me 'simple' means without useless locking.
I mean especially with locks they should only be used when absolutely necessary. In a sense of 'use the right tool for the job'. There are more lightweight tools possible in this context. Locks provide additional guarantees which are not needed here.

Quoted 3 lines

On the other hand I understand that using a lock is a 'better safe than sorry' approach which should also work. Just keep in mind that spinlock may sleep on RT.

It's not about a better safe than sorry, but easier to read, reason and maintain.

Quoted 22 lines
Quoted 3 lines

data_race() may be ok here, at least for one of the unprotected accesses it's fine, but would have to analyse the other 3 cases.

That's exactly my thoughts. Maybe not even the barriers are needed after all. This needs to be checked on a per-case basis.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

And then use btrfs_bock_group_used() everywhere in discard.c where we aren't holding a block group's lock.

Thanks.

Quoted 7 lines
fs/btrfs/discard.c +0 -1
                add_to_discard_unused_list(discard_ctl, block_group);
        else
                add_to_discard_list(discard_ctl, block_group);
--

2.34.1

#8 lore raw
Quoted 114 lines · haoran zheng
Quoted 106 lines · Filipe Manana
Quoted 104 lines · Daniel Vacek
Quoted 102 lines · Filipe Manana
Quoted 100 lines · Daniel Vacek
Quoted 98 lines · Filipe Manana
Quoted 73 lines · Hao-ran Zheng

A data race may occur when the function btrfs_discard_queue_work() and the function btrfs_update_block_group() is executed concurrently. Specifically, when the btrfs_update_block_group() function is executed to lines cache->used = old_val;, and btrfs_discard_queue_work() is accessing if(block_group->used == 0) will appear with data race, which may cause block_group to be placed unexpectedly in discard_list or discard_unused_list. The specific function call stack is as follows:

============DATA_RACE============

 btrfs_discard_queue_work+0x245/0x500 [btrfs]
 __btrfs_add_free_space+0x3066/0x32f0 [btrfs]
 btrfs_add_free_space+0x19a/0x200 [btrfs]
 unpin_extent_range+0x847/0x2120 [btrfs]
 btrfs_finish_extent_commit+0x9a3/0x1840 [btrfs]
 btrfs_commit_transaction+0x5f65/0xc0f0 [btrfs]
 transaction_kthread+0x764/0xc20 [btrfs]
 kthread+0x292/0x330
 ret_from_fork+0x4d/0x80
 ret_from_fork_asm+0x1a/0x30
============OTHER_INFO============
 btrfs_update_block_group+0xa9d/0x2430 [btrfs]
 __btrfs_free_extent+0x4f69/0x9920 [btrfs]
 __btrfs_run_delayed_refs+0x290e/0xd7d0 [btrfs]
 btrfs_run_delayed_refs+0x317/0x770 [btrfs]
 flush_space+0x388/0x1440 [btrfs]
 btrfs_preempt_reclaim_metadata_space+0xd65/0x14c0 [btrfs]
 process_scheduled_works+0x716/0xf10
 worker_thread+0xb6a/0x1190
 kthread+0x292/0x330
 ret_from_fork+0x4d/0x80
 ret_from_fork_asm+0x1a/0x30
=================================

Although the block_group->used was checked again in the use of the peek_discard_list function, considering that block_group->used is a 64-bit variable, we still think that the data race here is an unexpected behavior. It is recommended to add READ_ONCE and WRITE_ONCE to read and write.

---
 fs/btrfs/block-group.c | 4 ++--
 fs/btrfs/discard.c     | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/block-group.c +2 -2
index c0a8f7d92acc..c681b97f6835 100644
@@ -3678,7 +3678,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
        old_val = cache->used;
        if (alloc) {
                old_val += num_bytes;
-               cache->used = old_val;
+               WRITE_ONCE(cache->used, old_val);
                cache->reserved -= num_bytes;
                cache->reclaim_mark = 0;
                space_info->bytes_reserved -= num_bytes;
@@ -3690,7 +3690,7 @@ int btrfs_update_block_group(struct btrfs_trans_handle *trans,
                spin_unlock(&space_info->lock);
        } else {
                old_val -= num_bytes;
-               cache->used = old_val;
+               WRITE_ONCE(cache->used, old_val);
                cache->pinned += num_bytes;
                btrfs_space_info_update_bytes_pinned(space_info, num_bytes);
                space_info->bytes_used -= num_bytes;
fs/btrfs/discard.c +0 -0
index e815d165cccc..71c57b571d50 100644

fs/btrfs/discard.c void btrfs_discard_queue_work(struct btrfs_discard_ctl *discard_ctl, · line 366

Quoted 5 lines
fs/btrfs/discard.c +1 -1
@@ -363,7 +363,7 @@ void btrfs_discard_queue_work(struct btrfs_discard_ctl *discard_ctl,
        if (!block_group || !btrfs_test_opt(block_group->fs_info, DISCARD_ASYNC))
                return;
 
-       if (block_group->used == 0)
+       if (READ_ONCE(block_group->used) == 0)

There are at least 3 more places in discard.c where we access ->used without being under the protection of the block group's spinlock. So let's fix this for all places and not just a single one...

Also, this is quite ugly to spread READ_ONCE/WRITE_ONCE all over the place. What we typically do in btrfs is to add helpers that hide them, see block-rsv.h for example.

Also, I don't think we need READ_ONCE/WRITE_ONCE.
We could use data_race(), though I think that could be subject to load/store tearing, or just take the lock.
So adding a helper like this to block-group.h:

static inline u64 btrfs_block_group_used(struct btrfs_block_group *bg)

{
   u64 ret;
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
   spin_lock(&bg->lock);
   ret = bg->used;
   spin_unlock(&bg->lock);
    return ret;
}
Quoted 7 lines

I understand that using lock to protect block_group->used in discard.c file is feasible. In addition, I looked at the code of block-group.c and found that locks have been added in some places where block_group->used are used. , it seems that it is not appropriate to call btrfs_block_group_used again to obtain (because it will cause deadlock).

In places where we are reading it while holding the block group's spinlock, there's nothing that needs to be changed.

Also we can't call it btrfs_block_group_used() since there's already an accessor function for struct btrfs_block_group_item with that name (defined through macros at accessors.h).

I took a closer look at the cases in discard.c, and it's safe to use data_race() instead, even if load/store tearing happens or we get stale values, nothing harmful happens, only a few things can be done later or unnecessarily without side effects - like adding a non-empty block group to the list of unused block groups, which is fine since the we won't delete it the cleaner kthread in case it's not empty, or delay the discard.

Either give it another name like btrfs_get_block_group_used() or directly use data_race() in discard.c - I don't like much either of them, the first because there's the similar named accessor for block group items, the second due to spreading data_race(), but I don't see any more elegant alternative.

So a sample patch:

fs/btrfs/block-group.h +9 -0
index 36937eeab9b8..ba635a69a23d 100644
@@ -278,6 +278,15 @@ static inline bool btrfs_is_block_group_used(const struct btrfs_block_group *bg)
 	return (bg->used > 0 || bg->reserved > 0 || bg->pinned > 0);
 }
 
+/*
+ * To be used in contexts where we aren't holding the block group's spinlock and
+ * it's safe to do so.
+ */
+static inline u64 btrfs_get_block_group_used(const struct btrfs_block_group *bg)
+{
+	return data_race(bg->used);
+}
+
 static inline bool btrfs_is_block_group_data_only(const struct btrfs_block_group *block_group)
 {
 	/*
fs/btrfs/discard.c +4 -4
index e815d165cccc..eef1c0ab0a97 100644
@@ -247,7 +247,7 @@ static struct btrfs_block_group *peek_discard_list(
 
 	if (block_group && now >= block_group->discard_eligible_time) {
 		if (block_group->discard_index == BTRFS_DISCARD_INDEX_UNUSED &&
-		    block_group->used != 0) {
+		    btrfs_get_block_group_used(block_group) != 0) {
 			if (btrfs_is_block_group_data_only(block_group)) {
 				__add_to_discard_list(discard_ctl, block_group);
 			} else {
@@ -363,7 +363,7 @@ void btrfs_discard_queue_work(struct btrfs_discard_ctl *discard_ctl,
 	if (!block_group || !btrfs_test_opt(block_group->fs_info, DISCARD_ASYNC))
 		return;
 
-	if (block_group->used == 0)
+	if (btrfs_get_block_group_used(block_group) == 0)
 		add_to_discard_unused_list(discard_ctl, block_group);
 	else
 		add_to_discard_list(discard_ctl, block_group);
@@ -460,7 +460,7 @@ static void btrfs_finish_discard_pass(struct btrfs_discard_ctl *discard_ctl,
 {
 	remove_from_discard_list(discard_ctl, block_group);
 
-	if (block_group->used == 0) {
+	if (btrfs_get_block_group_used(block_group) == 0) {
 		if (btrfs_is_free_space_trimmed(block_group))
 			btrfs_mark_bg_unused(block_group);
 		else
@@ -719,7 +719,7 @@ static void btrfs_discard_purge_list(struct btrfs_discard_ctl *discard_ctl)
 					 discard_list) {
 			list_del_init(&block_group->discard_list);
 			spin_unlock(&discard_ctl->lock);
-			if (block_group->used == 0)
+			if (btrfs_get_block_group_used(block_group) == 0)
 				btrfs_mark_bg_unused(block_group);
 			spin_lock(&discard_ctl->lock);
 			btrfs_put_block_group(block_group);
Quoted 4 lines

I would like to ask other similar places
where block_group->used needs to call btrfs_block_group_used function in addition to the four places in discard.c?

I noticed one for block-group.c but the solution isn't the same because there's an actual bug there, see:

https://lore.kernel.org/linux-btrfs/[email protected]/

Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Would memory barriers be sufficient here? Taking a lock just for reading one member seems excessive...

Do I need to perform performance testing to ensure the impact if I lock it?

No, specially with data_race().

Thanks.

Quoted 77 lines
Quoted 77 lines
Quoted 11 lines
Quoted 3 lines

Do you think there's heavy contention on this lock?

This is not about contention. Spin lock should just never be used this way. Or any lock actually. The critical section only contains a single fetch operation which does not justify using a lock.
Hence the only guarantee such lock usage provides are the implicit memory barriers (from which maybe only one of them is really needed depending on the context where this helper is going to be used).

Quoted 3 lines

Simply put, the lock is degraded into a memory barrier this way. So why not just use the barriers in the first place if only ordering guarantees are required? It only makes sense.

As said earlier, it's a lot easier to reason about lock() and unlock() calls rather than spreading memory barriers in the write and read sides.
Historically he had several mistakes with barriers, they're simply not as straightforward to reason as locks.

Plus spreading the barriers in the read and write sides makes the code not so easy to read, not to mention in case of any new sites updating the member, we'll have to not forget adding a barrier.

It's just easier to reason and maintain.

Quoted 5 lines
Quoted 3 lines

Usually I prefer to go the simplest way, and using locks is a lot more straightforward and easier to understand than memory barriers.

How so? Locks provide the same memory barriers implicitly.

I'm not saying they don't.
I'm talking about easier code to read and maintain.

Quoted 11 lines
Quoted 3 lines

Unless it's clear there's an observable performance penalty, keeping it simple is better.

Exactly. Here is where our opinions differ. For me 'simple' means without useless locking.
I mean especially with locks they should only be used when absolutely necessary. In a sense of 'use the right tool for the job'. There are more lightweight tools possible in this context. Locks provide additional guarantees which are not needed here.

Quoted 3 lines

On the other hand I understand that using a lock is a 'better safe than sorry' approach which should also work. Just keep in mind that spinlock may sleep on RT.

It's not about a better safe than sorry, but easier to read, reason and maintain.

Quoted 22 lines
Quoted 3 lines

data_race() may be ok here, at least for one of the unprotected accesses it's fine, but would have to analyse the other 3 cases.

That's exactly my thoughts. Maybe not even the barriers are needed after all. This needs to be checked on a per-case basis.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

And then use btrfs_bock_group_used() everywhere in discard.c where we aren't holding a block group's lock.

Thanks.

Quoted 7 lines
fs/btrfs/discard.c +0 -1
                add_to_discard_unused_list(discard_ctl, block_group);
        else
                add_to_discard_list(discard_ctl, block_group);
--

2.34.1

#9 lore raw
Quoted 33 lines · Filipe Manana
Quoted 11 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

static inline u64 btrfs_block_group_used(struct btrfs_block_group *bg)

{
   u64 ret;

   spin_lock(&bg->lock);
   ret = bg->used;
   spin_unlock(&bg->lock);

    return ret;
}
Quoted 7 lines

I understand that using lock to protect block_group->used in discard.c file is feasible. In addition, I looked at the code of block-group.c and found that locks have been added in some places where block_group->used are used. , it seems that it is not appropriate to call btrfs_block_group_used again to obtain (because it will cause deadlock).

In places where we are reading it while holding the block group's spinlock, there's nothing that needs to be changed.

Also we can't call it btrfs_block_group_used() since there's already an accessor function for struct btrfs_block_group_item with that name (defined through macros at accessors.h).

I took a closer look at the cases in discard.c, and it's safe to use data_race() instead, even if load/store tearing happens or we get stale values, nothing harmful happens, only a few things can be done later or unnecessarily without side effects - like adding a non-empty block group to the list of unused block groups, which is fine since the we won't delete it the cleaner kthread in case it's not empty, or delay the discard.

Quoted 5 lines

Either give it another name like btrfs_get_block_group_used() or directly use data_race() in discard.c - I don't like much either of them, the first because there's the similar named accessor for block group items, the second due to spreading data_race(), but I don't see any more elegant alternative.

Agreed, this looks like a resonable "solution". It's not really a bug, we can live with data_race annotation, namely when it filters out known cases so the code analysis tools may find the real problems.

Quoted 1 line

So a sample patch:

[...]

Looks good to me.