Skip to contentSkip to conversation

Btrfs: fix bitwise vs logical condition

PATCH In review Dan Carpenter 2 participants 4 messages Jan 20, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Btrfs: fix bitwise vs logical condition

The intent here was to do a logical && instead of a bitwise &. The original condition tests whether they have the some of same bits set. I have fixed that and rewritten it to be more clear.

---
Warning: This is a static analysis bug and I'm not very familiar with the code. Please review carefully.

fs/btrfs/volumes.c +2 -2
index 0b4e2af..0c54027 100644
@@ -2376,8 +2376,8 @@ static int should_balance_chunk(struct btrfs_root *root,
 	u64 chunk_type = btrfs_chunk_type(leaf, chunk);
 
 	/* type filter */
-	if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
-	      (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
+	if (!(chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) ||
+	    !(bctl->flags & BTRFS_BALANCE_TYPE_MASK)) {
 		return 0;
 	}
 
#1 lore raw
Quoted 14 lines · Dan Carpenter

The intent here was to do a logical && instead of a bitwise &. The original condition tests whether they have the some of same bits set. I have fixed that and rewritten it to be more clear.

---
Warning: This is a static analysis bug and I'm not very familiar with the code. Please review carefully.

fs/btrfs/volumes.c +0 -0
index 0b4e2af..0c54027 100644

fs/btrfs/volumes.c static int should_balance_chunk(struct btrfs_root *root, · line 2383

Quoted 10 lines
fs/btrfs/volumes.c +2 -2
@@ -2376,8 +2376,8 @@ static int should_balance_chunk(struct btrfs_root *root,
 	u64 chunk_type = btrfs_chunk_type(leaf, chunk);
 
 	/* type filter */
-	if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
-	      (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
+	if (!(chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) ||
+	    !(bctl->flags & BTRFS_BALANCE_TYPE_MASK)) {
 		return 0;
 	}
 

The intent here is definitely bitwise &. The original code tests whether at least one of the bits set in (chunk_type & MASK) is set in (bctl_flags & MASK), and if not returns 0. IOW,

u64 mask = chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK;

if (((bctl->flags & BTRFS_BALANCE_TYPE_MASK) & mask) = 0)

	return 0;

Your patch does something completely different. However, I think we can strengthen that check (and make it more idiomatic). Can you try the below diff with your checker ?

Thanks,

Ilya

fs/btrfs/volumes.c +2 -3
index 7ffdb15..9c6a074 100644
@@ -2375,12 +2375,11 @@ static int should_balance_chunk(struct btrfs_root *root,
 	struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
 	struct btrfs_balance_args *bargs = NULL;
 	u64 chunk_type = btrfs_chunk_type(leaf, chunk);
+	u64 mask = chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK;
 
 	/* type filter */
-	if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
-	      (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
+	if (((bctl->flags & BTRFS_BALANCE_TYPE_MASK) & mask) != mask)
 		return 0;
-	}
 
 	if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
 		bargs = &bctl->data;
#2 lore raw

fs/btrfs/volumes.c static int should_balance_chunk(struct btrfs_root *root, · line 2381

Quoted 12 lines · Ilya Dryomov
fs/btrfs/volumes.c +2 -2
@@ -2375,12 +2375,11 @@ static int should_balance_chunk(struct btrfs_root *root,
 	struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
 	struct btrfs_balance_args *bargs = NULL;
 	u64 chunk_type = btrfs_chunk_type(leaf, chunk);
+	u64 mask = chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK;
 
 	/* type filter */
-	if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
-	      (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
+	if (((bctl->flags & BTRFS_BALANCE_TYPE_MASK) & mask) != mask)

I don't know if it matters, but semantically this is not equivalent to the original. If mask has no flags set then this will pass. This says that every flag in mask but has to be set in ->flags but in the original code, only one needed to be.

The original code was equivalent to:

	if (!(chunk_type & bctl->flags & BTRFS_BLOCK_GROUP_TYPE_MASK &
	      BTRFS_BALANCE_TYPE_MASK)) {...

It's weird that we have BTRFS_BLOCK_GROUP_TYPE_MASK and BTRFS_BALANCE_TYPE_MASK which are the same except that the bitfields have been renamed. Can't we just reuse the first definition?

But really, if this isn't a bug, then I don't care. The original is fine, or whatever you choose.

regards,
dan carpenter

#3 lore raw
Quoted 9 lines · Dan Carpenter
Quoted 8 lines · Ilya Dryomov
fs/btrfs/volumes.c +1 -0
@@ -2375,12 +2375,11 @@ static int should_balance_chunk(struct btrfs_root *root,
 	struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
 	struct btrfs_balance_args *bargs = NULL;
 	u64 chunk_type = btrfs_chunk_type(leaf, chunk);
+	u64 mask = chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK;
 
Quoted 9 lines

fs/btrfs/volumes.c static int should_balance_chunk(struct btrfs_root *root, · line 2381

Quoted 4 lines
fs/btrfs/volumes.c +1 -2
 	/* type filter */
-	if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
-	      (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
+	if (((bctl->flags & BTRFS_BALANCE_TYPE_MASK) & mask) != mask)

I don't know if it matters, but semantically this is not equivalent to the original. If mask has no flags set then this will pass. This says that every flag in mask but has to be set in ->flags but in the original code, only one needed to be.

Yeah, that's why I said "we can strengthen that check".

Quoted 11 lines

The original code was equivalent to:

	if (!(chunk_type & bctl->flags & BTRFS_BLOCK_GROUP_TYPE_MASK &
	      BTRFS_BALANCE_TYPE_MASK)) {...

It's weird that we have BTRFS_BLOCK_GROUP_TYPE_MASK and BTRFS_BALANCE_TYPE_MASK which are the same except that the bitfields have been renamed. Can't we just reuse the first definition?

But really, if this isn't a bug, then I don't care. The original is fine, or whatever you choose.

This is not a bug.

Thanks,

Ilya