Commit 5e62d53c authored by Perry Yuan's avatar Perry Yuan Committed by Rafael J. Wysocki
Browse files

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: default avatarPerry Yuan <perry.yuan@amd.com>
Reviewed-by: default avatarMario Limonciello <mario.limonciello@amd.com>
Link: https://patch.msgid.link/20240619081520.259971-1-perry.yuan@amd.com


[ rjw: Changelog edits ]
Signed-off-by: default avatarRafael J. Wysocki <rafael.j.wysocki@intel.com>
parent ede951c2
Loading
Loading
Loading
Loading
+18 −19
Original line number Diff line number Diff line
@@ -608,7 +608,7 @@ EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us);
static ssize_t show_boost(struct kobject *kobj,
			  struct kobj_attribute *attr, char *buf)
{
	return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
	return sysfs_emit(buf, "%d\n", cpufreq_driver->boost_enabled);
}

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)
static ssize_t show_##file_name				\
(struct cpufreq_policy *policy, char *buf)		\
{							\
	return sprintf(buf, "%u\n", policy->object);	\
	return sysfs_emit(buf, "%u\n", policy->object);	\
}

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)

	freq = arch_freq_get_on_cpu(policy->cpu);
	if (freq)
		ret = sprintf(buf, "%u\n", freq);
		ret = sysfs_emit(buf, "%u\n", freq);
	else if (cpufreq_driver->setpolicy && cpufreq_driver->get)
		ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
		ret = sysfs_emit(buf, "%u\n", cpufreq_driver->get(policy->cpu));
	else
		ret = sprintf(buf, "%u\n", policy->cur);
		ret = sysfs_emit(buf, "%u\n", policy->cur);
	return ret;
}

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

	if (cur_freq)
		return sprintf(buf, "%u\n", cur_freq);
		return sysfs_emit(buf, "%u\n", cur_freq);

	return sprintf(buf, "<unknown>\n");
	return sysfs_emit(buf, "<unknown>\n");
}

/*
@@ -809,12 +809,11 @@ static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
{
	if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
		return sprintf(buf, "powersave\n");
		return sysfs_emit(buf, "powersave\n");
	else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
		return sprintf(buf, "performance\n");
		return sysfs_emit(buf, "performance\n");
	else if (policy->governor)
		return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
				policy->governor->name);
		return sysfs_emit(buf, "%s\n", policy->governor->name);
	return -EINVAL;
}

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

	if (!has_target()) {
		i += sprintf(buf, "performance powersave");
		i += sysfs_emit(buf, "performance powersave");
		goto out;
	}

@@ -882,11 +881,11 @@ static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
		if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
		    - (CPUFREQ_NAME_LEN + 2)))
			break;
		i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
		i += sysfs_emit_at(buf, i, "%s ", t->name);
	}
	mutex_unlock(&cpufreq_governor_mutex);
out:
	i += sprintf(&buf[i], "\n");
	i += sysfs_emit_at(buf, i, "\n");
	return i;
}

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

	for_each_cpu(cpu, mask) {
		i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u ", cpu);
		i += sysfs_emit_at(buf, i, "%u ", cpu);
		if (i >= (PAGE_SIZE - 5))
			break;
	}
@@ -904,7 +903,7 @@ ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
	/* Remove the extra space at the end */
	i--;

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

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

cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);