Skip to contentSkip to conversation

btrfs: make ASSERT no-op in release builds

PATCH In review Gladyshev Ilya 2 participants 4 messages Nov 2, 2025 – Nov 4, 2025 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 btrfs: make ASSERT no-op in release builds

The current definition of ASSERT(cond) as (void)(cond) is redundant, since these checks have no side effects and don't affect code logic.

However, some checks contain READ_ONCE or other compiler-unfriendly constructs. For example, ASSERT(list_empty) in btrfs_add_dealloc_inode was compiled to a redundant mov instruction due to this issue.

This patch defines ASSERT as BUILD_BUG_ON_INVALID for !CONFIG_BTRFS_ASSERT builds. It also marks full_page_sectors_uptodate as __maybe_unused to suppress "unneeded declaration" warning (it's needed in compile time)

---
Changes from v1:
- Annotate full_page_sectors_uptodate as __maybe_unused to avoid compiler warning

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

---
 fs/btrfs/messages.h | 2 +-
 fs/btrfs/raid56.c   | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/messages.h +1 -1
index 4416c165644f..f80fe40a2c2b 100644
@@ -168,7 +168,7 @@ do {										\
 #endif
 
 #else
-#define ASSERT(cond, args...)			(void)(cond)
+#define ASSERT(cond, args...)			BUILD_BUG_ON_INVALID(cond)
 #endif
 
 #ifdef CONFIG_BTRFS_DEBUG
fs/btrfs/raid56.c +2 -2
index 0135dceb7baa..302f20d8c335 100644
@@ -299,8 +299,8 @@ static int rbio_bucket(struct btrfs_raid_bio *rbio)
 	return hash_64(num >> 16, BTRFS_STRIPE_HASH_TABLE_BITS);
 }
 
-static bool full_page_sectors_uptodate(struct btrfs_raid_bio *rbio,
-				       unsigned int page_nr)
+static __maybe_unused bool full_page_sectors_uptodate(struct btrfs_raid_bio *rbio,
+						      unsigned int page_nr)
 {
 	const u32 sectorsize = rbio->bioc->fs_info->sectorsize;
 	const u32 sectors_per_page = PAGE_SIZE / sectorsize;

base-commit: e53642b87a4f4b03a8d7e5f8507fc3cd0c595ea6

Signature · 1 line
2.51.1.dirty
#1 lore raw
Quoted 2 lines · Gladyshev Ilya

The current definition of ASSERT(cond) as (void)(cond) is redundant, since these checks have no side effects and don't affect code logic.

Have you checked that none of the assert expressions really don't have side effects other than touching the memory?

Quoted 25 lines

However, some checks contain READ_ONCE or other compiler-unfriendly constructs. For example, ASSERT(list_empty) in btrfs_add_dealloc_inode was compiled to a redundant mov instruction due to this issue.

This patch defines ASSERT as BUILD_BUG_ON_INVALID for !CONFIG_BTRFS_ASSERT builds. It also marks full_page_sectors_uptodate as __maybe_unused to suppress "unneeded declaration" warning (it's needed in compile time)

---
Changes from v1:
- Annotate full_page_sectors_uptodate as __maybe_unused to avoid compiler warning

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

---
 fs/btrfs/messages.h | 2 +-
 fs/btrfs/raid56.c   | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/messages.h +0 -0
index 4416c165644f..f80fe40a2c2b 100644

fs/btrfs/messages.h do { \ · line 171

Quoted 5 lines
fs/btrfs/messages.h +1 -1
@@ -168,7 +168,7 @@ do {										\
 #endif
 
 #else
-#define ASSERT(cond, args...)			(void)(cond)
+#define ASSERT(cond, args...)			BUILD_BUG_ON_INVALID(cond)

I'd rather have the expression open coded rather than using BUILD_BUG_ON_INVALID, the name is confusing as it's not checking build time condtitons.

#2 lore raw
Quoted 6 lines · David Sterba
Quoted 2 lines · Gladyshev Ilya

The current definition of ASSERT(cond) as (void)(cond) is redundant, since these checks have no side effects and don't affect code logic.

Have you checked that none of the assert expressions really don't have side effects other than touching the memory?

Yes, but visually only. Most checks are plain C comparisons, and some call folio/btrfs/refcount _check/test_ functions where I didn't find side effects.

However, fs/btrfs/ has ~880 asserts, so if you know more robust verification methods, I'd be glad to try them.

Quoted 27 lines
Quoted 27 lines

However, some checks contain READ_ONCE or other compiler-unfriendly constructs. For example, ASSERT(list_empty) in btrfs_add_dealloc_inode was compiled to a redundant mov instruction due to this issue.

This patch defines ASSERT as BUILD_BUG_ON_INVALID for !CONFIG_BTRFS_ASSERT builds. It also marks full_page_sectors_uptodate as __maybe_unused to suppress "unneeded declaration" warning (it's needed in compile time)

---
Changes from v1:
- Annotate full_page_sectors_uptodate as __maybe_unused to avoid compiler warning

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

---
  fs/btrfs/messages.h | 2 +-
  fs/btrfs/raid56.c   | 4 ++--
  2 files changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/messages.h +0 -0
index 4416c165644f..f80fe40a2c2b 100644
@@ -168,7 +168,7 @@ do {										\
  #endif
 
Quoted 7 lines

fs/btrfs/messages.h do { \ · line 171

Quoted 3 lines
fs/btrfs/messages.h +1 -1
  #else
-#define ASSERT(cond, args...)			(void)(cond)
+#define ASSERT(cond, args...)			BUILD_BUG_ON_INVALID(cond)

I'd rather have the expression open coded rather than using BUILD_BUG_ON_INVALID, the name is confusing as it's not checking build time condtitons.

The name kinda indicates that it triggers on invalid conditions, not false ones, but I understand that it can be confusing. While we could use direct sizeof() magic here, I prefer reusing the same infrastructure as VM_BUG_ON(), VFS_*_ON() and others.

Maybe adding a comment about its semantics above ASSERT definition will help clarify the usage? But if you prefer the sizeof() approach, I can change it - it's not a big deal.

#3 lore raw
Quoted 8 lines · Gladyshev Ilya
Quoted 6 lines · David Sterba
Quoted 2 lines · Gladyshev Ilya

The current definition of ASSERT(cond) as (void)(cond) is redundant, since these checks have no side effects and don't affect code logic.

Have you checked that none of the assert expressions really don't have side effects other than touching the memory?

Quoted 6 lines

Yes, but visually only. Most checks are plain C comparisons, and some call folio/btrfs/refcount _check/test_ functions where I didn't find side effects.

However, fs/btrfs/ has ~880 asserts, so if you know more robust verification methods, I'd be glad to try them.

Good, thanks. I tried the same, with some random grep filters for possible function calls but nothing out of scope found so I guess this is sufficient.

Quoted 35 lines
Quoted 27 lines
Quoted 27 lines

However, some checks contain READ_ONCE or other compiler-unfriendly constructs. For example, ASSERT(list_empty) in btrfs_add_dealloc_inode was compiled to a redundant mov instruction due to this issue.

This patch defines ASSERT as BUILD_BUG_ON_INVALID for !CONFIG_BTRFS_ASSERT builds. It also marks full_page_sectors_uptodate as __maybe_unused to suppress "unneeded declaration" warning (it's needed in compile time)

---
Changes from v1:
- Annotate full_page_sectors_uptodate as __maybe_unused to avoid compiler warning

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

---
  fs/btrfs/messages.h | 2 +-
  fs/btrfs/raid56.c   | 4 ++--
  2 files changed, 3 insertions(+), 3 deletions(-)
fs/btrfs/messages.h +0 -0
index 4416c165644f..f80fe40a2c2b 100644
@@ -168,7 +168,7 @@ do {										\
  #endif
 
Quoted 7 lines

fs/btrfs/messages.h do { \ · line 171

Quoted 3 lines
fs/btrfs/messages.h +1 -1
  #else
-#define ASSERT(cond, args...)			(void)(cond)
+#define ASSERT(cond, args...)			BUILD_BUG_ON_INVALID(cond)

I'd rather have the expression open coded rather than using BUILD_BUG_ON_INVALID, the name is confusing as it's not checking build time condtitons.

Quoted 8 lines

The name kinda indicates that it triggers on invalid conditions, not false ones, but I understand that it can be confusing. While we could use direct sizeof() magic here, I prefer reusing the same infrastructure as VM_BUG_ON(), VFS_*_ON() and others.

Maybe adding a comment about its semantics above ASSERT definition will help clarify the usage? But if you prefer the sizeof() approach, I can change it - it's not a big deal.

A comment for ASSERT works too. The BUILD_BUG_ON_INVALID is indeed widely used so I don't expect any sudden change in semantics. As adding the comment is simple I'll do that, no need to resend the patch. Thanks.