Skip to contentSkip to conversation

btrfs: use min_t() for mismatched type comparison

PATCH In review Arnd Bergmann 4 participants 5 messages Feb 25, 2025 – Feb 26, 2025 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 btrfs: use min_t() for mismatched type comparison

From: Arnd Bergmann <[email protected]>

loff_t is a signed type, so using min() to compare it against a u64 causes a compiler warning:

fs/btrfs/extent_io.c:2497:13: error: call to '__compiletime_assert_728' declared with 'error' attribute: min(folio_pos(folio) + folio_size(folio) - 1, end) signedness error
2497 | cur_end = min(folio_pos(folio) + folio_size(folio) - 1, end);
| ^

Use min_t() instead.

Fixes aba063bf9336 ("btrfs: prepare extent_io.c for future larger folio support")
---
 fs/btrfs/extent_io.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/btrfs/extent_io.c +2 -2
index f0a1da40d641..7dc996e7e249 100644
@@ -2485,7 +2485,7 @@ void extent_write_locked_range(struct inode *inode, const struct folio *locked_f
 		 * code is just in case, but shouldn't actually be run.
 		 */
 		if (IS_ERR(folio)) {
-			cur_end = min(round_down(cur, PAGE_SIZE) + PAGE_SIZE - 1, end);
+			cur_end = min_t(u64, round_down(cur, PAGE_SIZE) + PAGE_SIZE - 1, end);
 			cur_len = cur_end + 1 - cur;
 			btrfs_mark_ordered_io_finished(BTRFS_I(inode), NULL,
 						       cur, cur_len, false);
@@ -2494,7 +2494,7 @@ void extent_write_locked_range(struct inode *inode, const struct folio *locked_f
 			continue;
 		}
 
-		cur_end = min(folio_pos(folio) + folio_size(folio) - 1, end);
+		cur_end = min_t(u64, folio_pos(folio) + folio_size(folio) - 1, end);
 		cur_len = cur_end + 1 - cur;
 
 		ASSERT(folio_test_locked(folio));
Signature · 1 line
2.39.5
#1 lore raw

PATCH 2/2 btrfs: replace 64-bit division with a shift

From: Arnd Bergmann <[email protected]>

folio_size() is not a compile-time constant, so in some configurations, the DIV_ROUND_UP() turns into a 64-bit division that is not allowed on 32-bit architectures.

x86_64-linux-ld: fs/btrfs/extent_io.o: in function `writepage_delalloc':
extent_io.c:(.text+0x2b6e): undefined reference to `__udivdi3'

This is probably not the correct solution, but it should illustrate the issue. A trivial fix would be DIV64_U64_ROUND_UP(), which of course is very expensive. Maybe there should be a DIV_ROUND_UP_FOLIO macro?

Fixes aba063bf9336 ("btrfs: prepare extent_io.c for future larger folio support")
---
 fs/btrfs/extent_io.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/btrfs/extent_io.c +1 -2
index 7dc996e7e249..e4ba4fa3f48c 100644
@@ -1505,8 +1505,7 @@ static noinline_for_stack int writepage_delalloc(struct btrfs_inode *inode,
 	 * delalloc_end is already one less than the total length, so
 	 * we don't subtract one from folio_size().
 	 */
-	delalloc_to_write +=
-		DIV_ROUND_UP(delalloc_end + 1 - page_start, folio_size(folio));
+	delalloc_to_write += (delalloc_end + 1 - page_start + folio_size(folio) - 1) >> folio_order(folio);
 
 	/*
 	 * If all ranges are submitted asynchronously, we just need to account
Signature · 1 line
2.39.5
#2 lore raw
In reply to Arnd Bergmann
Quoted 9 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

loff_t is a signed type, so using min() to compare it against a u64 causes a compiler warning:

fs/btrfs/extent_io.c:2497:13: error: call to '__compiletime_assert_728' declared with 'error' attribute: min(folio_pos(folio) + folio_size(folio) - 1, end) signedness error
2497 | cur_end = min(folio_pos(folio) + folio_size(folio) - 1, end);
| ^

Quoted 6 lines

Use min_t() instead.

Fixes aba063bf9336 ("btrfs: prepare extent_io.c for future larger folio support")

Thanks a lot, those fixes will be merged into the next version.

For now the series is only for test purposes as there is a backlog of subpage block size related patches pending.

Thanks,
Qu

Quoted 26 lines
---
  fs/btrfs/extent_io.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
fs/btrfs/extent_io.c +2 -2
index f0a1da40d641..7dc996e7e249 100644
@@ -2485,7 +2485,7 @@ void extent_write_locked_range(struct inode *inode, const struct folio *locked_f
  		 * code is just in case, but shouldn't actually be run.
  		 */
  		if (IS_ERR(folio)) {
-			cur_end = min(round_down(cur, PAGE_SIZE) + PAGE_SIZE - 1, end);
+			cur_end = min_t(u64, round_down(cur, PAGE_SIZE) + PAGE_SIZE - 1, end);
  			cur_len = cur_end + 1 - cur;
  			btrfs_mark_ordered_io_finished(BTRFS_I(inode), NULL,
  						       cur, cur_len, false);
@@ -2494,7 +2494,7 @@ void extent_write_locked_range(struct inode *inode, const struct folio *locked_f
  			continue;
  		}
 
-		cur_end = min(folio_pos(folio) + folio_size(folio) - 1, end);
+		cur_end = min_t(u64, folio_pos(folio) + folio_size(folio) - 1, end);
  		cur_len = cur_end + 1 - cur;
 
  		ASSERT(folio_test_locked(folio));
#3 lore raw
Quoted 17 lines · Qu Wenruo
Quoted 9 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

loff_t is a signed type, so using min() to compare it against a u64 causes a compiler warning:

fs/btrfs/extent_io.c:2497:13: error: call to '__compiletime_assert_728' declared with 'error' attribute: min(folio_pos(folio) + folio_size(folio) - 1, end) signedness error
2497 | cur_end = min(folio_pos(folio) + folio_size(folio) - 1, end);
| ^

Quoted 6 lines

Use min_t() instead.

Fixes aba063bf9336 ("btrfs: prepare extent_io.c for future larger folio support")
Quoted 4 lines

Thanks a lot, those fixes will be merged into the next version.

For now the series is only for test purposes as there is a backlog of subpage block size related patches pending.

Ok, thanks! Please double-check that the calculation in patch 2/2 is actually correct though, as I wasn't entirely sure about that part.

Arnd

#4 lore raw
In reply to Arnd Bergmann
Quoted 7 lines · On Tue, 25 Feb 2025 20:44:10 +0100 Arnd Bergmann <[email protected]> wrote:

From: Arnd Bergmann <[email protected]>

loff_t is a signed type, so using min() to compare it against a u64 causes a compiler warning:

fs/btrfs/extent_io.c:2497:13: error: call to '__compiletime_assert_728' declared with 'error' attribute: min(folio_pos(folio) + folio_size(folio) - 1, end) signedness error
2497 | cur_end = min(folio_pos(folio) + folio_size(folio) - 1, end);

Isn't the actual problem that folio_pos() has the wrong return type. I can't remember what loff_t is supposed to be for, but here you want something that reduces to 'unsigned long'.

Quoted 1 line

Use min_t() instead.

If a signed variable is known to contain a non-negative value then min_unsigned() is better.
In particular it will never discard upper bits.

Enough min_t() cause bugs (usually due to high bits being discarded when the type of the destination (eg u8) is used) that is is tempting to start a 'duck shoot' season against them.

Quoted 14 lines
Fixes aba063bf9336 ("btrfs: prepare extent_io.c for future larger folio support")
---
 fs/btrfs/extent_io.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/btrfs/extent_io.c +0 -0
index f0a1da40d641..7dc996e7e249 100644

fs/btrfs/extent_io.c void extent_write_locked_range(struct inode *inode, const struct folio *locked_f · line 2488

Quoted 5 lines
fs/btrfs/extent_io.c +1 -1
@@ -2485,7 +2485,7 @@ void extent_write_locked_range(struct inode *inode, const struct folio *locked_f
 		 * code is just in case, but shouldn't actually be run.
 		 */
 		if (IS_ERR(folio)) {
-			cur_end = min(round_down(cur, PAGE_SIZE) + PAGE_SIZE - 1, end);
+			cur_end = min_t(u64, round_down(cur, PAGE_SIZE) + PAGE_SIZE - 1, end);

That one is fine and doesn't need changing.

fs/btrfs/extent_io.c void extent_write_locked_range(struct inode *inode, const struct folio *locked_f · line 2497

Quoted 9 lines
fs/btrfs/extent_io.c +1 -1
 			cur_len = cur_end + 1 - cur;
 			btrfs_mark_ordered_io_finished(BTRFS_I(inode), NULL,
 						       cur, cur_len, false);
@@ -2494,7 +2494,7 @@ void extent_write_locked_range(struct inode *inode, const struct folio *locked_f
 			continue;
 		}
 
-		cur_end = min(folio_pos(folio) + folio_size(folio) - 1, end);
+		cur_end = min_t(u64, folio_pos(folio) + folio_size(folio) - 1, end);

A subtle alternative to min_unsigned() is to change the 1 to 1ull.

David

Quoted 3 lines
fs/btrfs/extent_io.c +0 -0
 		cur_len = cur_end + 1 - cur;
 
 		ASSERT(folio_test_locked(folio));