Skip to contentSkip to conversation

Avoid 64-bit divisions that need __udivdi3() or __umoddi3()

PATCH In review Josef Schlehofer 6 participants 16 messages Nov 25 – Nov 26

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover Avoid 64-bit divisions that need __udivdi3() or __umoddi3()

U-Boot's private libgcc does not provide __udivdi3() and __umoddi3(). v3 of the mpc85xx LTO series links mpc85xx LTO builds with it and added both routines [1]. Christophe Leroy asked why the callers do not avoid '/' and '%' on 64-bit operands instead, and Tom Rini asked to update the common code [2].

Patches 1-4 avoid the divisions that make the mpc85xx LTO builds call __udivdi3() or __umoddi3(), in btrfs, squashfs, mmc_berase() and get_win_cnt() of the mpc8xxx PAMU code. They do not depend on each other. Patch 5 removes fs/btrfs/dev.c, which nothing uses and which holds another 64-bit modulo. Patch 6 converts the remaining divisions in btrfs. U-Boot never runs them, but builds without LTO still link the ones in the RAID5/6 code. My reply in that thread announced these conversions for v4 of the LTO series. They are sent on their own instead.

Patch 2 and my big-endian fix for squashfs [3] conflict in sqfs_read_nest(). I will rebase whichever of them goes in second.

Build tested with a buildman world build on a fork's CI, and sandbox test.py gives the same results as before. Run tested on qemu-ppce500 (btrfs) and qemu_arm (btrfs, squashfs, mmc erase), and on a Turris 1.1 with the mpc85xx LTO patches on top, which boots from an SD card and loads files from btrfs. The PAMU change is build tested only.

[1] https://lore.kernel.org/u-boot/[email protected]/
[2] https://lore.kernel.org/u-boot/20261119200924.GB3176972@bill-the-cat/
[3] https://lore.kernel.org/u-boot/[email protected]/

Josef Schlehofer (6):
fs: btrfs: Avoid a 64-bit modulo in __btrfs_devread() fs/squashfs: Avoid 64-bit divisions
mmc: Use div_u64() in mmc_berase()
powerpc: mpc8xxx: Avoid a 64-bit division in get_win_cnt()
fs: btrfs: Remove the unused dev.c
fs: btrfs: Avoid the remaining 64-bit divisions

 arch/powerpc/cpu/mpc8xxx/fsl_pamu.c |  2 +-
 drivers/mmc/mmc_write.c             |  2 +-
 fs/btrfs/Makefile                   |  2 +-
 fs/btrfs/compat.h                   |  8 ++------
 fs/btrfs/dev.c                      | 25 -------------------------
 fs/btrfs/volumes.c                  | 19 +++++++++++--------
 fs/btrfs/volumes.h                  |  9 +++++----
 fs/squashfs/sqfs.c                  | 21 +++++++++++----------
 fs/squashfs/sqfs_inode.c            |  5 +++--
 9 files changed, 35 insertions(+), 58 deletions(-)
 delete mode 100644 fs/btrfs/dev.c

base-commit: 79497816056122c49b3b135e14cbac59374e636c

Signature · 1 line
2.54.0 (Apple Git-157)
#1 lore raw

PATCH 1/6 fs: btrfs: Avoid a 64-bit modulo in __btrfs_devread()

On 32-bit PowerPC, GCC turns the u64 '%' in __btrfs_devread() into a call to __umoddi3(), which U-Boot's private libgcc does not provide.

The shift by log2blksz on the line above already requires a power-of-two block size, so take the offset within the block with a mask.

---
 fs/btrfs/compat.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/compat.h +1 -1
