2023-09-25 08:20:43

by Lukasz Luba

[permalink] [raw]
Subject: [PATCH v4 06/18] PM: EM: Check if the get_cost() callback is present in em_compute_costs()

The em_compute_cost() is going to be re-used in runtime modified EM
code path. Thus, make sure that this common code is safe and won't
try to use the NULL pointer. The former em_compute_cost() didn't have to
care about runtime modification code path. The upcoming changes introduce
such option, but with different callback. Those two paths which use
get_cost() (during first EM registration) or update_power() (during
runtime modification) need to be safely handled in em_compute_costs().

Signed-off-by: Lukasz Luba <[email protected]>
---
kernel/power/energy_model.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/power/energy_model.c b/kernel/power/energy_model.c
index 7ea882401833..35e07933b34a 100644
--- a/kernel/power/energy_model.c
+++ b/kernel/power/energy_model.c
@@ -116,7 +116,7 @@ static int em_compute_costs(struct device *dev, struct em_perf_state *table,
for (i = nr_states - 1; i >= 0; i--) {
unsigned long power_res, cost;

- if (flags & EM_PERF_DOMAIN_ARTIFICIAL) {
+ if (flags & EM_PERF_DOMAIN_ARTIFICIAL && cb->get_cost) {
ret = cb->get_cost(dev, table[i].frequency, &cost);
if (ret || !cost || cost > EM_MAX_POWER) {
dev_err(dev, "EM: invalid cost %lu %d\n",
--
2.25.1


2023-09-26 18:48:13

by Rafael J. Wysocki

[permalink] [raw]
Subject: Re: [PATCH v4 06/18] PM: EM: Check if the get_cost() callback is present in em_compute_costs()

On Mon, Sep 25, 2023 at 10:11 AM Lukasz Luba <[email protected]> wrote:
>
> The em_compute_cost() is going to be re-used in runtime modified EM
> code path. Thus, make sure that this common code is safe and won't
> try to use the NULL pointer. The former em_compute_cost() didn't have to
> care about runtime modification code path. The upcoming changes introduce
> such option, but with different callback. Those two paths which use
> get_cost() (during first EM registration) or update_power() (during
> runtime modification) need to be safely handled in em_compute_costs().

I would just say something like this:

"Subsequent changes will introduce a case in which cb->get_cost may
not be set in em_compute_costs(), so add a check to ensure that it is
not NULL before attempting to dereference it."

The rest of the changelog is just redundant IMO.

>
> Signed-off-by: Lukasz Luba <[email protected]>
> ---
> kernel/power/energy_model.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/kernel/power/energy_model.c b/kernel/power/energy_model.c
> index 7ea882401833..35e07933b34a 100644
> --- a/kernel/power/energy_model.c
> +++ b/kernel/power/energy_model.c
> @@ -116,7 +116,7 @@ static int em_compute_costs(struct device *dev, struct em_perf_state *table,
> for (i = nr_states - 1; i >= 0; i--) {
> unsigned long power_res, cost;
>
> - if (flags & EM_PERF_DOMAIN_ARTIFICIAL) {
> + if (flags & EM_PERF_DOMAIN_ARTIFICIAL && cb->get_cost) {
> ret = cb->get_cost(dev, table[i].frequency, &cost);
> if (ret || !cost || cost > EM_MAX_POWER) {
> dev_err(dev, "EM: invalid cost %lu %d\n",
> --
> 2.25.1
>

2023-09-29 08:43:09

by Lukasz Luba

[permalink] [raw]
Subject: Re: [PATCH v4 06/18] PM: EM: Check if the get_cost() callback is present in em_compute_costs()



On 9/26/23 19:46, Rafael J. Wysocki wrote:
> On Mon, Sep 25, 2023 at 10:11 AM Lukasz Luba <[email protected]> wrote:
>>
>> The em_compute_cost() is going to be re-used in runtime modified EM
>> code path. Thus, make sure that this common code is safe and won't
>> try to use the NULL pointer. The former em_compute_cost() didn't have to
>> care about runtime modification code path. The upcoming changes introduce
>> such option, but with different callback. Those two paths which use
>> get_cost() (during first EM registration) or update_power() (during
>> runtime modification) need to be safely handled in em_compute_costs().
>
> I would just say something like this:
>
> "Subsequent changes will introduce a case in which cb->get_cost may
> not be set in em_compute_costs(), so add a check to ensure that it is
> not NULL before attempting to dereference it."
>
> The rest of the changelog is just redundant IMO.
>

Make sense, thanks, I'll change that.

2023-10-23 18:24:11

by Daniel Lezcano

[permalink] [raw]
Subject: Re: [PATCH v4 06/18] PM: EM: Check if the get_cost() callback is present in em_compute_costs()

On 25/09/2023 10:11, Lukasz Luba wrote:
> The em_compute_cost() is going to be re-used in runtime modified EM
> code path. Thus, make sure that this common code is safe and won't
> try to use the NULL pointer. The former em_compute_cost() didn't have to
> care about runtime modification code path. The upcoming changes introduce
> such option, but with different callback. Those two paths which use
> get_cost() (during first EM registration) or update_power() (during
> runtime modification) need to be safely handled in em_compute_costs().
>
> Signed-off-by: Lukasz Luba <[email protected]>
> ---
> kernel/power/energy_model.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/kernel/power/energy_model.c b/kernel/power/energy_model.c
> index 7ea882401833..35e07933b34a 100644
> --- a/kernel/power/energy_model.c
> +++ b/kernel/power/energy_model.c
> @@ -116,7 +116,7 @@ static int em_compute_costs(struct device *dev, struct em_perf_state *table,
> for (i = nr_states - 1; i >= 0; i--) {
> unsigned long power_res, cost;
>
> - if (flags & EM_PERF_DOMAIN_ARTIFICIAL) {
> + if (flags & EM_PERF_DOMAIN_ARTIFICIAL && cb->get_cost) {
> ret = cb->get_cost(dev, table[i].frequency, &cost);
> if (ret || !cost || cost > EM_MAX_POWER) {
> dev_err(dev, "EM: invalid cost %lu %d\n",

I do believe & operator has lower precedence than && operator, thus the
test is actually:

(flags & (EM_PERF_DOMAIN_ARTIFICIAL && cb->get_cost))

but it should be

((flags & EM_PERF_DOMAIN_ARTIFICIAL) && cb->get_cost)

Right ?

--
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro: <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog

2023-10-24 08:13:49

by Lukasz Luba

[permalink] [raw]
Subject: Re: [PATCH v4 06/18] PM: EM: Check if the get_cost() callback is present in em_compute_costs()



On 10/23/23 19:23, Daniel Lezcano wrote:
> On 25/09/2023 10:11, Lukasz Luba wrote:
>> The em_compute_cost() is going to be re-used in runtime modified EM
>> code path. Thus, make sure that this common code is safe and won't
>> try to use the NULL pointer. The former em_compute_cost() didn't have to
>> care about runtime modification code path. The upcoming changes introduce
>> such option, but with different callback. Those two paths which use
>> get_cost() (during first EM registration) or update_power() (during
>> runtime modification) need to be safely handled in em_compute_costs().
>>
>> Signed-off-by: Lukasz Luba <[email protected]>
>> ---
>>   kernel/power/energy_model.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/kernel/power/energy_model.c b/kernel/power/energy_model.c
>> index 7ea882401833..35e07933b34a 100644
>> --- a/kernel/power/energy_model.c
>> +++ b/kernel/power/energy_model.c
>> @@ -116,7 +116,7 @@ static int em_compute_costs(struct device *dev,
>> struct em_perf_state *table,
>>       for (i = nr_states - 1; i >= 0; i--) {
>>           unsigned long power_res, cost;
>> -        if (flags & EM_PERF_DOMAIN_ARTIFICIAL) {
>> +        if (flags & EM_PERF_DOMAIN_ARTIFICIAL && cb->get_cost) {
>>               ret = cb->get_cost(dev, table[i].frequency, &cost);
>>               if (ret || !cost || cost > EM_MAX_POWER) {
>>                   dev_err(dev, "EM: invalid cost %lu %d\n",
>
> I do believe & operator has lower precedence than && operator, thus the
> test is actually:
>
>     (flags & (EM_PERF_DOMAIN_ARTIFICIAL && cb->get_cost))
>
> but it should be
>
>     ((flags & EM_PERF_DOMAIN_ARTIFICIAL) && cb->get_cost)
>
> Right ?
>

The bitwise '&' is stronger than logical '&&', so the code will
work as in your 2nd example. Although, I will change it and add
parentheses for better reading.

Thanks!