Skip to content

Commit a929904

Browse files
committed
btrfs: add unlikely annotations to branches leading to transaction abort
The unlikely() annotation is a static prediction hint that compiler may use to reorder code out of hot path. We use it elsewhere (namely tree-checker.c) for error branches that almost never happen. Transaction abort is one such error, the btrfs_abort_transaction() inlines code to check the state and print a warning, this ought to be out of the hot path. The most common pattern is when transaction abort is called after checking a return value and the control flow leads to a quick return. In other cases it may not be necessary to add unlikely() e.g. when the function returns anyway or the control flow is not changed noticeably. Reviewed-by: Filipe Manana <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent cc53bd2 commit a929904

20 files changed

+231
-232
lines changed

fs/btrfs/block-group.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3248,7 +3248,7 @@ static int cache_save_setup(struct btrfs_block_group *block_group,
32483248
*/
32493249
BTRFS_I(inode)->generation = 0;
32503250
ret = btrfs_update_inode(trans, BTRFS_I(inode));
3251-
if (ret) {
3251+
if (unlikely(ret)) {
32523252
/*
32533253
* So theoretically we could recover from this, simply set the
32543254
* super cache generation to 0 so we know to invalidate the
@@ -3995,7 +3995,7 @@ static struct btrfs_block_group *do_chunk_alloc(struct btrfs_trans_handle *trans
39953995
struct btrfs_space_info *sys_space_info;
39963996

39973997
sys_space_info = btrfs_find_space_info(trans->fs_info, sys_flags);
3998-
if (!sys_space_info) {
3998+
if (unlikely(!sys_space_info)) {
39993999
ret = -EINVAL;
40004000
btrfs_abort_transaction(trans, ret);
40014001
goto out;
@@ -4009,17 +4009,17 @@ static struct btrfs_block_group *do_chunk_alloc(struct btrfs_trans_handle *trans
40094009
}
40104010

40114011
ret = btrfs_chunk_alloc_add_chunk_item(trans, sys_bg);
4012-
if (ret) {
4012+
if (unlikely(ret)) {
40134013
btrfs_abort_transaction(trans, ret);
40144014
goto out;
40154015
}
40164016

40174017
ret = btrfs_chunk_alloc_add_chunk_item(trans, bg);
4018-
if (ret) {
4018+
if (unlikely(ret)) {
40194019
btrfs_abort_transaction(trans, ret);
40204020
goto out;
40214021
}
4022-
} else if (ret) {
4022+
} else if (unlikely(ret)) {
40234023
btrfs_abort_transaction(trans, ret);
40244024
goto out;
40254025
}

fs/btrfs/ctree.c

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,11 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans,
293293

294294
if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
295295
ret = btrfs_inc_ref(trans, root, cow, 1);
296-
if (ret)
296+
if (unlikely(ret))
297297
btrfs_abort_transaction(trans, ret);
298298
} else {
299299
ret = btrfs_inc_ref(trans, root, cow, 0);
300-
if (ret)
300+
if (unlikely(ret))
301301
btrfs_abort_transaction(trans, ret);
302302
}
303303
if (ret) {
@@ -536,14 +536,14 @@ int btrfs_force_cow_block(struct btrfs_trans_handle *trans,
536536
write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
537537

538538
ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
539-
if (ret) {
539+
if (unlikely(ret)) {
540540
btrfs_abort_transaction(trans, ret);
541541
goto error_unlock_cow;
542542
}
543543

544544
if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
545545
ret = btrfs_reloc_cow_block(trans, root, buf, cow);
546-
if (ret) {
546+
if (unlikely(ret)) {
547547
btrfs_abort_transaction(trans, ret);
548548
goto error_unlock_cow;
549549
}
@@ -556,7 +556,7 @@ int btrfs_force_cow_block(struct btrfs_trans_handle *trans,
556556
parent_start = buf->start;
557557

558558
ret = btrfs_tree_mod_log_insert_root(root->node, cow, true);
559-
if (ret < 0) {
559+
if (unlikely(ret < 0)) {
560560
btrfs_abort_transaction(trans, ret);
561561
goto error_unlock_cow;
562562
}
@@ -567,15 +567,15 @@ int btrfs_force_cow_block(struct btrfs_trans_handle *trans,
567567
parent_start, last_ref);
568568
free_extent_buffer(buf);
569569
add_root_to_dirty_list(root);
570-
if (ret < 0) {
570+
if (unlikely(ret < 0)) {
571571
btrfs_abort_transaction(trans, ret);
572572
goto error_unlock_cow;
573573
}
574574
} else {
575575
WARN_ON(trans->transid != btrfs_header_generation(parent));
576576
ret = btrfs_tree_mod_log_insert_key(parent, parent_slot,
577577
BTRFS_MOD_LOG_KEY_REPLACE);
578-
if (ret) {
578+
if (unlikely(ret)) {
579579
btrfs_abort_transaction(trans, ret);
580580
goto error_unlock_cow;
581581
}
@@ -586,14 +586,14 @@ int btrfs_force_cow_block(struct btrfs_trans_handle *trans,
586586
btrfs_mark_buffer_dirty(trans, parent);
587587
if (last_ref) {
588588
ret = btrfs_tree_mod_log_free_eb(buf);
589-
if (ret) {
589+
if (unlikely(ret)) {
590590
btrfs_abort_transaction(trans, ret);
591591
goto error_unlock_cow;
592592
}
593593
}
594594
ret = btrfs_free_tree_block(trans, btrfs_root_id(root), buf,
595595
parent_start, last_ref);
596-
if (ret < 0) {
596+
if (unlikely(ret < 0)) {
597597
btrfs_abort_transaction(trans, ret);
598598
goto error_unlock_cow;
599599
}
@@ -922,7 +922,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
922922
}
923923

924924
ret = btrfs_tree_mod_log_insert_root(root->node, child, true);
925-
if (ret < 0) {
925+
if (unlikely(ret < 0)) {
926926
btrfs_tree_unlock(child);
927927
free_extent_buffer(child);
928928
btrfs_abort_transaction(trans, ret);
@@ -944,7 +944,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
944944
ret = btrfs_free_tree_block(trans, btrfs_root_id(root), mid, 0, 1);
945945
/* once for the root ptr */
946946
free_extent_buffer_stale(mid);
947-
if (ret < 0) {
947+
if (unlikely(ret < 0)) {
948948
btrfs_abort_transaction(trans, ret);
949949
goto out;
950950
}
@@ -1019,7 +1019,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
10191019
right, 0, 1);
10201020
free_extent_buffer_stale(right);
10211021
right = NULL;
1022-
if (ret < 0) {
1022+
if (unlikely(ret < 0)) {
10231023
btrfs_abort_transaction(trans, ret);
10241024
goto out;
10251025
}
@@ -1028,7 +1028,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
10281028
btrfs_node_key(right, &right_key, 0);
10291029
ret = btrfs_tree_mod_log_insert_key(parent, pslot + 1,
10301030
BTRFS_MOD_LOG_KEY_REPLACE);
1031-
if (ret < 0) {
1031+
if (unlikely(ret < 0)) {
10321032
btrfs_abort_transaction(trans, ret);
10331033
goto out;
10341034
}
@@ -1080,7 +1080,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
10801080
ret = btrfs_free_tree_block(trans, btrfs_root_id(root), mid, 0, 1);
10811081
free_extent_buffer_stale(mid);
10821082
mid = NULL;
1083-
if (ret < 0) {
1083+
if (unlikely(ret < 0)) {
10841084
btrfs_abort_transaction(trans, ret);
10851085
goto out;
10861086
}
@@ -1090,7 +1090,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
10901090
btrfs_node_key(mid, &mid_key, 0);
10911091
ret = btrfs_tree_mod_log_insert_key(parent, pslot,
10921092
BTRFS_MOD_LOG_KEY_REPLACE);
1093-
if (ret < 0) {
1093+
if (unlikely(ret < 0)) {
10941094
btrfs_abort_transaction(trans, ret);
10951095
goto out;
10961096
}
@@ -1195,7 +1195,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
11951195
btrfs_node_key(mid, &disk_key, 0);
11961196
ret = btrfs_tree_mod_log_insert_key(parent, pslot,
11971197
BTRFS_MOD_LOG_KEY_REPLACE);
1198-
if (ret < 0) {
1198+
if (unlikely(ret < 0)) {
11991199
btrfs_tree_unlock(left);
12001200
free_extent_buffer(left);
12011201
btrfs_abort_transaction(trans, ret);
@@ -1255,7 +1255,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
12551255
btrfs_node_key(right, &disk_key, 0);
12561256
ret = btrfs_tree_mod_log_insert_key(parent, pslot + 1,
12571257
BTRFS_MOD_LOG_KEY_REPLACE);
1258-
if (ret < 0) {
1258+
if (unlikely(ret < 0)) {
12591259
btrfs_tree_unlock(right);
12601260
free_extent_buffer(right);
12611261
btrfs_abort_transaction(trans, ret);
@@ -2737,7 +2737,7 @@ static int push_node_left(struct btrfs_trans_handle *trans,
27372737
return ret;
27382738
}
27392739
ret = btrfs_tree_mod_log_eb_copy(dst, src, dst_nritems, 0, push_items);
2740-
if (ret) {
2740+
if (unlikely(ret)) {
27412741
btrfs_abort_transaction(trans, ret);
27422742
return ret;
27432743
}
@@ -2822,7 +2822,7 @@ static int balance_node_right(struct btrfs_trans_handle *trans,
28222822

28232823
ret = btrfs_tree_mod_log_eb_copy(dst, src, 0, src_nritems - push_items,
28242824
push_items);
2825-
if (ret) {
2825+
if (unlikely(ret)) {
28262826
btrfs_abort_transaction(trans, ret);
28272827
return ret;
28282828
}
@@ -2892,7 +2892,7 @@ static noinline int insert_new_root(struct btrfs_trans_handle *trans,
28922892

28932893
btrfs_clear_buffer_dirty(trans, c);
28942894
ret2 = btrfs_free_tree_block(trans, btrfs_root_id(root), c, 0, 1);
2895-
if (ret2 < 0)
2895+
if (unlikely(ret2 < 0))
28962896
btrfs_abort_transaction(trans, ret2);
28972897
btrfs_tree_unlock(c);
28982898
free_extent_buffer(c);
@@ -2937,7 +2937,7 @@ static int insert_ptr(struct btrfs_trans_handle *trans,
29372937
if (level) {
29382938
ret = btrfs_tree_mod_log_insert_move(lower, slot + 1,
29392939
slot, nritems - slot);
2940-
if (ret < 0) {
2940+
if (unlikely(ret < 0)) {
29412941
btrfs_abort_transaction(trans, ret);
29422942
return ret;
29432943
}
@@ -2950,7 +2950,7 @@ static int insert_ptr(struct btrfs_trans_handle *trans,
29502950
if (level) {
29512951
ret = btrfs_tree_mod_log_insert_key(lower, slot,
29522952
BTRFS_MOD_LOG_KEY_ADD);
2953-
if (ret < 0) {
2953+
if (unlikely(ret < 0)) {
29542954
btrfs_abort_transaction(trans, ret);
29552955
return ret;
29562956
}
@@ -3026,7 +3026,7 @@ static noinline int split_node(struct btrfs_trans_handle *trans,
30263026
ASSERT(btrfs_header_level(c) == level);
30273027

30283028
ret = btrfs_tree_mod_log_eb_copy(split, c, 0, mid, c_nritems - mid);
3029-
if (ret) {
3029+
if (unlikely(ret)) {
30303030
btrfs_tree_unlock(split);
30313031
free_extent_buffer(split);
30323032
btrfs_abort_transaction(trans, ret);
@@ -4383,7 +4383,7 @@ int btrfs_del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
43834383
if (level) {
43844384
ret = btrfs_tree_mod_log_insert_move(parent, slot,
43854385
slot + 1, nritems - slot - 1);
4386-
if (ret < 0) {
4386+
if (unlikely(ret < 0)) {
43874387
btrfs_abort_transaction(trans, ret);
43884388
return ret;
43894389
}
@@ -4396,7 +4396,7 @@ int btrfs_del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
43964396
} else if (level) {
43974397
ret = btrfs_tree_mod_log_insert_key(parent, slot,
43984398
BTRFS_MOD_LOG_KEY_REMOVE);
4399-
if (ret < 0) {
4399+
if (unlikely(ret < 0)) {
44004400
btrfs_abort_transaction(trans, ret);
44014401
return ret;
44024402
}

fs/btrfs/delayed-inode.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ static int __btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
10381038
* transaction, because we could leave the inode with the
10391039
* improper counts behind.
10401040
*/
1041-
if (ret != -ENOENT)
1041+
if (unlikely(ret != -ENOENT))
10421042
btrfs_abort_transaction(trans, ret);
10431043
goto out;
10441044
}
@@ -1066,7 +1066,7 @@ static int __btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
10661066

10671067
btrfs_release_path(path);
10681068
ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1069-
if (ret < 0) {
1069+
if (unlikely(ret < 0)) {
10701070
btrfs_abort_transaction(trans, ret);
10711071
goto err_out;
10721072
}
@@ -1175,7 +1175,7 @@ static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans, int nr)
11751175
while (curr_node && (!count || nr--)) {
11761176
ret = __btrfs_commit_inode_delayed_items(trans, path,
11771177
curr_node);
1178-
if (ret) {
1178+
if (unlikely(ret)) {
11791179
btrfs_abort_transaction(trans, ret);
11801180
break;
11811181
}

0 commit comments

Comments
 (0)