diff mbox

[v2] regulator: s2mps11: Simplify expression used in BUILD_BUG_ON

Message ID 1455755707-16844-1-git-send-email-k.kozlowski@samsung.com (mailing list archive)
State Not Applicable, archived
Headers show

Commit Message

Krzysztof Kozlowski Feb. 18, 2016, 12:35 a.m. UTC
Following BUILD_BUG_ON using a variable fails for some of the compilers
and optimization levels (reported for gcc 4.9):
	var = ARRAY_SIZE(s2mps15_regulators);
	BUILD_BUG_ON(S2MPS_REGULATOR_MAX < var);
Fix this by using ARRAY_SIZE directly.

Additionally add missing BUILD_BUG_ON check for S2MPS15 device (the
check ensures that internal arrays are big enough to hold data for all
of regulators on all devices).

Reported-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Krzysztof Kozlowski <k.kozlowski@samsung.com>

---

Changes since v1:
1. Rewritten description after comments from Jacob.

See results when UBSAN is enabled:
http://arm-soc.lixom.net/buildlogs/arm-soc/v4.5-rc4-39-g0d7baf0/buildall.arm.exynos_defconfig.log.failed
---
 drivers/regulator/s2mps11.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

Comments

Andi Shyti Feb. 18, 2016, 1:37 a.m. UTC | #1
Hi Krzysztof,

> Following BUILD_BUG_ON using a variable fails for some of the compilers
> and optimization levels (reported for gcc 4.9):
> 	var = ARRAY_SIZE(s2mps15_regulators);
> 	BUILD_BUG_ON(S2MPS_REGULATOR_MAX < var);
> Fix this by using ARRAY_SIZE directly.
> 
> Additionally add missing BUILD_BUG_ON check for S2MPS15 device (the
> check ensures that internal arrays are big enough to hold data for all
> of regulators on all devices).
> 
> Reported-by: Arnd Bergmann <arnd@arndb.de>
> Signed-off-by: Krzysztof Kozlowski <k.kozlowski@samsung.com>
> 

[...]

>  	case S2MPS11X:
>  		s2mps11->rdev_num = ARRAY_SIZE(s2mps11_regulators);

Why don't we remove rdev_num at all? It's not used that much
other than in the probe function.

>  		regulators = s2mps11_regulators;
> -		BUILD_BUG_ON(S2MPS_REGULATOR_MAX < s2mps11->rdev_num);
> +		BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps11_regulators));
>  		break;

Andi
--
To unsubscribe from this list: send the line "unsubscribe linux-samsung-soc" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Krzysztof Kozlowski Feb. 18, 2016, 1:38 a.m. UTC | #2
On 18.02.2016 10:37, Andi Shyti wrote:
> Hi Krzysztof,
> 
>> Following BUILD_BUG_ON using a variable fails for some of the compilers
>> and optimization levels (reported for gcc 4.9):
>> 	var = ARRAY_SIZE(s2mps15_regulators);
>> 	BUILD_BUG_ON(S2MPS_REGULATOR_MAX < var);
>> Fix this by using ARRAY_SIZE directly.
>>
>> Additionally add missing BUILD_BUG_ON check for S2MPS15 device (the
>> check ensures that internal arrays are big enough to hold data for all
>> of regulators on all devices).
>>
>> Reported-by: Arnd Bergmann <arnd@arndb.de>
>> Signed-off-by: Krzysztof Kozlowski <k.kozlowski@samsung.com>
>>
> 
> [...]
> 
>>  	case S2MPS11X:
>>  		s2mps11->rdev_num = ARRAY_SIZE(s2mps11_regulators);
> 
> Why don't we remove rdev_num at all? It's not used that much
> other than in the probe function.

Remove from probe? It is used in probe and removal would make the code
more complicated than it should be.

Best regards,
Krzysztof
--
To unsubscribe from this list: send the line "unsubscribe linux-samsung-soc" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Andi Shyti Feb. 18, 2016, 1:46 a.m. UTC | #3
> >> Following BUILD_BUG_ON using a variable fails for some of the compilers
> >> and optimization levels (reported for gcc 4.9):
> >> 	var = ARRAY_SIZE(s2mps15_regulators);
> >> 	BUILD_BUG_ON(S2MPS_REGULATOR_MAX < var);
> >> Fix this by using ARRAY_SIZE directly.
> >>
> >> Additionally add missing BUILD_BUG_ON check for S2MPS15 device (the
> >> check ensures that internal arrays are big enough to hold data for all
> >> of regulators on all devices).
> >>
> >> Reported-by: Arnd Bergmann <arnd@arndb.de>
> >> Signed-off-by: Krzysztof Kozlowski <k.kozlowski@samsung.com>
> >>
> > 
> > [...]
> > 
> >>  	case S2MPS11X:
> >>  		s2mps11->rdev_num = ARRAY_SIZE(s2mps11_regulators);
> > 
> > Why don't we remove rdev_num at all? It's not used that much
> > other than in the probe function.
> 
> Remove from probe? It is used in probe and removal would make the code
> more complicated than it should be.

no, I mean remove it from s2mps11_info. Other than in the probe
this value is used only once in s2mps11_pmic_dt_parse() (which is
called by probe()).

