Skip to contentSkip to conversation

md: it panice after reshape from raid1 to raid5

PATCH In review Xiao Ni 2 participants 6 messages Dec 10, 2021 – Jan 6, 2022 MD RAID

lore
Share & tools
lore

Xiao Ni

author
#0 lore raw

cover md: it panice after reshape from raid1 to raid5

Hi all

After reshape from raid1 to raid5, it can panice when there are I/Os

The steps can reproduce this:
mdadm -CR /dev/md0 -l1 -n2 /dev/loop0 /dev/loop1
mdadm --wait /dev/md0
mkfs.xfs /dev/md0
mdadm /dev/md0 --grow -l5
mount /dev/md0 /mnt

These two patches fix this problem.

Xiao Ni (2):
Free r0conf memory when register integrity failed Move alloc/free acct bioset in to personality

 drivers/md/md.c    | 27 +++++++++++++++++----------
 drivers/md/md.h    |  2 ++
 drivers/md/raid0.c | 28 ++++++++++++++++++++++++----
 drivers/md/raid5.c | 41 ++++++++++++++++++++++++++++++-----------
 4 files changed, 73 insertions(+), 25 deletions(-)
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH v2 1/2 md/raid0: Free r0conf memory when register integrity failed

It doesn't free memory when register integrity failed. And it will add acct_bioset_exit in raid0_free. So split free r0conf codes into a single function to make error handling more clear.

---
V2: set mddev->private to NULL and move free_conf/raid0_free above to avoid the extra declaration

---
 drivers/md/raid0.c | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)
drivers/md/raid0.c +19 -9
index 62c8b6adac70..88424d7a6ebd 100644
@@ -356,7 +356,20 @@ static sector_t raid0_size(struct mddev *mddev, sector_t sectors, int raid_disks
 	return array_sectors;
 }
 
-static void raid0_free(struct mddev *mddev, void *priv);
+static void free_conf(struct mddev *mddev, struct r0conf *conf)
+{
+	kfree(conf->strip_zone);
+	kfree(conf->devlist);
+	kfree(conf);
+	mddev->private = NULL;
+}
+
+static void raid0_free(struct mddev *mddev, void *priv)
+{
+	struct r0conf *conf = priv;
+
+	free_conf(mddev, conf);
+}
 
 static int raid0_run(struct mddev *mddev)
 {
@@ -413,17 +426,14 @@ static int raid0_run(struct mddev *mddev)
 	dump_zones(mddev);
 
 	ret = md_integrity_register(mddev);
+	if (ret)
+		goto free;
 
 	return ret;
-}
-
-static void raid0_free(struct mddev *mddev, void *priv)
-{
-	struct r0conf *conf = priv;
 
-	kfree(conf->strip_zone);
-	kfree(conf->devlist);
-	kfree(conf);
+free:
+	free_conf(mddev, conf);
+	return ret;
 }
 
 static void raid0_handle_discard(struct mddev *mddev, struct bio *bio)
Signature · 1 line
2.31.1
author
#2 lore raw

PATCH 2/2 md: Move alloc/free acct bioset in to personality

In reply to Xiao Ni

Now it alloc acct bioset in md_run and only raid0/raid5 need acct bioset. For example, it doesn't create acct bioset when creating raid1. Then reshape from raid1 to raid0/raid5, it will access acct bioset after reshaping. It can panic because of NULL pointer reference.

We can move alloc/free jobs to personality. pers->run alloc acct bioset and pers->clean free it.

Fixes daee2024715d (md: check level before create and exit io_acct_set)
---
 drivers/md/md.c    | 27 +++++++++++++++++----------
 drivers/md/md.h    |  2 ++
 drivers/md/raid0.c | 10 +++++++++-
 drivers/md/raid5.c | 41 ++++++++++++++++++++++++++++++-----------
 4 files changed, 58 insertions(+), 22 deletions(-)
drivers/md/md.c +17 -10
index e8666bdc0d28..0fc34a05a655 100644
@@ -5878,13 +5878,6 @@ int md_run(struct mddev *mddev)
 		if (err)
 			goto exit_bio_set;
 	}
-	if (mddev->level != 1 && mddev->level != 10 &&
-	    !bioset_initialized(&mddev->io_acct_set)) {
-		err = bioset_init(&mddev->io_acct_set, BIO_POOL_SIZE,
-				  offsetof(struct md_io_acct, bio_clone), 0);
-		if (err)
-			goto exit_sync_set;
-	}
 
 	spin_lock(&pers_lock);
 	pers = find_pers(mddev->level, mddev->clevel);
@@ -6061,9 +6054,6 @@ int md_run(struct mddev *mddev)
 	module_put(pers->owner);
 	md_bitmap_destroy(mddev);
 abort:
-	if (mddev->level != 1 && mddev->level != 10)
-		bioset_exit(&mddev->io_acct_set);
-exit_sync_set:
 	bioset_exit(&mddev->sync_set);
 exit_bio_set:
 	bioset_exit(&mddev->bio_set);
