Skip to content

Commit 026cc40

Browse files
committed
btrfs: accessors: inline eb bounds check and factor out the error report
There's a check in each set/get helper if the requested range is within extent buffer bounds, and if it's not then report it. This was in an ASSERT statement so with CONFIG_BTRFS_ASSERT this crashes right away, on other configs this is only reported but reading out of the bounds is done anyway. There are currently no known reports of this particular condition failing. There are some drawbacks though. The behaviour dependence on the assertions being compiled in or not and a less visible effect of inlining report_setget_bounds() into each helper. As the bounds check is expected to succeed almost always it's ok to inline it but make the report a function and move it out of the helper completely (__cold puts it to a different section). This also skips reading/writing the requested range in case it fails. This improves stack usage significantly: btrfs_get_16 -48 (80 -> 32) btrfs_get_32 -48 (80 -> 32) btrfs_get_64 -48 (80 -> 32) btrfs_get_8 -48 (72 -> 24) btrfs_set_16 -56 (88 -> 32) btrfs_set_32 -56 (88 -> 32) btrfs_set_64 -56 (88 -> 32) btrfs_set_8 -48 (80 -> 32) NEW (48): report_setget_bounds 48 LOST/NEW DELTA: +48 PRE/POST DELTA: -360 Same as .ko size: text data bss dec hex filename 1456079 115665 16088 1587832 183a78 pre/btrfs.ko 1454951 115665 16088 1586704 183610 post/btrfs.ko DELTA: -1128 Reviewed-by: Boris Burkov <boris@bur.io> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 54db7b9 commit 026cc40

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

fs/btrfs/accessors.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,15 @@
99
#include "fs.h"
1010
#include "accessors.h"
1111

12-
static bool check_setget_bounds(const struct extent_buffer *eb,
13-
const void *ptr, unsigned off, int size)
12+
static void __cold report_setget_bounds(const struct extent_buffer *eb,
13+
const void *ptr, unsigned off, int size)
1414
{
15-
const unsigned long member_offset = (unsigned long)ptr + off;
15+
unsigned long member_offset = (unsigned long)ptr + off;
1616

17-
if (unlikely(member_offset + size > eb->len)) {
18-
btrfs_warn(eb->fs_info,
19-
"bad eb member %s: ptr 0x%lx start %llu member offset %lu size %d",
20-
(member_offset > eb->len ? "start" : "end"),
21-
(unsigned long)ptr, eb->start, member_offset, size);
22-
return false;
23-
}
24-
25-
return true;
17+
btrfs_warn(eb->fs_info,
18+
"bad eb member %s: ptr 0x%lx start %llu member offset %lu size %d",
19+
(member_offset > eb->len ? "start" : "end"),
20+
(unsigned long)ptr, eb->start, member_offset, size);
2621
}
2722

2823
/*
@@ -56,7 +51,10 @@ u##bits btrfs_get_##bits(const struct extent_buffer *eb, \
5651
const int part = eb->folio_size - oil; \
5752
u8 lebytes[sizeof(u##bits)]; \
5853
\
59-
ASSERT(check_setget_bounds(eb, ptr, off, sizeof(u##bits))); \
54+
if (unlikely(member_offset + sizeof(u##bits) > eb->len)) { \
55+
report_setget_bounds(eb, ptr, off, sizeof(u##bits)); \
56+
return 0; \
57+
} \
6058
if (INLINE_EXTENT_BUFFER_PAGES == 1 || likely(sizeof(u##bits) <= part)) \
6159
return get_unaligned_le##bits(kaddr + oil); \
6260
\
@@ -76,7 +74,10 @@ void btrfs_set_##bits(const struct extent_buffer *eb, void *ptr, \
7674
const int part = eb->folio_size - oil; \
7775
u8 lebytes[sizeof(u##bits)]; \
7876
\
79-
ASSERT(check_setget_bounds(eb, ptr, off, sizeof(u##bits))); \
77+
if (unlikely(member_offset + sizeof(u##bits) > eb->len)) { \
78+
report_setget_bounds(eb, ptr, off, sizeof(u##bits)); \
79+
return; \
80+
} \
8081
if (INLINE_EXTENT_BUFFER_PAGES == 1 || \
8182
likely(sizeof(u##bits) <= part)) { \
8283
put_unaligned_le##bits(val, kaddr + oil); \

0 commit comments

Comments
 (0)