Re: [PATCH 04/42] arch/x86/events/core: Convert snprintf to sysfs_emit

From: Adrian Hunter
Date: Tue Jan 16 2024 - 02:02:25 EST


On 16/01/24 06:51, Li Zhijian wrote:
> Per filesystems/sysfs.rst, show() should only use sysfs_emit()
> or sysfs_emit_at() when formatting the value to be returned to user space.
>
> coccinelle complains that there are still a couple of functions that use
> snprintf(). Convert them to sysfs_emit().
>
>> ./arch/x86/events/core.c:1895:11-19: WARNING: please use sysfs_emit
>> ./arch/x86/events/core.c:2542:8-16: WARNING: please use sysfs_emit
>> ./arch/x86/events/core.c:2600:8-16: WARNING: please use sysfs_emit
>
> No functional change intended
>
> CC: Peter Zijlstra <peterz@xxxxxxxxxxxxx>
> CC: Ingo Molnar <mingo@xxxxxxxxxx>
> CC: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
> CC: Mark Rutland <mark.rutland@xxxxxxx>
> CC: Alexander Shishkin <alexander.shishkin@xxxxxxxxxxxxxxx>
> CC: Jiri Olsa <jolsa@xxxxxxxxxx>
> CC: Namhyung Kim <namhyung@xxxxxxxxxx>
> CC: Ian Rogers <irogers@xxxxxxxxxx>
> CC: Adrian Hunter <adrian.hunter@xxxxxxxxx>
> CC: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
> CC: Borislav Petkov <bp@xxxxxxxxx>
> CC: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>
> CC: x86@xxxxxxxxxx
> CC: "H. Peter Anvin" <hpa@xxxxxxxxx>
> CC: linux-perf-users@xxxxxxxxxxxxxxx
> Signed-off-by: Li Zhijian <lizhijian@xxxxxxxxxxx>
> ---
> arch/x86/events/core.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
> index 40ad1425ffa2..52e5707be03b 100644
> --- a/arch/x86/events/core.c
> +++ b/arch/x86/events/core.c
> @@ -1892,7 +1892,7 @@ ssize_t events_hybrid_sysfs_show(struct device *dev,
> if (x86_pmu.hybrid_pmu[i].pmu_type & pmu->pmu_type) {
> next_str = strchr(str, ';');
> if (next_str)
> - return snprintf(page, next_str - str + 1, "%s", str);
> + return sysfs_emit(page, "%s", str);

The intention seems to be to print only up to, and not including, the next ';',
but sysfs_emit() is not going to do that.

> else
> return sprintf(page, "%s", str);
> }
> @@ -2539,7 +2539,7 @@ static ssize_t get_attr_rdpmc(struct device *cdev,
> struct device_attribute *attr,
> char *buf)
> {
> - return snprintf(buf, 40, "%d\n", x86_pmu.attr_rdpmc);
> + return sysfs_emit(buf, "%d\n", x86_pmu.attr_rdpmc);
> }
>
> static ssize_t set_attr_rdpmc(struct device *cdev,
> @@ -2597,7 +2597,7 @@ static ssize_t max_precise_show(struct device *cdev,
> struct device_attribute *attr,
> char *buf)
> {
> - return snprintf(buf, PAGE_SIZE, "%d\n", x86_pmu_max_precise());
> + return sysfs_emit(buf, "%d\n", x86_pmu_max_precise());
> }
>
> static DEVICE_ATTR_RO(max_precise);