From: Ian Rogers <irogers@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>, Namhyung Kim <namhyung@kernel.org>,
Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
"Masami Hiramatsu (Google)" <mhiramat@kernel.org>,
"Steven Rostedt (Google)" <rostedt@goodmis.org>,
Ross Zwisler <zwisler@chromium.org>, Leo Yan <leo.yan@linaro.org>,
Tiezhu Yang <yangtiezhu@loongson.cn>,
Yang Jihong <yangjihong1@huawei.com>,
Andi Kleen <ak@linux.intel.com>,
Kan Liang <kan.liang@linux.intel.com>,
Ravi Bangoria <ravi.bangoria@amd.com>,
Sean Christopherson <seanjc@google.com>,
K Prateek Nayak <kprateek.nayak@amd.com>,
Paolo Bonzini <pbonzini@redhat.com>,
linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org
Subject: [PATCH v2 12/16] perf path: Make mkpath thread safe
Date: Fri, 26 May 2023 11:33:57 -0700 [thread overview]
Message-ID: <20230526183401.2326121-13-irogers@google.com> (raw)
In-Reply-To: <20230526183401.2326121-1-irogers@google.com>
Avoid 4 static arrays for paths, pass in a char[] buffer to use. Makes
mkpath thread safe for the small number of users. Also removes 16,384
bytes from .bss.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/builtin-config.c | 4 +++-
tools/perf/builtin-help.c | 4 +++-
tools/perf/util/cache.h | 2 +-
tools/perf/util/config.c | 3 ++-
tools/perf/util/path.c | 35 +++++------------------------------
5 files changed, 14 insertions(+), 34 deletions(-)
diff --git a/tools/perf/builtin-config.c b/tools/perf/builtin-config.c
index 2603015f98be..2e8363778935 100644
--- a/tools/perf/builtin-config.c
+++ b/tools/perf/builtin-config.c
@@ -12,6 +12,7 @@
#include "util/debug.h"
#include "util/config.h"
#include <linux/string.h>
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
@@ -157,7 +158,8 @@ int cmd_config(int argc, const char **argv)
{
int i, ret = -1;
struct perf_config_set *set;
- char *user_config = mkpath("%s/.perfconfig", getenv("HOME"));
+ char path[PATH_MAX];
+ char *user_config = mkpath(path, sizeof(path), "%s/.perfconfig", getenv("HOME"));
const char *config_filename;
bool changed = false;
diff --git a/tools/perf/builtin-help.c b/tools/perf/builtin-help.c
index 3e7f52054fac..b2a368ae295a 100644
--- a/tools/perf/builtin-help.c
+++ b/tools/perf/builtin-help.c
@@ -19,6 +19,7 @@
#include <linux/string.h>
#include <linux/zalloc.h>
#include <errno.h>
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -389,9 +390,10 @@ static int get_html_page_path(char **page_path, const char *page)
{
struct stat st;
const char *html_path = system_path(PERF_HTML_PATH);
+ char path[PATH_MAX];
/* Check that we have a perf documentation directory. */
- if (stat(mkpath("%s/perf.html", html_path), &st)
+ if (stat(mkpath(path, sizeof(path), "%s/perf.html", html_path), &st)
|| !S_ISREG(st.st_mode)) {
pr_err("'%s': not a documentation directory.", html_path);
return -1;
diff --git a/tools/perf/util/cache.h b/tools/perf/util/cache.h
index 9f2e36ef5072..0b61840d4226 100644
--- a/tools/perf/util/cache.h
+++ b/tools/perf/util/cache.h
@@ -26,6 +26,6 @@ static inline int is_absolute_path(const char *path)
return path[0] == '/';
}
-char *mkpath(const char *fmt, ...) __printf(1, 2);
+char *mkpath(char *path_buf, size_t sz, const char *fmt, ...) __printf(3, 4);
#endif /* __PERF_CACHE_H */
diff --git a/tools/perf/util/config.c b/tools/perf/util/config.c
index 658170b8dcef..f340dc73db6d 100644
--- a/tools/perf/util/config.c
+++ b/tools/perf/util/config.c
@@ -543,6 +543,7 @@ static char *home_perfconfig(void)
const char *home = NULL;
char *config;
struct stat st;
+ char path[PATH_MAX];
home = getenv("HOME");
@@ -554,7 +555,7 @@ static char *home_perfconfig(void)
if (!home || !*home || !perf_config_global())
return NULL;
- config = strdup(mkpath("%s/.perfconfig", home));
+ config = strdup(mkpath(path, sizeof(path), "%s/.perfconfig", home));
if (config == NULL) {
pr_warning("Not enough memory to process %s/.perfconfig, ignoring it.\n", home);
return NULL;
diff --git a/tools/perf/util/path.c b/tools/perf/util/path.c
index ce80b79be103..00adf872bf00 100644
--- a/tools/perf/util/path.c
+++ b/tools/perf/util/path.c
@@ -1,16 +1,4 @@
// SPDX-License-Identifier: GPL-2.0
-/*
- * I'm tired of doing "vsnprintf()" etc just to open a
- * file, so here's a "return static buffer with printf"
- * interface for paths.
- *
- * It's obviously not thread-safe. Sue me. But it's quite
- * useful for doing things like
- *
- * f = open(mkpath("%s/%s.perf", base, name), O_RDONLY);
- *
- * which is what it's designed for.
- */
#include "path.h"
#include "cache.h"
#include <linux/kernel.h>
@@ -22,18 +10,6 @@
#include <dirent.h>
#include <unistd.h>
-static char bad_path[] = "/bad-path/";
-/*
- * One hack:
- */
-static char *get_pathname(void)
-{
- static char pathname_array[4][PATH_MAX];
- static int idx;
-
- return pathname_array[3 & ++idx];
-}
-
static char *cleanup_path(char *path)
{
/* Clean it up */
@@ -45,18 +21,17 @@ static char *cleanup_path(char *path)
return path;
}
-char *mkpath(const char *fmt, ...)
+char *mkpath(char *path_buf, size_t sz, const char *fmt, ...)
{
va_list args;
unsigned len;
- char *pathname = get_pathname();
va_start(args, fmt);
- len = vsnprintf(pathname, PATH_MAX, fmt, args);
+ len = vsnprintf(path_buf, sz, fmt, args);
va_end(args);
- if (len >= PATH_MAX)
- return bad_path;
- return cleanup_path(pathname);
+ if (len >= sz)
+ strncpy(path_buf, "/bad-path/", sz);
+ return cleanup_path(path_buf);
}
int path__join(char *bf, size_t size, const char *path1, const char *path2)
--
2.41.0.rc0.172.g3f132b7071-goog
next prev parent reply other threads:[~2023-05-26 18:35 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-26 18:33 [PATCH v2 00/16] Address some perf memory/data size issues Ian Rogers
2023-05-26 18:33 ` [PATCH v2 01/16] perf header: Make nodes dynamic in write_mem_topology Ian Rogers
2023-05-26 18:33 ` [PATCH v2 02/16] perf test x86: insn-x86 test data is immutable so mark it const Ian Rogers
2023-05-26 18:33 ` [PATCH v2 03/16] perf test x86: intel-pt-test " Ian Rogers
2023-05-26 18:33 ` [PATCH v2 04/16] perf trace: Make some large static arrays const Ian Rogers
2023-05-26 18:33 ` [PATCH v2 05/16] perf trace beauty: Make MSR " Ian Rogers
2023-05-26 18:33 ` [PATCH v2 06/16] tools api fs: Avoid large static PATH_MAX arrays Ian Rogers
2023-05-26 18:33 ` [PATCH v2 07/16] tools lib api fs tracing_path: Remove two unused MAX_PATH paths Ian Rogers
2023-05-26 18:33 ` [PATCH v2 08/16] perf daemon: Dynamically allocate path to perf Ian Rogers
2023-05-26 18:33 ` [PATCH v2 09/16] perf lock: Dynamically allocate lockhash_table Ian Rogers
2023-05-26 18:33 ` [PATCH v2 10/16] perf timechart: Make large arrays dynamic Ian Rogers
2023-05-26 18:33 ` [PATCH v2 11/16] perf probe: Dynamically allocate params memory Ian Rogers
2023-06-01 4:50 ` Masami Hiramatsu
2023-05-26 18:33 ` Ian Rogers [this message]
2023-05-26 18:33 ` [PATCH v2 13/16] perf scripting-engines: Move static to local variable Ian Rogers
2023-05-26 18:33 ` [PATCH v2 14/16] tools api fs: Dynamically allocate cgroupfs mount point cache Ian Rogers
2023-05-26 18:34 ` [PATCH v2 15/16] perf test pmu: Avoid 2 static path arrays Ian Rogers
2023-05-26 18:34 ` [PATCH v2 16/16] libsubcmd: Avoid two path statics Ian Rogers
2023-05-30 4:57 ` [PATCH v2 00/16] Address some perf memory/data size issues Andi Kleen
2023-05-30 6:54 ` Ian Rogers
2023-05-30 7:59 ` Andi Kleen
2023-05-30 14:45 ` Ian Rogers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230526183401.2326121-13-irogers@google.com \
--to=irogers@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=ak@linux.intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=jolsa@kernel.org \
--cc=kan.liang@linux.intel.com \
--cc=kprateek.nayak@amd.com \
--cc=leo.yan@linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mhiramat@kernel.org \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=pbonzini@redhat.com \
--cc=peterz@infradead.org \
--cc=ravi.bangoria@amd.com \
--cc=rostedt@goodmis.org \
--cc=seanjc@google.com \
--cc=yangjihong1@huawei.com \
--cc=yangtiezhu@loongson.cn \
--cc=zwisler@chromium.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).