Skip to contentSkip to conversation

md/raid1: fix len reuse across rdevs in choose_first_rdev()

PATCH In review Abd-Alrhman Masalkhi 2 participants 3 messages Apr 26 – Apr 28 MD RAID

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 md/raid1: fix len reuse across rdevs in choose_first_rdev()

choose_first_rdev() initializes the variable len before iterating over all rdevs, but passes it by reference to raid1_check_read_range(), which it might update *len and return 0 depending on the layout of the bad block region. As a result, 'len' can be modified during the first iteration and reused for subsequent rdevs, causing later devices to be evaluated with an incorrect length value.

Fixes 31a73331752d3 ("md/raid1: factor out read_first_rdev() from read_balance()")
---
 drivers/md/raid1.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/raid1.c +1 -1
index b549be9174bb..5f5dbf79c903 100644
@@ -591,12 +591,12 @@ static int choose_first_rdev(struct r1conf *conf, struct r1bio *r1_bio,
 			     int *max_sectors)
 {
 	sector_t this_sector = r1_bio->sector;
-	int len = r1_bio->sectors;
 	int disk;
 
 	for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) {
 		struct md_rdev *rdev;
 		int read_len;
+		int len = r1_bio->sectors;
 
 		if (r1_bio->bios[disk] == IO_BLOCKED)
 			continue;
Signature · 1 line
2.43.0
#1 lore raw

Hi,

Quoted 21 lines · Abd-Alrhman Masalkhi

choose_first_rdev() initializes the variable len before iterating over all rdevs, but passes it by reference to raid1_check_read_range(), which it might update *len and return 0 depending on the layout of the bad block region. As a result, 'len' can be modified during the first iteration and reused for subsequent rdevs, causing later devices to be evaluated with an incorrect length value.

Fixes 31a73331752d3 ("md/raid1: factor out read_first_rdev() from read_balance()")
---
  drivers/md/raid1.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/raid1.c +0 -0
index b549be9174bb..5f5dbf79c903 100644
@@ -591,12 +591,12 @@ static int choose_first_rdev(struct r1conf *conf, struct r1bio *r1_bio,
  			     int *max_sectors)
  {
  	sector_t this_sector = r1_bio->sector;

drivers/md/raid1.c static int choose_first_rdev(struct r1conf *conf, struct r1bio *r1_bio, · line 602

Quoted 10 lines
drivers/md/raid1.c +1 -1
-	int len = r1_bio->sectors;
  	int disk;
 
  	for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) {
  		struct md_rdev *rdev;
  		int read_len;
+		int len = r1_bio->sectors;
 
  		if (r1_bio->bios[disk] == IO_BLOCKED)
  			continue;

This patch is wrong, choose_first_rdev() is used when raid1_should_read_first() is true, meaning the read overlaps an unsynced/resyncing area. Reset len can cause the problem that reading the same area can return different data.

Signature · 2 lines
Thansk,
Kuai
#2 lore raw

Hi Kaui,

Quoted 32 lines · Yu Kuai

Hi,

Quoted 29 lines · Abd-Alrhman Masalkhi

choose_first_rdev() initializes the variable len before iterating over all rdevs, but passes it by reference to raid1_check_read_range(), which it might update *len and return 0 depending on the layout of the bad block region. As a result, 'len' can be modified during the first iteration and reused for subsequent rdevs, causing later devices to be evaluated with an incorrect length value.

Fixes 31a73331752d3 ("md/raid1: factor out read_first_rdev() from read_balance()")
---
  drivers/md/raid1.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/raid1.c +1 -1
index b549be9174bb..5f5dbf79c903 100644
@@ -591,12 +591,12 @@ static int choose_first_rdev(struct r1conf *conf, struct r1bio *r1_bio,
  			     int *max_sectors)
  {
  	sector_t this_sector = r1_bio->sector;
-	int len = r1_bio->sectors;
  	int disk;
 
  	for (disk = 0 ; disk < conf->raid_disks * 2 ; disk++) {
  		struct md_rdev *rdev;
  		int read_len;
+		int len = r1_bio->sectors;
 
Quoted 7 lines

drivers/md/raid1.c static int choose_first_rdev(struct r1conf *conf, struct r1bio *r1_bio, · line 602

Quoted 2 lines
drivers/md/raid1.c +0 -0
  		if (r1_bio->bios[disk] == IO_BLOCKED)
  			continue;

This patch is wrong, choose_first_rdev() is used when raid1_should_read_first() is true, meaning the read overlaps an unsynced/resyncing area. Reset len can cause the problem that reading the same area can return different data.

Thank you for the detailed explanation. After carefully re-reading the code and your feedback, I understand why the patch is wrong.

Quoted 3 lines
Signature · 2 lines
Thansk,
Kuai
Signature · 2 lines
Best Regards,
Abd-Alrhman