Skip to contentSkip to conversation

Factor out code into md_should_do_recovery()

PATCH Applied Wu Guanghao 4 participants 4 messages Sep 8, 2025 – Sep 20, 2025 MD RAID

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Factor out code into md_should_do_recovery()

In md_check_recovery(), use new helper to make code cleaner.

v2: split judgment conditions and add comments

---
 drivers/md/md.c | 59 +++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 47 insertions(+), 12 deletions(-)
drivers/md/md.c +47 -12
index 1baaf52c603c..58ce3518f51b 100644
@@ -9741,6 +9741,52 @@ static void unregister_sync_thread(struct mddev *mddev)
 	md_reap_sync_thread(mddev);
 }
 
+static bool md_should_do_recovery(struct mddev *mddev)
+{
+	/*
+	 * As long as one of the following flags is set,
+	 * recovery needs to do or cleanup.
+	 */
+	if (test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
+	    test_bit(MD_RECOVERY_DONE, &mddev->recovery))
+		return true;
+
+	/*
+	 * If no flags are set and it is in read-only status,
+	 * there is nothing to do.
+	 */
+	if (!md_is_rdwr(mddev))
+		return false;
+
+	/*
+	 * MD_SB_CHANGE_PENDING indicates that the array is switching from clean to
+	 * active, and no action is needed for now.
+	 * All other MD_SB_* flags require to update the superblock.
+	 */
+	if (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING))
+		return true;
+
+	/*
+	 * If the array is not using external metadata and there has been no data
+	 * written for some time, then the array's status needs to be set to
+	 * in_sync.
+	 */
+	if (mddev->external == 0 && mddev->safemode == 1)
+		return true;
+
+	/*
+	 * When the system is about to restart or the process receives an signal,
+	 * the array needs to be synchronized as soon as possible.
+	 * Once the data synchronization is completed, need to change the array
+	 * status to in_sync.
+	 */
+	if (mddev->safemode == 2 && !mddev->in_sync &&
+	    mddev->resync_offset == MaxSector)
+		return true;
+
+	return false;
+}
+
 /*
  * This routine is regularly called by all per-raid-array threads to
  * deal with generic issues like resync and super-block update.
@@ -9777,18 +9823,7 @@ void md_check_recovery(struct mddev *mddev)
 		flush_signals(current);
 	}
 
-	if (!md_is_rdwr(mddev) &&
-	    !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) &&
-	    !test_bit(MD_RECOVERY_DONE, &mddev->recovery))
-		return;
-	if ( ! (
-		(mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||
-		test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
-		test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
-		(mddev->external == 0 && mddev->safemode == 1) ||
-		(mddev->safemode == 2
-		 && !mddev->in_sync && mddev->resync_offset == MaxSector)
-		))
+	if (!md_should_do_recovery(mddev))
 		return;
 
 	if (mddev_trylock(mddev)) {
Signature · 1 line
2.27.0
Applied
#2 lore raw
Quoted 7 lines · Yu Kuai
Quoted 1 line · Wu Guanghao

In md_check_recovery(), use new helper to make code cleaner.

Thanks

Applied to md-6.18
Thanks

#3 lore raw
In reply to Wu Guanghao

Dear Guanghao,

Thank you for your patch. The prefix is missing in the summary. (I believe, I didn’t write it explicitly in my answer to version 1.)

Some minor comments:

Am 08.09.25 um 10:20 schrieb Wu Guanghao:

Quoted 1 line

In md_check_recovery(), use new helper to make code cleaner.

… and add comments.

Quoted 1 line

v2: split judgment conditions and add comments

The change-log often goes below the separator ---.

Quoted 3 lines

---

(I mean here.)

Also, as the subject changed, a link to the first version would also help a few people.

https://lore.kernel.org/all/[email protected]/

Quoted 8 lines
  drivers/md/md.c | 59 +++++++++++++++++++++++++++++++++++++++----------
  1 file changed, 47 insertions(+), 12 deletions(-)
drivers/md/md.c +0 -0
index 1baaf52c603c..58ce3518f51b 100644

drivers/md/md.c static void unregister_sync_thread(struct mddev *mddev) · line 9748

Quoted 8 lines
drivers/md/md.c +5 -0
@@ -9741,6 +9741,52 @@ static void unregister_sync_thread(struct mddev *mddev)
  	md_reap_sync_thread(mddev);
  }
 
+static bool md_should_do_recovery(struct mddev *mddev)
+{
+	/*
+	 * As long as one of the following flags is set,
+	 * recovery needs to do or cleanup.

The first part of the comment is redundant, as it’s exactly what the code does. The second part about “cleanup” would warrant more information, in my humble opinion.

drivers/md/md.c static void unregister_sync_thread(struct mddev *mddev) · line 9759

Quoted 11 lines
drivers/md/md.c +11 -0
+	 */
+	if (test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
+	    test_bit(MD_RECOVERY_DONE, &mddev->recovery))
+		return true;
+
+	/*
+	 * If no flags are set and it is in read-only status,
+	 * there is nothing to do.
+	 */
+	if (!md_is_rdwr(mddev))
+		return false;

