Skip to content

Commit 101642e

Browse files
committed
Merge branches 'pm-cpuidle' and 'pm-powercap'
Merge cpuidle and power capping changes for 6.18-rc1: - Fail cpuidle device registration if there is one already to avoid sysfs-related issues (Rafael Wysocki) - Use sysfs_emit()/sysfs_emit_at() instead of sprintf()/scnprintf() in cpuidle (Vivek Yadav) - Fix device and OF node leaks at probe in the qcom-spm cpuidle driver and drop unnecessary initialisations from it (Johan Hovold) - Remove unnecessary address-of operators from the intel_idle cpuidle driver (Kaushlendra Kumar) - Rearrange main loop in menu_select() to make the code in that funtion easier to follow (Rafael Wysocki) - Convert values in microseconds to ktime using us_to_ktime() where applicable in the intel_idle power capping driver (Xichao Zhao) * pm-cpuidle: cpuidle: Fail cpuidle device registration if there is one already cpuidle: sysfs: Use sysfs_emit()/sysfs_emit_at() instead of sprintf()/scnprintf() cpuidle: qcom-spm: drop unnecessary initialisations cpuidle: qcom-spm: fix device and OF node leaks at probe intel_idle: Remove unnecessary address-of operators cpuidle: governors: menu: Rearrange main loop in menu_select() * pm-powercap: powercap: idle_inject: use us_to_ktime() where appropriate
3 parents d6fd599 + 7b1b796 + 03cf825 commit 101642e

6 files changed

Lines changed: 197 additions & 187 deletions

File tree

