[-next,3/7] md: don't rely on 'mddev->pers' to be set in mddev_suspend()

Message ID 20230803132751.2741652-4-yukuai1@huaweicloud.com
State New
Headers
Series md: initialize 'active_io' while allocating |

Commit Message

Yu Kuai Aug. 3, 2023, 1:27 p.m. UTC
  From: Yu Kuai <yukuai3@huawei.com>

Now that active io is initialized when mddev is allocated, it's safe to
call mddev_suspend() before 'mddev->pers' is set.

This also prevent null-ptr-def in some cases that caller doesn't
guarantee 'mddev->pers' to be set.

Signed-off-by: Yu Kuai <yukuai3@huawei.com>
---
 drivers/md/md.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Song Liu Aug. 22, 2023, 12:13 a.m. UTC | #1
On Thu, Aug 3, 2023 at 6:30 AM Yu Kuai <yukuai1@huaweicloud.com> wrote:
>
> From: Yu Kuai <yukuai3@huawei.com>
>
> Now that active io is initialized when mddev is allocated, it's safe to
> call mddev_suspend() before 'mddev->pers' is set.
>
> This also prevent null-ptr-def in some cases that caller doesn't
> guarantee 'mddev->pers' to be set.

This description is a little confusing (to me at least). Please revise it.

Other than that, the set looks good to me.

Thanks,
Song

>
> Signed-off-by: Yu Kuai <yukuai3@huawei.com>
> ---
>  drivers/md/md.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/md/md.c b/drivers/md/md.c
> index 897e94a9e47d..f14f2f0a9484 100644
> --- a/drivers/md/md.c
> +++ b/drivers/md/md.c
> @@ -448,7 +448,7 @@ void mddev_suspend(struct mddev *mddev)
>         set_bit(MD_ALLOW_SB_UPDATE, &mddev->flags);
>         percpu_ref_kill(&mddev->active_io);
>
> -       if (mddev->pers->prepare_suspend)
> +       if (mddev->pers && mddev->pers->prepare_suspend)
>                 mddev->pers->prepare_suspend(mddev);
>
>         wait_event(mddev->sb_wait, percpu_ref_is_zero(&mddev->active_io));
> --
> 2.39.2
>
  
Yu Kuai Aug. 22, 2023, 2:08 a.m. UTC | #2
Hi,

在 2023/08/22 8:13, Song Liu 写道:
> On Thu, Aug 3, 2023 at 6:30 AM Yu Kuai <yukuai1@huaweicloud.com> wrote:
>>
>> From: Yu Kuai <yukuai3@huawei.com>
>>
>> Now that active io is initialized when mddev is allocated, it's safe to
>> call mddev_suspend() before 'mddev->pers' is set.
>>
>> This also prevent null-ptr-def in some cases that caller doesn't
>> guarantee 'mddev->pers' to be set.
> 
> This description is a little confusing (to me at least). Please revise it.

Sorry about this, how about this:

'active_io' used to be initialized while the array is running, and
'mddev->pers' is set while the array is running as well. Hence caller
must hold 'reconfig_mutex' and guarantee 'mddev->pers' is set before
calling mddev_suspend().

Now that 'active_io' is initialized when mddev is allocated, such
restriction doesn't exist anymore. In the meantime, follow up patches
will refactor mddev_suspend(), hence add checking for 'mddev->pers' to
prevent null-ptr-deref.

Thanks,
Kuai

> 
> Other than that, the set looks good to me.
> 
> Thanks,
> Song
> 
>>
>> Signed-off-by: Yu Kuai <yukuai3@huawei.com>
>> ---
>>   drivers/md/md.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/md/md.c b/drivers/md/md.c
>> index 897e94a9e47d..f14f2f0a9484 100644
>> --- a/drivers/md/md.c
>> +++ b/drivers/md/md.c
>> @@ -448,7 +448,7 @@ void mddev_suspend(struct mddev *mddev)
>>          set_bit(MD_ALLOW_SB_UPDATE, &mddev->flags);
>>          percpu_ref_kill(&mddev->active_io);
>>
>> -       if (mddev->pers->prepare_suspend)
>> +       if (mddev->pers && mddev->pers->prepare_suspend)
>>                  mddev->pers->prepare_suspend(mddev);
>>
>>          wait_event(mddev->sb_wait, percpu_ref_is_zero(&mddev->active_io));
>> --
>> 2.39.2
>>
> .
>
  
Song Liu Aug. 22, 2023, 3:12 a.m. UTC | #3
On Mon, Aug 21, 2023 at 7:08 PM Yu Kuai <yukuai1@huaweicloud.com> wrote:
>
> Hi,
>
> 在 2023/08/22 8:13, Song Liu 写道:
> > On Thu, Aug 3, 2023 at 6:30 AM Yu Kuai <yukuai1@huaweicloud.com> wrote:
> >>
> >> From: Yu Kuai <yukuai3@huawei.com>
> >>
> >> Now that active io is initialized when mddev is allocated, it's safe to
> >> call mddev_suspend() before 'mddev->pers' is set.
> >>
> >> This also prevent null-ptr-def in some cases that caller doesn't
> >> guarantee 'mddev->pers' to be set.
> >
> > This description is a little confusing (to me at least). Please revise it.
>
> Sorry about this, how about this:
>
> 'active_io' used to be initialized while the array is running, and
> 'mddev->pers' is set while the array is running as well. Hence caller
> must hold 'reconfig_mutex' and guarantee 'mddev->pers' is set before
> calling mddev_suspend().
>
> Now that 'active_io' is initialized when mddev is allocated, such
> restriction doesn't exist anymore. In the meantime, follow up patches
> will refactor mddev_suspend(), hence add checking for 'mddev->pers' to
> prevent null-ptr-deref.

This is much better. Thanks!

Song
  

Patch

diff --git a/drivers/md/md.c b/drivers/md/md.c
index 897e94a9e47d..f14f2f0a9484 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -448,7 +448,7 @@  void mddev_suspend(struct mddev *mddev)
 	set_bit(MD_ALLOW_SB_UPDATE, &mddev->flags);
 	percpu_ref_kill(&mddev->active_io);
 
-	if (mddev->pers->prepare_suspend)
+	if (mddev->pers && mddev->pers->prepare_suspend)
 		mddev->pers->prepare_suspend(mddev);
 
 	wait_event(mddev->sb_wait, percpu_ref_is_zero(&mddev->active_io));