Skip to contentSkip to conversation

md/raid10: initialize r10_bio->read_slot before use.

PATCH Superseded Kevin Vigor 2 participants 5 messages Nov 5, 2020 – Nov 9, 2020 1 message not in the archive MD RAID

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 md/raid10: initialize r10_bio->read_slot before use.

In __make_request() a new r10bio is allocated and passed to raid10_read_request(). The read_slot member of the bio is not initialized, and the raid10_read_request() uses it to index an array. This leads to occasional panics.

Fix by initializing the field to invalid value and checking for valid value in raid10_read_request().

---
 drivers/md/raid10.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/md/raid10.c +2 -1
index 8a1354a08a1a..64b1306b0c4a 100644
@@ -1143,7 +1143,7 @@ static void raid10_read_request(struct mddev *mddev, struct bio *bio,
 	struct md_rdev *err_rdev = NULL;
 	gfp_t gfp = GFP_NOIO;
 
-	if (r10_bio->devs[slot].rdev) {
+	if (slot >= 0 && r10_bio->devs[slot].rdev) {
 		/*
 		 * This is an error retry, but we cannot
 		 * safely dereference the rdev in the r10_bio,
@@ -1508,6 +1508,7 @@ static void __make_request(struct mddev *mddev, struct bio *bio, int sectors)
 	r10_bio->mddev = mddev;
 	r10_bio->sector = bio->bi_iter.bi_sector;
 	r10_bio->state = 0;
+	r10_bio->read_slot = -1;
 	memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) * conf->copies);
 
 	if (bio_data_dir(bio) == READ)
Signature · 1 line
2.26.2
maintainer
#1 lore raw
Quoted 10 lines · Kevin Vigor

In __make_request() a new r10bio is allocated and passed to raid10_read_request(). The read_slot member of the bio is not initialized, and the raid10_read_request() uses it to index an array. This leads to occasional panics.

Fix by initializing the field to invalid value and checking for valid value in raid10_read_request().

Thanks for the fix!

I am having problem apply this patch, could you please rebase against

git://git.kernel.org/pub/scm/linux/kernel/git/song/md.git md-next

Quoted 9 lines
---
 drivers/md/raid10.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/md/raid10.c +0 -0
index 8a1354a08a1a..64b1306b0c4a 100644

drivers/md/raid10.c static void raid10_read_request(struct mddev *mddev, struct bio *bio, · line 1146

Quoted 5 lines
drivers/md/raid10.c +1 -1
@@ -1143,7 +1143,7 @@ static void raid10_read_request(struct mddev *mddev, struct bio *bio,
        struct md_rdev *err_rdev = NULL;
        gfp_t gfp = GFP_NOIO;
 
-       if (r10_bio->devs[slot].rdev) {
+       if (slot >= 0 && r10_bio->devs[slot].rdev) {

If we set default read_slot = 0, we should not need this change, right?

Quoted 14 lines
drivers/md/raid10.c +1 -1
                /*
                 * This is an error retry, but we cannot
                 * safely dereference the rdev in the r10_bio,
@@ -1508,6 +1508,7 @@ static void __make_request(struct mddev *mddev, struct bio *bio, int sectors)
        r10_bio->mddev = mddev;
        r10_bio->sector = bio->bi_iter.bi_sector;
        r10_bio->state = 0;
+       r10_bio->read_slot = -1;
        memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) * conf->copies);
 
        if (bio_data_dir(bio) == READ)
--

2.26.2

#2 lore raw

PATCH v2 1/1 md/raid10: initialize r10_bio->read_slot before use.

In __make_request() a new r10bio is allocated and passed to raid10_read_request(). The read_slot member of the bio is not initialized, and the raid10_read_request() uses it to index an array. This leads to occasional panics.

Fix by initializing the field to invalid value and checking for valid value in raid10_read_request().

---
v2:
- rebase onto md-next

---
 drivers/md/raid10.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/md/raid10.c +2 -1
index b7bca6703df8..3153183b7772 100644
@@ -1127,7 +1127,7 @@ static void raid10_read_request(struct mddev *mddev, struct bio *bio,
 	struct md_rdev *err_rdev = NULL;
 	gfp_t gfp = GFP_NOIO;
 
-	if (r10_bio->devs[slot].rdev) {
+	if (slot >= 0 && r10_bio->devs[slot].rdev) {
 		/*
 		 * This is an error retry, but we cannot
 		 * safely dereference the rdev in the r10_bio,
@@ -1508,6 +1508,7 @@ static void __make_request(struct mddev *mddev, struct bio *bio, int sectors)
 	r10_bio->mddev = mddev;
 	r10_bio->sector = bio->bi_iter.bi_sector;
 	r10_bio->state = 0;
+	r10_bio->read_slot = -1;
 	memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) * conf->geo.raid_disks);
 
 	if (bio_data_dir(bio) == READ)
Signature · 1 line
2.26.2
maintainer
#3 lore raw
Quoted 22 lines · Kevin Vigor

In __make_request() a new r10bio is allocated and passed to raid10_read_request(). The read_slot member of the bio is not initialized, and the raid10_read_request() uses it to index an array. This leads to occasional panics.

Fix by initializing the field to invalid value and checking for valid value in raid10_read_request().

---
v2:
- rebase onto md-next

---
 drivers/md/raid10.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/md/raid10.c +0 -0
index b7bca6703df8..3153183b7772 100644

drivers/md/raid10.c static void raid10_read_request(struct mddev *mddev, struct bio *bio, · line 1130

Quoted 5 lines
drivers/md/raid10.c +1 -1
@@ -1127,7 +1127,7 @@ static void raid10_read_request(struct mddev *mddev, struct bio *bio,
        struct md_rdev *err_rdev = NULL;
        gfp_t gfp = GFP_NOIO;
 
-       if (r10_bio->devs[slot].rdev) {
+       if (slot >= 0 && r10_bio->devs[slot].rdev) {

How about we initialize read_slot to 0, and get rid of this check?

Thanks,
Song

A message that is not in the archive (sent off-list or not yet received): CAFVaERqog8s80npb-w2g9He50=8D4-qYdCr9GicN_PU=Q5mt=Q@mail.gmail.com

maintainer
#5 lore raw
Quoted 6 lines · Kevin Vigor
Quoted 2 lines

How about we initialize read_slot to 0, and get rid of this check?

That will definitely work properly since we memset the rdev to null in __make_request. But the read_slot is truly invalid until we call read_balance() successfully and it feels wrong to set it to a valid value even if it os harmless by accident. So I prefer to give it an explicitly invalid value.

But your solution saves one test in the issue path and a line of diff, so I will happily change if you prefer the zero solution.

I think initialized to -1 is safer in long run.

Thanks for the fix! Applied to md-next.