lkml.org 
[lkml]   [2024]   [Feb]   [7]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
SubjectRe: [PATCH v2 2/4] arch/x86/events/core: Convert sprintf/snprintf to sysfs_emit
From
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 <peterz@infradead.org>
> CC: Ingo Molnar <mingo@redhat.com>
> CC: Arnaldo Carvalho de Melo <acme@kernel.org>
> CC: Mark Rutland <mark.rutland@arm.com>
> CC: Alexander Shishkin <alexander.shishkin@linux.intel.com>
> CC: Jiri Olsa <jolsa@kernel.org>
> CC: Namhyung Kim <namhyung@kernel.org>
> CC: Ian Rogers <irogers@google.com>
> CC: Adrian Hunter <adrian.hunter@intel.com>
> CC: Thomas Gleixner <tglx@linutronix.de>
> CC: Borislav Petkov <bp@alien8.de>
> CC: Dave Hansen <dave.hansen@linux.intel.com>
> CC: x86@kernel.org
> CC: "H. Peter Anvin" <hpa@zytor.com>
> CC: linux-perf-users@vger.kernel.org
> Signed-off-by: Li Zhijian <lizhijian@fujitsu.com>

Reviewed-by: Adrian Hunter <adrian.hunter@intel.com>

> ---
> 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@fujitsu.com/
> Signed-off-by: Li Zhijian <lizhijian@fujitsu.com>
> ---
> 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);


\
 
 \ /
  Last update: 2024-05-27 14:51    [W:0.078 / U:0.008 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site