cpuidle/coupled: Add sanity check for safe_state_index
PATCH v3 1/1 cpuidle/coupled: Add sanity check for safe_state_index
From: Xunlei Pang <[email protected]>
Since we are using cpuidle_driver::safe_state_index directly as the target state index, it is better to add the sanity check at the point of registering the driver.
---v2->v3:
Move the code to a new cpuidle_coupled_state_verify() depending on CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED, to avoid using #ifdef in function bodies. Thanks Rafael's comments on this.
drivers/cpuidle/coupled.c | 22 ++++++++++++++++++++++ drivers/cpuidle/cpuidle.h | 6 ++++++ drivers/cpuidle/driver.c | 4 ++++ 3 files changed, 32 insertions(+)
drivers/cpuidle/coupled.c +22 -0
@@ -187,6 +187,28 @@ bool cpuidle_state_is_coupled(struct cpuidle_driver *drv, int state) }/** +* cpuidle_coupled_state_verify - check if the coupled states are correctly set. +* @drv: struct cpuidle_driver for the platform +* +* Returns 0 for valid state values, a negative error code otherwise: +* * -EINVAL if any coupled state(safe_state_index) is wrongly set. +*/ +int cpuidle_coupled_state_verify (struct cpuidle_driver *drv) +{ +int i; + +for (i = drv->state_count -1 ; i >=0 ; i--) { +if (cpuidle_state_is_coupled (drv, i) && + (drv->safe_state_index == i || + drv->safe_state_index <0 || + drv->safe_state_index >= drv->state_count)) +return -EINVAL ; + } + +return 0 ; +} + +/** * cpuidle_coupled_set_ready - mark a cpu as ready * @coupled: the struct coupled that contains the current cpu */
drivers/cpuidle/cpuidle.h +6 -0
@@ -35,6 +35,7 @@ extern void cpuidle_remove_sysfs(struct cpuidle_device *dev);#ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED bool cpuidle_state_is_coupled (struct cpuidle_driver *drv,int state); +int cpuidle_coupled_state_verify (struct cpuidle_driver *drv);int cpuidle_enter_state_coupled (struct cpuidle_device *dev,struct cpuidle_driver *drv,int next_state);int cpuidle_coupled_register_device (struct cpuidle_device *dev);
@@ -46,6 +47,11 @@ bool cpuidle_state_is_coupled(struct cpuidle_driver *drv, int state)return false; } +static inline int cpuidle_coupled_state_verify (struct cpuidle_driver *drv) +{ +return 0 ; +} +static inline int cpuidle_enter_state_coupled (struct cpuidle_device *dev,struct cpuidle_driver *drv,int next_state) {
drivers/cpuidle/driver.c +4 -0
@@ -227,6 +227,10 @@ static int __cpuidle_register_driver(struct cpuidle_driver *drv)if (!drv || !drv->state_count)return -EINVAL ; + ret =cpuidle_coupled_state_verify (drv); +if (ret) +return ret; +if (cpuidle_disabled ())return -ENODEV ;
Signature · 1 line
1.9.1
Quoted 7 lines · Xunlei Pang
From: Xunlei Pang <[email protected]>
Since we are using
cpuidle_driver::safe_state_indexdirectly as the target state index, it is better to add the sanity check at the point of registering the driver.Signed-off-by Xunlei Pang <[email protected]>
I'm queuing this one up for the next PM pull request, thanks!
Quoted 84 lines
---
v2->v3:
Move the code to a newcpuidle_coupled_state_verify()depending onCONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED, to avoid using #ifdef in function bodies. Thanks Rafael's comments on this.drivers/cpuidle/coupled.c | 22 ++++++++++++++++++++++ drivers/cpuidle/cpuidle.h | 6 ++++++ drivers/cpuidle/driver.c | 4 ++++ 3 files changed, 32 insertions(+)drivers/cpuidle/coupled.c +22
-0@@ -187,6 +187,28 @@ bool cpuidle_state_is_coupled(struct cpuidle_driver *drv, int state) }/** +* cpuidle_coupled_state_verify - check if the coupled states are correctly set. +* @drv: struct cpuidle_driver for the platform +* +* Returns 0 for valid state values, a negative error code otherwise: +* * -EINVAL if any coupled state(safe_state_index) is wrongly set. +*/ +int cpuidle_coupled_state_verify (struct cpuidle_driver *drv) +{ +int i; + +for (i = drv->state_count -1 ; i >=0 ; i--) { +if (cpuidle_state_is_coupled (drv, i) && + (drv->safe_state_index == i || + drv->safe_state_index <0 || + drv->safe_state_index >= drv->state_count)) +return -EINVAL ; + } + +return 0 ; +} + +/** * cpuidle_coupled_set_ready - mark a cpu as ready * @coupled: the struct coupled that contains the current cpu */ drivers/cpuidle/cpuidle.h +6
-0@@ -35,6 +35,7 @@ extern void cpuidle_remove_sysfs(struct cpuidle_device *dev);#ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED bool cpuidle_state_is_coupled (struct cpuidle_driver *drv,int state); +int cpuidle_coupled_state_verify (struct cpuidle_driver *drv);int cpuidle_enter_state_coupled (struct cpuidle_device *dev,struct cpuidle_driver *drv,int next_state);int cpuidle_coupled_register_device (struct cpuidle_device *dev);@@ -46,6 +47,11 @@ bool cpuidle_state_is_coupled(struct cpuidle_driver *drv, int state)return false; } +static inline int cpuidle_coupled_state_verify (struct cpuidle_driver *drv) +{ +return 0 ; +} +static inline int cpuidle_enter_state_coupled (struct cpuidle_device *dev,struct cpuidle_driver *drv,int next_state) {drivers/cpuidle/driver.c +4
-0@@ -227,6 +227,10 @@ static int __cpuidle_register_driver(struct cpuidle_driver *drv)if (!drv || !drv->state_count)return -EINVAL ; + ret =cpuidle_coupled_state_verify (drv); +if (ret) +return ret; +if (cpuidle_disabled ())return -ENODEV ;
Signature · 2 lines
I speak only for myself. Rafael J. Wysocki, Intel Open Source Technology Center.
Hi Rafael,
Quoted 10 lines · Rafael J. Wysocki
Quoted 7 lines · Xunlei Pang
From: Xunlei Pang <[email protected]>
Since we are using
cpuidle_driver::safe_state_indexdirectly as the target state index, it is better to add the sanity check at the point of registering the driver.Signed-off-by Xunlei Pang <[email protected]>I'm queuing this one up for the next PM pull request, thanks!
Thanks!
Regards,
Xunlei
Quoted 90 lines
Quoted 76 lines
---
v2->v3:
Move the code to a newcpuidle_coupled_state_verify()depending onCONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED, to avoid using #ifdef in function bodies. Thanks Rafael's comments on this.drivers/cpuidle/coupled.c | 22 ++++++++++++++++++++++ drivers/cpuidle/cpuidle.h | 6 ++++++ drivers/cpuidle/driver.c | 4 ++++ 3 files changed, 32 insertions(+)drivers/cpuidle/coupled.c +22
-0@@ -187,6 +187,28 @@ bool cpuidle_state_is_coupled(struct cpuidle_driver *drv, int state) }/** +* cpuidle_coupled_state_verify - check if the coupled states are correctly set. +* @drv: struct cpuidle_driver for the platform +* +* Returns 0 for valid state values, a negative error code otherwise: +* * -EINVAL if any coupled state(safe_state_index) is wrongly set. +*/ +int cpuidle_coupled_state_verify (struct cpuidle_driver *drv) +{ +int i; + +for (i = drv->state_count -1 ; i >=0 ; i--) { +if (cpuidle_state_is_coupled (drv, i) && + (drv->safe_state_index == i || + drv->safe_state_index <0 || + drv->safe_state_index >= drv->state_count)) +return -EINVAL ; + } + +return 0 ; +} + +/** * cpuidle_coupled_set_ready - mark a cpu as ready * @coupled: the struct coupled that contains the current cpu */ drivers/cpuidle/cpuidle.h +6
-0@@ -35,6 +35,7 @@ extern void cpuidle_remove_sysfs(struct cpuidle_device *dev);#ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED bool cpuidle_state_is_coupled (struct cpuidle_driver *drv,int state); +int cpuidle_coupled_state_verify (struct cpuidle_driver *drv);int cpuidle_enter_state_coupled (struct cpuidle_device *dev,struct cpuidle_driver *drv,int next_state);int cpuidle_coupled_register_device (struct cpuidle_device *dev);@@ -46,6 +47,11 @@ bool cpuidle_state_is_coupled(struct cpuidle_driver *drv, int state)return false; } +static inline int cpuidle_coupled_state_verify (struct cpuidle_driver *drv) +{ +return 0 ; +} +static inline int cpuidle_enter_state_coupled (struct cpuidle_device *dev,struct cpuidle_driver *drv,int next_state) {drivers/cpuidle/driver.c +0
-0drivers/cpuidle/driver.c static int __cpuidle_register_driver(struct cpuidle_driver *drv) · line 236
Quoted 10 lines
drivers/cpuidle/driver.c +4
-0@@ -227,6 +227,10 @@ static int __cpuidle_register_driver(struct cpuidle_driver *drv)if (!drv || !drv->state_count)return -EINVAL ; + ret =cpuidle_coupled_state_verify (drv); +if (ret) +return ret; +if (cpuidle_disabled ())return -ENODEV ;--
I speak only for myself.
Rafael J. Wysocki, Intel Open Source Technology Center.