Skip to contentSkip to conversation

dm-raid: fix bug when round up region_size

PATCH [email protected] 2 participants 2 messages Sep 30, 2014 Device mapper

lore
Share & tools
lore

[email protected]

author Acked-by Tested-by
#0 lore raw

From: Heinz Mauelshagen <[email protected]>

Liong,
thanks for the catch.
Introduced minor modification utilizing the is_power_of_2() inline.

---

If region_size is not supplied by userspace and the device is very large, there is a bug in current code preventing region_size from being correctly set.

---
 drivers/md/dm-raid.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/md/dm-raid.c +4 -2
index 4880b69..264aef4 100644
@@ -325,8 +325,10 @@ static int validate_region_size(struct raid_set *rs, unsigned long region_size)
 		 */
 		if (min_region_size > (1 << 13)) {
 			/* If not a power of 2, make it the next power of 2 */
-			if (min_region_size & (min_region_size - 1))
-				region_size = 1 << fls(region_size);
+			if (is_power_of_2(min_region_size))
+				region_size = min_region_size;
+			else
+				region_size = 1 << fls(min_region_size);
 			DMINFO("Choosing default region size of %lu sectors",
 			       region_size);
 		} else {
Signature · 1 line
1.9.3
#1 lore raw
Quoted 28 lines · On Tue, 30 Sep 2014 13:00:36 +0200 [email protected] wrote:

From: Heinz Mauelshagen <[email protected]>

Liong,
thanks for the catch.
Introduced minor modification utilizing the is_power_of_2() inline.

---

If region_size is not supplied by userspace and the device is very large, there is a bug in current code preventing region_size from being correctly set.

---
 drivers/md/dm-raid.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/md/dm-raid.c +0 -0
index 4880b69..264aef4 100644
@@ -325,8 +325,10 @@ static int validate_region_size(struct raid_set *rs, unsigned long region_size)
 		 */
 		if (min_region_size > (1 << 13)) {

drivers/md/dm-raid.c static int validate_region_size(struct raid_set *rs, unsigned long region_size) · line 334

Quoted 10 lines
drivers/md/dm-raid.c +4 -2
 			/* If not a power of 2, make it the next power of 2 */
-			if (min_region_size & (min_region_size - 1))
-				region_size = 1 << fls(region_size);
+			if (is_power_of_2(min_region_size))
+				region_size = min_region_size;
+			else
+				region_size = 1 << fls(min_region_size);
 			DMINFO("Choosing default region size of %lu sectors",
 			       region_size);
 		} else {

If you really wanted to optimize, you could just do
region_size = roundup_pow_of_two(min_region_size);

there is all sorts of cool stuff in log2.h !!

NeilBrown