2018-07-04 17:29:16

by Kees Cook

[permalink] [raw]
Subject: [PATCH] drm/amd/display: Use 2-factor allocator calls

As already done treewide, switch from open-coded multiplication to
2-factor allocation helper.

Signed-off-by: Kees Cook <[email protected]>
---
drivers/gpu/drm/amd/display/modules/color/color_gamma.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/gpu/drm/amd/display/modules/color/color_gamma.c b/drivers/gpu/drm/amd/display/modules/color/color_gamma.c
index 98edaefa2b47..ee69c949bfbf 100644
--- a/drivers/gpu/drm/amd/display/modules/color/color_gamma.c
+++ b/drivers/gpu/drm/amd/display/modules/color/color_gamma.c
@@ -1723,8 +1723,8 @@ bool mod_color_calculate_curve(enum dc_transfer_func_predefined trans,
kvfree(rgb_regamma);
} else if (trans == TRANSFER_FUNCTION_HLG ||
trans == TRANSFER_FUNCTION_HLG12) {
- rgb_regamma = kvzalloc(sizeof(*rgb_regamma) *
- (MAX_HW_POINTS + _EXTRA_POINTS),
+ rgb_regamma = kvcalloc(MAX_HW_POINTS + _EXTRA_POINTS,
+ sizeof(*rgb_regamma),
GFP_KERNEL);
if (!rgb_regamma)
goto rgb_regamma_alloc_fail;
@@ -1802,8 +1802,8 @@ bool mod_color_calculate_degamma_curve(enum dc_transfer_func_predefined trans,
kvfree(rgb_degamma);
} else if (trans == TRANSFER_FUNCTION_HLG ||
trans == TRANSFER_FUNCTION_HLG12) {
- rgb_degamma = kvzalloc(sizeof(*rgb_degamma) *
- (MAX_HW_POINTS + _EXTRA_POINTS),
+ rgb_degamma = kvcalloc(MAX_HW_POINTS + _EXTRA_POINTS,
+ sizeof(*rgb_degamma),
GFP_KERNEL);
if (!rgb_degamma)
goto rgb_degamma_alloc_fail;
--
2.17.1


--
Kees Cook
Pixel Security


2018-07-04 17:54:37

by Harry Wentland

[permalink] [raw]
Subject: Re: [PATCH] drm/amd/display: Use 2-factor allocator calls

On 2018-07-04 01:27 PM, Kees Cook wrote:
> As already done treewide, switch from open-coded multiplication to
> 2-factor allocation helper.
>
> Signed-off-by: Kees Cook <[email protected]>

Reviewed-by: Harry Wentland <[email protected]>

Harry

> ---
> drivers/gpu/drm/amd/display/modules/color/color_gamma.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/gpu/drm/amd/display/modules/color/color_gamma.c b/drivers/gpu/drm/amd/display/modules/color/color_gamma.c
> index 98edaefa2b47..ee69c949bfbf 100644
> --- a/drivers/gpu/drm/amd/display/modules/color/color_gamma.c
> +++ b/drivers/gpu/drm/amd/display/modules/color/color_gamma.c
> @@ -1723,8 +1723,8 @@ bool mod_color_calculate_curve(enum dc_transfer_func_predefined trans,
> kvfree(rgb_regamma);
> } else if (trans == TRANSFER_FUNCTION_HLG ||
> trans == TRANSFER_FUNCTION_HLG12) {
> - rgb_regamma = kvzalloc(sizeof(*rgb_regamma) *
> - (MAX_HW_POINTS + _EXTRA_POINTS),
> + rgb_regamma = kvcalloc(MAX_HW_POINTS + _EXTRA_POINTS,
> + sizeof(*rgb_regamma),
> GFP_KERNEL);
> if (!rgb_regamma)
> goto rgb_regamma_alloc_fail;
> @@ -1802,8 +1802,8 @@ bool mod_color_calculate_degamma_curve(enum dc_transfer_func_predefined trans,
> kvfree(rgb_degamma);
> } else if (trans == TRANSFER_FUNCTION_HLG ||
> trans == TRANSFER_FUNCTION_HLG12) {
> - rgb_degamma = kvzalloc(sizeof(*rgb_degamma) *
> - (MAX_HW_POINTS + _EXTRA_POINTS),
> + rgb_degamma = kvcalloc(MAX_HW_POINTS + _EXTRA_POINTS,
> + sizeof(*rgb_degamma),
> GFP_KERNEL);
> if (!rgb_degamma)
> goto rgb_degamma_alloc_fail;
>

2018-07-05 13:38:43

by Michel Dänzer

[permalink] [raw]
Subject: Re: [PATCH] drm/amd/display: Use 2-factor allocator calls

On 2018-07-04 07:27 PM, Kees Cook wrote:
> As already done treewide, switch from open-coded multiplication to
> 2-factor allocation helper.
>
> Signed-off-by: Kees Cook <[email protected]>
> ---
> drivers/gpu/drm/amd/display/modules/color/color_gamma.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/gpu/drm/amd/display/modules/color/color_gamma.c b/drivers/gpu/drm/amd/display/modules/color/color_gamma.c
> index 98edaefa2b47..ee69c949bfbf 100644
> --- a/drivers/gpu/drm/amd/display/modules/color/color_gamma.c
> +++ b/drivers/gpu/drm/amd/display/modules/color/color_gamma.c
> @@ -1723,8 +1723,8 @@ bool mod_color_calculate_curve(enum dc_transfer_func_predefined trans,
> kvfree(rgb_regamma);
> } else if (trans == TRANSFER_FUNCTION_HLG ||
> trans == TRANSFER_FUNCTION_HLG12) {
> - rgb_regamma = kvzalloc(sizeof(*rgb_regamma) *
> - (MAX_HW_POINTS + _EXTRA_POINTS),
> + rgb_regamma = kvcalloc(MAX_HW_POINTS + _EXTRA_POINTS,
> + sizeof(*rgb_regamma),
> GFP_KERNEL);
> if (!rgb_regamma)
> goto rgb_regamma_alloc_fail;
> @@ -1802,8 +1802,8 @@ bool mod_color_calculate_degamma_curve(enum dc_transfer_func_predefined trans,
> kvfree(rgb_degamma);
> } else if (trans == TRANSFER_FUNCTION_HLG ||
> trans == TRANSFER_FUNCTION_HLG12) {
> - rgb_degamma = kvzalloc(sizeof(*rgb_degamma) *
> - (MAX_HW_POINTS + _EXTRA_POINTS),
> + rgb_degamma = kvcalloc(MAX_HW_POINTS + _EXTRA_POINTS,
> + sizeof(*rgb_degamma),
> GFP_KERNEL);
> if (!rgb_degamma)
> goto rgb_degamma_alloc_fail;
>

Since the values are constant, kvcalloc incurs the overflow checking
overhead for no gain.


--
Earthling Michel Dänzer | http://www.amd.com
Libre software enthusiast | Mesa and X developer

2018-07-05 15:56:22

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH] drm/amd/display: Use 2-factor allocator calls

On Thu, Jul 5, 2018 at 6:37 AM, Michel Dänzer <[email protected]> wrote:
> On 2018-07-04 07:27 PM, Kees Cook wrote:
>> As already done treewide, switch from open-coded multiplication to
>> 2-factor allocation helper.
>>
>> Signed-off-by: Kees Cook <[email protected]>
>> ---
>> drivers/gpu/drm/amd/display/modules/color/color_gamma.c | 8 ++++----
>> 1 file changed, 4 insertions(+), 4 deletions(-)
>>
>> diff --git a/drivers/gpu/drm/amd/display/modules/color/color_gamma.c b/drivers/gpu/drm/amd/display/modules/color/color_gamma.c
>> index 98edaefa2b47..ee69c949bfbf 100644
>> --- a/drivers/gpu/drm/amd/display/modules/color/color_gamma.c
>> +++ b/drivers/gpu/drm/amd/display/modules/color/color_gamma.c
>> @@ -1723,8 +1723,8 @@ bool mod_color_calculate_curve(enum dc_transfer_func_predefined trans,
>> kvfree(rgb_regamma);
>> } else if (trans == TRANSFER_FUNCTION_HLG ||
>> trans == TRANSFER_FUNCTION_HLG12) {
>> - rgb_regamma = kvzalloc(sizeof(*rgb_regamma) *
>> - (MAX_HW_POINTS + _EXTRA_POINTS),
>> + rgb_regamma = kvcalloc(MAX_HW_POINTS + _EXTRA_POINTS,
>> + sizeof(*rgb_regamma),
>> GFP_KERNEL);
>> if (!rgb_regamma)
>> goto rgb_regamma_alloc_fail;
>> @@ -1802,8 +1802,8 @@ bool mod_color_calculate_degamma_curve(enum dc_transfer_func_predefined trans,
>> kvfree(rgb_degamma);
>> } else if (trans == TRANSFER_FUNCTION_HLG ||
>> trans == TRANSFER_FUNCTION_HLG12) {
>> - rgb_degamma = kvzalloc(sizeof(*rgb_degamma) *
>> - (MAX_HW_POINTS + _EXTRA_POINTS),
>> + rgb_degamma = kvcalloc(MAX_HW_POINTS + _EXTRA_POINTS,
>> + sizeof(*rgb_degamma),
>> GFP_KERNEL);
>> if (!rgb_degamma)
>> goto rgb_degamma_alloc_fail;
>>
>
> Since the values are constant, kvcalloc incurs the overflow checking
> overhead for no gain.

The way the macros are designed, they'll get entirely optimized away
in that case:
http://lkml.kernel.org/r/CAGXu5jLw9kMZRkzL_Q7JdyyprntHF6bdVSE_vjyzect3CenDng@mail.gmail.com

Using this patch means if the code ever changes away from constants,
it'll still be safe. And static checkers won't yell about the
open-coded multiplication here. So it's only up-sides. :)

-Kees

--
Kees Cook
Pixel Security