[1/2] ext4: fix race when setting the bitmap corrupted flag again

Message ID 1545134401-104523-1-git-send-email-yi.zhang@huawei.com
State New
Headers show
Series
  • [1/2] ext4: fix race when setting the bitmap corrupted flag again
Related show

Commit Message

zhangyi (F) Dec. 18, 2018, noon
Commit 9af0b3d12577 "ext4: fix race when setting the bitmap corrupted
flag" want to fix race between setting inode/block bitmap corrupted
flag and reducing free group inodes/clusters counter to prevent
multiple frees. But ext4_test_and_set_bit() will invoke
__test_and_set_bit() which is non-atomic, so the race is still there.
Fix this by invoke test_and_set_bit() instead.

Fixes: 9af0b3d12577 ("ext4: fix race when setting the bitmap corrupted flag")
Signed-off-by: zhangyi (F) <yi.zhang@huawei.com>
---
 fs/ext4/ext4.h  | 6 ++++++
 fs/ext4/super.c | 6 ++----
 2 files changed, 8 insertions(+), 4 deletions(-)

Comments

Wang Shilong Dec. 18, 2018, 2:25 p.m. | #1
Hi,

在 2018/12/18 下午7:57,“zhangyi (F)”<yi.zhang@huawei.com> 写入:

    Commit 9af0b3d12577 "ext4: fix race when setting the bitmap corrupted
    flag" want to fix race between setting inode/block bitmap corrupted
    flag and reducing free group inodes/clusters counter to prevent
    multiple frees. But ext4_test_and_set_bit() will invoke
    __test_and_set_bit() which is non-atomic, so the race is still there.
    Fix this by invoke test_and_set_bit() instead.
   
    Fixes: 9af0b3d12577 ("ext4: fix race when setting the bitmap corrupted flag")
    Signed-off-by: zhangyi (F) <yi.zhang@huawei.com>

Thanks for fixing this!, I was not aware of __test_and_set_bit() is not non-atomic operation before.

Reviewed-by: Wang Shilong <wshilong@ddn.com>

Btw for a stable process question, commit 9af0b3d12577 had CC tag to Stable kernel, should we
add it again here or with 'Fixes' tag, it will be included automatically?

Thanks,
Shilong
    ---
     fs/ext4/ext4.h  | 6 ++++++
     fs/ext4/super.c | 6 ++----
     2 files changed, 8 insertions(+), 4 deletions(-)
    
    diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
    index 3f89d0a..755ba14 100644
    --- a/fs/ext4/ext4.h
    +++ b/fs/ext4/ext4.h
    @@ -2888,6 +2888,12 @@ struct ext4_group_info {
     	(test_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, &((grp)->bb_state)))
     #define EXT4_MB_GRP_IBITMAP_CORRUPT(grp)	\
     	(test_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &((grp)->bb_state)))
    +#define EXT4_MB_GRP_TEST_AND_SET_BBITMAP_CORRUPT(grp)	\
    +	(test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, \
    +			  &((grp)->bb_state)))
    +#define EXT4_MB_GRP_TEST_AND_SET_IBITMAP_CORRUPT(grp)	\
    +	(test_and_set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, \
    +			  &((grp)->bb_state)))
     
     #define EXT4_MB_GRP_WAS_TRIMMED(grp)	\
     	(test_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT, &((grp)->bb_state)))
    diff --git a/fs/ext4/super.c b/fs/ext4/super.c
    index 53ff6c2..5b83765 100644
    --- a/fs/ext4/super.c
    +++ b/fs/ext4/super.c
    @@ -798,16 +798,14 @@ void ext4_mark_group_bitmap_corrupted(struct super_block *sb,
     	int ret;
     
     	if (flags & EXT4_GROUP_INFO_BBITMAP_CORRUPT) {
    -		ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT,
    -					    &grp->bb_state);
    +		ret = EXT4_MB_GRP_TEST_AND_SET_BBITMAP_CORRUPT(grp);
     		if (!ret)
     			percpu_counter_sub(&sbi->s_freeclusters_counter,
     					   grp->bb_free);
     	}
     
     	if (flags & EXT4_GROUP_INFO_IBITMAP_CORRUPT) {
    -		ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT,
    -					    &grp->bb_state);
    +		ret = EXT4_MB_GRP_TEST_AND_SET_IBITMAP_CORRUPT(grp);
     		if (!ret && gdp) {
     			int count;
     
    -- 
    2.7.4
zhangyi (F) Dec. 19, 2018, 3:35 a.m. | #2
On 2018/12/18 22:25, Wang Shilong Wrote:
> Hi,
> 
> 在 2018/12/18 下午7:57,“zhangyi (F)”<yi.zhang@huawei.com> 写入:
> 
>     Commit 9af0b3d12577 "ext4: fix race when setting the bitmap corrupted
>     flag" want to fix race between setting inode/block bitmap corrupted
>     flag and reducing free group inodes/clusters counter to prevent
>     multiple frees. But ext4_test_and_set_bit() will invoke
>     __test_and_set_bit() which is non-atomic, so the race is still there.
>     Fix this by invoke test_and_set_bit() instead.
>    
>     Fixes: 9af0b3d12577 ("ext4: fix race when setting the bitmap corrupted flag")
>     Signed-off-by: zhangyi (F) <yi.zhang@huawei.com>
> 
> Thanks for fixing this!, I was not aware of __test_and_set_bit() is not non-atomic operation before.
> 
> Reviewed-by: Wang Shilong <wshilong@ddn.com>
> 
> Btw for a stable process question, commit 9af0b3d12577 had CC tag to Stable kernel, should we
> add it again here or with 'Fixes' tag, it will be included automatically?
> 
Thank you for reminding me of the CC stable tag, I missed this tag and I'm not sure it
will be added automatically or not. Anyway, I will add this tag in the next iteration.

Thanks,
Yi.

Patch

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 3f89d0a..755ba14 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -2888,6 +2888,12 @@  struct ext4_group_info {
 	(test_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, &((grp)->bb_state)))
 #define EXT4_MB_GRP_IBITMAP_CORRUPT(grp)	\
 	(test_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &((grp)->bb_state)))
+#define EXT4_MB_GRP_TEST_AND_SET_BBITMAP_CORRUPT(grp)	\
+	(test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, \
+			  &((grp)->bb_state)))
+#define EXT4_MB_GRP_TEST_AND_SET_IBITMAP_CORRUPT(grp)	\
+	(test_and_set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, \
+			  &((grp)->bb_state)))
 
 #define EXT4_MB_GRP_WAS_TRIMMED(grp)	\
 	(test_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT, &((grp)->bb_state)))
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 53ff6c2..5b83765 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -798,16 +798,14 @@  void ext4_mark_group_bitmap_corrupted(struct super_block *sb,
 	int ret;
 
 	if (flags & EXT4_GROUP_INFO_BBITMAP_CORRUPT) {
-		ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT,
-					    &grp->bb_state);
+		ret = EXT4_MB_GRP_TEST_AND_SET_BBITMAP_CORRUPT(grp);
 		if (!ret)
 			percpu_counter_sub(&sbi->s_freeclusters_counter,
 					   grp->bb_free);
 	}
 
 	if (flags & EXT4_GROUP_INFO_IBITMAP_CORRUPT) {
-		ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT,
-					    &grp->bb_state);
+		ret = EXT4_MB_GRP_TEST_AND_SET_IBITMAP_CORRUPT(grp);
 		if (!ret && gdp) {
 			int count;