lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20231228125553.2697765-4-yukuai1@huaweicloud.com>
Date: Thu, 28 Dec 2023 20:55:53 +0800
From: Yu Kuai <yukuai1@...weicloud.com>
To: song@...nel.org,
	yukuai3@...wei.com,
	neilb@...e.de
Cc: linux-raid@...r.kernel.org,
	linux-kernel@...r.kernel.org,
	yukuai1@...weicloud.com,
	yi.zhang@...wei.com,
	yangerkun@...wei.com
Subject: [PATCH -next 3/3] md: use interruptible apis in idle/frozen_sync_thread()

From: Yu Kuai <yukuai3@...wei.com>

Before refactoring idle and frozen from action_store, interruptible apis
is used so that hungtask warning won't be triggered if it takes too long
to finish idle/frozen sync_thread. So change to use interruptible apis.

In order not to make stop_sync_thread() more complicated, factor out a
helper prepare_to_stop_sync_thread() to replace stop_sync_thread().

Also return error to user if idle/frozen_sync_thread() failed, otherwise
user will be misleaded.

Fixes: 130443d60b1b ("md: refactor idle/frozen_sync_thread() to fix deadlock")
Fixes: 8e8e2518fcec ("md: Close race when setting 'action' to 'idle'.")
Signed-off-by: Yu Kuai <yukuai3@...wei.com>
---
 drivers/md/md.c | 105 ++++++++++++++++++++++++++++++------------------
 1 file changed, 67 insertions(+), 38 deletions(-)

diff --git a/drivers/md/md.c b/drivers/md/md.c
index 60f99768a1a9..9ea05de79fe4 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -4846,26 +4846,34 @@ action_show(struct mddev *mddev, char *page)
 	return sprintf(page, "%s\n", type);
 }
 
+static bool sync_thread_stopped(struct mddev *mddev, int *sync_seq)
+{
+	if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
+		return true;
+
+	if (sync_seq && *sync_seq != atomic_read(&mddev->sync_seq))
+		return true;
+
+	return false;
+}
+
 /**
- * stop_sync_thread() - wait for sync_thread to stop if it's running.
+ * prepare_to_stop_sync_thread() - prepare to stop sync_thread if it's running.
  * @mddev:	the array.
- * @locked:	if set, reconfig_mutex will still be held after this function
- *		return; if not set, reconfig_mutex will be released after this
- *		function return.
- * @check_seq:	if set, only wait for curent running sync_thread to stop, noted
- *		that new sync_thread can still start.
+ * @unlock:	whether or not caller want to release reconfig_mutex if
+ *		sync_thread is not running.
+ *
+ * Return true if sync_thread is running, release reconfig_mutex and do
+ * preparatory work to stop sync_thread, caller should wait for
+ * sync_thread_stopped() to return true. Return false if sync_thread is not
+ * running, reconfig_mutex will be released if @unlock is set.
  */
-static void stop_sync_thread(struct mddev *mddev, bool locked, bool check_seq)
+static bool prepare_to_stop_sync_thread(struct mddev *mddev, bool unlock)
 {
-	int sync_seq;
-
-	if (check_seq)
-		sync_seq = atomic_read(&mddev->sync_seq);
-
 	if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
-		if (!locked)
+		if (unlock)
 			mddev_unlock(mddev);
-		return;
+		return false;
 	}
 
 	mddev_unlock(mddev);
@@ -4879,53 +4887,67 @@ static void stop_sync_thread(struct mddev *mddev, bool locked, bool check_seq)
 	if (work_pending(&mddev->sync_work))
 		flush_work(&mddev->sync_work);
 
-	wait_event(resync_wait,
-		   !test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
-		   (check_seq && sync_seq != atomic_read(&mddev->sync_seq)));
-
-	if (locked)
-		mddev_lock_nointr(mddev);
+	return true;
 }
 
