[PATCH v2 2/4] arch/x86/events/core: Convert sprintf/snprintf to sysfs_emit

From: Li Zhijian
Date: Fri Jan 26 2024 - 01:33:51 EST


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
^^^^^ this one cannot convert to sysfs_emit
where it intends to print a sub-string.

> ./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

Most of this patch is generated by:
$ make coccicheck MODE=patch COCCI=scripts/coccinelle/api/device_attr_show.cocci M=arch/x86/events/core.c | sed -n '6,$p' | patch -p1

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>
---
V2:
- Address Adrian's comment:
leave "snprintf(page, next_str - str + 1, "%s", str)" alone, it
intends to print a sub-string, that cannot covert to sysfs_emit simply.
-
V2: extract patch from the patch set[1] so that maintainer accept it separately.
[1] https://lore.kernel.org/lkml/20240116041129.3937800-1-lizhijian@xxxxxxxxxxx/
Signed-off-by: Li Zhijian <lizhijian@xxxxxxxxxxx>
---
arch/x86/events/core.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
index 09050641ce5d..20d963196e4c 100644
--- a/arch/x86/events/core.c
+++ b/arch/x86/events/core.c
@@ -1832,7 +1832,7 @@ ssize_t events_sysfs_show(struct device *dev, struct device_attribute *attr, cha

/* string trumps id */
if (pmu_attr->event_str)
- return sprintf(page, "%s\n", pmu_attr->event_str);
+ return sysfs_emit(page, "%s\n", pmu_attr->event_str);

return x86_pmu.events_sysfs_show(page, config);
}
@@ -1855,7 +1855,7 @@ ssize_t events_ht_sysfs_show(struct device *dev, struct device_attribute *attr,
* if they are owned by some other guest. The user tool
* has to re-read when a thread sibling gets onlined later.
*/
- return sprintf(page, "%s",
+ return sysfs_emit(page, "%s",
topology_max_smt_threads() > 1 ?
pmu_attr->event_str_ht :
pmu_attr->event_str_noht);
@@ -1872,7 +1872,7 @@ ssize_t events_hybrid_sysfs_show(struct device *dev,
int i;

if (hweight64(pmu_attr->pmu_type) == 1)
- return sprintf(page, "%s", pmu_attr->event_str);
+ return sysfs_emit(page, "%s", pmu_attr->event_str);

/*
* Hybrid PMUs may support the same event name, but with different
@@ -1894,7 +1894,7 @@ ssize_t events_hybrid_sysfs_show(struct device *dev,
if (next_str)
return snprintf(page, next_str - str + 1, "%s", str);
else
- return sprintf(page, "%s", str);
+ return sysfs_emit(page, "%s", str);
}
str = strchr(str, ';');
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);
--
2.29.2