2024-01-26 06:34:12

by Zhijian Li (Fujitsu)

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

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 <[email protected]>
CC: Ingo Molnar <[email protected]>
CC: Arnaldo Carvalho de Melo <[email protected]>
CC: Mark Rutland <[email protected]>
CC: Alexander Shishkin <[email protected]>
CC: Jiri Olsa <[email protected]>
CC: Namhyung Kim <[email protected]>
CC: Ian Rogers <[email protected]>
CC: Adrian Hunter <[email protected]>
CC: Thomas Gleixner <[email protected]>
CC: Borislav Petkov <[email protected]>
CC: Dave Hansen <[email protected]>
CC: [email protected]
CC: "H. Peter Anvin" <[email protected]>
CC: [email protected]
Signed-off-by: Li Zhijian <[email protected]>
---
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/[email protected]/
Signed-off-by: Li Zhijian <[email protected]>
---
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



2024-02-07 08:13:09

by Adrian Hunter

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

On 26/01/24 08:13, 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
> ^^^^^ 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 <[email protected]>
> CC: Ingo Molnar <[email protected]>
> CC: Arnaldo Carvalho de Melo <[email protected]>
> CC: Mark Rutland <[email protected]>
> CC: Alexander Shishkin <[email protected]>
> CC: Jiri Olsa <[email protected]>
> CC: Namhyung Kim <[email protected]>
> CC: Ian Rogers <[email protected]>
> CC: Adrian Hunter <[email protected]>
> CC: Thomas Gleixner <[email protected]>
> CC: Borislav Petkov <[email protected]>
> CC: Dave Hansen <[email protected]>
> CC: [email protected]
> CC: "H. Peter Anvin" <[email protected]>
> CC: [email protected]
> Signed-off-by: Li Zhijian <[email protected]>

Reviewed-by: Adrian Hunter <[email protected]>

> ---
> 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/[email protected]/
> Signed-off-by: Li Zhijian <[email protected]>
> ---
> 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);