The comment is redundant.

drivers/md/md.c static void unregister_sync_thread(struct mddev *mddev) · line 9767

Quoted 8 lines
drivers/md/md.c +8 -0
+
+	/*
+	 * MD_SB_CHANGE_PENDING indicates that the array is switching from clean to
+	 * active, and no action is needed for now.
+	 * All other MD_SB_* flags require to update the superblock.
+	 */
+	if (mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING))
+		return true;

The function name includes do_recovery. Isn’t that an “action”?

drivers/md/md.c static void unregister_sync_thread(struct mddev *mddev) · line 9775

Quoted 8 lines
drivers/md/md.c +8 -0
+
+	/*
+	 * If the array is not using external metadata and there has been no data
+	 * written for some time, then the array's status needs to be set to
+	 * in_sync.
+	 */
+	if (mddev->external == 0 && mddev->safemode == 1)
+		return true;

The comment confuses me. What does in_sync have to do with the code?

drivers/md/md.c static void unregister_sync_thread(struct mddev *mddev) · line 9778

Quoted 3 lines
drivers/md/md.c +3 -0
+
+	/*
+	 * When the system is about to restart or the process receives an signal,

a signal (no *n*)

drivers/md/md.c static void unregister_sync_thread(struct mddev *mddev) · line 9783

Quoted 5 lines
drivers/md/md.c +5 -0
+	 * the array needs to be synchronized as soon as possible.
+	 * Once the data synchronization is completed, need to change the array
+	 * status to in_sync.
+	 */
+	if (mddev->safemode == 2 && !mddev->in_sync &&

(I guess macros/enums should be defined for the different safe modes. This would be a separate patch though.)

drivers/md/md.c static void unregister_sync_thread(struct mddev *mddev) · line 9784

Quoted 1 line
drivers/md/md.c +1 -0
+	    mddev->resync_offset == MaxSector)

Excuse my ignorance, but why the last condition? Would this be something to add to the comment?

Quoted 18 lines
drivers/md/md.c +5 -6
+		return true;
+
+	return false;
+}
+
  /*
   * This routine is regularly called by all per-raid-array threads to
   * deal with generic issues like resync and super-block update.
@@ -9777,18 +9823,7 @@ void md_check_recovery(struct mddev *mddev)
  		flush_signals(current);
  	}
 
-	if (!md_is_rdwr(mddev) &&
-	    !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) &&
-	    !test_bit(MD_RECOVERY_DONE, &mddev->recovery))
-		return;
-	if ( ! (
-		(mddev->sb_flags & ~ (1<<MD_SB_CHANGE_PENDING)) ||

drivers/md/md.c void md_check_recovery(struct mddev *mddev) · line 9829

Quoted 10 lines
drivers/md/md.c +1 -6
-		test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
-		test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
-		(mddev->external == 0 && mddev->safemode == 1) ||
-		(mddev->safemode == 2
-		 && !mddev->in_sync && mddev->resync_offset == MaxSector)
-		))
+	if (!md_should_do_recovery(mddev))
  		return;
 
  	if (mddev_trylock(mddev)) {

Did you check, if the compiler generates different code? If you do/did, please add it to the commit message.

Kind regards,

Paul