[PATCH v2 4/4] arch/x86/events/intel: Convert snprintf/sprintf to sysfs_emit
From: Li Zhijian
Date: Fri Jan 26 2024 - 01:37:27 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/intel/core.c:5614:8-16: WARNING: please use sysfs_emit or sysfs_emit_at
> ./arch/x86/events/intel/core.c:5623:8-16: WARNING: please use sysfs_emit or sysfs_emit_at
> ./arch/x86/events/intel/core.c:5605:8-16: WARNING: please use sysfs_emit or sysfs_emit_at
> ./arch/x86/events/intel/core.c:5651:8-16: WARNING: please use sysfs_emit or sysfs_emit_at
> ./arch/x86/events/intel/core.c:5571:8-16: WARNING: please use sysfs_emit or sysfs_emit_at
Most of this patch is generated by:
$ make coccicheck MODE=patch COCCI=scripts/coccinelle/api/device_attr_show.cocci M=arch/x86/events/intel | 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:
- Rebase to v6.8-rc1(Fix 2 newly introduced abused cases)
- 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/intel/core.c | 12 ++++++------
arch/x86/events/intel/pt.c | 10 +++++-----
arch/x86/events/intel/uncore.c | 2 +-
arch/x86/events/intel/uncore_snbep.c | 2 +-
4 files changed, 13 insertions(+), 13 deletions(-)
diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c
index 3804f21ab049..7effb715dfaa 100644
--- a/arch/x86/events/intel/core.c
+++ b/arch/x86/events/intel/core.c
@@ -5517,7 +5517,7 @@ static ssize_t freeze_on_smi_show(struct device *cdev,
struct device_attribute *attr,
char *buf)
{
- return sprintf(buf, "%lu\n", x86_pmu.attr_freeze_on_smi);
+ return sysfs_emit(buf, "%lu\n", x86_pmu.attr_freeze_on_smi);
}
static DEFINE_MUTEX(freeze_on_smi_mutex);
@@ -5568,7 +5568,7 @@ static ssize_t show_sysctl_tfa(struct device *cdev,
struct device_attribute *attr,
char *buf)
{
- return snprintf(buf, 40, "%d\n", allow_tsx_force_abort);
+ return sysfs_emit(buf, "%d\n", allow_tsx_force_abort);
}
static ssize_t set_sysctl_tfa(struct device *cdev,
@@ -5602,7 +5602,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);
@@ -5611,7 +5611,7 @@ static ssize_t branch_counter_nr_show(struct device *cdev,
struct device_attribute *attr,
char *buf)
{
- return snprintf(buf, PAGE_SIZE, "%d\n", fls(x86_pmu.lbr_counters));
+ return sysfs_emit(buf, "%d\n", fls(x86_pmu.lbr_counters));
}
static DEVICE_ATTR_RO(branch_counter_nr);
@@ -5620,7 +5620,7 @@ static ssize_t branch_counter_width_show(struct device *cdev,
struct device_attribute *attr,
char *buf)
{
- return snprintf(buf, PAGE_SIZE, "%d\n", LBR_INFO_BR_CNTR_BITS);
+ return sysfs_emit(buf, "%d\n", LBR_INFO_BR_CNTR_BITS);
}
static DEVICE_ATTR_RO(branch_counter_width);
@@ -5648,7 +5648,7 @@ static ssize_t pmu_name_show(struct device *cdev,
struct device_attribute *attr,
char *buf)
{
- return snprintf(buf, PAGE_SIZE, "%s\n", pmu_name_str);
+ return sysfs_emit(buf, "%s\n", pmu_name_str);
}
static DEVICE_ATTR_RO(pmu_name);
diff --git a/arch/x86/events/intel/pt.c b/arch/x86/events/intel/pt.c
index 8e2a12235e62..178238d096cd 100644
--- a/arch/x86/events/intel/pt.c
+++ b/arch/x86/events/intel/pt.c
@@ -96,7 +96,7 @@ static ssize_t pt_cap_show(struct device *cdev,
container_of(attr, struct dev_ext_attribute, attr);
enum pt_capabilities cap = (long)ea->var;
- return snprintf(buf, PAGE_SIZE, "%x\n", intel_pt_validate_hw_cap(cap));
+ return sysfs_emit(buf, "%x\n", intel_pt_validate_hw_cap(cap));
}
static struct attribute_group pt_cap_group __ro_after_init = {
@@ -150,11 +150,11 @@ pt_timing_attr_show(struct device *dev, struct device_attribute *attr,
switch (pmu_attr->id) {
case 0:
- return sprintf(page, "%lu\n", pt_pmu.max_nonturbo_ratio);
+ return sysfs_emit(page, "%lu\n", pt_pmu.max_nonturbo_ratio);
case 1:
- return sprintf(page, "%u:%u\n",
- pt_pmu.tsc_art_num,
- pt_pmu.tsc_art_den);
+ return sysfs_emit(page, "%u:%u\n",
+ pt_pmu.tsc_art_num,
+ pt_pmu.tsc_art_den);
default:
break;
}
diff --git a/arch/x86/events/intel/uncore.c b/arch/x86/events/intel/uncore.c
index 7927c0b832fa..b6bb3aa286fe 100644
--- a/arch/x86/events/intel/uncore.c
+++ b/arch/x86/events/intel/uncore.c
@@ -131,7 +131,7 @@ ssize_t uncore_event_show(struct device *dev,
{
struct uncore_event_desc *event =
container_of(attr, struct uncore_event_desc, attr);
- return sprintf(buf, "%s", event->config);
+ return sysfs_emit(buf, "%s", event->config);
}
struct intel_uncore_box *uncore_pmu_to_box(struct intel_uncore_pmu *pmu, int cpu)
diff --git a/arch/x86/events/intel/uncore_snbep.c b/arch/x86/events/intel/uncore_snbep.c
index a96496bef678..553ba9422677 100644
--- a/arch/x86/events/intel/uncore_snbep.c
+++ b/arch/x86/events/intel/uncore_snbep.c
@@ -3754,7 +3754,7 @@ static ssize_t skx_iio_mapping_show(struct device *dev,
long die = (long)ea->var;
struct intel_uncore_topology *pmut = pmu_topology(pmu, die);
- return sprintf(buf, "%04x:%02x\n", pmut ? pmut->iio->segment : 0,
+ return sysfs_emit(buf, "%04x:%02x\n", pmut ? pmut->iio->segment : 0,
pmut ? pmut->iio->pci_bus_no : 0);
}
--
2.29.2