Andi
--
To unsubscribe from this list: send the line "unsubscribe linux-samsung-soc" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Krzysztof Kozlowski Feb. 18, 2016, 1:48 a.m. UTC | #4
On 18.02.2016 10:46, Andi Shyti wrote:
>>>> Following BUILD_BUG_ON using a variable fails for some of the compilers
>>>> and optimization levels (reported for gcc 4.9):
>>>> 	var = ARRAY_SIZE(s2mps15_regulators);
>>>> 	BUILD_BUG_ON(S2MPS_REGULATOR_MAX < var);
>>>> Fix this by using ARRAY_SIZE directly.
>>>>
>>>> Additionally add missing BUILD_BUG_ON check for S2MPS15 device (the
>>>> check ensures that internal arrays are big enough to hold data for all
>>>> of regulators on all devices).
>>>>
>>>> Reported-by: Arnd Bergmann <arnd@arndb.de>
>>>> Signed-off-by: Krzysztof Kozlowski <k.kozlowski@samsung.com>
>>>>
>>>
>>> [...]
>>>
>>>>  	case S2MPS11X:
>>>>  		s2mps11->rdev_num = ARRAY_SIZE(s2mps11_regulators);
>>>
>>> Why don't we remove rdev_num at all? It's not used that much
>>> other than in the probe function.
>>
>> Remove from probe? It is used in probe and removal would make the code
>> more complicated than it should be.
> 
> no, I mean remove it from s2mps11_info. Other than in the probe
> this value is used only once in s2mps11_pmic_dt_parse() (which is
> called by probe()).

Sure, it can be safely removed from s2mps11_info... but it won't affect
this issue and this patch. Still the local variable would be used in
probe leading to compiler optimization choices impacting BUILD_BUG_ON.

BR,
Krzysztof

--
To unsubscribe from this list: send the line "unsubscribe linux-samsung-soc" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Arnd Bergmann Feb. 18, 2016, 9:05 a.m. UTC | #5
On Thursday 18 February 2016 10:48:39 Krzysztof Kozlowski wrote:
> 
> Sure, it can be safely removed from s2mps11_info... but it won't affect
> this issue and this patch. Still the local variable would be used in
> probe leading to compiler optimization choices impacting BUILD_BUG_ON.

A local variable probably works fine, I can test that if we think that
would improve the code.

	Arnd
--
To unsubscribe from this list: send the line "unsubscribe linux-samsung-soc" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Arnd Bergmann Feb. 18, 2016, 9:06 a.m. UTC | #6
On Thursday 18 February 2016 09:35:07 Krzysztof Kozlowski wrote:
> Following BUILD_BUG_ON using a variable fails for some of the compilers
> and optimization levels (reported for gcc 4.9):
>         var = ARRAY_SIZE(s2mps15_regulators);
>         BUILD_BUG_ON(S2MPS_REGULATOR_MAX < var);
> Fix this by using ARRAY_SIZE directly.
> 
> Additionally add missing BUILD_BUG_ON check for S2MPS15 device (the
> check ensures that internal arrays are big enough to hold data for all
> of regulators on all devices).
> 
> Reported-by: Arnd Bergmann <arnd@arndb.de>
> Signed-off-by: Krzysztof Kozlowski <k.kozlowski@samsung.com>

I've verified that this is the exact patch I have successfully tested
on locally and in Olof's autobuilder which reported the problem.

>         case S2MPS15X:
>                 s2mps11->rdev_num = ARRAY_SIZE(s2mps15_regulators);
>                 regulators = s2mps15_regulators;
> +               BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps15_regulators));
>                 break;
> 

My version did not add this line, but it seems correct.

Tested-by: Arnd Bergmann <arnd@arndb.de>
Reviewed-by: Arnd Bergmann <arnd@arndb.de>
--
To unsubscribe from this list: send the line "unsubscribe linux-samsung-soc" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/drivers/regulator/s2mps11.c b/drivers/regulator/s2mps11.c
index 3242ffc0cb25..df553fb40d82 100644
--- a/drivers/regulator/s2mps11.c
+++ b/drivers/regulator/s2mps11.c
@@ -1090,26 +1090,27 @@  static int s2mps11_pmic_probe(struct platform_device *pdev)
 	case S2MPS11X:
 		s2mps11->rdev_num = ARRAY_SIZE(s2mps11_regulators);
 		regulators = s2mps11_regulators;
-		BUILD_BUG_ON(S2MPS_REGULATOR_MAX < s2mps11->rdev_num);
+		BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps11_regulators));
 		break;
 	case S2MPS13X:
 		s2mps11->rdev_num = ARRAY_SIZE(s2mps13_regulators);
 		regulators = s2mps13_regulators;
-		BUILD_BUG_ON(S2MPS_REGULATOR_MAX < s2mps11->rdev_num);
+		BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps13_regulators));
 		break;
 	case S2MPS14X:
 		s2mps11->rdev_num = ARRAY_SIZE(s2mps14_regulators);
 		regulators = s2mps14_regulators;
-		BUILD_BUG_ON(S2MPS_REGULATOR_MAX < s2mps11->rdev_num);
+		BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps14_regulators));
 		break;
 	case S2MPS15X:
 		s2mps11->rdev_num = ARRAY_SIZE(s2mps15_regulators);
 		regulators = s2mps15_regulators;
+		BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mps15_regulators));
 		break;
 	case S2MPU02:
 		s2mps11->rdev_num = ARRAY_SIZE(s2mpu02_regulators);
 		regulators = s2mpu02_regulators;
-		BUILD_BUG_ON(S2MPS_REGULATOR_MAX < s2mps11->rdev_num);
+		BUILD_BUG_ON(S2MPS_REGULATOR_MAX < ARRAY_SIZE(s2mpu02_regulators));
 		break;
 	default:
 		dev_err(&pdev->dev, "Invalid device type: %u\n",