Skip to contentSkip to conversation

Page and folio count helper cleanups

PATCH New David Sterba 1 participant 3 messages Feb 25, 2025 Btrfs file system

lore
Share & tools
lore

David Sterba

author maintainer
#0 lore raw

cover Page and folio count helper cleanups

Adding the __pure attribute to num_extent_folios() allows some optimizations that can reduce stack space usage.

David Sterba (2):
btrfs: add __pure attribute to eb page and folio counters
btrfs: use num_extent_folios() in for loop bounds

 fs/btrfs/disk-io.c   |  3 +--
 fs/btrfs/extent_io.c | 48 +++++++++++++++++---------------------------
 fs/btrfs/extent_io.h |  7 +++++--
 3 files changed, 24 insertions(+), 34 deletions(-)
Signature · 1 line
2.47.1
author maintainer
#1 lore raw

PATCH 1/2 btrfs: add __pure attribute to eb page and folio counters

The functions qualify for the pure attribute as they always return the same value for the same argument (in the given scope). This allows to optimize the calls and cache the value.

---
 fs/btrfs/extent_io.h | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
fs/btrfs/extent_io.h +5 -2
index 6c5328bfabc2..6440ba7cf35e 100644
@@ -276,7 +276,8 @@ void btrfs_readahead_tree_block(struct btrfs_fs_info *fs_info,
 				u64 bytenr, u64 owner_root, u64 gen, int level);
 void btrfs_readahead_node_child(struct extent_buffer *node, int slot);
 
-static inline int num_extent_pages(const struct extent_buffer *eb)
+/* Note: this can be used in for loops without caching the value in a variable. */
+static inline int __pure num_extent_pages(const struct extent_buffer *eb)
 {
 	/*
 	 * For sectorsize == PAGE_SIZE case, since nodesize is always aligned to
@@ -294,8 +295,10 @@ static inline int num_extent_pages(const struct extent_buffer *eb)
  * As we can have either one large folio covering the whole eb
  * (either nodesize <= PAGE_SIZE, or high order folio), or multiple
  * single-paged folios.
+ *
+ * Note: this can be used in for loops without caching the value in a variable.
  */
-static inline int num_extent_folios(const struct extent_buffer *eb)
+static inline int __pure num_extent_folios(const struct extent_buffer *eb)
 {
 	if (folio_order(eb->folios[0]))
 		return 1;
Signature · 1 line
2.47.1
author maintainer
#2 lore raw

PATCH 2/2 btrfs: use num_extent_folios() in for loop bounds

In reply to David Sterba

As the helper num_extent_folios() is now __pure, we can use it in for loop without storing its value in a variable explicitly, the compiler will do this for us.

The effects on btrfs.ko is -200 bytes and there are stack space savings too:

btrfs_clone_extent_buffer                               -8 (32 -> 24)
btrfs_clear_buffer_dirty                                -8 (48 -> 40)
clear_extent_buffer_uptodate                            -8 (40 -> 32)
set_extent_buffer_dirty                                 -8 (32 -> 24)
write_one_eb                                            -8 (88 -> 80)
set_extent_buffer_uptodate                              -8 (40 -> 32)

read_extent_buffer_pages_nowait -16 (64 -> 48)
find_extent_buffer -8 (32 -> 24)

---
 fs/btrfs/disk-io.c   |  3 +--
 fs/btrfs/extent_io.c | 48 +++++++++++++++++---------------------------
 2 files changed, 19 insertions(+), 32 deletions(-)
fs/btrfs/disk-io.c +1 -2
index a799216aa264..91c7da1ced7d 100644
@@ -182,13 +182,12 @@ static int btrfs_repair_eb_io_failure(const struct extent_buffer *eb,
 				      int mirror_num)
 {
 	struct btrfs_fs_info *fs_info = eb->fs_info;
-	int num_folios = num_extent_folios(eb);
 	int ret = 0;
 
 	if (sb_rdonly(fs_info->sb))
 		return -EROFS;
 
-	for (int i = 0; i < num_folios; i++) {
+	for (int i = 0; i < num_extent_folios(eb); i++) {
 		struct folio *folio = eb->folios[i];
 		u64 start = max_t(u64, eb->start, folio_pos(folio));
 		u64 end = min_t(u64, eb->start + eb->len,
fs/btrfs/extent_io.c +18 -30

189 lines not on this page · raw message

Signature · 1 line
2.47.1