-static void idle_sync_thread(struct mddev *mddev)
+static int idle_sync_thread(struct mddev *mddev)
 {
-	mutex_lock(&mddev->sync_mutex);
+	int sync_seq = atomic_read(&mddev->sync_seq);
+	int err = mutex_lock_interruptible(&mddev->sync_mutex);
+
+	if (err)
+		return err;
 	clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
 
-	if (mddev_lock(mddev)) {
+	err = mddev_lock(mddev);
+	if (err) {
 		mutex_unlock(&mddev->sync_mutex);
-		return;
+		return err;
 	}
 
-	stop_sync_thread(mddev, false, true);
+	if (prepare_to_stop_sync_thread(mddev, true))
+		err = wait_event_interruptible(resync_wait,
+			   sync_thread_stopped(mddev, &sync_seq));
+
 	mutex_unlock(&mddev->sync_mutex);
+
+	return err;
 }
 
-static void frozen_sync_thread(struct mddev *mddev)
+static int frozen_sync_thread(struct mddev *mddev)
 {
-	mutex_lock(&mddev->sync_mutex);
+	int err = mutex_lock_interruptible(&mddev->sync_mutex);
+
+	if (err)
+		return err;
 	set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
 
-	if (mddev_lock(mddev)) {
+	err = mddev_lock(mddev);
+	if (err) {
 		mutex_unlock(&mddev->sync_mutex);
-		return;
+		return err;
 	}
 
-	stop_sync_thread(mddev, false, false);
+	if (prepare_to_stop_sync_thread(mddev, true))
+		err = wait_event_interruptible(resync_wait,
+			   sync_thread_stopped(mddev, NULL));
 	mutex_unlock(&mddev->sync_mutex);
+
+	return err;
 }
 
 static ssize_t
 action_store(struct mddev *mddev, const char *page, size_t len)
 {
+	int err = 0;
+
 	if (!mddev->pers || !mddev->pers->sync_request)
 		return -EINVAL;
 
-
 	if (cmd_match(page, "idle"))
-		idle_sync_thread(mddev);
+		err = idle_sync_thread(mddev);
 	else if (cmd_match(page, "frozen"))
-		frozen_sync_thread(mddev);
+		err = frozen_sync_thread(mddev);
 	else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
 		return -EBUSY;
 	else if (cmd_match(page, "resync"))
@@ -4934,7 +4956,6 @@ action_store(struct mddev *mddev, const char *page, size_t len)
 		clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
 		set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
 	} else if (cmd_match(page, "reshape")) {
-		int err;
 		if (mddev->pers->start_reshape == NULL)
 			return -EINVAL;
 		err = mddev_lock(mddev);
@@ -4980,7 +5001,7 @@ action_store(struct mddev *mddev, const char *page, size_t len)
 	set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
 	md_wakeup_thread(mddev->thread);
 	sysfs_notify_dirent_safe(mddev->sysfs_action);
-	return len;
+	return err ? err : len;
 }
 
 static struct md_sysfs_entry md_scan_mode =
@@ -6280,7 +6301,11 @@ static void md_clean(struct mddev *mddev)
 
 static void __md_stop_writes(struct mddev *mddev)
 {
-	stop_sync_thread(mddev, true, false);
+	if (prepare_to_stop_sync_thread(mddev, false)) {
+		wait_event(resync_wait, sync_thread_stopped(mddev, NULL));
+		mddev_lock_nointr(mddev);
+	}
+
 	del_timer_sync(&mddev->safemode_timer);
 
 	if (mddev->pers && mddev->pers->quiesce) {
@@ -6369,7 +6394,8 @@ static int md_set_readonly(struct mddev *mddev, struct block_device *bdev)
 		set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
 	}
 
-	stop_sync_thread(mddev, false, false);
+	if (prepare_to_stop_sync_thread(mddev, true))
+		wait_event(resync_wait, sync_thread_stopped(mddev, NULL));
 	wait_event(mddev->sb_wait,
 		   !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
 	mddev_lock_nointr(mddev);
@@ -6421,7 +6447,10 @@ static int do_md_stop(struct mddev *mddev, int mode,
 		set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
 	}
 
-	stop_sync_thread(mddev, true, false);
+	if (prepare_to_stop_sync_thread(mddev, false)) {
+		wait_event(resync_wait, sync_thread_stopped(mddev, NULL));
+		mddev_lock_nointr(mddev);
+	}
 
 	mutex_lock(&mddev->open_mutex);
 	if ((mddev->pers && atomic_read(&mddev->openers) > !!bdev) ||
-- 
2.39.2


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