Skip to content

Commit 5e62d53

Browse files
Perry Yuanrafaeljw
authored andcommitted
cpufreq: update to sysfs_emit() for safer buffer handling
Replace sprintf() and scnprintf() with sysfs_emit() and sysfs_emit_at() in the cpufreq core. This ensures safer buffer handling and consistency with sysfs interfaces. Update show_scaling_available_governors() and related functions for compliance with the new API. Signed-off-by: Perry Yuan <[email protected]> Reviewed-by: Mario Limonciello <[email protected]> Link: https://patch.msgid.link/[email protected] [ rjw: Changelog edits ] Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent ede951c commit 5e62d53

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

drivers/cpufreq/cpufreq.c

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us);
608608
static ssize_t show_boost(struct kobject *kobj,
609609
struct kobj_attribute *attr, char *buf)
610610
{
611-
return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
611+
return sysfs_emit(buf, "%d\n", cpufreq_driver->boost_enabled);
612612
}
613613

614614
static ssize_t store_boost(struct kobject *kobj, struct kobj_attribute *attr,
@@ -739,7 +739,7 @@ static struct cpufreq_governor *cpufreq_parse_governor(char *str_governor)
739739
static ssize_t show_##file_name \
740740
(struct cpufreq_policy *policy, char *buf) \
741741
{ \
742-
return sprintf(buf, "%u\n", policy->object); \
742+
return sysfs_emit(buf, "%u\n", policy->object); \
743743
}
744744

745745
show_one(cpuinfo_min_freq, cpuinfo.min_freq);
@@ -760,11 +760,11 @@ static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
760760

761761
freq = arch_freq_get_on_cpu(policy->cpu);
762762
if (freq)
763-
ret = sprintf(buf, "%u\n", freq);
763+
ret = sysfs_emit(buf, "%u\n", freq);
764764
else if (cpufreq_driver->setpolicy && cpufreq_driver->get)
765-
ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
765+
ret = sysfs_emit(buf, "%u\n", cpufreq_driver->get(policy->cpu));
766766
else
767-
ret = sprintf(buf, "%u\n", policy->cur);
767+
ret = sysfs_emit(buf, "%u\n", policy->cur);
768768
return ret;
769769
}
770770

@@ -798,9 +798,9 @@ static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
798798
unsigned int cur_freq = __cpufreq_get(policy);
799799

800800
if (cur_freq)
801-
return sprintf(buf, "%u\n", cur_freq);
801+
return sysfs_emit(buf, "%u\n", cur_freq);
802802

803-
return sprintf(buf, "<unknown>\n");
803+
return sysfs_emit(buf, "<unknown>\n");
804804
}
805805

806806
/*
@@ -809,12 +809,11 @@ static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
809809
static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
810810
{
811811
if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
812-
return sprintf(buf, "powersave\n");
812+
return sysfs_emit(buf, "powersave\n");
813813
else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
814-
return sprintf(buf, "performance\n");
814+
return sysfs_emit(buf, "performance\n");
815815
else if (policy->governor)
816-
return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
817-
policy->governor->name);
816+
return sysfs_emit(buf, "%s\n", policy->governor->name);
818817
return -EINVAL;
819818
}
820819

@@ -873,7 +872,7 @@ static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
873872
struct cpufreq_governor *t;
874873

875874
if (!has_target()) {
876-
i += sprintf(buf, "performance powersave");
875+
i += sysfs_emit(buf, "performance powersave");
877876
goto out;
878877
}
879878

@@ -882,11 +881,11 @@ static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
882881
if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
883882
- (CPUFREQ_NAME_LEN + 2)))
884883
break;
885-
i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
884+
i += sysfs_emit_at(buf, i, "%s ", t->name);
886885
}
887886
mutex_unlock(&cpufreq_governor_mutex);
888887
out:
889-
i += sprintf(&buf[i], "\n");
888+
i += sysfs_emit_at(buf, i, "\n");
890889
return i;
891890
}
892891

@@ -896,15 +895,15 @@ ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
896895
unsigned int cpu;
897896

898897
for_each_cpu(cpu, mask) {
899-
i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u ", cpu);
898+
i += sysfs_emit_at(buf, i, "%u ", cpu);
900899
if (i >= (PAGE_SIZE - 5))
901900
break;
902901
}
903902

904903
/* Remove the extra space at the end */
905904
i--;
906905

907-
i += sprintf(&buf[i], "\n");
906+
i += sysfs_emit_at(buf, i, "\n");
908907
return i;
909908
}
910909
EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
@@ -947,7 +946,7 @@ static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
947946
static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
948947
{
949948
if (!policy->governor || !policy->governor->show_setspeed)
950-
return sprintf(buf, "<unsupported>\n");
949+
return sysfs_emit(buf, "<unsupported>\n");
951950

952951
return policy->governor->show_setspeed(policy, buf);
953952
}
@@ -961,8 +960,8 @@ static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
961960
int ret;
962961
ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
963962
if (!ret)
964-
return sprintf(buf, "%u\n", limit);
965-
return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
963+
return sysfs_emit(buf, "%u\n", limit);
964+
return sysfs_emit(buf, "%u\n", policy->cpuinfo.max_freq);
966965
}
967966

968967
cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);

0 commit comments

Comments
 (0)