drivers/cpuidle/cpuidle-qcom-spm.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,30 +86,33 @@ static const struct of_device_id qcom_idle_state_match[] = {
8686

8787
static int spm_cpuidle_register(struct device *cpuidle_dev, int cpu)
8888
{
89-
struct platform_device *pdev = NULL;
89+
struct platform_device *pdev;
9090
struct device_node *cpu_node, *saw_node;
91-
struct cpuidle_qcom_spm_data *data = NULL;
91+
struct cpuidle_qcom_spm_data *data;
9292
int ret;
9393

9494
cpu_node = of_cpu_device_node_get(cpu);
9595
if (!cpu_node)
9696
return -ENODEV;
9797

9898
saw_node = of_parse_phandle(cpu_node, "qcom,saw", 0);
99+
of_node_put(cpu_node);
99100
if (!saw_node)
100101
return -ENODEV;
101102

102103
pdev = of_find_device_by_node(saw_node);
103104
of_node_put(saw_node);
104-
of_node_put(cpu_node);
105105
if (!pdev)
106106
return -ENODEV;
107107

108108
data = devm_kzalloc(cpuidle_dev, sizeof(*data), GFP_KERNEL);
109-
if (!data)
109+
if (!data) {
110+
put_device(&pdev->dev);
110111
return -ENOMEM;
112+
}
111113

112114
data->spm = dev_get_drvdata(&pdev->dev);
115+
put_device(&pdev->dev);
113116
if (!data->spm)
114117
return -EINVAL;
115118

drivers/cpuidle/cpuidle.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,8 +635,14 @@ static void __cpuidle_device_init(struct cpuidle_device *dev)
635635
static int __cpuidle_register_device(struct cpuidle_device *dev)
636636
{
637637
struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
638+
unsigned int cpu = dev->cpu;
638639
int i, ret;
639640

641+
if (per_cpu(cpuidle_devices, cpu)) {
642+
pr_info("CPU%d: cpuidle device already registered\n", cpu);
643+
return -EEXIST;
644+
}
645+
640646
if (!try_module_get(drv->owner))
641647
return -EINVAL;
642648

@@ -648,7 +654,7 @@ static int __cpuidle_register_device(struct cpuidle_device *dev)
648654
dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_USER;
649655
}
650656

651-
per_cpu(cpuidle_devices, dev->cpu) = dev;
657+
per_cpu(cpuidle_devices, cpu) = dev;
652658
list_add(&dev->device_list, &cpuidle_detected_devices);
653659

654660
ret = cpuidle_coupled_register_device(dev);

drivers/cpuidle/governors/menu.c

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -314,45 +314,47 @@ static int menu_select(struct cpuidle_driver *drv, struct cpuidle_device *dev,
314314
if (s->exit_latency_ns > latency_req)
315315
break;
316316

317-
if (s->target_residency_ns > predicted_ns) {
318-
/*
319-
* Use a physical idle state, not busy polling, unless
320-
* a timer is going to trigger soon enough.
321-
*/
322-
if ((drv->states[idx].flags & CPUIDLE_FLAG_POLLING) &&
323-
s->target_residency_ns <= data->next_timer_ns) {
324-
predicted_ns = s->target_residency_ns;
325-
idx = i;
326-
break;
327-
}
328-
if (predicted_ns < TICK_NSEC)
329-
break;
330-
331-
if (!tick_nohz_tick_stopped()) {
332-
/*
333-
* If the state selected so far is shallow,
334-
* waking up early won't hurt, so retain the
335-
* tick in that case and let the governor run
336-
* again in the next iteration of the loop.
337-
*/
338-
predicted_ns = drv->states[idx].target_residency_ns;
339-
break;
340-
}
317+
if (s->target_residency_ns <= predicted_ns) {
318+
idx = i;
319+
continue;
320+
}
321+
322+
/*
323+
* Use a physical idle state, not busy polling, unless a timer
324+
* is going to trigger soon enough.
325+
*/
326+
if ((drv->states[idx].flags & CPUIDLE_FLAG_POLLING) &&
327+
s->target_residency_ns <= data->next_timer_ns) {
328+
predicted_ns = s->target_residency_ns;
329+
idx = i;
330+
break;
331+
}
341332

333+
if (predicted_ns < TICK_NSEC)
334+
break;
335+
336+
if (!tick_nohz_tick_stopped()) {
342337
/*
343-
* If the state selected so far is shallow and this
344-
* state's target residency matches the time till the
345-
* closest timer event, select this one to avoid getting
346-
* stuck in the shallow one for too long.
338+
* If the state selected so far is shallow, waking up
339+
* early won't hurt, so retain the tick in that case and
340+
* let the governor run again in the next iteration of
341+
* the idle loop.
347342
*/
348-
if (drv->states[idx].target_residency_ns < TICK_NSEC &&
349-
s->target_residency_ns <= delta_tick)
350-
idx = i;
351-
352-
return idx;
343+
predicted_ns = drv->states[idx].target_residency_ns;
344+
break;
353345
}
354346

355-
idx = i;
347+
/*
348+
* If the state selected so far is shallow and this state's
349+
* target residency matches the time till the closest timer
350+
* event, select this one to avoid getting stuck in the shallow
351+
* one for too long.
352+
*/
353+
if (drv->states[idx].target_residency_ns < TICK_NSEC &&
354+
s->target_residency_ns <= delta_tick)
355+
idx = i;
356+
357+
return idx;
356358
}
357359

358360
if (idx == -1)

drivers/cpuidle/sysfs.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ static ssize_t show_available_governors(struct device *dev,
2727

2828
mutex_lock(&cpuidle_lock);
2929
list_for_each_entry(tmp, &cpuidle_governors, governor_list) {
30-
if (i >= (ssize_t) (PAGE_SIZE - (CPUIDLE_NAME_LEN + 2)))
30+
if (i >= (ssize_t)(PAGE_SIZE - (CPUIDLE_NAME_LEN + 2)))
3131
goto out;
3232

33-
i += scnprintf(&buf[i], CPUIDLE_NAME_LEN + 1, "%s ", tmp->name);
33+
i += sysfs_emit_at(buf, i, "%.*s ", CPUIDLE_NAME_LEN, tmp->name);
3434
}
3535

3636
out:
37-
i+= sprintf(&buf[i], "\n");
37+
i += sysfs_emit_at(buf, i, "\n");
3838
mutex_unlock(&cpuidle_lock);
3939
return i;
4040
}
@@ -49,9 +49,9 @@ static ssize_t show_current_driver(struct device *dev,
4949
spin_lock(&cpuidle_driver_lock);
5050
drv = cpuidle_get_driver();
5151
if (drv)
52-
ret = sprintf(buf, "%s\n", drv->name);
52+
ret = sysfs_emit(buf, "%s\n", drv->name);
5353
else
54-
ret = sprintf(buf, "none\n");
54+
ret = sysfs_emit(buf, "none\n");
5555
spin_unlock(&cpuidle_driver_lock);
5656

5757
return ret;
@@ -65,9 +65,9 @@ static ssize_t show_current_governor(struct device *dev,
6565

6666
mutex_lock(&cpuidle_lock);
6767
if (cpuidle_curr_governor)
68-
ret = sprintf(buf, "%s\n", cpuidle_curr_governor->name);
68+
ret = sysfs_emit(buf, "%s\n", cpuidle_curr_governor->name);
6969
else
70-
ret = sprintf(buf, "none\n");
70+
ret = sysfs_emit(buf, "none\n");
7171
mutex_unlock(&cpuidle_lock);
7272

7373
return ret;
@@ -230,15 +230,15 @@ static struct cpuidle_state_attr attr_##_name = __ATTR(_name, 0644, show, store)
230230
static ssize_t show_state_##_name(struct cpuidle_state *state, \
231231
struct cpuidle_state_usage *state_usage, char *buf) \
232232
{ \
233-
return sprintf(buf, "%u\n", state->_name);\
233+
return sysfs_emit(buf, "%u\n", state->_name);\
234234
}
235235

236236
#define define_show_state_ull_function(_name) \
237237
static ssize_t show_state_##_name(struct cpuidle_state *state, \
238238
struct cpuidle_state_usage *state_usage, \
239239
char *buf) \
240240
{ \
241-
return sprintf(buf, "%llu\n", state_usage->_name);\
241+
return sysfs_emit(buf, "%llu\n", state_usage->_name);\
242242
}
243243

244244
#define define_show_state_str_function(_name) \
@@ -247,16 +247,16 @@ static ssize_t show_state_##_name(struct cpuidle_state *state, \
247247
char *buf) \
248248
{ \
249249
if (state->_name[0] == '\0')\
250-
return sprintf(buf, "<null>\n");\
251-
return sprintf(buf, "%s\n", state->_name);\
250+
return sysfs_emit(buf, "<null>\n");\
251+
return sysfs_emit(buf, "%s\n", state->_name);\
252252
}
253253

254254
#define define_show_state_time_function(_name) \
255255
static ssize_t show_state_##_name(struct cpuidle_state *state, \
256256
struct cpuidle_state_usage *state_usage, \
257257
char *buf) \
258258
{ \
259-
return sprintf(buf, "%llu\n", ktime_to_us(state->_name##_ns)); \
259+
return sysfs_emit(buf, "%llu\n", ktime_to_us(state->_name##_ns)); \
260260
}
261261

262262
define_show_state_time_function(exit_latency)
@@ -273,14 +273,14 @@ static ssize_t show_state_time(struct cpuidle_state *state,
273273
struct cpuidle_state_usage *state_usage,
274274
char *buf)
275275
{
276-
return sprintf(buf, "%llu\n", ktime_to_us(state_usage->time_ns));
276+
return sysfs_emit(buf, "%llu\n", ktime_to_us(state_usage->time_ns));
277277
}
278278

279279
static ssize_t show_state_disable(struct cpuidle_state *state,
280280
struct cpuidle_state_usage *state_usage,
281281
char *buf)
282282
{
283-
return sprintf(buf, "%llu\n",
283+
return sysfs_emit(buf, "%llu\n",
284284
state_usage->disable & CPUIDLE_STATE_DISABLED_BY_USER);
285285
}
286286

@@ -310,7 +310,7 @@ static ssize_t show_state_default_status(struct cpuidle_state *state,
310310
struct cpuidle_state_usage *state_usage,
311311
char *buf)
312312
{
313-
return sprintf(buf, "%s\n",
313+
return sysfs_emit(buf, "%s\n",
314314
state->flags & CPUIDLE_FLAG_OFF ? "disabled" : "enabled");
315315
}
316316

@@ -358,7 +358,7 @@ static ssize_t show_state_s2idle_##_name(struct cpuidle_state *state, \
358358
struct cpuidle_state_usage *state_usage, \
359359
char *buf) \
360360
{ \
361-
return sprintf(buf, "%llu\n", state_usage->s2idle_##_name);\
361+
return sysfs_emit(buf, "%llu\n", state_usage->s2idle_##_name);\
362362
}
363363

364364
define_show_state_s2idle_ull_function(usage);
@@ -550,7 +550,7 @@ static ssize_t show_driver_name(struct cpuidle_driver *drv, char *buf)
550550
ssize_t ret;
551551

552552
spin_lock(&cpuidle_driver_lock);
553-
ret = sprintf(buf, "%s\n", drv ? drv->name : "none");
553+
ret = sysfs_emit(buf, "%s\n", drv ? drv->name : "none");
554554
spin_unlock(&cpuidle_driver_lock);
555555

556556
return ret;

0 commit comments

Comments
 (0)