diff mbox series

[-next,v3,4/7] md: factor out a helper rdev_removeable() from remove_and_add_spares()

Message ID 20230820090949.2874537-5-yukuai1@huaweicloud.com (mailing list archive)
State New, archived
Headers show
Series md: make rdev addition and removal independent from daemon thread | expand

Commit Message

Yu Kuai Aug. 20, 2023, 9:09 a.m. UTC
From: Yu Kuai <yukuai3@huawei.com>

There are no functional changes, just to make the code simpler and
prepare to delay remove_and_add_spares() to md_start_sync().

Signed-off-by: Yu Kuai <yukuai3@huawei.com>
---
 drivers/md/md.c | 33 +++++++++++++++++++--------------
 1 file changed, 19 insertions(+), 14 deletions(-)

Comments

Xiao Ni Aug. 22, 2023, 10:19 a.m. UTC | #1
On Sun, Aug 20, 2023 at 5:13 PM Yu Kuai <yukuai1@huaweicloud.com> wrote:
>
> From: Yu Kuai <yukuai3@huawei.com>
>
> There are no functional changes, just to make the code simpler and
> prepare to delay remove_and_add_spares() to md_start_sync().
>
> Signed-off-by: Yu Kuai <yukuai3@huawei.com>
> ---
>  drivers/md/md.c | 33 +++++++++++++++++++--------------
>  1 file changed, 19 insertions(+), 14 deletions(-)
>
> diff --git a/drivers/md/md.c b/drivers/md/md.c
> index 561cac13ff96..ceace5ffadd6 100644
> --- a/drivers/md/md.c
> +++ b/drivers/md/md.c
> @@ -9153,6 +9153,22 @@ void md_do_sync(struct md_thread *thread)
>  }
>  EXPORT_SYMBOL_GPL(md_do_sync);
>
> +static bool rdev_removeable(struct md_rdev *rdev)
> +{
> +       if (rdev->raid_disk < 0 || test_bit(Blocked, &rdev->flags) ||
> +           atomic_read(&rdev->nr_pending))
> +               return false;
> +
> +       if (test_bit(RemoveSynchronized, &rdev->flags))
> +               return true;
> +
> +       if (test_bit(In_sync, &rdev->flags) ||
> +           test_bit(Journal, &rdev->flags))
> +               return false;
> +
> +       return true;
> +}
> +
>  static int remove_and_add_spares(struct mddev *mddev,
>                                  struct md_rdev *this)
>  {
> @@ -9166,11 +9182,7 @@ static int remove_and_add_spares(struct mddev *mddev,
>                 return 0;
>
>         rdev_for_each(rdev, mddev) {
> -               if ((this == NULL || rdev == this) &&
> -                   rdev->raid_disk >= 0 &&
> -                   !test_bit(Blocked, &rdev->flags) &&
> -                   test_bit(Faulty, &rdev->flags) &&
> -                   atomic_read(&rdev->nr_pending)==0) {
> +               if ((this == NULL || rdev == this) && rdev_removeable(rdev)) {

There is a small change with the original method. Before this patch,
it checks the Faulty flag when setting RemoveSynchronized and it
checks RemoveSynchronized and "!In_sync && !Journal". I'm not sure if
it's right or not.

>                         /* Faulty non-Blocked devices with nr_pending == 0
>                          * never get nr_pending incremented,
>                          * never get Faulty cleared, and never get Blocked set.
> @@ -9185,19 +9197,12 @@ static int remove_and_add_spares(struct mddev *mddev,
>                 synchronize_rcu();
>         rdev_for_each(rdev, mddev) {
>                 if ((this == NULL || rdev == this) &&
> -                   rdev->raid_disk >= 0 &&
> -                   !test_bit(Blocked, &rdev->flags) &&
> -                   ((test_bit(RemoveSynchronized, &rdev->flags) ||
> -                    (!test_bit(In_sync, &rdev->flags) &&
> -                     !test_bit(Journal, &rdev->flags))) &&
> -                   atomic_read(&rdev->nr_pending)==0)) {
> -                       if (mddev->pers->hot_remove_disk(
> -                                   mddev, rdev) == 0) {
> +                   rdev_removeable(rdev) &&
> +                   mddev->pers->hot_remove_disk(mddev, rdev) == 0) {
>                                 sysfs_unlink_rdev(mddev, rdev);
>                                 rdev->saved_raid_disk = rdev->raid_disk;
>                                 rdev->raid_disk = -1;
>                                 removed++;
> -                       }
>                 }
>                 if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
>                         clear_bit(RemoveSynchronized, &rdev->flags);
> --
> 2.39.2
>
Yu Kuai Aug. 23, 2023, 2:45 a.m. UTC | #2
Hi,

在 2023/08/22 18:19, Xiao Ni 写道:
> On Sun, Aug 20, 2023 at 5:13 PM Yu Kuai <yukuai1@huaweicloud.com> wrote:
>>
>> From: Yu Kuai <yukuai3@huawei.com>
>>
>> There are no functional changes, just to make the code simpler and
>> prepare to delay remove_and_add_spares() to md_start_sync().
>>
>> Signed-off-by: Yu Kuai <yukuai3@huawei.com>
>> ---
>>   drivers/md/md.c | 33 +++++++++++++++++++--------------
>>   1 file changed, 19 insertions(+), 14 deletions(-)
>>
>> diff --git a/drivers/md/md.c b/drivers/md/md.c
>> index 561cac13ff96..ceace5ffadd6 100644
>> --- a/drivers/md/md.c
>> +++ b/drivers/md/md.c
>> @@ -9153,6 +9153,22 @@ void md_do_sync(struct md_thread *thread)
>>   }
>>   EXPORT_SYMBOL_GPL(md_do_sync);
>>
>> +static bool rdev_removeable(struct md_rdev *rdev)
>> +{
>> +       if (rdev->raid_disk < 0 || test_bit(Blocked, &rdev->flags) ||
>> +           atomic_read(&rdev->nr_pending))
>> +               return false;
>> +
>> +       if (test_bit(RemoveSynchronized, &rdev->flags))
>> +               return true;
>> +
>> +       if (test_bit(In_sync, &rdev->flags) ||
>> +           test_bit(Journal, &rdev->flags))
>> +               return false;
>> +
>> +       return true;
>> +}
>> +
>>   static int remove_and_add_spares(struct mddev *mddev,
>>                                   struct md_rdev *this)
>>   {
>> @@ -9166,11 +9182,7 @@ static int remove_and_add_spares(struct mddev *mddev,
>>                  return 0;
>>
>>          rdev_for_each(rdev, mddev) {
>> -               if ((this == NULL || rdev == this) &&
>> -                   rdev->raid_disk >= 0 &&
>> -                   !test_bit(Blocked, &rdev->flags) &&
>> -                   test_bit(Faulty, &rdev->flags) &&
>> -                   atomic_read(&rdev->nr_pending)==0) {
>> +               if ((this == NULL || rdev == this) && rdev_removeable(rdev)) {
> 
> There is a small change with the original method. Before this patch,
> it checks the Faulty flag when setting RemoveSynchronized and it
> checks RemoveSynchronized and "!In_sync && !Journal". I'm not sure if
> it's right or not.

Yes, there is a small change. After a second thought, I think it's OK
to leave the code to set RemoveSynchronized where it is for now, because
it'll be removed later. I don't need to bother factor out a common code
to set RemoveSynchronized and call hot_remove_disk().

By the way, once refactor of mddev_suspend() is done, then access to
rdev from fastpath will be replaced from:

rcu_read_lock()
...
rcu_read_unlock()

to:

md_array_enter()
// grab 'active_io', 'active_io' will probably be renamed
...
md_array_exit()

That's why I said RemoveSynchronized will be removed.

Thanks,
Kuai

> 
>>                          /* Faulty non-Blocked devices with nr_pending == 0
>>                           * never get nr_pending incremented,
>>                           * never get Faulty cleared, and never get Blocked set.
>> @@ -9185,19 +9197,12 @@ static int remove_and_add_spares(struct mddev *mddev,
>>                  synchronize_rcu();
>>          rdev_for_each(rdev, mddev) {
>>                  if ((this == NULL || rdev == this) &&
>> -                   rdev->raid_disk >= 0 &&
>> -                   !test_bit(Blocked, &rdev->flags) &&
>> -                   ((test_bit(RemoveSynchronized, &rdev->flags) ||
>> -                    (!test_bit(In_sync, &rdev->flags) &&
>> -                     !test_bit(Journal, &rdev->flags))) &&
>> -                   atomic_read(&rdev->nr_pending)==0)) {
>> -                       if (mddev->pers->hot_remove_disk(
>> -                                   mddev, rdev) == 0) {
>> +                   rdev_removeable(rdev) &&
>> +                   mddev->pers->hot_remove_disk(mddev, rdev) == 0) {
>>                                  sysfs_unlink_rdev(mddev, rdev);
>>                                  rdev->saved_raid_disk = rdev->raid_disk;
>>                                  rdev->raid_disk = -1;
>>                                  removed++;
>> -                       }
>>                  }
>>                  if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
>>                          clear_bit(RemoveSynchronized, &rdev->flags);
>> --
>> 2.39.2
>>
> 
> .
>
Xiao Ni Aug. 23, 2023, 3:42 a.m. UTC | #3
On Wed, Aug 23, 2023 at 10:45 AM Yu Kuai <yukuai1@huaweicloud.com> wrote:
>
> Hi,
>
> 在 2023/08/22 18:19, Xiao Ni 写道:
> > On Sun, Aug 20, 2023 at 5:13 PM Yu Kuai <yukuai1@huaweicloud.com> wrote:
> >>
> >> From: Yu Kuai <yukuai3@huawei.com>
> >>
> >> There are no functional changes, just to make the code simpler and
> >> prepare to delay remove_and_add_spares() to md_start_sync().
> >>
> >> Signed-off-by: Yu Kuai <yukuai3@huawei.com>
> >> ---
> >>   drivers/md/md.c | 33 +++++++++++++++++++--------------
> >>   1 file changed, 19 insertions(+), 14 deletions(-)
> >>
> >> diff --git a/drivers/md/md.c b/drivers/md/md.c
> >> index 561cac13ff96..ceace5ffadd6 100644
> >> --- a/drivers/md/md.c
> >> +++ b/drivers/md/md.c
> >> @@ -9153,6 +9153,22 @@ void md_do_sync(struct md_thread *thread)
> >>   }
> >>   EXPORT_SYMBOL_GPL(md_do_sync);
> >>
> >> +static bool rdev_removeable(struct md_rdev *rdev)
> >> +{
> >> +       if (rdev->raid_disk < 0 || test_bit(Blocked, &rdev->flags) ||
> >> +           atomic_read(&rdev->nr_pending))
> >> +               return false;
> >> +
> >> +       if (test_bit(RemoveSynchronized, &rdev->flags))
> >> +               return true;
> >> +
> >> +       if (test_bit(In_sync, &rdev->flags) ||
> >> +           test_bit(Journal, &rdev->flags))
> >> +               return false;
> >> +
> >> +       return true;
> >> +}
> >> +
> >>   static int remove_and_add_spares(struct mddev *mddev,
> >>                                   struct md_rdev *this)
> >>   {
> >> @@ -9166,11 +9182,7 @@ static int remove_and_add_spares(struct mddev *mddev,
> >>                  return 0;
> >>
> >>          rdev_for_each(rdev, mddev) {
> >> -               if ((this == NULL || rdev == this) &&
> >> -                   rdev->raid_disk >= 0 &&
> >> -                   !test_bit(Blocked, &rdev->flags) &&
> >> -                   test_bit(Faulty, &rdev->flags) &&
> >> -                   atomic_read(&rdev->nr_pending)==0) {
> >> +               if ((this == NULL || rdev == this) && rdev_removeable(rdev)) {
> >
> > There is a small change with the original method. Before this patch,
> > it checks the Faulty flag when setting RemoveSynchronized and it
> > checks RemoveSynchronized and "!In_sync && !Journal". I'm not sure if
> > it's right or not.
>
> Yes, there is a small change. After a second thought, I think it's OK
> to leave the code to set RemoveSynchronized where it is for now, because
> it'll be removed later. I don't need to bother factor out a common code
> to set RemoveSynchronized and call hot_remove_disk().

This will be easier for review, thanks.

>
> By the way, once refactor of mddev_suspend() is done, then access to
> rdev from fastpath will be replaced from:
>
> rcu_read_lock()
> ...
> rcu_read_unlock()
>
> to:
>
> md_array_enter()
> // grab 'active_io', 'active_io' will probably be renamed
> ...
> md_array_exit()
>
> That's why I said RemoveSynchronized will be removed.

:) I'll try to understand it in your following patches.

Regards
Xiao
>
> Thanks,
> Kuai
>
> >
> >>                          /* Faulty non-Blocked devices with nr_pending == 0
> >>                           * never get nr_pending incremented,
> >>                           * never get Faulty cleared, and never get Blocked set.
> >> @@ -9185,19 +9197,12 @@ static int remove_and_add_spares(struct mddev *mddev,
> >>                  synchronize_rcu();
> >>          rdev_for_each(rdev, mddev) {
> >>                  if ((this == NULL || rdev == this) &&
> >> -                   rdev->raid_disk >= 0 &&
> >> -                   !test_bit(Blocked, &rdev->flags) &&
> >> -                   ((test_bit(RemoveSynchronized, &rdev->flags) ||
> >> -                    (!test_bit(In_sync, &rdev->flags) &&
> >> -                     !test_bit(Journal, &rdev->flags))) &&
> >> -                   atomic_read(&rdev->nr_pending)==0)) {
> >> -                       if (mddev->pers->hot_remove_disk(
> >> -                                   mddev, rdev) == 0) {
> >> +                   rdev_removeable(rdev) &&
> >> +                   mddev->pers->hot_remove_disk(mddev, rdev) == 0) {
> >>                                  sysfs_unlink_rdev(mddev, rdev);
> >>                                  rdev->saved_raid_disk = rdev->raid_disk;
> >>                                  rdev->raid_disk = -1;
> >>                                  removed++;
> >> -                       }
> >>                  }
> >>                  if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
> >>                          clear_bit(RemoveSynchronized, &rdev->flags);
> >> --
> >> 2.39.2
> >>
> >
> > .
> >
>
diff mbox series

Patch

diff --git a/drivers/md/md.c b/drivers/md/md.c
index 561cac13ff96..ceace5ffadd6 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -9153,6 +9153,22 @@  void md_do_sync(struct md_thread *thread)
 }
 EXPORT_SYMBOL_GPL(md_do_sync);
 
+static bool rdev_removeable(struct md_rdev *rdev)
+{
+	if (rdev->raid_disk < 0 || test_bit(Blocked, &rdev->flags) ||
+	    atomic_read(&rdev->nr_pending))
+		return false;
+
+	if (test_bit(RemoveSynchronized, &rdev->flags))
+		return true;
+
+	if (test_bit(In_sync, &rdev->flags) ||
+	    test_bit(Journal, &rdev->flags))
+		return false;
+
+	return true;
+}
+
 static int remove_and_add_spares(struct mddev *mddev,
 				 struct md_rdev *this)
 {
@@ -9166,11 +9182,7 @@  static int remove_and_add_spares(struct mddev *mddev,
 		return 0;
 
 	rdev_for_each(rdev, mddev) {
-		if ((this == NULL || rdev == this) &&
-		    rdev->raid_disk >= 0 &&
-		    !test_bit(Blocked, &rdev->flags) &&
-		    test_bit(Faulty, &rdev->flags) &&
-		    atomic_read(&rdev->nr_pending)==0) {
+		if ((this == NULL || rdev == this) && rdev_removeable(rdev)) {
 			/* Faulty non-Blocked devices with nr_pending == 0
 			 * never get nr_pending incremented,
 			 * never get Faulty cleared, and never get Blocked set.
@@ -9185,19 +9197,12 @@  static int remove_and_add_spares(struct mddev *mddev,
 		synchronize_rcu();
 	rdev_for_each(rdev, mddev) {
 		if ((this == NULL || rdev == this) &&
-		    rdev->raid_disk >= 0 &&
-		    !test_bit(Blocked, &rdev->flags) &&
-		    ((test_bit(RemoveSynchronized, &rdev->flags) ||
-		     (!test_bit(In_sync, &rdev->flags) &&
-		      !test_bit(Journal, &rdev->flags))) &&
-		    atomic_read(&rdev->nr_pending)==0)) {
-			if (mddev->pers->hot_remove_disk(
-				    mddev, rdev) == 0) {
+		    rdev_removeable(rdev) &&
+		    mddev->pers->hot_remove_disk(mddev, rdev) == 0) {
 				sysfs_unlink_rdev(mddev, rdev);
 				rdev->saved_raid_disk = rdev->raid_disk;
 				rdev->raid_disk = -1;
 				removed++;
-			}
 		}
 		if (remove_some && test_bit(RemoveSynchronized, &rdev->flags))
 			clear_bit(RemoveSynchronized, &rdev->flags);