All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] divide by 3*sizeof(u32) when computing array_size
@ 2021-07-12 23:19 ` Salah Triki
  0 siblings, 0 replies; 18+ messages in thread
From: Salah Triki @ 2021-07-12 23:19 UTC (permalink / raw)
  To: fabrice.gasnier, thierry.reding, u.kleine-koenig, lee.jones,
	mcoquelin.stm32, alexandre.torgue
  Cc: linux-pwm, linux-stm32, linux-arm-kernel, linux-kernel

Divide by 3*sizeof(u32) when computing array_size, since stm32_breakinput
has 3 fields of type u32.

Signed-off-by: Salah Triki <salah.triki@gmail.com>
---
 drivers/pwm/pwm-stm32.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/pwm/pwm-stm32.c b/drivers/pwm/pwm-stm32.c
index 794ca5b02968..fb21bc2b2dd6 100644
--- a/drivers/pwm/pwm-stm32.c
+++ b/drivers/pwm/pwm-stm32.c
@@ -544,7 +544,7 @@ static int stm32_pwm_probe_breakinputs(struct stm32_pwm *priv,
 		return -EINVAL;
 
 	priv->num_breakinputs = nb;
-	array_size = nb * sizeof(struct stm32_breakinput) / sizeof(u32);
+	array_size = nb * sizeof(struct stm32_breakinput) / (3 * sizeof(u32));
 	ret = of_property_read_u32_array(np, "st,breakinput",
 					 (u32 *)priv->breakinputs, array_size);
 	if (ret)
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH] divide by 3*sizeof(u32) when computing array_size
@ 2021-07-12 23:19 ` Salah Triki
  0 siblings, 0 replies; 18+ messages in thread
From: Salah Triki @ 2021-07-12 23:19 UTC (permalink / raw)
  To: fabrice.gasnier, thierry.reding, u.kleine-koenig, lee.jones,
	mcoquelin.stm32, alexandre.torgue
  Cc: linux-pwm, linux-stm32, linux-arm-kernel, linux-kernel

Divide by 3*sizeof(u32) when computing array_size, since stm32_breakinput
has 3 fields of type u32.

Signed-off-by: Salah Triki <salah.triki@gmail.com>
---
 drivers/pwm/pwm-stm32.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/pwm/pwm-stm32.c b/drivers/pwm/pwm-stm32.c
index 794ca5b02968..fb21bc2b2dd6 100644
--- a/drivers/pwm/pwm-stm32.c
+++ b/drivers/pwm/pwm-stm32.c
@@ -544,7 +544,7 @@ static int stm32_pwm_probe_breakinputs(struct stm32_pwm *priv,
 		return -EINVAL;
 
 	priv->num_breakinputs = nb;
-	array_size = nb * sizeof(struct stm32_breakinput) / sizeof(u32);
+	array_size = nb * sizeof(struct stm32_breakinput) / (3 * sizeof(u32));
 	ret = of_property_read_u32_array(np, "st,breakinput",
 					 (u32 *)priv->breakinputs, array_size);
 	if (ret)
-- 
2.25.1


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH] divide by 3*sizeof(u32) when computing array_size
  2021-07-12 23:19 ` Salah Triki
@ 2021-07-13  6:02   ` Philipp Hahn
  -1 siblings, 0 replies; 18+ messages in thread
From: Philipp Hahn @ 2021-07-13  6:02 UTC (permalink / raw)
  To: Salah Triki, fabrice.gasnier, thierry.reding, u.kleine-koenig,
	lee.jones, mcoquelin.stm32, alexandre.torgue
  Cc: linux-pwm, linux-stm32, linux-arm-kernel, linux-kernel

Hello,

Disclaimer: I have no idea what 'pwm-stm32' is or does

