perf pmu: Avoid a path name copy
authorIan Rogers <irogers@google.com>
Wed, 23 Aug 2023 08:08:05 +0000 (01:08 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 23 Aug 2023 11:42:17 +0000 (08:42 -0300)
Rather than read a base path and append into a 2nd path, read the base
path directly into output buffer and append to that.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Gaosheng Cui <cuigaosheng1@huawei.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jing Zhang <renyu.zj@linux.alibaba.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: John Garry <john.g.garry@oracle.com>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Rob Herring <robh@kernel.org>
Link: https://lore.kernel.org/r/20230823080828.1460376-3-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/pmu.c

index a7f05e4..7683c67 100644 (file)
@@ -1756,17 +1756,19 @@ int perf_pmu__event_source_devices_fd(void)
  * then pathname will be filled with
  * "/sys/bus/event_source/devices/cs_etm/format"
  *
- * Return 0 if the sysfs mountpoint couldn't be found or if no
- * characters were written.
+ * Return 0 if the sysfs mountpoint couldn't be found, if no characters were
+ * written or if the buffer size is exceeded.
  */
 int perf_pmu__pathname_scnprintf(char *buf, size_t size,
                                 const char *pmu_name, const char *filename)
 {
-       char base_path[PATH_MAX];
+       size_t len;
 
-       if (!perf_pmu__event_source_devices_scnprintf(base_path, sizeof(base_path)))
+       len = perf_pmu__event_source_devices_scnprintf(buf, size);
+       if (!len || (len + strlen(pmu_name) + strlen(filename) + 1)  >= size)
                return 0;
-       return scnprintf(buf, size, "%s%s/%s", base_path, pmu_name, filename);
+
+       return scnprintf(buf + len, size - len, "%s/%s", pmu_name, filename);
 }
 
 int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags)