Skip to contentSkip to conversation

DM RAID: Ensure the bitmap region_size is a power of 2

PATCH In review Jonathan Brassow 2 participants 4 messages Dec 11, 2012 – Dec 12, 2012

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

PATCH 1/1 DM RAID: Ensure the bitmap region_size is a power of 2

DM RAID: Ensure the bitmap region_size is a power of 2

If the user does not supply a region_size, a reasonable size is computed automatically. However, region_size needs to be a power of 2 and the code was not properly ensuring that.

drivers/md/dm-raid.c +5 -1
Index: linux-upstream/drivers/md/dm-raid.c
===================================================================
@@ -288,6 +288,7 @@ static int dev_parms(struct raid_set *rs
  */
 static int validate_region_size(struct raid_set *rs, unsigned long region_size)
 {
+	int i;
 	unsigned long min_region_size = rs->ti->len / (1 << 21);
 
 	if (!region_size) {
@@ -295,9 +296,12 @@ static int validate_region_size(struct r
 		 * Choose a reasonable default.  All figures in sectors.
 		 */
 		if (min_region_size > (1 << 13)) {
+			/* region_size must be a power of 2 */
+			for (i = 13; min_region_size > (1 << i); i++);
+
+			region_size = (1 << i);
 			DMINFO("Choosing default region size of %lu sectors",
 			       region_size);
-			region_size = min_region_size;
 		} else {
 			DMINFO("Choosing default region size of 4MiB");
 			region_size = 1 << 13; /* sectors */
#1 lore raw

drivers/md/dm-raid.c static int validate_region_size(struct r · line 302

Quoted 5 lines · Jon Brassow
drivers/md/dm-raid.c +4 -0
 		if (min_region_size > (1 << 13)) {
+			/* region_size must be a power of 2 */
+			for (i = 13; min_region_size > (1 << i); i++);
+
+			region_size = (1 << i);

Can the loop be replaced with a direct bit calculation, perhaps?

Alasdair

#2 lore raw
In reply to Jonathan Brassow

This is probably a better way to do it...

brassow

DM RAID: Ensure the bitmap region_size is a power of 2

If the user does not supply a region_size, a reasonable size is computed automatically. However, region_size needs to be a power of 2 and the code was not properly ensuring that.

drivers/md/dm-raid.c +3 -1
Index: linux-upstream/drivers/md/dm-raid.c
===================================================================
@@ -295,9 +295,11 @@ static int validate_region_size(struct r
 		 * Choose a reasonable default.  All figures in sectors.
 		 */
 		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);
 			DMINFO("Choosing default region size of %lu sectors",
 			       region_size);
-			region_size = min_region_size;
 		} else {
 			DMINFO("Choosing default region size of 4MiB");
 			region_size = 1 << 13; /* sectors */
#3 lore raw
Quoted 1 line · Jon Brassow

This is probably a better way to do it...

Indeed. I'll pick this one up.

Quoted 5 lines

DM RAID: Ensure the bitmap region_size is a power of 2

If the user does not supply a region_size, a reasonable size is computed automatically. However, region_size needs to be a power of 2 and the code was not properly ensuring that.

And this is just a cleanup patch: to provide a controlled error message here, rather than triggering a controlled error inside the md code later.

Alasdair