hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/md/raid10.c
....@@ -751,8 +751,16 @@
751751 disk = r10_bio->devs[slot].devnum;
752752 rdev = rcu_dereference(conf->mirrors[disk].replacement);
753753 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
754
- r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
754
+ r10_bio->devs[slot].addr + sectors >
755
+ rdev->recovery_offset) {
756
+ /*
757
+ * Read replacement first to prevent reading both rdev
758
+ * and replacement as NULL during replacement replace
759
+ * rdev.
760
+ */
761
+ smp_mb();
755762 rdev = rcu_dereference(conf->mirrors[disk].rdev);
763
+ }
756764 if (rdev == NULL ||
757765 test_bit(Faulty, &rdev->flags))
758766 continue;
....@@ -894,6 +902,7 @@
894902 else
895903 submit_bio_noacct(bio);
896904 bio = next;
905
+ cond_resched();
897906 }
898907 blk_finish_plug(&plug);
899908 } else
....@@ -1087,6 +1096,7 @@
10871096 else
10881097 submit_bio_noacct(bio);
10891098 bio = next;
1099
+ cond_resched();
10901100 }
10911101 kfree(plug);
10921102 }
....@@ -1346,9 +1356,15 @@
13461356
13471357 for (i = 0; i < conf->copies; i++) {
13481358 int d = r10_bio->devs[i].devnum;
1349
- struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
1350
- struct md_rdev *rrdev = rcu_dereference(
1351
- conf->mirrors[d].replacement);
1359
+ struct md_rdev *rdev, *rrdev;
1360
+
1361
+ rrdev = rcu_dereference(conf->mirrors[d].replacement);
1362
+ /*
1363
+ * Read replacement first to prevent reading both rdev and
1364
+ * replacement as NULL during replacement replace rdev.
1365
+ */
1366
+ smp_mb();
1367
+ rdev = rcu_dereference(conf->mirrors[d].rdev);
13521368 if (rdev == rrdev)
13531369 rrdev = NULL;
13541370 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
....@@ -2212,11 +2228,22 @@
22122228 {
22132229 struct r10conf *conf = mddev->private;
22142230 int d;
2215
- struct bio *wbio, *wbio2;
2231
+ struct bio *wbio = r10_bio->devs[1].bio;
2232
+ struct bio *wbio2 = r10_bio->devs[1].repl_bio;
2233
+
2234
+ /* Need to test wbio2->bi_end_io before we call
2235
+ * submit_bio_noacct as if the former is NULL,
2236
+ * the latter is free to free wbio2.
2237
+ */
2238
+ if (wbio2 && !wbio2->bi_end_io)
2239
+ wbio2 = NULL;
22162240
22172241 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
22182242 fix_recovery_read_error(r10_bio);
2219
- end_sync_request(r10_bio);
2243
+ if (wbio->bi_end_io)
2244
+ end_sync_request(r10_bio);
2245
+ if (wbio2)
2246
+ end_sync_request(r10_bio);
22202247 return;
22212248 }
22222249
....@@ -2225,14 +2252,6 @@
22252252 * and submit the write request
22262253 */
22272254 d = r10_bio->devs[1].devnum;
2228
- wbio = r10_bio->devs[1].bio;
2229
- wbio2 = r10_bio->devs[1].repl_bio;
2230
- /* Need to test wbio2->bi_end_io before we call
2231
- * submit_bio_noacct as if the former is NULL,
2232
- * the latter is free to free wbio2.
2233
- */
2234
- if (wbio2 && !wbio2->bi_end_io)
2235
- wbio2 = NULL;
22362255 if (wbio->bi_end_io) {
22372256 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
22382257 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio));
....@@ -2900,10 +2919,6 @@
29002919 sector_t chunk_mask = conf->geo.chunk_mask;
29012920 int page_idx = 0;
29022921
2903
- if (!mempool_initialized(&conf->r10buf_pool))
2904
- if (init_resync(conf))
2905
- return 0;
2906
-
29072922 /*
29082923 * Allow skipping a full rebuild for incremental assembly
29092924 * of a clean array, like RAID1 does.
....@@ -2918,6 +2933,10 @@
29182933 *skipped = 1;
29192934 return mddev->dev_sectors - sector_nr;
29202935 }
2936
+
2937
+ if (!mempool_initialized(&conf->r10buf_pool))
2938
+ if (init_resync(conf))
2939
+ return 0;
29212940
29222941 skipped:
29232942 max_sector = mddev->dev_sectors;
....@@ -3034,7 +3053,6 @@
30343053 int must_sync;
30353054 int any_working;
30363055 int need_recover = 0;
3037
- int need_replace = 0;
30383056 struct raid10_info *mirror = &conf->mirrors[i];
30393057 struct md_rdev *mrdev, *mreplace;
30403058
....@@ -3046,11 +3064,10 @@
30463064 !test_bit(Faulty, &mrdev->flags) &&
30473065 !test_bit(In_sync, &mrdev->flags))
30483066 need_recover = 1;
3049
- if (mreplace != NULL &&
3050
- !test_bit(Faulty, &mreplace->flags))
3051
- need_replace = 1;
3067
+ if (mreplace && test_bit(Faulty, &mreplace->flags))
3068
+ mreplace = NULL;
30523069
3053
- if (!need_recover && !need_replace) {
3070
+ if (!need_recover && !mreplace) {
30543071 rcu_read_unlock();
30553072 continue;
30563073 }
....@@ -3066,8 +3083,6 @@
30663083 rcu_read_unlock();
30673084 continue;
30683085 }
3069
- if (mreplace && test_bit(Faulty, &mreplace->flags))
3070
- mreplace = NULL;
30713086 /* Unless we are doing a full sync, or a replacement
30723087 * we only need to recover the block if it is set in
30733088 * the bitmap
....@@ -3190,11 +3205,11 @@
31903205 bio = r10_bio->devs[1].repl_bio;
31913206 if (bio)
31923207 bio->bi_end_io = NULL;
3193
- /* Note: if need_replace, then bio
3208
+ /* Note: if replace is not NULL, then bio
31943209 * cannot be NULL as r10buf_pool_alloc will
31953210 * have allocated it.
31963211 */
3197
- if (!need_replace)
3212
+ if (!mreplace)
31983213 break;
31993214 bio->bi_next = biolist;
32003215 biolist = bio;
....@@ -3615,6 +3630,20 @@
36153630 return nc*fc;
36163631 }
36173632
3633
+static void raid10_free_conf(struct r10conf *conf)
3634
+{
3635
+ if (!conf)
3636
+ return;
3637
+
3638
+ mempool_exit(&conf->r10bio_pool);
3639
+ kfree(conf->mirrors);
3640
+ kfree(conf->mirrors_old);
3641
+ kfree(conf->mirrors_new);
3642
+ safe_put_page(conf->tmppage);
3643
+ bioset_exit(&conf->bio_split);
3644
+ kfree(conf);
3645
+}
3646
+
36183647 static struct r10conf *setup_conf(struct mddev *mddev)
36193648 {
36203649 struct r10conf *conf = NULL;
....@@ -3697,13 +3726,7 @@
36973726 return conf;
36983727
36993728 out:
3700
- if (conf) {
3701
- mempool_exit(&conf->r10bio_pool);
3702
- kfree(conf->mirrors);
3703
- safe_put_page(conf->tmppage);
3704
- bioset_exit(&conf->bio_split);
3705
- kfree(conf);
3706
- }
3729
+ raid10_free_conf(conf);
37073730 return ERR_PTR(err);
37083731 }
37093732
....@@ -3741,6 +3764,9 @@
37413764 if (!conf)
37423765 goto out;
37433766
3767
+ mddev->thread = conf->thread;
3768
+ conf->thread = NULL;
3769
+
37443770 if (mddev_is_clustered(conf->mddev)) {
37453771 int fc, fo;
37463772
....@@ -3752,9 +3778,6 @@
37523778 goto out_free_conf;
37533779 }
37543780 }
3755
-
3756
- mddev->thread = conf->thread;
3757
- conf->thread = NULL;
37583781
37593782 if (mddev->queue) {
37603783 blk_queue_max_discard_sectors(mddev->queue,
....@@ -3909,10 +3932,7 @@
39093932
39103933 out_free_conf:
39113934 md_unregister_thread(&mddev->thread);
3912
- mempool_exit(&conf->r10bio_pool);
3913
- safe_put_page(conf->tmppage);
3914
- kfree(conf->mirrors);
3915
- kfree(conf);
3935
+ raid10_free_conf(conf);
39163936 mddev->private = NULL;
39173937 out:
39183938 return -EIO;
....@@ -3920,15 +3940,7 @@
39203940
39213941 static void raid10_free(struct mddev *mddev, void *priv)
39223942 {
3923
- struct r10conf *conf = priv;
3924
-
3925
- mempool_exit(&conf->r10bio_pool);
3926
- safe_put_page(conf->tmppage);
3927
- kfree(conf->mirrors);
3928
- kfree(conf->mirrors_old);
3929
- kfree(conf->mirrors_new);
3930
- bioset_exit(&conf->bio_split);
3931
- kfree(conf);
3943
+ raid10_free_conf(priv);
39323944 }
39333945
39343946 static void raid10_quiesce(struct mddev *mddev, int quiesce)