index 4596b9d1dd3..e6784884835 100644
@@ -60,7 +60,7 @@ static inline int __btrfs_devread(struct blk_desc *desc,
 	int ret;
 
 	sector = offset >> desc->log2blksz;
-	byte_offset = offset % desc->blksz;
+	byte_offset = offset & (desc->blksz - 1);
 
 	/* fs_devread() return 0 for error, >0 for success */
 	ret = fs_devread(desc, part, sector, byte_offset, size, buf);
Signature · 1 line
2.54.0 (Apple Git-157)
Reviewed-by on v1 1/6
#2 lore raw

Re: [PATCH 1/6] fs: btrfs: Avoid a 64-bit modulo in __btrfs_devread()

Quoted 11 lines · Josef Schlehofer

On 32-bit PowerPC, GCC turns the u64 '%' in __btrfs_devread() into a call to __umoddi3(), which U-Boot's private libgcc does not provide.

The shift by log2blksz on the line above already requires a power-of-two block size, so take the offset within the block with a mask.

Thanks,
Qu

Quoted 17 lines
---
  fs/btrfs/compat.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/compat.h +1 -1
index 4596b9d1dd3..e6784884835 100644
@@ -60,7 +60,7 @@ static inline int __btrfs_devread(struct blk_desc *desc,
  	int ret;
 
  	sector = offset >> desc->log2blksz;
-	byte_offset = offset % desc->blksz;
+	byte_offset = offset & (desc->blksz - 1);
 
  	/* fs_devread() return 0 for error, >0 for success */
  	ret = fs_devread(desc, part, sector, byte_offset, size, buf);
#3 lore raw

PATCH 2/6 fs/squashfs: Avoid 64-bit divisions

In reply to Josef Schlehofer

On 32-bit PowerPC, GCC turns '/' on u64 values into calls to __udivdi3(), which U-Boot's private libgcc does not provide.

Use lldiv() and DIV_ROUND_UP_ULL(), as commit 92080c6ef6c7 ("fs/squashfs:
use lldiv function for math") and commit aeea67f9a033 ("fs/squashfs: use lldiv function for math") already did for other divisions here. The divisors are block sizes and fit into 32 bits.

Cc Joao Marcos Costa <[email protected]>
Cc Richard Genoud <[email protected]>
Cc Thomas Petazzoni <[email protected]>
Cc Miquel Raynal <[email protected]>
Cc Tom Rini <[email protected]>
---
 fs/squashfs/sqfs.c       | 21 +++++++++++----------
 fs/squashfs/sqfs_inode.c |  5 +++--
 2 files changed, 14 insertions(+), 12 deletions(-)
fs/squashfs/sqfs.c +11 -10
index 0ea6dcaba1a..7b4f1310c00 100644
@@ -95,7 +95,7 @@ static int sqfs_calc_n_blks(__le64 start, __le64 end, u64 *offset)
 	start_ = lldiv(le64_to_cpu(start), ctxt.cur_dev->blksz);
 	*offset = le64_to_cpu(start) - (start_ * ctxt.cur_dev->blksz);
 
-	return DIV_ROUND_UP(table_size + *offset, ctxt.cur_dev->blksz);
+	return DIV_ROUND_UP_ULL(table_size + *offset, ctxt.cur_dev->blksz);
 }
 
 /*
@@ -131,7 +131,7 @@ static int sqfs_frag_lookup(u32 inode_fragment_index,
 	n_blks = sqfs_calc_n_blks(sblk->fragment_table_start,
 				  cpu_to_le64(end), &table_offset);
 
-	start /= ctxt.cur_dev->blksz;
+	start = lldiv(start, ctxt.cur_dev->blksz);
 
 	/* Allocate a proper sized buffer to store the fragment index table */
 	if (__builtin_mul_overflow(n_blks, ctxt.cur_dev->blksz, &table_size)) {
@@ -170,7 +170,7 @@ static int sqfs_frag_lookup(u32 inode_fragment_index,
 	start_block = get_unaligned_le64(table + table_offset + block *
 					 sizeof(u64));
 
-	start = start_block / ctxt.cur_dev->blksz;
+	start = lldiv(start_block, ctxt.cur_dev->blksz);
 	n_blks = sqfs_calc_n_blks(cpu_to_le64(start_block),
 				  sblk->fragment_table_start, &table_offset);
 
@@ -792,8 +792,8 @@ static int sqfs_read_inode_table(unsigned char **inode_table, size_t *out_size)
 
 	table_size = get_unaligned_le64(&sblk->directory_table_start) -
 		get_unaligned_le64(&sblk->inode_table_start);
-	start = get_unaligned_le64(&sblk->inode_table_start) /
-		ctxt.cur_dev->blksz;
+	start = lldiv(get_unaligned_le64(&sblk->inode_table_start),
+		      ctxt.cur_dev->blksz);
 	n_blks = sqfs_calc_n_blks(sblk->inode_table_start,
 				  sblk->directory_table_start, &table_offset);
 
@@ -888,8 +888,8 @@ static int sqfs_read_directory_table(unsigned char **dir_table, u32 **pos_list)
 	/* DIRECTORY TABLE */
 	table_size = get_unaligned_le64(&sblk->fragment_table_start) -
 		get_unaligned_le64(&sblk->directory_table_start);
-	start = get_unaligned_le64(&sblk->directory_table_start) /
-		ctxt.cur_dev->blksz;
+	start = lldiv(get_unaligned_le64(&sblk->directory_table_start),
+		      ctxt.cur_dev->blksz);
 	n_blks = sqfs_calc_n_blks(sblk->directory_table_start,
 				  sblk->fragment_table_start, &table_offset);
 
@@ -1603,8 +1603,8 @@ static int sqfs_read_nest(const char *filename, void *buf, loff_t offset,
 		start = lldiv(data_offset, ctxt.cur_dev->blksz);
 		table_size = SQFS_BLOCK_SIZE(finfo.blk_sizes[j]);
 		table_offset = data_offset - (start * ctxt.cur_dev->blksz);
-		n_blks = DIV_ROUND_UP(table_size + table_offset,
-				      ctxt.cur_dev->blksz);
+		n_blks = DIV_ROUND_UP_ULL(table_size + table_offset,
+					  ctxt.cur_dev->blksz);
 
 		/* Don't load any data for sparse blocks */
 		if (finfo.blk_sizes[j] == 0) {
@@ -1677,7 +1677,8 @@ static int sqfs_read_nest(const char *filename, void *buf, loff_t offset,
 	start = lldiv(frag_entry.start, ctxt.cur_dev->blksz);
 	table_size = SQFS_BLOCK_SIZE(frag_entry.size);
 	table_offset = frag_entry.start - (start * ctxt.cur_dev->blksz);
-	n_blks = DIV_ROUND_UP(table_size + table_offset, ctxt.cur_dev->blksz);
+	n_blks = DIV_ROUND_UP_ULL(table_size + table_offset,
+				  ctxt.cur_dev->blksz);
 
 	if (__builtin_mul_overflow(n_blks, ctxt.cur_dev->blksz, &buf_size)) {
 		ret = -EINVAL;
fs/squashfs/sqfs_inode.c +3 -2
index 20085f8912b..d43277f06e7 100644
@@ -7,6 +7,7 @@
 
 #include <asm/unaligned.h>
 #include <compiler.h>
+#include <div64.h>
 #include <errno.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -122,9 +123,9 @@ int sqfs_inode_size(struct squashfs_base_inode *inode, u32 blk_size, size_t max)
 			return -EINVAL;
 
 		if (fragment == 0xFFFFFFFF)
-			blk_list_size = DIV_ROUND_UP(file_size, blk_size);
+			blk_list_size = DIV_ROUND_UP_ULL(file_size, blk_size);
 		else
-			blk_list_size = file_size / blk_size;
+			blk_list_size = lldiv(file_size, blk_size);
 
 		if (__builtin_mul_overflow(blk_list_size, (unsigned int)sizeof(u32),
 					   &blk_list_size) ||
Signature · 1 line
2.54.0 (Apple Git-157)
#4 lore raw

PATCH 3/6 mmc: Use div_u64() in mmc_berase()

In reply to Josef Schlehofer

With CONFIG_SYS_64BIT_LBA, GCC turns the lbaint_t division in mmc_berase() into a call to __udivdi3() on 32-bit PowerPC, which U-Boot's private libgcc does not provide.

Use div_u64(), as the remainders above already use div_u64_rem().

---
 drivers/mmc/mmc_write.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mmc/mmc_write.c +1 -1
index e739720b77c..ce0592aa659 100644
@@ -126,7 +126,7 @@ ulong mmc_berase(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt)
 			blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
 				mmc->erase_grp_size : (blkcnt - blk);
 
-			grpcnt = (blkcnt - blk) / mmc->erase_grp_size;
+			grpcnt = div_u64(blkcnt - blk, mmc->erase_grp_size);
 			/* Max 2GB per spec */
 			if ((blkcnt - blk) > 0x400000)
 				blk_r = 0x400000;
Signature · 1 line
2.54.0 (Apple Git-157)
Reviewed-by Peng Fan on v1 3/6
#5 lore raw

Re: [PATCH 3/6] mmc: Use div_u64() in mmc_berase()

Quoted 8 lines · On Wed, 25 Nov 2026 13:42:56 +0100 Josef Schlehofer…

With CONFIG_SYS_64BIT_LBA, GCC turns the lbaint_t division in mmc_berase() into a call to __udivdi3() on 32-bit PowerPC, which U-Boot's private libgcc does not provide.

Use div_u64(), as the remainders above already use div_u64_rem().

#6 lore raw

PATCH 4/6 powerpc: mpc8xxx: Avoid a 64-bit division in get_win_cnt()

In reply to Josef Schlehofer

GCC turns the u64 division in get_win_cnt() into a call to __udivdi3(), which U-Boot's private libgcc does not provide.

Compare size with the product of win_cnt and PAMU_PAGE_SIZE instead. For a non-zero win_cnt, size / win_cnt < PAMU_PAGE_SIZE holds exactly when size < win_cnt * PAMU_PAGE_SIZE, and the product is at most 128 * 4096.

---
 arch/powerpc/cpu/mpc8xxx/fsl_pamu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/cpu/mpc8xxx/fsl_pamu.c +1 -1
index 638c3a6fee5..b6f8552d7d8 100644
@@ -364,7 +364,7 @@ static uint32_t get_win_cnt(uint64_t size)
 {
 	uint32_t win_cnt = DEFAULT_NUM_SUBWINDOWS;
 
-	while (win_cnt && (size/win_cnt) < PAMU_PAGE_SIZE)
+	while (win_cnt && size < (uint64_t)win_cnt * PAMU_PAGE_SIZE)
 		win_cnt >>= 1;
 
 	return win_cnt;
Signature · 1 line
2.54.0 (Apple Git-157)
Acked-by on v1 4/6
#7 lore raw

Re: [PATCH 4/6] powerpc: mpc8xxx: Avoid a 64-bit division in get_win_cnt()

Quoted 11 lines · Josef Schlehofer

GCC turns the u64 division in get_win_cnt() into a call to __udivdi3(), which U-Boot's private libgcc does not provide.

Compare size with the product of win_cnt and PAMU_PAGE_SIZE instead. For a non-zero win_cnt, size / win_cnt < PAMU_PAGE_SIZE holds exactly when size < win_cnt * PAMU_PAGE_SIZE, and the product is at most 128 * 4096.

Quoted 11 lines
Cc Tom Rini <[email protected]>

---
arch/powerpc/cpu/mpc8xxx/fsl_pamu.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/cpu/mpc8xxx/fsl_pamu.c +0 -0
index 638c3a6fee5..b6f8552d7d8 100644

arch/powerpc/cpu/mpc8xxx/fsl_pamu.c static uint32_t get_win_cnt(uint64_t size) · line 370

Quoted 8 lines
arch/powerpc/cpu/mpc8xxx/fsl_pamu.c +1 -1
@@ -364,7 +364,7 @@ static uint32_t get_win_cnt(uint64_t size)
 {
 	uint32_t win_cnt = DEFAULT_NUM_SUBWINDOWS;
 
-	while (win_cnt && (size/win_cnt) < PAMU_PAGE_SIZE)
+	while (win_cnt && size < (uint64_t)win_cnt * PAMU_PAGE_SIZE)
 		win_cnt >>= 1;
 
 	return win_cnt;

--- Thanks!

#8 lore raw

PATCH 5/6 fs: btrfs: Remove the unused dev.c

In reply to Josef Schlehofer

Nothing uses btrfs_devread(), btrfs_blk_desc or btrfs_part_info, which are all that fs/btrfs/dev.c defines. On 32-bit PowerPC, GCC turns the u64 '%' in btrfs_devread() into a call to __umoddi3(), which U-Boot's private libgcc does not provide.

Remove the file.

Cc Marek Behún <[email protected]>
Cc Qu Wenruo <[email protected]>
Cc Tom Rini <[email protected]>
---
 fs/btrfs/Makefile |  2 +-
 fs/btrfs/dev.c    | 25 -------------------------
 2 files changed, 1 insertion(+), 26 deletions(-)
 delete mode 100644 fs/btrfs/dev.c
fs/btrfs/Makefile +1 -1
index 31b426d572a..79d96c6e0ca 100644
@@ -2,6 +2,6 @@
 #
 # 2017 Marek Behún, CZ.NIC, [email protected]
 
-obj-y := btrfs.o compression.o ctree.o dev.o dir-item.o \
+obj-y := btrfs.o compression.o ctree.o dir-item.o \
 	extent-io.o inode.o subvolume.o crypto/hash.o disk-io.o \
 	common/rbtree-utils.o extent-cache.o volumes.o root-tree.o
fs/btrfs/dev.c deleted +0 -25
deleted file mode 100644
index e27a032c9f6..00000000000
@@ -1,25 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0+
-/*
- * BTRFS filesystem implementation for U-Boot
- *
- * 2017 Marek Behún, CZ.NIC, [email protected]
- */
-
-#include <blk.h>
-#include <compiler.h>
-#include <fs_internal.h>
-
-struct blk_desc *btrfs_blk_desc;
-struct disk_partition *btrfs_part_info;
-
-int btrfs_devread(u64 address, int byte_len, void *buf)
-{
-	lbaint_t sector;
-	int byte_offset;
-
-	sector = address >> btrfs_blk_desc->log2blksz;
-	byte_offset = address % btrfs_blk_desc->blksz;
-
-	return fs_devread(btrfs_blk_desc, btrfs_part_info, sector, byte_offset,
-			  byte_len, buf);
-}
Signature · 1 line
2.54.0 (Apple Git-157)
#9 lore raw

Re: [PATCH 5/6] fs: btrfs: Remove the unused dev.c

Quoted 11 lines · Josef Schlehofer

Nothing uses btrfs_devread(), btrfs_blk_desc or btrfs_part_info, which are all that fs/btrfs/dev.c defines. On 32-bit PowerPC, GCC turns the u64 '%' in btrfs_devread() into a call to __umoddi3(), which U-Boot's private libgcc does not provide.

Remove the file.

Cc Marek Behún <[email protected]>
Cc Qu Wenruo <[email protected]>
Cc Tom Rini <[email protected]>

The removal looks fine to me, however since there are only __btrfs_devread() callers, it would make more sense to rename __btrfs_devread() to btrfs_devread().

Thanks,
Qu

Quoted 49 lines
---
  fs/btrfs/Makefile |  2 +-
  fs/btrfs/dev.c    | 25 -------------------------
  2 files changed, 1 insertion(+), 26 deletions(-)

delete mode 100644 fs/btrfs/dev.c

fs/btrfs/Makefile +1 -1
index 31b426d572a..79d96c6e0ca 100644
@@ -2,6 +2,6 @@
  #
  # 2017 Marek Behún, CZ.NIC, [email protected]
 
-obj-y := btrfs.o compression.o ctree.o dev.o dir-item.o \
+obj-y := btrfs.o compression.o ctree.o dir-item.o \
  	extent-io.o inode.o subvolume.o crypto/hash.o disk-io.o \
  	common/rbtree-utils.o extent-cache.o volumes.o root-tree.o
fs/btrfs/dev.c deleted +0 -25
deleted file mode 100644
index e27a032c9f6..00000000000
@@ -1,25 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0+
-/*
- * BTRFS filesystem implementation for U-Boot
- *
- * 2017 Marek Behún, CZ.NIC, [email protected]
- */
-
-#include <blk.h>
-#include <compiler.h>
-#include <fs_internal.h>
-
-struct blk_desc *btrfs_blk_desc;
-struct disk_partition *btrfs_part_info;
-
-int btrfs_devread(u64 address, int byte_len, void *buf)
-{
-	lbaint_t sector;
-	int byte_offset;
-
-	sector = address >> btrfs_blk_desc->log2blksz;
-	byte_offset = address % btrfs_blk_desc->blksz;
-
-	return fs_devread(btrfs_blk_desc, btrfs_part_info, sector, byte_offset,
-			  byte_len, buf);
-}
#10 lore raw

Re: [PATCH 5/6] fs: btrfs: Remove the unused dev.c

Hi Qu,

Quoted 3 lines · Qu Wenruo

The removal looks fine to me, however since there are only __btrfs_devread() callers, it would make more sense to rename __btrfs_devread() to btrfs_devread().

Whoa, good catch, I'm not sure at the moment. Happy to leave it as is for now and do a follow-up once it's merged, or I can send a v2. Whichever you and Tom prefer.

Josef

Reviewed-by
#11 lore raw

Re: [PATCH 5/6] fs: btrfs: Remove the unused dev.c

Quoted 10 lines · Josef Schlehofer

Hi Qu,

Quoted 3 lines · Qu Wenruo

The removal looks fine to me, however since there are only __btrfs_devread() callers, it would make more sense to rename __btrfs_devread() to btrfs_devread().

Whoa, good catch, I'm not sure at the moment. Happy to leave it as is for now and do a follow-up once it's merged, or I can send a v2. Whichever you and Tom prefer.

Either looks good to me. If there is no other review, I think you can wait for merge and got for a follow up.

Otherwise you can go a v2 do the rename in the same patch.

Anyway, the removal itself is fine. So:

Thanks,
Qu

Quoted 2 lines

Josef

#12 lore raw

PATCH 6/6 fs: btrfs: Avoid the remaining 64-bit divisions

In reply to Josef Schlehofer

On 32-bit PowerPC, GCC turns the u64 divisions in btrfs_stripe_length() and in the RAID5/6 code of __btrfs_map_block() into calls to __udivdi3(), which U-Boot's private libgcc does not provide. calc_stripe_length() has the same divisions.

U-Boot never runs these divisions. btrfs_stripe_length() and calc_stripe_length() have no caller, and the RAID5/6 divisions sit in the branch that fills a raid map, which needs a raid_map_ret. Both callers of btrfs_map_block() set it to NULL. Builds of turris_1x_nor and turris_1x_sdcard still link that branch, and with it __udivdi3().

Use div_u64(). Its divisors fit into 32 bits: num_stripes is a u32 or an int and stripe_len is an int. full_stripe_len is the product of two ints, so declare it as a u32.

linux/math64.h pulls in linux/log2.h, whose is_power_of_2() conflicts with the copy in compat.h, which returns int instead of bool. Include linux/log2.h in compat.h and drop the copy.

---
 fs/btrfs/compat.h  |  6 +-----
 fs/btrfs/volumes.c | 19 +++++++++++--------
 fs/btrfs/volumes.h |  9 +++++----
 3 files changed, 17 insertions(+), 17 deletions(-)
fs/btrfs/compat.h +1 -5
index e6784884835..f973f3f1185 100644
@@ -4,6 +4,7 @@
 #define __BTRFS_COMPAT_H__
 
 #include <linux/errno.h>
+#include <linux/log2.h>
 #include <fs_internal.h>
 #include <u-boot/uuid.h>
 
@@ -74,9 +75,4 @@ static inline void uuid_unparse(const u8 *uuid, char *out)
 	return uuid_bin_to_str((unsigned char *)uuid, out, 0);
 }
 
-static inline int is_power_of_2(unsigned long n)
-{
-	return (n != 0 && ((n & (n - 1)) == 0));
-}
-
 #endif
fs/btrfs/volumes.c +11 -8
index 71b0b55b9c6..fcb20a761f5 100644
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <fs_internal.h>
+#include <linux/math64.h>
 #include "ctree.h"
 #include "disk-io.h"
 #include "volumes.h"
@@ -825,17 +826,17 @@ u64 btrfs_stripe_length(struct btrfs_fs_info *fs_info,
 		stripe_len = chunk_len;
 		break;
 	case BTRFS_BLOCK_GROUP_RAID0:
-		stripe_len = chunk_len / num_stripes;
+		stripe_len = div_u64(chunk_len, num_stripes);
 		break;
 	case BTRFS_BLOCK_GROUP_RAID5:
-		stripe_len = chunk_len / (num_stripes - 1);
+		stripe_len = div_u64(chunk_len, num_stripes - 1);
 		break;
 	case BTRFS_BLOCK_GROUP_RAID6:
-		stripe_len = chunk_len / (num_stripes - 2);
+		stripe_len = div_u64(chunk_len, num_stripes - 2);
 		break;
 	case BTRFS_BLOCK_GROUP_RAID10:
-		stripe_len = chunk_len / (num_stripes /
-				btrfs_chunk_sub_stripes(leaf, chunk));
+		stripe_len = div_u64(chunk_len, num_stripes /
+				     btrfs_chunk_sub_stripes(leaf, chunk));
 		break;
 	default:
 		/* Invalid chunk profile found */
@@ -1085,17 +1086,19 @@ again:
 			int rot;
 			u64 tmp;
 			u64 raid56_full_stripe_start;
-			u64 full_stripe_len = nr_data_stripes(map) * map->stripe_len;
+			u32 full_stripe_len = nr_data_stripes(map) * map->stripe_len;
 
 			/*
 			 * align the start of our data stripe in the logical
 			 * address space
 			 */
-			raid56_full_stripe_start = offset / full_stripe_len;
+			raid56_full_stripe_start = div_u64(offset,
+							   full_stripe_len);
 			raid56_full_stripe_start *= full_stripe_len;
 
 			/* get the data stripe number */
-			stripe_nr = raid56_full_stripe_start / map->stripe_len;
+			stripe_nr = div_u64(raid56_full_stripe_start,
+					    map->stripe_len);
 			stripe_nr = stripe_nr / nr_data_stripes(map);
 
 			/* Work out the disk rotation on this stripe-set */
fs/btrfs/volumes.h +5 -4
index 9d1a07ae789..d66547c6c10 100644
@@ -4,6 +4,7 @@
 #define __BTRFS_VOLUMES_H__
 
 #include <fs_internal.h>
+#include <linux/math64.h>
 #include "ctree.h"
 
 #define BTRFS_STRIPE_LEN	SZ_64K
@@ -138,16 +139,16 @@ static inline u64 calc_stripe_length(u64 type, u64 length, int num_stripes)
 
 	if (type & BTRFS_BLOCK_GROUP_RAID0) {
 		stripe_size = length;
-		stripe_size /= num_stripes;
+		stripe_size = div_u64(stripe_size, num_stripes);
 	} else if (type & BTRFS_BLOCK_GROUP_RAID10) {
 		stripe_size = length * 2;
-		stripe_size /= num_stripes;
+		stripe_size = div_u64(stripe_size, num_stripes);
 	} else if (type & BTRFS_BLOCK_GROUP_RAID5) {
 		stripe_size = length;
-		stripe_size /= (num_stripes - 1);
+		stripe_size = div_u64(stripe_size, num_stripes - 1);
 	} else if (type & BTRFS_BLOCK_GROUP_RAID6) {
 		stripe_size = length;
-		stripe_size /= (num_stripes - 2);
+		stripe_size = div_u64(stripe_size, num_stripes - 2);
 	} else {
 		stripe_size = length;
 	}
Signature · 1 line
2.54.0 (Apple Git-157)
Reviewed-by on v1 6/6
#13 lore raw

Re: [PATCH 6/6] fs: btrfs: Avoid the remaining 64-bit divisions

Quoted 19 lines · Josef Schlehofer

On 32-bit PowerPC, GCC turns the u64 divisions in btrfs_stripe_length() and in the RAID5/6 code of __btrfs_map_block() into calls to __udivdi3(), which U-Boot's private libgcc does not provide. calc_stripe_length() has the same divisions.

U-Boot never runs these divisions. btrfs_stripe_length() and calc_stripe_length() have no caller, and the RAID5/6 divisions sit in the branch that fills a raid map, which needs a raid_map_ret. Both callers of btrfs_map_block() set it to NULL. Builds of turris_1x_nor and turris_1x_sdcard still link that branch, and with it __udivdi3().

Use div_u64(). Its divisors fit into 32 bits: num_stripes is a u32 or an int and stripe_len is an int. full_stripe_len is the product of two ints, so declare it as a u32.

linux/math64.h pulls in linux/log2.h, whose is_power_of_2() conflicts with the copy in compat.h, which returns int instead of bool. Include linux/log2.h in compat.h and drop the copy.

Quoted 5 lines

Thanks,
Qu

Quoted 119 lines
---
  fs/btrfs/compat.h  |  6 +-----
  fs/btrfs/volumes.c | 19 +++++++++++--------
  fs/btrfs/volumes.h |  9 +++++----
  3 files changed, 17 insertions(+), 17 deletions(-)
fs/btrfs/compat.h +1 -5
index e6784884835..f973f3f1185 100644
@@ -4,6 +4,7 @@
  #define __BTRFS_COMPAT_H__
 
  #include <linux/errno.h>
+#include <linux/log2.h>
  #include <fs_internal.h>
  #include <u-boot/uuid.h>
 
@@ -74,9 +75,4 @@ static inline void uuid_unparse(const u8 *uuid, char *out)
  	return uuid_bin_to_str((unsigned char *)uuid, out, 0);
  }
 
-static inline int is_power_of_2(unsigned long n)
-{
-	return (n != 0 && ((n & (n - 1)) == 0));
-}
-
  #endif
fs/btrfs/volumes.c +11 -8
index 71b0b55b9c6..fcb20a761f5 100644
@@ -2,6 +2,7 @@
  #include <stdlib.h>
  #include <errno.h>
  #include <fs_internal.h>
+#include <linux/math64.h>
  #include "ctree.h"
  #include "disk-io.h"
  #include "volumes.h"
@@ -825,17 +826,17 @@ u64 btrfs_stripe_length(struct btrfs_fs_info *fs_info,
  		stripe_len = chunk_len;
  		break;
  	case BTRFS_BLOCK_GROUP_RAID0:
-		stripe_len = chunk_len / num_stripes;
+		stripe_len = div_u64(chunk_len, num_stripes);
  		break;
  	case BTRFS_BLOCK_GROUP_RAID5:
-		stripe_len = chunk_len / (num_stripes - 1);
+		stripe_len = div_u64(chunk_len, num_stripes - 1);
  		break;
  	case BTRFS_BLOCK_GROUP_RAID6:
-		stripe_len = chunk_len / (num_stripes - 2);
+		stripe_len = div_u64(chunk_len, num_stripes - 2);
  		break;
  	case BTRFS_BLOCK_GROUP_RAID10:
-		stripe_len = chunk_len / (num_stripes /
-				btrfs_chunk_sub_stripes(leaf, chunk));
+		stripe_len = div_u64(chunk_len, num_stripes /
+				     btrfs_chunk_sub_stripes(leaf, chunk));
  		break;
  	default:
  		/* Invalid chunk profile found */
@@ -1085,17 +1086,19 @@ again:
  			int rot;
  			u64 tmp;
  			u64 raid56_full_stripe_start;
-			u64 full_stripe_len = nr_data_stripes(map) * map->stripe_len;
+			u32 full_stripe_len = nr_data_stripes(map) * map->stripe_len;
 
  			/*
  			 * align the start of our data stripe in the logical
  			 * address space
  			 */
-			raid56_full_stripe_start = offset / full_stripe_len;
+			raid56_full_stripe_start = div_u64(offset,
+							   full_stripe_len);
  			raid56_full_stripe_start *= full_stripe_len;
 
  			/* get the data stripe number */
-			stripe_nr = raid56_full_stripe_start / map->stripe_len;
+			stripe_nr = div_u64(raid56_full_stripe_start,
+					    map->stripe_len);
  			stripe_nr = stripe_nr / nr_data_stripes(map);
 
  			/* Work out the disk rotation on this stripe-set */
fs/btrfs/volumes.h +5 -4
index 9d1a07ae789..d66547c6c10 100644
@@ -4,6 +4,7 @@
  #define __BTRFS_VOLUMES_H__
 
  #include <fs_internal.h>
+#include <linux/math64.h>
  #include "ctree.h"
 
  #define BTRFS_STRIPE_LEN	SZ_64K
@@ -138,16 +139,16 @@ static inline u64 calc_stripe_length(u64 type, u64 length, int num_stripes)
 
  	if (type & BTRFS_BLOCK_GROUP_RAID0) {
  		stripe_size = length;
-		stripe_size /= num_stripes;
+		stripe_size = div_u64(stripe_size, num_stripes);
  	} else if (type & BTRFS_BLOCK_GROUP_RAID10) {
  		stripe_size = length * 2;
-		stripe_size /= num_stripes;
+		stripe_size = div_u64(stripe_size, num_stripes);
  	} else if (type & BTRFS_BLOCK_GROUP_RAID5) {
  		stripe_size = length;
-		stripe_size /= (num_stripes - 1);
+		stripe_size = div_u64(stripe_size, num_stripes - 1);
  	} else if (type & BTRFS_BLOCK_GROUP_RAID6) {
  		stripe_size = length;
-		stripe_size /= (num_stripes - 2);
+		stripe_size = div_u64(stripe_size, num_stripes - 2);
  	} else {
  		stripe_size = length;
  	}
#14 lore raw
In reply to Josef Schlehofer
Quoted 25 lines · Josef Schlehofer

U-Boot's private libgcc does not provide __udivdi3() and __umoddi3(). v3 of the mpc85xx LTO series links mpc85xx LTO builds with it and added both routines [1]. Christophe Leroy asked why the callers do not avoid '/' and '%' on 64-bit operands instead, and Tom Rini asked to update the common code [2].

Patches 1-4 avoid the divisions that make the mpc85xx LTO builds call __udivdi3() or __umoddi3(), in btrfs, squashfs, mmc_berase() and get_win_cnt() of the mpc8xxx PAMU code. They do not depend on each other. Patch 5 removes fs/btrfs/dev.c, which nothing uses and which holds another 64-bit modulo. Patch 6 converts the remaining divisions in btrfs. U-Boot never runs them, but builds without LTO still link the ones in the RAID5/6 code. My reply in that thread announced these conversions for v4 of the LTO series. They are sent on their own instead.

Patch 2 and my big-endian fix for squashfs [3] conflict in sqfs_read_nest(). I will rebase whichever of them goes in second.

Build tested with a buildman world build on a fork's CI, and sandbox test.py gives the same results as before. Run tested on qemu-ppce500 (btrfs) and qemu_arm (btrfs, squashfs, mmc erase), and on a Turris 1.1 with the mpc85xx LTO patches on top, which boots from an SD card and loads files from btrfs. The PAMU change is build tested only.

Quoted 5 lines

[1]
https://lore.kernel.org/u-boot/[email protected]/
[2] https://lore.kernel.org/u-boot/20261119200924.GB3176972@bill-the-cat/
[3]
https://lore.kernel.org/u-boot/[email protected]/

Thanks, after review, who will merge? Me? Or anyone else up to?

Quoted 19 lines

Josef Schlehofer (6):
fs: btrfs: Avoid a 64-bit modulo in __btrfs_devread() fs/squashfs: Avoid 64-bit divisions
mmc: Use div_u64() in mmc_berase()
powerpc: mpc8xxx: Avoid a 64-bit division in get_win_cnt()
fs: btrfs: Remove the unused dev.c
fs: btrfs: Avoid the remaining 64-bit divisions

arch/powerpc/cpu/mpc8xxx/fsl_pamu.c |  2 +-
drivers/mmc/mmc_write.c             |  2 +-
fs/btrfs/Makefile                   |  2 +-
fs/btrfs/compat.h                   |  8 ++------
fs/btrfs/dev.c                      | 25 -------------------------
fs/btrfs/volumes.c                  | 19 +++++++++++--------
fs/btrfs/volumes.h                  |  9 +++++----
fs/squashfs/sqfs.c                  | 21 +++++++++++----------
fs/squashfs/sqfs_inode.c            |  5 +++--
9 files changed, 35 insertions(+), 58 deletions(-)
Quoted 5 lines

delete mode 100644 fs/btrfs/dev.c

base-commit: 79497816056122c49b3b135e14cbac59374e636c

--- Thanks!

#15 lore raw
Quoted 27 lines · Bradley Morgan
Quoted 25 lines · Josef Schlehofer

U-Boot's private libgcc does not provide __udivdi3() and __umoddi3(). v3 of the mpc85xx LTO series links mpc85xx LTO builds with it and added both routines [1]. Christophe Leroy asked why the callers do not avoid '/' and '%' on 64-bit operands instead, and Tom Rini asked to update the common code [2].

Patches 1-4 avoid the divisions that make the mpc85xx LTO builds call __udivdi3() or __umoddi3(), in btrfs, squashfs, mmc_berase() and get_win_cnt() of the mpc8xxx PAMU code. They do not depend on each other. Patch 5 removes fs/btrfs/dev.c, which nothing uses and which holds another 64-bit modulo. Patch 6 converts the remaining divisions in btrfs. U-Boot never runs them, but builds without LTO still link the ones in the RAID5/6 code. My reply in that thread announced these conversions for v4 of the LTO series. They are sent on their own instead.

Patch 2 and my big-endian fix for squashfs [3] conflict in sqfs_read_nest(). I will rebase whichever of them goes in second.

Build tested with a buildman world build on a fork's CI, and sandbox test.py gives the same results as before. Run tested on qemu-ppce500 (btrfs) and qemu_arm (btrfs, squashfs, mmc erase), and on a Turris 1.1 with the mpc85xx LTO patches on top, which boots from an SD card and loads files from btrfs. The PAMU change is build tested only.

Quoted 8 lines
Quoted 5 lines

[1]
https://lore.kernel.org/u-boot/[email protected]/
[2] https://lore.kernel.org/u-boot/20261119200924.GB3176972@bill-the-cat/
[3]
https://lore.kernel.org/u-boot/[email protected]/

Thanks, after review, who will merge? Me? Or anyone else up to?

I'll pick up the series, once there's been enough review on it.

Signature · 1 line
Tom