]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
btrfs: add missing unlikely to if branches leading to a DEBUG_WARN()
authorFilipe Manana <fdmanana@suse.com>
Thu, 23 Apr 2026 11:31:46 +0000 (12:31 +0100)
committerDavid Sterba <dsterba@suse.com>
Mon, 8 Jun 2026 13:53:30 +0000 (15:53 +0200)
If statement branches that lead to a DEBUG_WARN() are unexpected to happen
and in most places we surround their expressions with the unlikely tag,
however a few places are missing. Add the unlikely tag to those missing
places to make it explicit to a reader that it's not expected and to hint
the compiler to generate better code.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/block-group.c
fs/btrfs/disk-io.c
fs/btrfs/extent-tree.c
fs/btrfs/free-space-tree.c
fs/btrfs/inode.c
fs/btrfs/qgroup.c
fs/btrfs/volumes.c

index 01560307014383e1f2c704205e1134efa31d5092..a0cb0db68c9a4f236483f72c88fb33e3215457ef 100644 (file)
@@ -4117,7 +4117,7 @@ int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type)
        struct btrfs_space_info *space_info;
 
        space_info = btrfs_find_space_info(trans->fs_info, type);
-       if (!space_info) {
+       if (unlikely(!space_info)) {
                DEBUG_WARN();
                return -EINVAL;
        }
index 9d0b80600e9c380893bff74e83583269fda2184b..91ff0346f7222fd436c15578b687f79f7d4afd70 100644 (file)
@@ -4398,7 +4398,7 @@ void __cold close_ctree(struct btrfs_fs_info *fs_info)
        ASSERT(list_empty(&fs_info->delayed_iputs));
        set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags);
 
