Skip to contentSkip to conversation

btrfs: reduce div64 calls for __btrfs_map_block() and its variants

PATCH In review Qu Wenruo 2 participants 4 messages Feb 6, 2023 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

v3 cover btrfs: reduce div64 calls for __btrfs_map_block() and its variants

Changelog:
v3:
- Move more BTRFS_STRIPE_LEN_SHIFT cleanup to patch 1
- Use const_ilog2() to define BTRFS_STRIPE_LEN_SHIFT
- Introduce BTRFS_STRIPE_LEN_MASK to make offset calculation easier.
- Convert all "u64 stripe_nr" to "u32 stripe_nr" Previously there is one in get_raid56_logic_offset() not converted.

v2:
- Fix a linkage error for 32bits platform in block-group.c We have a line "stripe_nr = (stripe_nr * num_stripes + i) / sub_stripes."
In that case, @stripe_nr itself is already u64, thus the division is dividing u64 with u32.

And we can not easily do a forced u32 conversion, as we only have ensured @stripe_nr can be contained in u32.
Even with 10G chunk size in mined, if the RAID10 array has over 26 devices, then we can overflow U32 and cause problems.

For now, keeps the old div_u64 call.

Div64 is much slower than 32 bit division, and only get improved in the most recent CPUs, that's why we have dedicated div64* helpers.

One usage of div64 is in __btrfs_map_block() and its variants, where we got @stripe_nr as u64, and all later division has to go the div64 helpers.

But the truth is, with our current chunk size limit (10G) and fixed stripe length (64K), we can have at most 160K stripes in a chunk, which is small enough for u32 already.

So this patchset would reduce div64 calls by:

- Remove map_lookup::stripe_len first
So now we don't need to call div64 to calculate @stripe_nr, just a simple right shift, then truncate to u32.

This is a prerequisite for the 2nd patch, without the fixed stripe length, we have to rely on div64.

- Reduce the width of various @stripe_nr from u64 to u32

- Use regular division and modulo to do the calculation Now we can get rid of the fear that we missed some div64 helpers.

Qu Wenruo (2):
btrfs: remove map_lookup->stripe_len
btrfs: reduce div64 calls by limiting the number of stripes of a chunk

    to u32
 fs/btrfs/block-group.c            |  22 +++---
 fs/btrfs/scrub.c                  |  55 +++++++-------
 fs/btrfs/tests/extent-map-tests.c |   1 -
 fs/btrfs/tree-checker.c           |  14 ++++
 fs/btrfs/volumes.c                | 118 +++++++++++++++---------------
 fs/btrfs/volumes.h                |   6 +-
 6 files changed, 116 insertions(+), 100 deletions(-)
Signature · 1 line
2.39.1
author
#1 lore raw

PATCH v3 1/2 btrfs: remove map_lookup->stripe_len

Currently btrfs doesn't support stripe lengths other than 64KiB. This is already in the tree-checker.

Thus there is really no meaning to record that fixed value in map_lookup, and can all be replaced with BTRFS_STRIPE_LEN.

Furthermore we can use the fix stripe length to do the following optimization:

- Use BTRFS_STRIPE_LEN_SHIFT to replace some 64bit division Now we only need to do a right shift.

And BTRFS_STRIPE_LEN itself is already too large, thus if we accidentally use BTRFS_STRIPE_LEN to do bit shift, a compiler warning would be triggered.

Thus this bit shift optimization would be very safe.

- Use BTRFS_STRIPE_LEN_MASK to calculate the offset inside a stripe

---
 fs/btrfs/block-group.c            | 10 +++---
 fs/btrfs/scrub.c                  | 43 ++++++++++++-------------
 fs/btrfs/tests/extent-map-tests.c |  1 -
 fs/btrfs/volumes.c                | 53 ++++++++++++++-----------------
 fs/btrfs/volumes.h                |  4 ++-
 5 files changed, 53 insertions(+), 58 deletions(-)
fs/btrfs/block-group.c +6 -4
index 45ccb25c5b1f..ded80f791adc 100644
@@ -1994,12 +1994,12 @@ int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
 
 	map = em->map_lookup;
 	data_stripe_length = em->orig_block_len;
-	io_stripe_size = map->stripe_len;
+	io_stripe_size = BTRFS_STRIPE_LEN;
 	chunk_start = em->start;
 
 	/* For RAID5/6 adjust to a full IO stripe length */
 	if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