@@ -8596,6 +8586,23 @@ void md_submit_discard_bio(struct mddev *mddev, struct md_rdev *rdev,
 }
 EXPORT_SYMBOL_GPL(md_submit_discard_bio);
 
+int acct_bioset_init(struct mddev *mddev)
+{
+	int err = 0;
+
+	if (!bioset_initialized(&mddev->io_acct_set))
+		err = bioset_init(&mddev->io_acct_set, BIO_POOL_SIZE,
+			offsetof(struct md_io_acct, bio_clone), 0);
+	return err;
+}
+EXPORT_SYMBOL_GPL(acct_bioset_init);
+
+void acct_bioset_exit(struct mddev *mddev)
+{
+	bioset_exit(&mddev->io_acct_set);
+}
+EXPORT_SYMBOL_GPL(acct_bioset_exit);
+
 static void md_end_io_acct(struct bio *bio)
 {
 	struct md_io_acct *md_io_acct = bio->bi_private;
drivers/md/md.h +2 -0
index 53ea7a6961de..f1bf3625ef4c 100644
@@ -721,6 +721,8 @@ extern void md_error(struct mddev *mddev, struct md_rdev *rdev);
 extern void md_finish_reshape(struct mddev *mddev);
 void md_submit_discard_bio(struct mddev *mddev, struct md_rdev *rdev,
 			struct bio *bio, sector_t start, sector_t size);
+int acct_bioset_init(struct mddev *mddev);
+void acct_bioset_exit(struct mddev *mddev);
 void md_account_bio(struct mddev *mddev, struct bio **bio);
 
 extern bool __must_check md_flush_request(struct mddev *mddev, struct bio *bio);
drivers/md/raid0.c +9 -1
index 88424d7a6ebd..b59a77b31b90 100644
@@ -369,6 +369,7 @@ static void raid0_free(struct mddev *mddev, void *priv)
 	struct r0conf *conf = priv;
 
 	free_conf(mddev, conf);
+	acct_bioset_exit(mddev);
 }
 
 static int raid0_run(struct mddev *mddev)
@@ -383,11 +384,16 @@ static int raid0_run(struct mddev *mddev)
 	if (md_check_no_bitmap(mddev))
 		return -EINVAL;
 
+	if (acct_bioset_init(mddev)) {
+		pr_err("md/raid0:%s: alloc acct bioset failed.\n", mdname(mddev));
+		return -ENOMEM;
+	}
+
 	/* if private is not null, we are here after takeover */
 	if (mddev->private == NULL) {
 		ret = create_strip_zones(mddev, &conf);
 		if (ret < 0)
-			return ret;
+			goto exit_acct_set;
 		mddev->private = conf;
 	}
 	conf = mddev->private;
@@ -433,6 +439,8 @@ static int raid0_run(struct mddev *mddev)
 
 free:
 	free_conf(mddev, conf);
+exit_acct_set:
+	acct_bioset_exit(mddev);
 	return ret;
 }
 
drivers/md/raid5.c +30 -11

104 lines not on this page · raw message

Signature · 1 line
2.31.1
author
#3 lore raw
In reply to Xiao Ni

Hi Song

Ping. Do I still change something else?

Regards

Xiao

Quoted 23 lines · Xiao Ni

Hi all

After reshape from raid1 to raid5, it can panice when there are I/Os

The steps can reproduce this:
mdadm -CR /dev/md0 -l1 -n2 /dev/loop0 /dev/loop1
mdadm --wait /dev/md0
mkfs.xfs /dev/md0
mdadm /dev/md0 --grow -l5
mount /dev/md0 /mnt

These two patches fix this problem.

Xiao Ni (2):
Free r0conf memory when register integrity failed Move alloc/free acct bioset in to personality

  drivers/md/md.c    | 27 +++++++++++++++++----------
  drivers/md/md.h    |  2 ++
  drivers/md/raid0.c | 28 ++++++++++++++++++++++++----
  drivers/md/raid5.c | 41 ++++++++++++++++++++++++++++++-----------
  4 files changed, 73 insertions(+), 25 deletions(-)
maintainer
#4 lore raw
Quoted 4 lines · Xiao Ni

Hi Song

Ping. Do I still change something else?

I merged the two patches into one, rewrote the commit log, added Guoqing's Acked-by, and applied it to md-next.

For future patches, please write the commit log according to the guidance in Documentation/process/submitting-patches.rst.

Thanks,
Song

author
#5 lore raw
Quoted 7 lines · Song Liu
Quoted 4 lines · Xiao Ni

Hi Song

Ping. Do I still change something else?

Quoted 9 lines

I merged the two patches into one, rewrote the commit log, added Guoqing's Acked-by, and applied it to md-next.

For future patches, please write the commit log according to the guidance in Documentation/process/submitting-patches.rst.

Thanks,
Song

Thanks. I'll read this doc and follow the instructions.

Regards
Xiao