[RFC PATCH 4/5] md: introduce MAX_RAID_DISKS macro to replace magic number

Zheng Qixing posted 5 patches 1 month, 1 week ago
There is a newer version of this series
[RFC PATCH 4/5] md: introduce MAX_RAID_DISKS macro to replace magic number
Posted by Zheng Qixing 1 month, 1 week ago
From: Zheng Qixing <zhengqixing@huawei.com>

Define MAX_RAID_DISKS macro for the maximum number of RAID disks.
No functional change.

Signed-off-by: Zheng Qixing <zhengqixing@huawei.com>
---
 drivers/md/md.c | 4 ++--
 drivers/md/md.h | 1 +
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/md/md.c b/drivers/md/md.c
index 9eeab5258189..d2f136706f6c 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -1888,7 +1888,7 @@ static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_
 
 	if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
 	    sb->major_version != cpu_to_le32(1) ||
-	    le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
+	    le32_to_cpu(sb->max_dev) > MAX_RAID_DISKS ||
 	    le64_to_cpu(sb->super_offset) != rdev->sb_start ||
 	    (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
 		return -EINVAL;
@@ -2065,7 +2065,7 @@ static int super_1_validate(struct mddev *mddev, struct md_rdev *freshest, struc
 		mddev->resync_offset = le64_to_cpu(sb->resync_offset);
 		memcpy(mddev->uuid, sb->set_uuid, 16);
 
-		mddev->max_disks =  (4096-256)/2;
+		mddev->max_disks = MAX_RAID_DISKS;
 
 		if (!mddev->logical_block_size)
 			mddev->logical_block_size = le32_to_cpu(sb->logical_block_size);
diff --git a/drivers/md/md.h b/drivers/md/md.h
index a083f37374d0..6a4af4a1959c 100644
--- a/drivers/md/md.h
+++ b/drivers/md/md.h
@@ -22,6 +22,7 @@
 #include <trace/events/block.h>
 
 #define MaxSector (~(sector_t)0)
+#define MAX_RAID_DISKS ((4096-256)/2)
 
 enum md_submodule_type {
 	MD_PERSONALITY = 0,
-- 
2.39.2
Re: [RFC PATCH 4/5] md: introduce MAX_RAID_DISKS macro to replace magic number
Posted by John Stoffel 1 month, 1 week ago
>>>>> "Zheng" == Zheng Qixing <zhengqixing@huaweicloud.com> writes:

> From: Zheng Qixing <zhengqixing@huawei.com>

> Define MAX_RAID_DISKS macro for the maximum number of RAID disks.
> No functional change.

> Signed-off-by: Zheng Qixing <zhengqixing@huawei.com>
> ---
>  drivers/md/md.c | 4 ++--
>  drivers/md/md.h | 1 +
>  2 files changed, 3 insertions(+), 2 deletions(-)

> diff --git a/drivers/md/md.c b/drivers/md/md.c
> index 9eeab5258189..d2f136706f6c 100644
> --- a/drivers/md/md.c
> +++ b/drivers/md/md.c
> @@ -1888,7 +1888,7 @@ static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_
 
>  	if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
sb-> major_version != cpu_to_le32(1) ||
> -	    le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
> +	    le32_to_cpu(sb->max_dev) > MAX_RAID_DISKS ||
>  	    le64_to_cpu(sb->super_offset) != rdev->sb_start ||
>  	    (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
>  		return -EINVAL;
> @@ -2065,7 +2065,7 @@ static int super_1_validate(struct mddev *mddev, struct md_rdev *freshest, struc
mddev-> resync_offset = le64_to_cpu(sb->resync_offset);
>  		memcpy(mddev->uuid, sb->set_uuid, 16);
 
> -		mddev->max_disks =  (4096-256)/2;
> +		mddev->max_disks = MAX_RAID_DISKS;
 
>  		if (!mddev->logical_block_size)
mddev-> logical_block_size = le32_to_cpu(sb->logical_block_size);
> diff --git a/drivers/md/md.h b/drivers/md/md.h
> index a083f37374d0..6a4af4a1959c 100644
> --- a/drivers/md/md.h
> +++ b/drivers/md/md.h
> @@ -22,6 +22,7 @@
>  #include <trace/events/block.h>
 
>  #define MaxSector (~(sector_t)0)
> +#define MAX_RAID_DISKS ((4096-256)/2)

Looks fine to me, except there's no explanation for the magic numbers
here.  Sure, it's 1916 devices max, but WHY?  Other than that nit,
looks fine.
Re: [RFC PATCH 4/5] md: introduce MAX_RAID_DISKS macro to replace magic number
Posted by Zheng Qixing 1 month ago
在 2026/1/1 2:00, John Stoffel 写道:

>>   #define MaxSector (~(sector_t)0)
>> +#define MAX_RAID_DISKS ((4096-256)/2)
> Looks fine to me, except there's no explanation for the magic numbers
> here.  Sure, it's 1916 devices max, but WHY?  Other than that nit,
> looks fine.
>
In include/uapi/linux/raid/md_p.h :
/*
  * The version-1 superblock :
  * All numeric fields are little-endian.
  *
  * total size: 256 bytes plus 2 per device.
  *  1K allows 384 devices.
  */
struct mdp_superblock_1 {


1.x superblock:

Per-device state is stored as a __u16 dev_roles[] array (2 bytes per 
device) plus a fixed 256-byte header, still within a 4 KiB superblock. 
Therefore the theoretical maximum is (4096 - 256) / 2 = 1920 entries.

0.90 superblock (27 devices):

The superblock is fixed at 4 KiB (1024 32-bit words). It must store 
several fixed sections (generic + personality) and also reserves space 
for one “this_disk” descriptor in addition to the per-member disks[] 
table. Each member descriptor consumes 32 words = 128 bytes. After 
accounting for the fixed sections and the extra descriptor, the 
remaining space fits exactly 27 member descriptors.


Best regards,

Qixing