Am 13.07.21 um 01:19 schrieb Salah Triki:
> Divide by 3*sizeof(u32) when computing array_size, since stm32_breakinput
> has 3 fields of type u32.
...
> --- a/drivers/pwm/pwm-stm32.c
> +++ b/drivers/pwm/pwm-stm32.c
> @@ -544,7 +544,7 @@ static int stm32_pwm_probe_breakinputs(struct stm32_pwm *priv,
>   		return -EINVAL;
>   
>   	priv->num_breakinputs = nb;
> -	array_size = nb * sizeof(struct stm32_breakinput) / sizeof(u32);
> +	array_size = nb * sizeof(struct stm32_breakinput) / (3 * sizeof(u32));

Maybe it's too early in the morning for me, but this does not look right:

> struct stm32_breakinput {
> 	u32 index;
> 	u32 level;
> 	u32 filter;
> };

then "sizeof(struct stm32_breakinput)" == "(3 * sizeof(u32))", which 
would simply make "arrray_site := nb" ?

Philipp

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] divide by 3*sizeof(u32) when computing array_size
@ 2021-07-13  6:02   ` Philipp Hahn
  0 siblings, 0 replies; 18+ messages in thread
From: Philipp Hahn @ 2021-07-13  6:02 UTC (permalink / raw)
  To: Salah Triki, fabrice.gasnier, thierry.reding, u.kleine-koenig,
	lee.jones, mcoquelin.stm32, alexandre.torgue
  Cc: linux-pwm, linux-stm32, linux-arm-kernel, linux-kernel

Hello,

Disclaimer: I have no idea what 'pwm-stm32' is or does

Am 13.07.21 um 01:19 schrieb Salah Triki:
> Divide by 3*sizeof(u32) when computing array_size, since stm32_breakinput
> has 3 fields of type u32.
...
> --- a/drivers/pwm/pwm-stm32.c
> +++ b/drivers/pwm/pwm-stm32.c
> @@ -544,7 +544,7 @@ static int stm32_pwm_probe_breakinputs(struct stm32_pwm *priv,
>   		return -EINVAL;
>   
>   	priv->num_breakinputs = nb;
> -	array_size = nb * sizeof(struct stm32_breakinput) / sizeof(u32);
> +	array_size = nb * sizeof(struct stm32_breakinput) / (3 * sizeof(u32));

Maybe it's too early in the morning for me, but this does not look right:

> struct stm32_breakinput {
> 	u32 index;
> 	u32 level;
> 	u32 filter;
> };

then "sizeof(struct stm32_breakinput)" == "(3 * sizeof(u32))", which 
would simply make "arrray_site := nb" ?

Philipp

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] divide by 3*sizeof(u32) when computing array_size
  2021-07-12 23:19 ` Salah Triki
@ 2021-07-13  6:30   ` Uwe Kleine-König
  -1 siblings, 0 replies; 18+ messages in thread
From: Uwe Kleine-König @ 2021-07-13  6:30 UTC (permalink / raw)
  To: Salah Triki
  Cc: fabrice.gasnier, thierry.reding, lee.jones, mcoquelin.stm32,
	alexandre.torgue, linux-pwm, linux-stm32, linux-arm-kernel,
	linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1640 bytes --]

Hello Salah,

On Tue, Jul 13, 2021 at 12:19:10AM +0100, Salah Triki wrote:
> Divide by 3*sizeof(u32) when computing array_size, since stm32_breakinput
> has 3 fields of type u32.
> 
> Signed-off-by: Salah Triki <salah.triki@gmail.com>
> ---
>  drivers/pwm/pwm-stm32.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/pwm/pwm-stm32.c b/drivers/pwm/pwm-stm32.c
> index 794ca5b02968..fb21bc2b2dd6 100644
> --- a/drivers/pwm/pwm-stm32.c
> +++ b/drivers/pwm/pwm-stm32.c
> @@ -544,7 +544,7 @@ static int stm32_pwm_probe_breakinputs(struct stm32_pwm *priv,
>  		return -EINVAL;
>  
>  	priv->num_breakinputs = nb;
> -	array_size = nb * sizeof(struct stm32_breakinput) / sizeof(u32);
> +	array_size = nb * sizeof(struct stm32_breakinput) / (3 * sizeof(u32));
>  	ret = of_property_read_u32_array(np, "st,breakinput",
>  					 (u32 *)priv->breakinputs, array_size);
>  	if (ret)

I agree with Philipp here; this looks strange and needs a better
description.

Looking a bit more in details:

 - priv->breakinputs has type struct stm32_breakinput[MAX_BREAKINPUT]
 - nb is in [0 .. MAX_BREAKINPUT]
 - sizeof(struct stm32_breakinput) == 3 * sizeof(u32)
 - of_property_read_u32_array reads $array_size u32 quantities

so to read $nb members of type stm32_breakinput array_size must be a
multiple of 3 which isn't given any more after your patch. This makes me
believe your suggested change to be wrong.

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] divide by 3*sizeof(u32) when computing array_size
@ 2021-07-13  6:30   ` Uwe Kleine-König
  0 siblings, 0 replies; 18+ messages in thread
