Skip to content

Commit ad3b3be

Browse files
fdmananakdave
authored andcommitted
btrfs: send: add unlikely to all unexpected overflow checks
There are several checks for unexpected overflows of buffers and path lengths that makes us fail the send operation with an error if for some highly unexpected reason they happen. So add the unlikely tag to those checks to hint the compiler to generate better code, while also making it more explicit in the source that it's highly unexpected. With gcc 14.2.0-19 from Debian on x86_64, I also got a small reduction the text size of the btrfs module. Before: $ size fs/btrfs/btrfs.ko text data bss dec hex filename 1936917 162723 15592 2115232 2046a0 fs/btrfs/btrfs.ko After: $ size fs/btrfs/btrfs.ko text data bss dec hex filename 1936789 162723 15592 2115104 204620 fs/btrfs/btrfs.ko 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>
1 parent d8e5214 commit ad3b3be

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

fs/btrfs/send.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,20 +1134,20 @@ static int iterate_dir_item(struct btrfs_root *root, struct btrfs_path *path,
11341134
btrfs_dir_item_key_to_cpu(eb, di, &di_key);
11351135

11361136
if (btrfs_dir_ftype(eb, di) == BTRFS_FT_XATTR) {
1137-
if (name_len > XATTR_NAME_MAX) {
1137+
if (unlikely(name_len > XATTR_NAME_MAX)) {
11381138
ret = -ENAMETOOLONG;
11391139
goto out;
11401140
}
1141-
if (name_len + data_len >
1142-
BTRFS_MAX_XATTR_SIZE(root->fs_info)) {
1141+
if (unlikely(name_len + data_len >
1142+
BTRFS_MAX_XATTR_SIZE(root->fs_info))) {
11431143
ret = -E2BIG;
11441144
goto out;
11451145
}
11461146
} else {
11471147
/*
11481148
* Path too long
11491149
*/
1150-
if (name_len + data_len > PATH_MAX) {
1150+
if (unlikely(name_len + data_len > PATH_MAX)) {
11511151
ret = -ENAMETOOLONG;
11521152
goto out;
11531153
}
@@ -5129,7 +5129,7 @@ static int process_verity(struct send_ctx *sctx)
51295129
if (ret < 0)
51305130
goto iput;
51315131

5132-
if (ret > FS_VERITY_MAX_DESCRIPTOR_SIZE) {
5132+
if (unlikely(ret > FS_VERITY_MAX_DESCRIPTOR_SIZE)) {
51335133
ret = -EMSGSIZE;
51345134
goto iput;
51355135
}
@@ -5173,14 +5173,14 @@ static int put_data_header(struct send_ctx *sctx, u32 len)
51735173
* Since v2, the data attribute header doesn't include a length,
51745174
* it is implicitly to the end of the command.
51755175
*/
5176-
if (sctx->send_max_size - sctx->send_size < sizeof(__le16) + len)
5176+
if (unlikely(sctx->send_max_size - sctx->send_size < sizeof(__le16) + len))
51775177
return -EOVERFLOW;
51785178
put_unaligned_le16(BTRFS_SEND_A_DATA, sctx->send_buf + sctx->send_size);
51795179
sctx->send_size += sizeof(__le16);
51805180
} else {
51815181
struct btrfs_tlv_header *hdr;
51825182

5183-
if (sctx->send_max_size - sctx->send_size < sizeof(*hdr) + len)
5183+
if (unlikely(sctx->send_max_size - sctx->send_size < sizeof(*hdr) + len))
51845184
return -EOVERFLOW;
51855185
hdr = (struct btrfs_tlv_header *)(sctx->send_buf + sctx->send_size);
51865186
put_unaligned_le16(BTRFS_SEND_A_DATA, &hdr->tlv_type);
@@ -5580,8 +5580,8 @@ static int send_encoded_extent(struct send_ctx *sctx, struct btrfs_path *path,
55805580
* between the beginning of the command and the file data.
55815581
*/
55825582
data_offset = PAGE_ALIGN(sctx->send_size);
5583-
if (data_offset > sctx->send_max_size ||
5584-
sctx->send_max_size - data_offset < disk_num_bytes) {
5583+
if (unlikely(data_offset > sctx->send_max_size ||
5584+
sctx->send_max_size - data_offset < disk_num_bytes)) {
55855585
ret = -EOVERFLOW;
55865586
goto out;
55875587
}

0 commit comments

Comments
 (0)