md: don't add empty badblocks record table in super_1_load()
PATCH 1/1 md: don't add empty badblocks record table in super_1_load()
From: Coly Li <[email protected]>
In super_1_load() when badblocks table is loaded from component disk, current code adds all records including empty ones into in-memory badblocks table. Because empty record's sectors count is 0, calling badblocks_set() with parameter sectors=0 will return -EINVAL. This isn't expected behavior and adding a correct component disk into the array will incorrectly fail.
This patch fixes the issue by checking the badblock record before call- ing badblocks_set(). If this badblock record is empty (bb == 0), then skip this one and continue to try next bad record.
--- drivers/md/md.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/md/md.c +8 -3
@@ -1873,9 +1873,14 @@ static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_ bbp = (__le64 *)page_address (rdev->bb_page); rdev->badblocks.shift = sb->bblog_shift;for (i =0 ; i < (sectors << (9 -3 )) ; i++, bbp++) { -u64 bb =le64_to_cpu (*bbp); -int count = bb & (0x3ff ); -u64 sector = bb >>10 ; +u64 bb, sector; +int count; + + bb =le64_to_cpu (*bbp); +if (bb ==0 ) +continue ; + count = bb & (0x3ff ); + sector = bb >>10 ; sector <<= sb->bblog_shift; count <<= sb->bblog_shift;if (bb +1 ==0 )
Signature · 1 line
2.47.3
Dear Coly,
Thank you for your patch.
Am 05.10.25 um 18:21 schrieb [email protected]:
Quoted 9 lines
From: Coly Li <[email protected]>
In
super_1_load()when badblocks table is loaded from component disk, current code adds all records including empty ones into in-memory badblocks table. Because empty record's sectors count is 0, callingbadblocks_set()with parameter sectors=0 will return-EINVAL. This isn't expected behavior and adding a correct component disk into the array will incorrectly fail.
Quoted 3 lines
This patch fixes the issue by checking the badblock record before call- ing
badblocks_set(). If this badblock record is empty (bb == 0), then skip this one and continue to try next bad record.
It’d be great if you added the commands to reproduce this.
Quoted 17 lines
Signed-off-by Coly Li <[email protected]>--- drivers/md/md.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)drivers/md/md.c +1
-3@@ -1873,9 +1873,14 @@ static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_ bbp = (__le64 *)page_address (rdev->bb_page); rdev->badblocks.shift = sb->bblog_shift;for (i =0 ; i < (sectors << (9 -3 )) ; i++, bbp++) { -u64 bb =le64_to_cpu (*bbp); -int count = bb & (0x3ff ); -u64 sector = bb >>10 ; +u64 bb, sector;
drivers/md/md.c static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_ · line 1886
Quoted 10 lines
drivers/md/md.c +7
-0+int count; + + bb =le64_to_cpu (*bbp); +if (bb ==0 ) +continue ; + count = bb & (0x3ff ); + sector = bb >>10 ; sector <<= sb->bblog_shift; count <<= sb->bblog_shift;if (bb +1 ==0 )
Kind regards,
Paul
Quoted 18 lines · Paul Menzel
Dear Coly,
Thank you for your patch.
Am 05.10.25 um 18:21 schrieb [email protected]:
Quoted 12 lines
From: Coly Li <[email protected]>
In
super_1_load()when badblocks table is loaded from component disk, current code adds all records including empty ones into in-memory badblocks table. Because empty record's sectors count is 0, callingbadblocks_set()with parameter sectors=0 will return-EINVAL. This isn't expected behavior and adding a correct component disk into the array will incorrectly fail.This patch fixes the issue by checking the badblock record before call- ing
badblocks_set(). If this badblock record is empty (bb == 0), then skip this one and continue to try next bad record.
Hi Paul,
Quoted 3 lines
It’d be great if you added the commands to reproduce this.
It is reported by a online testing system, this is a md raid5 array with 5 disks. What I can observe is, one of the component disks has around 200 badblocks records. If another component disk is failed and removed from the array, this raid5 array will fail to add another disk to replace the failed one.
The reason is described in commit log, badblocks_set() refuses to add an empty record into its badblocks table.
I also tried to write to badblocks sysfs interface e.g.
/sys/block/md0/md/dev-sdx1/badblocks
but failed to reproduce the observed failed behavior. I guess it was becasue my operation didn't trigger super_1_sync() to be called.
Why this component disk has so many badblocks record? I don't think this disk drive is broken, it might be from a previous obsrved incompatible issue between this hard drive and the mpt3sas driver of the HBA card.
After applying this patch, another disk can replace the failed-and-removed hard drive. But at this moment I don't reproduce such failure on other md raid5 array yet.
Quoted 21 lines
Quoted 21 lines
Signed-off-by Coly Li <[email protected]>--- drivers/md/md.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)drivers/md/md.c +5
-3@@ -1873,9 +1873,14 @@ static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_ bbp = (__le64 *)page_address (rdev->bb_page); rdev->badblocks.shift = sb->bblog_shift;for (i =0 ; i < (sectors << (9 -3 )) ; i++, bbp++) { -u64 bb =le64_to_cpu (*bbp); -int count = bb & (0x3ff ); -u64 sector = bb >>10 ; +u64 bb, sector; +int count; + + bb =le64_to_cpu (*bbp); +if (bb ==0 )
Quoted 8 lines
drivers/md/md.c static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_ · line 1886
Quoted 6 lines
drivers/md/md.c +3
-0+continue ; + count = bb & (0x3ff ); + sector = bb >>10 ; sector <<= sb->bblog_shift; count <<= sb->bblog_shift;if (bb +1 ==0 )Reviewed-by Paul Menzel <[email protected]>
Thanks for the review.
Coly Li
Hi,
Quoted 9 lines · 在 2025/10/6 0:21, [email protected] 写道:
From: Coly Li <[email protected]>
In
super_1_load()when badblocks table is loaded from component disk, current code adds all records including empty ones into in-memory badblocks table. Because empty record's sectors count is 0, callingbadblocks_set()with parameter sectors=0 will return-EINVAL. This isn't expected behavior and adding a correct component disk into the array will incorrectly fail.
Quoted 5 lines
This patch fixes the issue by checking the badblock record before call- ing
badblocks_set(). If this badblock record is empty (bb == 0), then skip this one and continue to try next bad record.Signed-off-by Coly Li <[email protected]>
This problem is from user space tools, the flag MD_FEATURE_BAD_BLCOKS means that the rdev have badblocks presents, and this flag should not be set in the superblock for new disk.
Please take a look at following mdadm commit, and see if your problem still exist.
commit 4e2e208c8d3e9ba0fae88136d7c4cd0292af73b0
Author: Wu Guanghao <[email protected]> Date: Tue Mar 11 03:11:55 2025 +0000
super1: Clear extra flags when initializing metadata
Thanks,
Kuai
Quoted 26 lines
--- drivers/md/md.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)drivers/md/md.c +8
-3@@ -1873,9 +1873,14 @@ static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_ bbp = (__le64 *)page_address (rdev->bb_page); rdev->badblocks.shift = sb->bblog_shift;for (i =0 ; i < (sectors << (9 -3 )) ; i++, bbp++) { -u64 bb =le64_to_cpu (*bbp); -int count = bb & (0x3ff ); -u64 sector = bb >>10 ; +u64 bb, sector; +int count; + + bb =le64_to_cpu (*bbp); +if (bb ==0 ) +continue ; + count = bb & (0x3ff ); + sector = bb >>10 ; sector <<= sb->bblog_shift; count <<= sb->bblog_shift;if (bb +1 ==0 )
Quoted 22 lines · Yu Kuai
Hi,
Quoted 9 lines · 在 2025/10/6 0:21, [email protected] 写道:
From: Coly Li <[email protected]>
In
super_1_load()when badblocks table is loaded from component disk, current code adds all records including empty ones into in-memory badblocks table. Because empty record's sectors count is 0, callingbadblocks_set()with parameter sectors=0 will return-EINVAL. This isn't expected behavior and adding a correct component disk into the array will incorrectly fail.Quoted 5 lines
This patch fixes the issue by checking the badblock record before call- ing
badblocks_set(). If this badblock record is empty (bb == 0), then skip this one and continue to try next bad record.Signed-off-by Coly Li <[email protected]>This problem is from user space tools, the flag
MD_FEATURE_BAD_BLCOKSmeans that the rdev have badblocks presents, and this flag should not be set in the superblock for new disk.
Quoted 10 lines
Please take a look at following mdadm commit, and see if your problem still exist.
commit
4e2e208c8d3e9ba0fae88136d7c4cd0292af73b0Author: Wu Guanghao <[email protected]> Date: Tue Mar 11 03:11:55 2025 +0000super1: Clear extra flags when initializing metadata
I feel this patch doesn't go into the debian 12 package.
Yeah, it is also necessary to fix kernel part, in case of old user space installtion with updated stable kernel.
Thanks for the hint.
Coly Li
Quoted 27 lines
Quoted 27 lines
--- drivers/md/md.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)drivers/md/md.c +8
-3@@ -1873,9 +1873,14 @@ static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_ bbp = (__le64 *)page_address (rdev->bb_page); rdev->badblocks.shift = sb->bblog_shift;for (i =0 ; i < (sectors << (9 -3 )) ; i++, bbp++) { -u64 bb =le64_to_cpu (*bbp); -int count = bb & (0x3ff ); -u64 sector = bb >>10 ; +u64 bb, sector; +int count; + + bb =le64_to_cpu (*bbp); +if (bb ==0 ) +continue ; + count = bb & (0x3ff ); + sector = bb >>10 ; sector <<= sb->bblog_shift; count <<= sb->bblog_shift;if (bb +1 ==0 )
Quoted 25 lines · 在 2025/10/6 0:21, [email protected] 写道:
From: Coly Li <[email protected]>
In
super_1_load()when badblocks table is loaded from component disk, current code adds all records including empty ones into in-memory badblocks table. Because empty record's sectors count is 0, callingbadblocks_set()with parameter sectors=0 will return-EINVAL. This isn't expected behavior and adding a correct component disk into the array will incorrectly fail.This patch fixes the issue by checking the badblock record before call- ing
badblocks_set(). If this badblock record is empty (bb == 0), then skip this one and continue to try next bad record.Signed-off-by Coly Li <[email protected]>--- drivers/md/md.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)drivers/md/md.c +0
-0@@ -1873,9 +1873,14 @@ static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_ bbp = (__le64 *)page_address (rdev->bb_page); rdev->badblocks.shift = sb->bblog_shift;
drivers/md/md.c static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_ · line 1881
Quoted 10 lines
drivers/md/md.c +6
-3for (i =0 ; i < (sectors << (9 -3 )) ; i++, bbp++) { -u64 bb =le64_to_cpu (*bbp); -int count = bb & (0x3ff ); -u64 sector = bb >>10 ; +u64 bb, sector; +int count; + + bb =le64_to_cpu (*bbp); +if (bb ==0 ) +continue ;
Can we just break:
if (bb + 1 == 0 || bb == 0)
break;
Otherwise LGTM.
Quoted 5 lines
drivers/md/md.c +2
-0+ count = bb & (0x3ff ); + sector = bb >>10 ; sector <<= sb->bblog_shift; count <<= sb->bblog_shift;if (bb +1 ==0 )
Signature · 2 lines
Thanks, Nan
Quoted 41 lines · Li Nan
Quoted 25 lines · 在 2025/10/6 0:21, [email protected] 写道:
From: Coly Li <[email protected]>
In
super_1_load()when badblocks table is loaded from component disk, current code adds all records including empty ones into in-memory badblocks table. Because empty record's sectors count is 0, callingbadblocks_set()with parameter sectors=0 will return-EINVAL. This isn't expected behavior and adding a correct component disk into the array will incorrectly fail.This patch fixes the issue by checking the badblock record before call- ing
badblocks_set(). If this badblock record is empty (bb == 0), then skip this one and continue to try next bad record.Signed-off-by Coly Li <[email protected]>--- drivers/md/md.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)drivers/md/md.c +0
-0@@ -1873,9 +1873,14 @@ static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_ bbp = (__le64 *)page_address (rdev->bb_page); rdev->badblocks.shift = sb->bblog_shift;drivers/md/md.c static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_ · line 1881
Quoted 10 lines
drivers/md/md.c +6
-3for (i =0 ; i < (sectors << (9 -3 )) ; i++, bbp++) { -u64 bb =le64_to_cpu (*bbp); -int count = bb & (0x3ff ); -u64 sector = bb >>10 ; +u64 bb, sector; +int count; + + bb =le64_to_cpu (*bbp); +if (bb ==0 ) +continue ;Can we just break:
Quoted 3 lines
if (bb + 1 == 0 || bb == 0) break;
I just realized even the badblock feature bit is set, it is also possible to have empty or cleared badblock record in the badblock table. So ignore the empty record is necessary.
An empty record could appear in any location inside the badblock table, ignore it by 'continue' is correct. If there is available badblock record after this empty record, 'break' will lose the available and non-empty record.
Quoted 1 line
Otherwise LGTM.
Thanks for the review.
Quoted 6 lines
drivers/md/md.c static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_ · line 1886
Quoted 6 lines
drivers/md/md.c +2
-0+ count = bb & (0x3ff ); + sector = bb >>10 ; sector <<= sb->bblog_shift; count <<= sb->bblog_shift;if (bb +1 ==0 )
Coly Li
Quoted 46 lines · Coly Li
Quoted 41 lines · Li Nan
Quoted 25 lines · 在 2025/10/6 0:21, [email protected] 写道:
From: Coly Li <[email protected]>
In
super_1_load()when badblocks table is loaded from component disk, current code adds all records including empty ones into in-memory badblocks table. Because empty record's sectors count is 0, callingbadblocks_set()with parameter sectors=0 will return-EINVAL. This isn't expected behavior and adding a correct component disk into the array will incorrectly fail.This patch fixes the issue by checking the badblock record before call- ing
badblocks_set(). If this badblock record is empty (bb == 0), then skip this one and continue to try next bad record.Signed-off-by Coly Li <[email protected]>--- drivers/md/md.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)drivers/md/md.c +0
-0@@ -1873,9 +1873,14 @@ static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_ bbp = (__le64 *)page_address (rdev->bb_page); rdev->badblocks.shift = sb->bblog_shift;drivers/md/md.c static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_ · line 1881
Quoted 10 lines
drivers/md/md.c +6
-3for (i =0 ; i < (sectors << (9 -3 )) ; i++, bbp++) { -u64 bb =le64_to_cpu (*bbp); -int count = bb & (0x3ff ); -u64 sector = bb >>10 ; +u64 bb, sector; +int count; + + bb =le64_to_cpu (*bbp); +if (bb ==0 ) +continue ;Can we just break:
Quoted 3 lines
if (bb + 1 == 0 || bb == 0) break;
Quoted 8 lines
I just realized even the badblock feature bit is set, it is also possible to have empty or cleared badblock record in the badblock table. So ignore the empty record is necessary.
An empty record could appear in any location inside the badblock table, ignore it by 'continue' is correct. If there is available badblock record after this empty record, 'break' will lose the available and non-empty record.
I'm not sure how an empty record can occur, as the commit log says:
Quoted 15 lines
calling
badblocks_set()with parameter sectors=0 will return-EINVALQuoted 1 line
Otherwise LGTM.
Thanks for the review.
Quoted 6 lines
drivers/md/md.c static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_ · line 1886
Quoted 6 lines
drivers/md/md.c +2
-0+ count = bb & (0x3ff ); + sector = bb >>10 ; sector <<= sb->bblog_shift; count <<= sb->bblog_shift;if (bb +1 ==0 )Coly Li
.
Signature · 2 lines
Thanks, Nan
Quoted 53 lines · Li Nan
Quoted 50 lines · Coly Li
Quoted 41 lines · Li Nan
Quoted 25 lines · 在 2025/10/6 0:21, [email protected] 写道:
From: Coly Li <[email protected]>
In
super_1_load()when badblocks table is loaded from component disk, current code adds all records including empty ones into in-memory badblocks table. Because empty record's sectors count is 0, callingbadblocks_set()with parameter sectors=0 will return-EINVAL. This isn't expected behavior and adding a correct component disk into the array will incorrectly fail.This patch fixes the issue by checking the badblock record before call- ing
badblocks_set(). If this badblock record is empty (bb == 0), then skip this one and continue to try next bad record.Signed-off-by Coly Li <[email protected]>--- drivers/md/md.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)drivers/md/md.c +0
-0@@ -1873,9 +1873,14 @@ static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_ bbp = (__le64 *)page_address (rdev->bb_page); rdev->badblocks.shift = sb->bblog_shift;drivers/md/md.c static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_ · line 1881
Quoted 10 lines
drivers/md/md.c +6
-3for (i =0 ; i < (sectors << (9 -3 )) ; i++, bbp++) { -u64 bb =le64_to_cpu (*bbp); -int count = bb & (0x3ff ); -u64 sector = bb >>10 ; +u64 bb, sector; +int count; + + bb =le64_to_cpu (*bbp); +if (bb ==0 ) +continue ;Can we just break:
Quoted 3 lines
if (bb + 1 == 0 || bb == 0) break;I just realized even the badblock feature bit is set, it is also possible to have empty or cleared badblock record in the badblock table. So ignore the empty record is necessary.
Quoted 9 lines
Quoted 4 lines
An empty record could appear in any location inside the badblock table, ignore it by 'continue' is correct. If there is available badblock record after this empty record, 'break' will lose the available and non-empty record.
I'm not sure how an empty record can occur, as the commit log says:
Quoted 1 line
calling
badblocks_set()with parameter sectors=0 will return-EINVAL
Yes you are correct. There is no empty record inside the badblocks table. Yes, a break is better. Then even user space is still use old mdadm version, people may still add the disk into array with patched kernel, and the incorrect meta- data even has no chance to call badblocks_set() and is ignored directly.
Thanks for the comments.
Coly Li