From: Uwe Kleine-König @ 2021-07-13  6:30 UTC (permalink / raw)
  To: Salah Triki
  Cc: fabrice.gasnier, thierry.reding, lee.jones, mcoquelin.stm32,
	alexandre.torgue, linux-pwm, linux-stm32, linux-arm-kernel,
	linux-kernel


[-- Attachment #1.1: Type: text/plain, Size: 1640 bytes --]

Hello Salah,

On Tue, Jul 13, 2021 at 12:19:10AM +0100, Salah Triki wrote:
> Divide by 3*sizeof(u32) when computing array_size, since stm32_breakinput
> has 3 fields of type u32.
> 
> Signed-off-by: Salah Triki <salah.triki@gmail.com>
> ---
>  drivers/pwm/pwm-stm32.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/pwm/pwm-stm32.c b/drivers/pwm/pwm-stm32.c
> index 794ca5b02968..fb21bc2b2dd6 100644
> --- a/drivers/pwm/pwm-stm32.c
> +++ b/drivers/pwm/pwm-stm32.c
> @@ -544,7 +544,7 @@ static int stm32_pwm_probe_breakinputs(struct stm32_pwm *priv,
>  		return -EINVAL;
>  
>  	priv->num_breakinputs = nb;
> -	array_size = nb * sizeof(struct stm32_breakinput) / sizeof(u32);
> +	array_size = nb * sizeof(struct stm32_breakinput) / (3 * sizeof(u32));
>  	ret = of_property_read_u32_array(np, "st,breakinput",
>  					 (u32 *)priv->breakinputs, array_size);
>  	if (ret)

I agree with Philipp here; this looks strange and needs a better
description.

Looking a bit more in details:

 - priv->breakinputs has type struct stm32_breakinput[MAX_BREAKINPUT]
 - nb is in [0 .. MAX_BREAKINPUT]
 - sizeof(struct stm32_breakinput) == 3 * sizeof(u32)
 - of_property_read_u32_array reads $array_size u32 quantities

so to read $nb members of type stm32_breakinput array_size must be a
multiple of 3 which isn't given any more after your patch. This makes me
believe your suggested change to be wrong.

Best regards
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

[-- Attachment #2: Type: text/plain, Size: 176 bytes --]

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] divide by 3*sizeof(u32) when computing array_size
  2021-07-13  6:30   ` Uwe Kleine-König
@ 2021-07-13  9:07     ` Uwe Kleine-König
  -1 siblings, 0 replies; 18+ messages in thread
From: Uwe Kleine-König @ 2021-07-13  9:07 UTC (permalink / raw)
  To: Salah Triki
  Cc: fabrice.gasnier, thierry.reding, lee.jones, mcoquelin.stm32,
	alexandre.torgue, linux-pwm, linux-stm32, linux-arm-kernel,
	linux-kernel

[-- Attachment #1: Type: text/plain, Size: 295 bytes --]

Hello again,

one more thing: If and when you resend a reworked patch, please start
the Subject with

	pwm: stm32:

Thanks
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] divide by 3*sizeof(u32) when computing array_size
@ 2021-07-13  9:07     ` Uwe Kleine-König
  0 siblings, 0 replies; 18+ messages in thread
From: Uwe Kleine-König @ 2021-07-13  9:07 UTC (permalink / raw)
  To: Salah Triki
  Cc: fabrice.gasnier, thierry.reding, lee.jones, mcoquelin.stm32,
	alexandre.torgue, linux-pwm, linux-stm32, linux-arm-kernel,
	linux-kernel


[-- Attachment #1.1: Type: text/plain, Size: 295 bytes --]

Hello again,

one more thing: If and when you resend a reworked patch, please start
the Subject with

	pwm: stm32:

Thanks
Uwe

-- 
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

[-- Attachment #2: Type: text/plain, Size: 176 bytes --]

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] divide by 3*sizeof(u32) when computing array_size
  2021-07-13  6:30   ` Uwe Kleine-König
@ 2021-07-13  9:19     ` Russell King (Oracle)
  -1 siblings, 0 replies; 18+ messages in thread
From: Russell King (Oracle) @ 2021-07-13  9:19 UTC (permalink / raw)
  To: Uwe Kleine-König
  Cc: Salah Triki, fabrice.gasnier, thierry.reding, lee.jones,
	mcoquelin.stm32, alexandre.torgue, linux-pwm, linux-stm32,
	linux-arm-kernel, linux-kernel

On Tue, Jul 13, 2021 at 08:30:53AM +0200, Uwe Kleine-König wrote:
> Hello Salah,
> 
> On Tue, Jul 13, 2021 at 12:19:10AM +0100, Salah Triki wrote:
> > Divide by 3*sizeof(u32) when computing array_size, since stm32_breakinput
> > has 3 fields of type u32.
> > 
> > Signed-off-by: Salah Triki <salah.triki@gmail.com>
> > ---
> >  drivers/pwm/pwm-stm32.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/drivers/pwm/pwm-stm32.c b/drivers/pwm/pwm-stm32.c
> > index 794ca5b02968..fb21bc2b2dd6 100644
> > --- a/drivers/pwm/pwm-stm32.c
> > +++ b/drivers/pwm/pwm-stm32.c
> > @@ -544,7 +544,7 @@ static int stm32_pwm_probe_breakinputs(struct stm32_pwm *priv,
> >  		return -EINVAL;
> >  
> >  	priv->num_breakinputs = nb;
> > -	array_size = nb * sizeof(struct stm32_breakinput) / sizeof(u32);
> > +	array_size = nb * sizeof(struct stm32_breakinput) / (3 * sizeof(u32));
> >  	ret = of_property_read_u32_array(np, "st,breakinput",
> >  					 (u32 *)priv->breakinputs, array_size);
> >  	if (ret)
> 
> I agree with Philipp here; this looks strange and needs a better
> description.
> 
> Looking a bit more in details:
> 
>  - priv->breakinputs has type struct stm32_breakinput[MAX_BREAKINPUT]
>  - nb is in [0 .. MAX_BREAKINPUT]
>  - sizeof(struct stm32_breakinput) == 3 * sizeof(u32)
>  - of_property_read_u32_array reads $array_size u32 quantities
> 
> so to read $nb members of type stm32_breakinput array_size must be a
> multiple of 3 which isn't given any more after your patch. This makes me
> believe your suggested change to be wrong.

I concur with your analysis. "array_size" is the number of u32 values
to read from DT. It is not the number of entries in priv->breakinputs.

I would also note that the code relies on there being no padding in
struct stm32_breakinput - it should be noted that a strict
interpretation of the C standard allows padding to be added anywhere
to a structure - at the start, end or between members.

Some further thoughts... DT is effectively an interface (we maintain
definitions of what we expect.) The way the code is structured,
"struct stm32_breakinput" defines that interface. Maybe this should
be commented, and maybe there should be a build time assert that
"sizeof(struct stm32_breakinput)" is "3 * sizeof(u32)" since the
code is relying on that property?

-- 
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] divide by 3*sizeof(u32) when computing array_size
@ 2021-07-13  9:19     ` Russell King (Oracle)
  0 siblings, 0 replies; 18+ messages in thread
From: Russell King (Oracle) @ 2021-07-13  9:19 UTC (permalink / raw)
  To: Uwe Kleine-König
  Cc: Salah Triki, fabrice.gasnier, thierry.reding, lee.jones,
	mcoquelin.stm32, alexandre.torgue, linux-pwm, linux-stm32,
	linux-arm-kernel, linux-kernel

On Tue, Jul 13, 2021 at 08:30:53AM +0200, Uwe Kleine-König wrote:
> Hello Salah,
> 
> On Tue, Jul 13, 2021 at 12:19:10AM +0100, Salah Triki wrote:
> > Divide by 3*sizeof(u32) when computing array_size, since stm32_breakinput
> > has 3 fields of type u32.
> > 
> > Signed-off-by: Salah Triki <salah.triki@gmail.com>
> > ---
> >  drivers/pwm/pwm-stm32.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/drivers/pwm/pwm-stm32.c b/drivers/pwm/pwm-stm32.c
> > index 794ca5b02968..fb21bc2b2dd6 100644
> > --- a/drivers/pwm/pwm-stm32.c
> > +++ b/drivers/pwm/pwm-stm32.c
> > @@ -544,7 +544,7 @@ static int stm32_pwm_probe_breakinputs(struct stm32_pwm *priv,
> >  		return -EINVAL;
> >  
> >  	priv->num_breakinputs = nb;
> > -	array_size = nb * sizeof(struct stm32_breakinput) / sizeof(u32);
> > +	array_size = nb * sizeof(struct stm32_breakinput) / (3 * sizeof(u32));
> >  	ret = of_property_read_u32_array(np, "st,breakinput",
> >  					 (u32 *)priv->breakinputs, array_size);
> >  	if (ret)
> 
> I agree with Philipp here; this looks strange and needs a better
> description.
> 
> Looking a bit more in details:
> 
>  - priv->breakinputs has type struct stm32_breakinput[MAX_BREAKINPUT]
>  - nb is in [0 .. MAX_BREAKINPUT]
>  - sizeof(struct stm32_breakinput) == 3 * sizeof(u32)
>  - of_property_read_u32_array reads $array_size u32 quantities
> 
> so to read $nb members of type stm32_breakinput array_size must be a
> multiple of 3 which isn't given any more after your patch. This makes me
> believe your suggested change to be wrong.

I concur with your analysis. "array_size" is the number of u32 values
to read from DT. It is not the number of entries in priv->breakinputs.

I would also note that the code relies on there being no padding in
struct stm32_breakinput - it should be noted that a strict
interpretation of the C standard allows padding to be added anywhere
to a structure - at the start, end or between members.

Some further thoughts... DT is effectively an interface (we maintain
definitions of what we expect.) The way the code is structured,
"struct stm32_breakinput" defines that interface. Maybe this should
be commented, and maybe there should be a build time assert that
"sizeof(struct stm32_breakinput)" is "3 * sizeof(u32)" since the
code is relying on that property?

-- 
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* RE: [PATCH] divide by 3*sizeof(u32) when computing array_size
  2021-07-13  9:19     ` Russell King (Oracle)
@ 2021-07-13 11:07       ` David Laight
  -1 siblings, 0 replies; 18+ messages in thread
From: David Laight @ 2021-07-13 11:07 UTC (permalink / raw)
  To: 'Russell King', Uwe Kleine-König
  Cc: Salah Triki, fabrice.gasnier, thierry.reding, lee.jones,
	mcoquelin.stm32, alexandre.torgue, linux-pwm, linux-stm32,
	linux-arm-kernel, linux-kernel

From: Russell King
> Sent: 13 July 2021 10:20
....
> I would also note that the code relies on there being no padding in
> struct stm32_breakinput - it should be noted that a strict
> interpretation of the C standard allows padding to be added anywhere
> to a structure - at the start, end or between members.

I'm pretty certain I remember that padding before the first member
isn't allowed.

In any case the kernel generally assumes there is no extra padding.
(eg for structures that map hardware registers.)

For big structures it is worth adding a compile-time check of
the structure size - but not really for three u32.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


^ permalink raw reply	[flat|nested] 18+ messages in thread

* RE: [PATCH] divide by 3*sizeof(u32) when computing array_size
@ 2021-07-13 11:07       ` David Laight
  0 siblings, 0 replies; 18+ messages in thread
From: David Laight @ 2021-07-13 11:07 UTC (permalink / raw)
  To: 'Russell King', Uwe Kleine-König
  Cc: Salah Triki, fabrice.gasnier, thierry.reding, lee.jones,
	mcoquelin.stm32, alexandre.torgue, linux-pwm, linux-stm32,
	linux-arm-kernel, linux-kernel

From: Russell King
> Sent: 13 July 2021 10:20
....
> I would also note that the code relies on there being no padding in
> struct stm32_breakinput - it should be noted that a strict
> interpretation of the C standard allows padding to be added anywhere
> to a structure - at the start, end or between members.

I'm pretty certain I remember that padding before the first member
isn't allowed.

In any case the kernel generally assumes there is no extra padding.
(eg for structures that map hardware registers.)

For big structures it is worth adding a compile-time check of
the structure size - but not really for three u32.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] divide by 3*sizeof(u32) when computing array_size
  2021-07-13 11:07       ` David Laight
@ 2021-07-13 11:22         ` Russell King (Oracle)
  -1 siblings, 0 replies; 18+ messages in thread
From: Russell King (Oracle) @ 2021-07-13 11:22 UTC (permalink / raw)
  To: David Laight
  Cc: Uwe Kleine-König, Salah Triki, fabrice.gasnier,
	thierry.reding, lee.jones, mcoquelin.stm32, alexandre.torgue,
	linux-pwm, linux-stm32, linux-arm-kernel, linux-kernel

On Tue, Jul 13, 2021 at 11:07:00AM +0000, David Laight wrote:
> From: Russell King
> > Sent: 13 July 2021 10:20
> ....
> > I would also note that the code relies on there being no padding in
> > struct stm32_breakinput - it should be noted that a strict
> > interpretation of the C standard allows padding to be added anywhere
> > to a structure - at the start, end or between members.
> 
> I'm pretty certain I remember that padding before the first member
> isn't allowed.

You may be right there.

> In any case the kernel generally assumes there is no extra padding.
> (eg for structures that map hardware registers.)

That's incorrect. Places where we care either generally end up with
__packed or are carefully layed out to ensure members are naturally
aligned to reduce the likelyhood of it. 32-bit OABI ARM has been
particularly "fun" in this respect.

> For big structures it is worth adding a compile-time check of
> the structure size - but not really for three u32.

Sorry, structure size has absolutely nothing to do with whether it's
a good idea to have a compile-time check. The deciding factor is
whether the code relies on some property such as it being a certain
size. Such as in this exact case. If you grep for "BUILD_BUG_ON.*sizeof"
in fs/ for example, this illustrates the point rather well.

-- 
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] divide by 3*sizeof(u32) when computing array_size
@ 2021-07-13 11:22         ` Russell King (Oracle)
  0 siblings, 0 replies; 18+ messages in thread
From: Russell King (Oracle) @ 2021-07-13 11:22 UTC (permalink / raw)
  To: David Laight
  Cc: Uwe Kleine-König, Salah Triki, fabrice.gasnier,
	thierry.reding, lee.jones, mcoquelin.stm32, alexandre.torgue,
	linux-pwm, linux-stm32, linux-arm-kernel, linux-kernel

On Tue, Jul 13, 2021 at 11:07:00AM +0000, David Laight wrote:
> From: Russell King
> > Sent: 13 July 2021 10:20
> ....
> > I would also note that the code relies on there being no padding in
> > struct stm32_breakinput - it should be noted that a strict
> > interpretation of the C standard allows padding to be added anywhere
> > to a structure - at the start, end or between members.
> 
> I'm pretty certain I remember that padding before the first member
> isn't allowed.

You may be right there.

> In any case the kernel generally assumes there is no extra padding.
> (eg for structures that map hardware registers.)

That's incorrect. Places where we care either generally end up with
__packed or are carefully layed out to ensure members are naturally
aligned to reduce the likelyhood of it. 32-bit OABI ARM has been
particularly "fun" in this respect.

> For big structures it is worth adding a compile-time check of
> the structure size - but not really for three u32.

Sorry, structure size has absolutely nothing to do with whether it's
a good idea to have a compile-time check. The deciding factor is
whether the code relies on some property such as it being a certain
size. Such as in this exact case. If you grep for "BUILD_BUG_ON.*sizeof"
in fs/ for example, this illustrates the point rather well.

-- 
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* RE: [PATCH] divide by 3*sizeof(u32) when computing array_size
  2021-07-13 11:22         ` Russell King (Oracle)
@ 2021-07-13 12:20           ` David Laight
  -1 siblings, 0 replies; 18+ messages in thread
From: David Laight @ 2021-07-13 12:20 UTC (permalink / raw)
  To: 'Russell King'
  Cc: Uwe Kleine-König, Salah Triki, fabrice.gasnier,
	thierry.reding, lee.jones, mcoquelin.stm32, alexandre.torgue,
	linux-pwm, linux-stm32, linux-arm-kernel, linux-kernel

From: Russell King
> Sent: 13 July 2021 12:23
> 
> On Tue, Jul 13, 2021 at 11:07:00AM +0000, David Laight wrote:
> > From: Russell King
> > > Sent: 13 July 2021 10:20
> > ....
> > > I would also note that the code relies on there being no padding in
> > > struct stm32_breakinput - it should be noted that a strict
> > > interpretation of the C standard allows padding to be added anywhere
> > > to a structure - at the start, end or between members.
> >
> > I'm pretty certain I remember that padding before the first member
> > isn't allowed.
> 
> You may be right there.
> 
> > In any case the kernel generally assumes there is no extra padding.
> > (eg for structures that map hardware registers.)
> 
> That's incorrect. Places where we care either generally end up with
> __packed or are carefully layed out to ensure members are naturally
> aligned to reduce the likelyhood of it. 32-bit OABI ARM has been
> particularly "fun" in this respect.

I did say 'extra padding'.
Ensuring everything is naturally aligned is best - shame the standards
bodies don't do that - just look at the SCTP socket options.

Adding __packed is right sometimes, but it isn't without cost
and is probably wrong for anything hardware related.
Definitely useful on structure members to remove the padding
before that specific member (eg for 64bit in x86 compat code).
But marking a structure __packed is usually wrong (or bad).

> > For big structures it is worth adding a compile-time check of
> > the structure size - but not really for three u32.
> 
> Sorry, structure size has absolutely nothing to do with whether it's
> a good idea to have a compile-time check. The deciding factor is
> whether the code relies on some property such as it being a certain
> size. Such as in this exact case. If you grep for "BUILD_BUG_ON.*sizeof"
> in fs/ for example, this illustrates the point rather well.

I'd not bother if the size is obviously going to be correct.

I did get some odd bugs a few years ago from a compiler that aligned
all structures on 4-byte boundaries.
I had to change a structure of two u16 into an array :-)

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


^ permalink raw reply	[flat|nested] 18+ messages in thread

* RE: [PATCH] divide by 3*sizeof(u32) when computing array_size
@ 2021-07-13 12:20           ` David Laight
  0 siblings, 0 replies; 18+ messages in thread
From: David Laight @ 2021-07-13 12:20 UTC (permalink / raw)
  To: 'Russell King'
  Cc: Uwe Kleine-König, Salah Triki, fabrice.gasnier,
	thierry.reding, lee.jones, mcoquelin.stm32, alexandre.torgue,
	linux-pwm, linux-stm32, linux-arm-kernel, linux-kernel

From: Russell King
> Sent: 13 July 2021 12:23
> 
> On Tue, Jul 13, 2021 at 11:07:00AM +0000, David Laight wrote:
> > From: Russell King
> > > Sent: 13 July 2021 10:20
> > ....
> > > I would also note that the code relies on there being no padding in
> > > struct stm32_breakinput - it should be noted that a strict
> > > interpretation of the C standard allows padding to be added anywhere
> > > to a structure - at the start, end or between members.
> >
> > I'm pretty certain I remember that padding before the first member
> > isn't allowed.
> 
> You may be right there.
> 
> > In any case the kernel generally assumes there is no extra padding.
> > (eg for structures that map hardware registers.)
> 
> That's incorrect. Places where we care either generally end up with
> __packed or are carefully layed out to ensure members are naturally
> aligned to reduce the likelyhood of it. 32-bit OABI ARM has been
> particularly "fun" in this respect.

I did say 'extra padding'.
Ensuring everything is naturally aligned is best - shame the standards
bodies don't do that - just look at the SCTP socket options.

Adding __packed is right sometimes, but it isn't without cost
and is probably wrong for anything hardware related.
Definitely useful on structure members to remove the padding
before that specific member (eg for 64bit in x86 compat code).
But marking a structure __packed is usually wrong (or bad).

> > For big structures it is worth adding a compile-time check of
> > the structure size - but not really for three u32.
> 
> Sorry, structure size has absolutely nothing to do with whether it's
> a good idea to have a compile-time check. The deciding factor is
> whether the code relies on some property such as it being a certain
> size. Such as in this exact case. If you grep for "BUILD_BUG_ON.*sizeof"
> in fs/ for example, this illustrates the point rather well.

I'd not bother if the size is obviously going to be correct.

I did get some odd bugs a few years ago from a compiler that aligned
all structures on 4-byte boundaries.
I had to change a structure of two u16 into an array :-)

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] divide by 3*sizeof(u32) when computing array_size
  2021-07-13 12:20           ` David Laight
@ 2021-07-13 12:35             ` Russell King (Oracle)
  -1 siblings, 0 replies; 18+ messages in thread
From: Russell King (Oracle) @ 2021-07-13 12:35 UTC (permalink / raw)
  To: David Laight
  Cc: Uwe Kleine-König, Salah Triki, fabrice.gasnier,
	thierry.reding, lee.jones, mcoquelin.stm32, alexandre.torgue,
	linux-pwm, linux-stm32, linux-arm-kernel, linux-kernel

On Tue, Jul 13, 2021 at 12:20:26PM +0000, David Laight wrote:
> > > For big structures it is worth adding a compile-time check of
> > > the structure size - but not really for three u32.
> > 
> > Sorry, structure size has absolutely nothing to do with whether it's
> > a good idea to have a compile-time check. The deciding factor is
> > whether the code relies on some property such as it being a certain
> > size. Such as in this exact case. If you grep for "BUILD_BUG_ON.*sizeof"
> > in fs/ for example, this illustrates the point rather well.
> 
> I'd not bother if the size is obviously going to be correct.

That's fine if you assume that the structure isn't going to be changed.
In this case, you can't do that - the structure looks to be a driver
internal structure. It certainly doesn't look like an interface to
anything that matters.

The code as written relies on the assumption that an array of
struct stm32_breakinput can be directly mapped to an array of u32,
where every third element of the u32 array falls on the first member
of each stm32_breakinput member. That is a _significant_ assumption
that the code _should_ be checking for.

> I did get some odd bugs a few years ago from a compiler that aligned
> all structures on 4-byte boundaries.
> I had to change a structure of two u16 into an array :-)

ARM OABI will do exactly that.

-- 
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [PATCH] divide by 3*sizeof(u32) when computing array_size
@ 2021-07-13 12:35             ` Russell King (Oracle)
  0 siblings, 0 replies; 18+ messages in thread
From: Russell King (Oracle) @ 2021-07-13 12:35 UTC (permalink / raw)
  To: David Laight
  Cc: Uwe Kleine-König, Salah Triki, fabrice.gasnier,
	thierry.reding, lee.jones, mcoquelin.stm32, alexandre.torgue,
	linux-pwm, linux-stm32, linux-arm-kernel, linux-kernel

On Tue, Jul 13, 2021 at 12:20:26PM +0000, David Laight wrote:
> > > For big structures it is worth adding a compile-time check of
> > > the structure size - but not really for three u32.
> > 
> > Sorry, structure size has absolutely nothing to do with whether it's
> > a good idea to have a compile-time check. The deciding factor is
> > whether the code relies on some property such as it being a certain
> > size. Such as in this exact case. If you grep for "BUILD_BUG_ON.*sizeof"
> > in fs/ for example, this illustrates the point rather well.
> 
> I'd not bother if the size is obviously going to be correct.

That's fine if you assume that the structure isn't going to be changed.
In this case, you can't do that - the structure looks to be a driver
internal structure. It certainly doesn't look like an interface to
anything that matters.

The code as written relies on the assumption that an array of
struct stm32_breakinput can be directly mapped to an array of u32,
where every third element of the u32 array falls on the first member
of each stm32_breakinput member. That is a _significant_ assumption
that the code _should_ be checking for.

> I did get some odd bugs a few years ago from a compiler that aligned
> all structures on 4-byte boundaries.
> I had to change a structure of two u16 into an array :-)

ARM OABI will do exactly that.

-- 
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 40Mbps down 10Mbps up. Decent connectivity at last!

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2021-07-13 12:37 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-12 23:19 [PATCH] divide by 3*sizeof(u32) when computing array_size Salah Triki
2021-07-12 23:19 ` Salah Triki
2021-07-13  6:02 ` Philipp Hahn
2021-07-13  6:02   ` Philipp Hahn
2021-07-13  6:30 ` Uwe Kleine-König
2021-07-13  6:30   ` Uwe Kleine-König
2021-07-13  9:07   ` Uwe Kleine-König
2021-07-13  9:07     ` Uwe Kleine-König
2021-07-13  9:19   ` Russell King (Oracle)
2021-07-13  9:19     ` Russell King (Oracle)
2021-07-13 11:07     ` David Laight
2021-07-13 11:07       ` David Laight
2021-07-13 11:22       ` Russell King (Oracle)
2021-07-13 11:22         ` Russell King (Oracle)
2021-07-13 12:20         ` David Laight
2021-07-13 12:20           ` David Laight
2021-07-13 12:35           ` Russell King (Oracle)
2021-07-13 12:35             ` Russell King (Oracle)

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.