-		io_stripe_size = map->stripe_len * nr_data_stripes(map);
+		io_stripe_size = nr_data_stripes(map) << BTRFS_STRIPE_LEN_SHIFT;
 
 	buf = kcalloc(map->num_stripes, sizeof(u64), GFP_NOFS);
 	if (!buf) {
@@ -2020,8 +2020,10 @@ int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
 		if (bdev && map->stripes[i].dev->bdev != bdev)
 			continue;
 
-		stripe_nr = physical - map->stripes[i].physical;
-		stripe_nr = div64_u64_rem(stripe_nr, map->stripe_len, &offset);
+		stripe_nr = (physical - map->stripes[i].physical) >>
+			    BTRFS_STRIPE_LEN_SHIFT;
+		offset = (physical - map->stripes[i].physical) &
+			 BTRFS_STRIPE_LEN_MASK;
 
 		if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
 				 BTRFS_BLOCK_GROUP_RAID10)) {
fs/btrfs/scrub.c +21 -22

157 lines not on this page · raw message

fs/btrfs/tests/extent-map-tests.c +0 -1
index c5b3a631bf4f..5d09fc4e1c3c 100644
@@ -486,7 +486,6 @@ static int test_rmap_block(struct btrfs_fs_info *fs_info,
 	em->map_lookup = map;
 
 	map->num_stripes = test->num_stripes;
-	map->stripe_len = BTRFS_STRIPE_LEN;
 	map->type = test->raid_type;
 
 	for (i = 0; i < map->num_stripes; i++) {
fs/btrfs/volumes.c +23 -30

178 lines not on this page · raw message

fs/btrfs/volumes.h +3 -1
index 6b7a05f6cf82..c2248b60de62 100644
@@ -19,6 +19,9 @@ extern struct mutex uuid_mutex;
 
 #define BTRFS_STRIPE_LEN	SZ_64K
 
+#define BTRFS_STRIPE_LEN_SHIFT	(const_ilog2(BTRFS_STRIPE_LEN))
+#define BTRFS_STRIPE_LEN_MASK	(BTRFS_STRIPE_LEN - 1)
+
 /* Used by sanity check for btrfs_raid_types. */
 #define const_ffs(n) (__builtin_ctzll(n) + 1)
 
@@ -461,7 +464,6 @@ struct map_lookup {
 	u64 type;
 	int io_align;
 	int io_width;
-	u32 stripe_len;
 	int num_stripes;
 	int sub_stripes;
 	int verified_stripes; /* For mount time dev extent verification */
Signature · 1 line
2.39.1
author
#3 lore raw

PATCH v3 2/2 btrfs: reduce div64 calls by limiting the number of stripes of a chunk to u32

In reply to Qu Wenruo

There are quite some div64 calls inside btrfs_map_block() and its variants.

Such calls are for @stripe_nr, where @stripe_nr is the number of stripes before our logical bytenr inside a chunk.

However we can eliminate such div64 calls by just reducing the width of @stripe_nr from 64 to 32.

This can be done because our chunk size limit is already 10G, with fixed stripe length 64K.
Thus a U32 is definitely enough to contain the number of stripes.

With such width reduction, we can get rid of slower div64, and extra warning for certain 32bit arch.

This patch would do:

- Add a new tree-checker chunk validation on chunk length Make sure no chunk can reach 256G, which can also act as a bitflip checker.

- Reduce the width from u64 to u32 for @stripe_nr variables

- Replace unnecessary div64 calls with regular modulo and division 32bit division and modulo are much faster than 64bit operations, and we are finally free of the div64 fear at least in those involved functions.

---
 fs/btrfs/block-group.c  | 12 ++++----
 fs/btrfs/scrub.c        | 14 +++++----
 fs/btrfs/tree-checker.c | 14 +++++++++
 fs/btrfs/volumes.c      | 65 +++++++++++++++++++++++------------------
 fs/btrfs/volumes.h      |  2 +-
 5 files changed, 64 insertions(+), 43 deletions(-)
fs/btrfs/block-group.c +5 -7
index ded80f791adc..9c6425398c74 100644
@@ -2009,8 +2009,8 @@ int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
 
 	for (i = 0; i < map->num_stripes; i++) {
 		bool already_inserted = false;
-		u64 stripe_nr;
-		u64 offset;
+		u32 stripe_nr;
+		u32 offset;
 		int j;
 
 		if (!in_range(physical, map->stripes[i].physical,
@@ -2026,16 +2026,14 @@ int btrfs_rmap_block(struct btrfs_fs_info *fs_info, u64 chunk_start,
 			 BTRFS_STRIPE_LEN_MASK;
 
 		if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
-				 BTRFS_BLOCK_GROUP_RAID10)) {
-			stripe_nr = stripe_nr * map->num_stripes + i;
-			stripe_nr = div_u64(stripe_nr, map->sub_stripes);
-		}
+				 BTRFS_BLOCK_GROUP_RAID10))
+			stripe_nr = div_u64(stripe_nr * map->num_stripes + i,
+					    map->sub_stripes);
 		/*
 		 * The remaining case would be for RAID56, multiply by
 		 * nr_data_stripes().  Alternatively, just use rmap_len below
 		 * instead of map->stripe_len
 		 */
-
 		bytenr = chunk_start + stripe_nr * io_stripe_size + offset;
 
 		/* Ensure we don't add duplicate addresses */
fs/btrfs/scrub.c +8 -6
index 190536b90779..3b4e621e822f 100644
@@ -2908,10 +2908,7 @@ static int get_raid56_logic_offset(u64 physical, int num,
 {
 	int i;
 	int j = 0;
-	u64 stripe_nr;
 	u64 last_offset;
-	u32 stripe_index;
-	u32 rot;
 	const int data_stripes = nr_data_stripes(map);
 
 	last_offset = (physical - map->stripes[num].physical) * data_stripes;
@@ -2920,13 +2917,18 @@ static int get_raid56_logic_offset(u64 physical, int num,
 
 	*offset = last_offset;
 	for (i = 0; i < data_stripes; i++) {
+		u32 stripe_nr;
+		u32 stripe_index;
+		u32 rot;
+
 		*offset = last_offset + (i << BTRFS_STRIPE_LEN_SHIFT);
 
-		stripe_nr = *offset >> BTRFS_STRIPE_LEN_SHIFT;
-		stripe_nr = div_u64(stripe_nr, data_stripes);
+		stripe_nr = (u32)(*offset >> BTRFS_STRIPE_LEN_SHIFT) /
+			    data_stripes;
 
 		/* Work out the disk rotation on this stripe-set */
-		stripe_nr = div_u64_rem(stripe_nr, map->num_stripes, &rot);
+		rot = stripe_nr % map->num_stripes;
+		stripe_nr /= map->num_stripes;
 		/* calculate which stripe this data locates */
 		rot += i;
 		stripe_index = rot % map->num_stripes;
fs/btrfs/tree-checker.c +14 -0
index baad1ed7e111..af04f6661035 100644
@@ -849,6 +849,20 @@ int btrfs_check_chunk_valid(struct extent_buffer *leaf,
 			  stripe_len);
 		return -EUCLEAN;
 	}
+	/*
+	 * We artificially limit the chunk size, so that the number of stripes
+	 * inside a chunk can be fit into a U32.
+	 * The current limit (256G) is way too large for real world usage
+	 * anyway, and it's also much larger than our existing limit (10G).
+	 *
+	 * Thus it should be a good way to catch obvious bitflip.
+	 */
+	if (unlikely(length >= ((u64)U32_MAX << BTRFS_STRIPE_LEN_SHIFT))) {
+		chunk_err(leaf, chunk, logical,
+			  "chunk length too large: have %llu limit %llu",
+			  length, (u64)U32_MAX << BTRFS_STRIPE_LEN_SHIFT);
+		return -EUCLEAN;
+	}
 	if (unlikely(type & ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
 			      BTRFS_BLOCK_GROUP_PROFILE_MASK))) {
 		chunk_err(leaf, chunk, logical,
fs/btrfs/volumes.c +36 -29

148 lines not on this page · raw message

fs/btrfs/volumes.h +1 -1
index c2248b60de62..46f33b66d85e 100644
@@ -66,7 +66,7 @@ struct btrfs_io_geometry {
 	/* offset of address in stripe */
 	u32 stripe_offset;
 	/* number of stripe where address falls */
-	u64 stripe_nr;
+	u32 stripe_nr;
 	/* offset of raid56 stripe into the chunk */
 	u64 raid56_stripe_offset;
 };
Signature · 1 line
2.39.1
Reviewed-by on v3 1/2
#2 lore raw

Re: [PATCH v3 1/2] btrfs: remove map_lookup->stripe_len

In reply to Qu Wenruo

LGTM.

Nit:

fs/btrfs/volumes.c int btrfs_get_io_geometry(struct btrfs_fs_info *fs_info, struct extent_map *em, · line 6336

Quoted 6 lines
fs/btrfs/volumes.c +1 -1
@@ -6338,7 +6333,7 @@ int btrfs_get_io_geometry(struct btrfs_fs_info *fs_info, struct extent_map *em,
  			 * reads, just allow a single stripe (on a single disk).
  			 */
  			if (op == BTRFS_MAP_WRITE) {
-				max_len = stripe_len * data_stripes -
+				max_len = (data_stripes << BTRFS_STRIPE_LEN_SHIFT) -

This can be:
max_len = full_stripe_len - offset - raid56_full_stripe_start;