Skip to content

Commit abceaa9

Browse files
Xunlei Pangrafaeljw
authored andcommitted
cpuidle/coupled: Add sanity check for safe_state_index
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. Signed-off-by: Xunlei Pang <pang.xunlei@linaro.org> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent 4c1ed5a commit abceaa9

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

drivers/cpuidle/coupled.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,28 @@ bool cpuidle_state_is_coupled(struct cpuidle_driver *drv, int state)
186186
return drv->states[state].flags & CPUIDLE_FLAG_COUPLED;
187187
}
188188

189+
/**
190+
* cpuidle_coupled_state_verify - check if the coupled states are correctly set.
191+
* @drv: struct cpuidle_driver for the platform
192+
*
193+
* Returns 0 for valid state values, a negative error code otherwise:
194+
* * -EINVAL if any coupled state(safe_state_index) is wrongly set.
195+
*/
196+
int cpuidle_coupled_state_verify(struct cpuidle_driver *drv)
197+
{
198+
int i;
199+
200+
for (i = drv->state_count - 1; i >= 0; i--) {
201+
if (cpuidle_state_is_coupled(drv, i) &&
202+
(drv->safe_state_index == i ||
203+
drv->safe_state_index < 0 ||
204+
drv->safe_state_index >= drv->state_count))
205+
return -EINVAL;
206+
}
207+
208+
return 0;
209+
}
210+
189211
/**
190212
* cpuidle_coupled_set_ready - mark a cpu as ready
191213
* @coupled: the struct coupled that contains the current cpu

drivers/cpuidle/cpuidle.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ extern void cpuidle_remove_sysfs(struct cpuidle_device *dev);
3535

3636
#ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
3737
bool cpuidle_state_is_coupled(struct cpuidle_driver *drv, int state);
38+
int cpuidle_coupled_state_verify(struct cpuidle_driver *drv);
3839
int cpuidle_enter_state_coupled(struct cpuidle_device *dev,
3940
struct cpuidle_driver *drv, int next_state);
4041
int cpuidle_coupled_register_device(struct cpuidle_device *dev);
@@ -46,6 +47,11 @@ bool cpuidle_state_is_coupled(struct cpuidle_driver *drv, int state)
4647
return false;
4748
}
4849

50+
static inline int cpuidle_coupled_state_verify(struct cpuidle_driver *drv)
51+
{
52+
return 0;
53+
}
54+
4955
static inline int cpuidle_enter_state_coupled(struct cpuidle_device *dev,
5056
struct cpuidle_driver *drv, int next_state)
5157
{

drivers/cpuidle/driver.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ static int __cpuidle_register_driver(struct cpuidle_driver *drv)
227227
if (!drv || !drv->state_count)
228228
return -EINVAL;
229229

230+
ret = cpuidle_coupled_state_verify(drv);
231+
if (ret)
232+
return ret;
233+
230234
if (cpuidle_disabled())
231235
return -ENODEV;
232236

0 commit comments

Comments
 (0)