diff mbox series

[05/11] staging: vchiq_arm: Get the rid off struct vchiq_2835_state

Message ID 20240604172904.61613-6-wahrenst@gmx.net (mailing list archive)
State New, archived
Headers show
Series staging: vc04_services: Random cleanups | expand

Commit Message

Stefan Wahren June 4, 2024, 5:28 p.m. UTC
The whole benefit of this encapsulating struct is questionable.
It just stores a flag to signalize the init state of vchiq_arm_state.
Beside the fact this flag is set too soon, the access to uninitialized
members should be avoided per design. So initialize vchiq_arm_state
properly before assign it directly to vchiq_state.

Signed-off-by: Stefan Wahren <wahrenst@gmx.net>
---
 .../interface/vchiq_arm/vchiq_arm.c           | 25 +++++--------------
 1 file changed, 6 insertions(+), 19 deletions(-)

--
2.34.1

Comments

Laurent Pinchart June 5, 2024, 7:11 a.m. UTC | #1
Hi Stefan,

Thank you for the patch.

On Tue, Jun 04, 2024 at 07:28:58PM +0200, Stefan Wahren wrote:
> The whole benefit of this encapsulating struct is questionable.
> It just stores a flag to signalize the init state of vchiq_arm_state.
> Beside the fact this flag is set too soon, the access to uninitialized
> members should be avoided per design.

Do you have plans to address the design ?

