2024-01-26 06:44:41

by Zhijian Li (Fujitsu)

[permalink] [raw]
Subject: [PATCH v2 1/4] arch/x86/events/amd: Convert snprintf/sprintf 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/amd/core.c:1282:8-16: WARNING: please use sysfs_emit

This patch is generated by
$ make coccicheck MODE=patch COCCI=scripts/coccinelle/api/device_attr_show.cocci M=arch/x86/events/amd | 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]>
Signed-off-by: Li Zhijian <[email protected]>
---
V2: extract patch from the patch set[1] so that maintainer can accept it separately.
[1] https://lore.kernel.org/lkml/[email protected]/
---
arch/x86/events/amd/core.c | 2 +-
arch/x86/events/amd/iommu.c | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/x86/events/amd/core.c b/arch/x86/events/amd/core.c
index 81f6d8275b6b..0d520592010c 100644
--- a/arch/x86/events/amd/core.c
+++ b/arch/x86/events/amd/core.c
@@ -1279,7 +1279,7 @@ static ssize_t branches_show(struct device *cdev,
struct device_attribute *attr,
char *buf)
{
- return snprintf(buf, PAGE_SIZE, "%d\n", x86_pmu.lbr_nr);
+ return sysfs_emit(buf, "%d\n", x86_pmu.lbr_nr);
}

static DEVICE_ATTR_RO(branches);
diff --git a/arch/x86/events/amd/iommu.c b/arch/x86/events/amd/iommu.c
index b15f7b950d2e..1126bda96d8f 100644
--- a/arch/x86/events/amd/iommu.c
+++ b/arch/x86/events/amd/iommu.c
@@ -89,7 +89,7 @@ static ssize_t _iommu_event_show(struct device *dev,
{
struct amd_iommu_event_desc *event =
container_of(attr, struct amd_iommu_event_desc, attr);
- return sprintf(buf, "%s\n", event->event);
+ return sysfs_emit(buf, "%s\n", event->event);
}

#define AMD_IOMMU_EVENT_DESC(_name, _event) \
--
2.29.2



2024-02-07 08:10:39

by Adrian Hunter

[permalink] [raw]
Subject: Re: [PATCH v2 1/4] arch/x86/events/amd: Convert snprintf/sprintf 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/amd/core.c:1282:8-16: WARNING: please use sysfs_emit
>
> This patch is generated by
> $ make coccicheck MODE=patch COCCI=scripts/coccinelle/api/device_attr_show.cocci M=arch/x86/events/amd | 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]>
> Signed-off-by: Li Zhijian <[email protected]>

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

> ---
> V2: extract patch from the patch set[1] so that maintainer can accept it separately.
> [1] https://lore.kernel.org/lkml/[email protected]/
> ---
> arch/x86/events/amd/core.c | 2 +-
> arch/x86/events/amd/iommu.c | 2 +-
> 2 files changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/arch/x86/events/amd/core.c b/arch/x86/events/amd/core.c
> index 81f6d8275b6b..0d520592010c 100644
> --- a/arch/x86/events/amd/core.c
> +++ b/arch/x86/events/amd/core.c
> @@ -1279,7 +1279,7 @@ static ssize_t branches_show(struct device *cdev,
> struct device_attribute *attr,
> char *buf)
> {
> - return snprintf(buf, PAGE_SIZE, "%d\n", x86_pmu.lbr_nr);
> + return sysfs_emit(buf, "%d\n", x86_pmu.lbr_nr);
> }
>
> static DEVICE_ATTR_RO(branches);
> diff --git a/arch/x86/events/amd/iommu.c b/arch/x86/events/amd/iommu.c
> index b15f7b950d2e..1126bda96d8f 100644
> --- a/arch/x86/events/amd/iommu.c
> +++ b/arch/x86/events/amd/iommu.c
> @@ -89,7 +89,7 @@ static ssize_t _iommu_event_show(struct device *dev,
> {
> struct amd_iommu_event_desc *event =
> container_of(attr, struct amd_iommu_event_desc, attr);
> - return sprintf(buf, "%s\n", event->event);
> + return sysfs_emit(buf, "%s\n", event->event);
> }
>
> #define AMD_IOMMU_EVENT_DESC(_name, _event) \