btrfs: tree-checker: cache accessor return value in CHECK_FE_ALIGNED()

We are calling an accessor for a file extent item multiple times in the
CHECK_FE_ALIGNED() macro, even when we don't find a corruption (once in
the if statement's expression and then once again in the expression for
the return value). This adds extra runtime overhead (which is critical
since the tree checker runs against every extent buffer when it's read
or before persisting it) and increases the module's size.

So cache the accessor's return value in a variable and use it, reducing
runtime, object size and making the source code shorter too. Also avoid
repeating twice the IS_ALIGNED() computation.

Before:

  $ size fs/btrfs/btrfs.ko
     text	   data	    bss	    dec	    hex	filename
  2073340	 217928	  15624	2306892	 23334c	fs/btrfs/btrfs.ko

After:

  $ size fs/btrfs/btrfs.ko
     text	   data	    bss	    dec	    hex	filename
  2073076	 217928	  15624	2306628	 233244	fs/btrfs/btrfs.ko

Also running the following fsstress test and capturing the runtime of
check_extent_data_item() (the only caller of CHECK_FE_ALIGNED()) in
nanoseconds (using bpftrace), showed the following runtime improvements:

Test:

  mkfs.btrfs -f /dev/nullb0
  mount /dev/nullb0 /mnt
  fsstress -w -p 8 -n 5000 -s 12345 -d /mnt
  umount /mnt

Before:

  Count: 2033671
  Range:  0.000 - 1336060.000; Mean: 679.642; Median: 656.000; Stddev: 2031.323
  Percentiles:  90th: 850.000; 95th: 909.000; 99th: 1272.000
       0.000 -       6.647:      11 |
       6.647 -      28.241:      36 |
      28.241 -     110.807:     176 |
     110.807 -     426.512:   38012 #
     426.512 -    1633.663: 1983372 #####################################################
    1633.663 -    6249.399:    8315 |
    6249.399 -   23898.422:    3243 |
   23898.422 -   91382.340:     153 |
   91382.340 -  349418.114:      36 |
  349418.114 - 1336060.000:      11 |

After:

  Count: 2092797
  Range:  0.000 - 1677284.000; Mean: 627.650; Median: 617.000; Stddev: 1848.010
  Percentiles:  90th: 809.000; 95th: 859.000; 99th: 1209.000
       0.000 -       6.823:       18 |
       6.823 -      29.602:       82 |
      29.602 -     118.702:      416 |
     118.702 -     467.232:   515774 #################
     467.232 -    1830.549:  1565599 #####################################################
    1830.549 -    7163.339:     5297 |
    7163.339 -   28023.237:     4186 |
   28023.237 -  109619.427:      166 |
  109619.427 -  428793.471:       25 |
  428793.471 - 1677284.000:        4 |

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Filipe Manana 2026-09-11 11:56:29 +01:00 committed by David Sterba
parent 4ef2c403d2
commit 7a881b1139

View File

@ -108,13 +108,14 @@ static void file_extent_err(const struct extent_buffer *eb, int slot,
*/
#define CHECK_FE_ALIGNED(leaf, slot, fi, name, alignment) \
({ \
if (unlikely(!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), \
(alignment)))) \
const u64 val = btrfs_file_extent_##name((leaf), (fi)); \
const bool not_aligned = !IS_ALIGNED(val, (alignment)); \
\
if (unlikely(not_aligned)) \
file_extent_err((leaf), (slot), \
"invalid %s for file extent, have %llu, should be aligned to %u", \
(#name), btrfs_file_extent_##name((leaf), (fi)), \
(alignment)); \
(!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))); \
(#name), val, (alignment)); \
not_aligned; \
})
static u64 file_extent_end(struct extent_buffer *leaf,