> So initialize vchiq_arm_state
> properly before assign it directly to vchiq_state.
> 
> Signed-off-by: Stefan Wahren <wahrenst@gmx.net>
> ---
>  .../interface/vchiq_arm/vchiq_arm.c           | 25 +++++--------------
>  1 file changed, 6 insertions(+), 19 deletions(-)
> 
> diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> index 515cdcba043d..98a0b2d52af5 100644
> --- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> +++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> @@ -109,11 +109,6 @@ struct vchiq_arm_state {
>  	int first_connect;
>  };
> 
> -struct vchiq_2835_state {
> -	int inited;
> -	struct vchiq_arm_state arm_state;
> -};
> -
>  struct vchiq_pagelist_info {
>  	struct pagelist *pagelist;
>  	size_t pagelist_buffer_size;
> @@ -613,29 +608,21 @@ vchiq_arm_init_state(struct vchiq_state *state,
>  int
>  vchiq_platform_init_state(struct vchiq_state *state)
>  {
> -	struct vchiq_2835_state *platform_state;
> +	struct vchiq_arm_state *platform_state;
> 
> -	state->platform_state = kzalloc(sizeof(*platform_state), GFP_KERNEL);
> -	if (!state->platform_state)
> +	platform_state = kzalloc(sizeof(*platform_state), GFP_KERNEL);
> +	if (!platform_state)
>  		return -ENOMEM;
> 
> -	platform_state = (struct vchiq_2835_state *)state->platform_state;
> -
> -	platform_state->inited = 1;
> -	vchiq_arm_init_state(state, &platform_state->arm_state);
> +	vchiq_arm_init_state(state, platform_state);
> +	state->platform_state = (struct opaque_platform_state *)platform_state;
> 
>  	return 0;
>  }
> 
>  static struct vchiq_arm_state *vchiq_platform_get_arm_state(struct vchiq_state *state)
>  {
> -	struct vchiq_2835_state *platform_state;
> -
> -	platform_state   = (struct vchiq_2835_state *)state->platform_state;
> -
> -	WARN_ON_ONCE(!platform_state->inited);
> -
> -	return &platform_state->arm_state;
> +	return (struct vchiq_arm_state *)state->platform_state;
>  }
> 
>  void
Stefan Wahren June 5, 2024, 10:11 a.m. UTC | #2
Hi Laurent,

Am 05.06.24 um 09:11 schrieb Laurent Pinchart:
> Hi Stefan,
>
> Thank you for the patch.
>
> On Tue, Jun 04, 2024 at 07:28:58PM +0200, Stefan Wahren wrote:
>> The whole benefit of this encapsulating struct is questionable.
>> It just stores a flag to signalize the init state of vchiq_arm_state.
>> Beside the fact this flag is set too soon, the access to uninitialized
>> members should be avoided per design.
> Do you have plans to address the design ?
by using kzalloc and assigning platform_state at the end of
vchiq_platform_init_state, i would consider this as fulfilled. Or do you
care about the possible platform_state NULL pointer?
>
>> So initialize vchiq_arm_state
>> properly before assign it directly to vchiq_state.
>>
>> Signed-off-by: Stefan Wahren <wahrenst@gmx.net>
>> ---
>>   .../interface/vchiq_arm/vchiq_arm.c           | 25 +++++--------------
>>   1 file changed, 6 insertions(+), 19 deletions(-)
>>
>> diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
>> index 515cdcba043d..98a0b2d52af5 100644
>> --- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
>> +++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
>> @@ -109,11 +109,6 @@ struct vchiq_arm_state {
>>   	int first_connect;
>>   };
>>
>> -struct vchiq_2835_state {
>> -	int inited;
>> -	struct vchiq_arm_state arm_state;
>> -};
>> -
>>   struct vchiq_pagelist_info {
>>   	struct pagelist *pagelist;
>>   	size_t pagelist_buffer_size;
>> @@ -613,29 +608,21 @@ vchiq_arm_init_state(struct vchiq_state *state,
>>   int
>>   vchiq_platform_init_state(struct vchiq_state *state)
>>   {
>> -	struct vchiq_2835_state *platform_state;
>> +	struct vchiq_arm_state *platform_state;
>>
>> -	state->platform_state = kzalloc(sizeof(*platform_state), GFP_KERNEL);
>> -	if (!state->platform_state)
>> +	platform_state = kzalloc(sizeof(*platform_state), GFP_KERNEL);
>> +	if (!platform_state)
>>   		return -ENOMEM;
>>
>> -	platform_state = (struct vchiq_2835_state *)state->platform_state;
>> -
>> -	platform_state->inited = 1;
>> -	vchiq_arm_init_state(state, &platform_state->arm_state);
>> +	vchiq_arm_init_state(state, platform_state);
>> +	state->platform_state = (struct opaque_platform_state *)platform_state;
>>
>>   	return 0;
>>   }
>>
>>   static struct vchiq_arm_state *vchiq_platform_get_arm_state(struct vchiq_state *state)
>>   {
>> -	struct vchiq_2835_state *platform_state;
>> -
>> -	platform_state   = (struct vchiq_2835_state *)state->platform_state;
>> -
>> -	WARN_ON_ONCE(!platform_state->inited);
>> -
>> -	return &platform_state->arm_state;
>> +	return (struct vchiq_arm_state *)state->platform_state;
>>   }
>>
>>   void
Laurent Pinchart June 11, 2024, 9:22 p.m. UTC | #3
Hi Stefan,

On Wed, Jun 05, 2024 at 12:11:41PM +0200, Stefan Wahren wrote:
> Am 05.06.24 um 09:11 schrieb Laurent Pinchart:
> > On Tue, Jun 04, 2024 at 07:28:58PM +0200, Stefan Wahren wrote:
> >> The whole benefit of this encapsulating struct is questionable.
> >> It just stores a flag to signalize the init state of vchiq_arm_state.
> >> Beside the fact this flag is set too soon, the access to uninitialized
> >> members should be avoided per design.
> >
> > Do you have plans to address the design ?
>
> by using kzalloc and assigning platform_state at the end of
> vchiq_platform_init_state, i would consider this as fulfilled. Or do you
> care about the possible platform_state NULL pointer?

Reading the commit message, I thought you meant further changes were
need to fix the design. A clarification in the commit message could be
useful.

> >> So initialize vchiq_arm_state
> >> properly before assign it directly to vchiq_state.
> >>
> >> Signed-off-by: Stefan Wahren <wahrenst@gmx.net>
> >> ---
> >>   .../interface/vchiq_arm/vchiq_arm.c           | 25 +++++--------------
> >>   1 file changed, 6 insertions(+), 19 deletions(-)
> >>
> >> diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> >> index 515cdcba043d..98a0b2d52af5 100644
> >> --- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> >> +++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> >> @@ -109,11 +109,6 @@ struct vchiq_arm_state {
> >>   	int first_connect;
> >>   };
> >>
> >> -struct vchiq_2835_state {
> >> -	int inited;
> >> -	struct vchiq_arm_state arm_state;
> >> -};
> >> -
> >>   struct vchiq_pagelist_info {
> >>   	struct pagelist *pagelist;
> >>   	size_t pagelist_buffer_size;
> >> @@ -613,29 +608,21 @@ vchiq_arm_init_state(struct vchiq_state *state,
> >>   int
> >>   vchiq_platform_init_state(struct vchiq_state *state)
> >>   {
> >> -	struct vchiq_2835_state *platform_state;
> >> +	struct vchiq_arm_state *platform_state;
> >>
> >> -	state->platform_state = kzalloc(sizeof(*platform_state), GFP_KERNEL);
> >> -	if (!state->platform_state)
> >> +	platform_state = kzalloc(sizeof(*platform_state), GFP_KERNEL);
> >> +	if (!platform_state)
> >>   		return -ENOMEM;
> >>
> >> -	platform_state = (struct vchiq_2835_state *)state->platform_state;
> >> -
> >> -	platform_state->inited = 1;
> >> -	vchiq_arm_init_state(state, &platform_state->arm_state);
> >> +	vchiq_arm_init_state(state, platform_state);
> >> +	state->platform_state = (struct opaque_platform_state *)platform_state;
> >>
> >>   	return 0;
> >>   }
> >>
> >>   static struct vchiq_arm_state *vchiq_platform_get_arm_state(struct vchiq_state *state)
> >>   {
> >> -	struct vchiq_2835_state *platform_state;
> >> -
> >> -	platform_state   = (struct vchiq_2835_state *)state->platform_state;
> >> -
> >> -	WARN_ON_ONCE(!platform_state->inited);
> >> -
> >> -	return &platform_state->arm_state;
> >> +	return (struct vchiq_arm_state *)state->platform_state;
> >>   }
> >>
> >>   void
Stefan Wahren June 12, 2024, 5:12 a.m. UTC | #4
Hi Laurent,

Am 11.06.24 um 23:22 schrieb Laurent Pinchart:
> Hi Stefan,
>
> On Wed, Jun 05, 2024 at 12:11:41PM +0200, Stefan Wahren wrote:
>> Am 05.06.24 um 09:11 schrieb Laurent Pinchart:
>>> On Tue, Jun 04, 2024 at 07:28:58PM +0200, Stefan Wahren wrote:
>>>> The whole benefit of this encapsulating struct is questionable.
>>>> It just stores a flag to signalize the init state of vchiq_arm_state.
>>>> Beside the fact this flag is set too soon, the access to uninitialized
>>>> members should be avoided per design.
>>> Do you have plans to address the design ?
>> by using kzalloc and assigning platform_state at the end of
>> vchiq_platform_init_state, i would consider this as fulfilled. Or do you
>> care about the possible platform_state NULL pointer?
> Reading the commit message, I thought you meant further changes were
> need to fix the design. A clarification in the commit message could be
> useful.
i missed to mention it in the V2 changelog, but i shorten the sentence
there to avoid the confusion. Is it better in V2 or does it still need
adjustment?
diff mbox series

Patch

diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
index 515cdcba043d..98a0b2d52af5 100644
--- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
+++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
@@ -109,11 +109,6 @@  struct vchiq_arm_state {
 	int first_connect;
 };

-struct vchiq_2835_state {
-	int inited;
-	struct vchiq_arm_state arm_state;
-};
-
 struct vchiq_pagelist_info {
 	struct pagelist *pagelist;
 	size_t pagelist_buffer_size;
@@ -613,29 +608,21 @@  vchiq_arm_init_state(struct vchiq_state *state,
 int
 vchiq_platform_init_state(struct vchiq_state *state)
 {
-	struct vchiq_2835_state *platform_state;
+	struct vchiq_arm_state *platform_state;

-	state->platform_state = kzalloc(sizeof(*platform_state), GFP_KERNEL);
-	if (!state->platform_state)
+	platform_state = kzalloc(sizeof(*platform_state), GFP_KERNEL);
+	if (!platform_state)
 		return -ENOMEM;

-	platform_state = (struct vchiq_2835_state *)state->platform_state;
-
-	platform_state->inited = 1;
-	vchiq_arm_init_state(state, &platform_state->arm_state);
+	vchiq_arm_init_state(state, platform_state);
+	state->platform_state = (struct opaque_platform_state *)platform_state;

 	return 0;
 }

 static struct vchiq_arm_state *vchiq_platform_get_arm_state(struct vchiq_state *state)
 {
-	struct vchiq_2835_state *platform_state;
-
-	platform_state   = (struct vchiq_2835_state *)state->platform_state;
-
-	WARN_ON_ONCE(!platform_state->inited);
-
-	return &platform_state->arm_state;
+	return (struct vchiq_arm_state *)state->platform_state;
 }

 void