-       if (btrfs_check_quota_leak(fs_info)) {
+       if (unlikely(btrfs_check_quota_leak(fs_info))) {
                DEBUG_WARN("qgroup reserved space leaked");
                btrfs_err(fs_info, "qgroup reserved space leaked");
        }
index 420d52b097d990961a86671292675689a0725414..ecc1acb1e3408d30560f1df70a3f40e1e01f6788 100644 (file)
@@ -6655,7 +6655,7 @@ static int btrfs_trim_free_extents_throttle(struct btrfs_device *device,
                start = max(start, cur_start);
 
                /* Check if there are any CHUNK_* bits left */
-               if (start > device->total_bytes) {
+               if (unlikely(start > device->total_bytes)) {
                        DEBUG_WARN();
                        btrfs_warn(fs_info,
 "ignoring attempt to trim beyond device size: offset %llu length %llu device %s device size %llu",
index 472b3060e5ac32ef83eb282ee3aad8e835e28b10..5566226254125dd8f0b0c63d3744da6ac264d754 100644 (file)
@@ -109,7 +109,7 @@ struct btrfs_free_space_info *btrfs_search_free_space_info(
        ret = btrfs_search_slot(trans, root, &key, path, 0, cow);
        if (ret < 0)
                return ERR_PTR(ret);
-       if (ret != 0) {
+       if (unlikely(ret != 0)) {
                btrfs_warn(fs_info, "missing free space info for %llu",
                           block_group->start);
                DEBUG_WARN();
index e58cd85b847473075548a58c476c081c7629f20f..201010ced09df3916206d923136af66c76c33919 100644 (file)
@@ -755,7 +755,7 @@ static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
 {
        struct btrfs_fs_info *fs_info = inode->root->fs_info;
 
-       if (!btrfs_inode_can_compress(inode)) {
+       if (unlikely(!btrfs_inode_can_compress(inode))) {
                DEBUG_WARN("BTRFS: unexpected compression for ino %llu", btrfs_ino(inode));
                return 0;
        }
index 6838faceb6d5c09725ff76d6e983fb7f921fb60d..384622bd78699788ad3be790f0bc878445d97c78 100644 (file)
@@ -1858,9 +1858,9 @@ int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
         * Thus its reserved space should all be zero, no matter if qgroup
         * is consistent or the mode.
         */
-       if (qgroup->rsv.values[BTRFS_QGROUP_RSV_DATA] ||
-           qgroup->rsv.values[BTRFS_QGROUP_RSV_META_PREALLOC] ||
-           qgroup->rsv.values[BTRFS_QGROUP_RSV_META_PERTRANS]) {
+       if (unlikely(qgroup->rsv.values[BTRFS_QGROUP_RSV_DATA] ||
+                    qgroup->rsv.values[BTRFS_QGROUP_RSV_META_PREALLOC] ||
+                    qgroup->rsv.values[BTRFS_QGROUP_RSV_META_PERTRANS])) {
                DEBUG_WARN();
                btrfs_warn_rl(fs_info,
 "to be deleted qgroup %u/%llu has non-zero numbers, data %llu meta prealloc %llu meta pertrans %llu",
@@ -1879,8 +1879,8 @@ int btrfs_remove_qgroup(struct btrfs_trans_handle *trans, u64 qgroupid)
         */
        if (btrfs_qgroup_mode(fs_info) == BTRFS_QGROUP_MODE_FULL &&
            !(fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_INCONSISTENT)) {
-               if (qgroup->rfer || qgroup->excl ||
-                   qgroup->rfer_cmpr || qgroup->excl_cmpr) {
+               if (unlikely(qgroup->rfer || qgroup->excl ||
+                            qgroup->rfer_cmpr || qgroup->excl_cmpr)) {
                        DEBUG_WARN();
                        qgroup_mark_inconsistent(fs_info,
                                "to be deleted qgroup %u/%llu has non-zero numbers, rfer %llu rfer_cmpr %llu excl %llu excl_cmpr %llu",
@@ -4822,9 +4822,9 @@ int btrfs_qgroup_add_swapped_blocks(struct btrfs_root *subvol_root,
 
                entry = rb_entry(node, struct btrfs_qgroup_swapped_block, node);
 
-               if (entry->subvol_generation != block->subvol_generation ||
-                   entry->reloc_bytenr != block->reloc_bytenr ||
-                   entry->reloc_generation != block->reloc_generation) {
+               if (unlikely(entry->subvol_generation != block->subvol_generation ||
+                            entry->reloc_bytenr != block->reloc_bytenr ||
+                            entry->reloc_generation != block->reloc_generation)) {
                        /*
                         * Duplicated but mismatch entry found.  Shouldn't happen.
                         * Marking qgroup inconsistent should be enough for end
index 23c91fc3bb5c1821f3e8b69f0cebe2931c95092d..ab0c63b0fc59f4e43b07759b21c675d5c97a2d9f 100644 (file)
@@ -6081,7 +6081,7 @@ struct btrfs_block_group *btrfs_create_chunk(struct btrfs_trans_handle *trans,
 
        lockdep_assert_held(&info->chunk_mutex);
 
-       if (!alloc_profile_is_valid(type, 0)) {
+       if (unlikely(!alloc_profile_is_valid(type, 0))) {
                DEBUG_WARN("invalid alloc profile for type %llu", type);
                return ERR_PTR(-EINVAL);
        }
@@ -6092,7 +6092,7 @@ struct btrfs_block_group *btrfs_create_chunk(struct btrfs_trans_handle *trans,
                return ERR_PTR(-ENOSPC);
        }
 
-       if (!(type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
+       if (unlikely(!(type & BTRFS_BLOCK_GROUP_TYPE_MASK))) {
                btrfs_err(info, "invalid chunk type 0x%llx requested", type);
                DEBUG_WARN();
                return ERR_PTR(-EINVAL);
@@ -6262,7 +6262,7 @@ static noinline int init_first_rw_device(struct btrfs_trans_handle *trans)
 
        alloc_profile = btrfs_metadata_alloc_profile(fs_info);
        meta_space_info = btrfs_find_space_info(fs_info, alloc_profile);
-       if (!meta_space_info) {
+       if (unlikely(!meta_space_info)) {
                DEBUG_WARN();
                return -EINVAL;
        }
@@ -6272,7 +6272,7 @@ static noinline int init_first_rw_device(struct btrfs_trans_handle *trans)
 
        alloc_profile = btrfs_system_alloc_profile(fs_info);
        sys_space_info = btrfs_find_space_info(fs_info, alloc_profile);
-       if (!sys_space_info) {
+       if (unlikely(!sys_space_info)) {
                DEBUG_WARN();
                return -EINVAL;
        }