[PATCH] md: use RCU lock to protect traversal in md_spares_need_change()

Li Lingfeng posted 1 patch 1 year, 11 months ago
drivers/md/md.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
[PATCH] md: use RCU lock to protect traversal in md_spares_need_change()
Posted by Li Lingfeng 1 year, 11 months ago
From: Li Lingfeng <lilingfeng3@huawei.com>

Since md_start_sync() will be called without the protect of mddev_lock,
and it can run concurrently with array reconfiguration, traversal of rdev
in it should be protected by RCU lock.
Commit bc08041b32ab ("md: suspend array in md_start_sync() if array need
reconfiguration") added md_spares_need_change() to md_start_sync(),
casusing use of rdev without any protection.
Fix this by adding RCU lock in md_spares_need_change().

Fixes: bc08041b32ab ("md: suspend array in md_start_sync() if array need reconfiguration")
Signed-off-by: Li Lingfeng <lilingfeng3@huawei.com>
---
 drivers/md/md.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/drivers/md/md.c b/drivers/md/md.c
index 9bdd57324c37..902b43b65052 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -9228,9 +9228,14 @@ static bool md_spares_need_change(struct mddev *mddev)
 {
 	struct md_rdev *rdev;
 
-	rdev_for_each(rdev, mddev)
-		if (rdev_removeable(rdev) || rdev_addable(rdev))
+	rcu_read_lock();
+	rdev_for_each_rcu(rdev, mddev) {
+		if (rdev_removeable(rdev) || rdev_addable(rdev)) {
+			rcu_read_unlock();
 			return true;
+		}
+	}
+	rcu_read_unlock();
 	return false;
 }
 
-- 
2.31.1
Re: [PATCH] md: use RCU lock to protect traversal in md_spares_need_change()
Posted by Song Liu 1 year, 11 months ago
On Thu, Jan 4, 2024 at 5:39 AM Li Lingfeng <lilingfeng@huaweicloud.com> wrote:
>
> From: Li Lingfeng <lilingfeng3@huawei.com>
>
> Since md_start_sync() will be called without the protect of mddev_lock,
> and it can run concurrently with array reconfiguration, traversal of rdev
> in it should be protected by RCU lock.
> Commit bc08041b32ab ("md: suspend array in md_start_sync() if array need
> reconfiguration") added md_spares_need_change() to md_start_sync(),
> casusing use of rdev without any protection.
> Fix this by adding RCU lock in md_spares_need_change().
>
> Fixes: bc08041b32ab ("md: suspend array in md_start_sync() if array need reconfiguration")
> Signed-off-by: Li Lingfeng <lilingfeng3@huawei.com>

Looks good to me. Applied to md-tmp-6.9.

Thanks,
Song