perf path: Make mkpath thread safe, remove 16384 bytes from .bss

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>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: K Prateek Nayak <kprateek.nayak@amd.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Ross Zwisler <zwisler@chromium.org>
Cc: Sean Christopherson <seanjc@google.com>
Cc: Steven Rostedt (VMware) <rostedt@goodmis.org>
Cc: Tiezhu Yang <yangtiezhu@loongson.cn>
Cc: Yang Jihong <yangjihong1@huawei.com>
Link: https://lore.kernel.org/r/20230526183401.2326121-13-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Ian Rogers 2023-05-26 11:33:57 -07:00 committed by Arnaldo Carvalho de Melo
parent 430952e6d7
commit 370ce164de
5 changed files with 14 additions and 34 deletions

View File

@ -12,6 +12,7 @@
#include "util/debug.h" #include "util/debug.h"
#include "util/config.h" #include "util/config.h"
#include <linux/string.h> #include <linux/string.h>
#include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -157,7 +158,8 @@ int cmd_config(int argc, const char **argv)
{ {
int i, ret = -1; int i, ret = -1;
struct perf_config_set *set; 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; const char *config_filename;
bool changed = false; bool changed = false;

View File

@ -19,6 +19,7 @@
#include <linux/string.h> #include <linux/string.h>
#include <linux/zalloc.h> #include <linux/zalloc.h>
#include <errno.h> #include <errno.h>
#include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -389,9 +390,10 @@ static int get_html_page_path(char **page_path, const char *page)
{ {
struct stat st; struct stat st;
const char *html_path = system_path(PERF_HTML_PATH); const char *html_path = system_path(PERF_HTML_PATH);
char path[PATH_MAX];
/* Check that we have a perf documentation directory. */ /* 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)) { || !S_ISREG(st.st_mode)) {
pr_err("'%s': not a documentation directory.", html_path); pr_err("'%s': not a documentation directory.", html_path);
return -1; return -1;

View File

@ -26,6 +26,6 @@ static inline int is_absolute_path(const char *path)
return path[0] == '/'; 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 */ #endif /* __PERF_CACHE_H */

View File

@ -543,6 +543,7 @@ static char *home_perfconfig(void)
const char *home = NULL; const char *home = NULL;
char *config; char *config;
struct stat st; struct stat st;
char path[PATH_MAX];
home = getenv("HOME"); home = getenv("HOME");
@ -554,7 +555,7 @@ static char *home_perfconfig(void)
if (!home || !*home || !perf_config_global()) if (!home || !*home || !perf_config_global())
return NULL; return NULL;
config = strdup(mkpath("%s/.perfconfig", home)); config = strdup(mkpath(path, sizeof(path), "%s/.perfconfig", home));
if (config == NULL) { if (config == NULL) {
pr_warning("Not enough memory to process %s/.perfconfig, ignoring it.\n", home); pr_warning("Not enough memory to process %s/.perfconfig, ignoring it.\n", home);
return NULL; return NULL;

View File

@ -1,16 +1,4 @@
// SPDX-License-Identifier: GPL-2.0 // 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 "path.h"
#include "cache.h" #include "cache.h"
#include <linux/kernel.h> #include <linux/kernel.h>
@ -22,18 +10,6 @@
#include <dirent.h> #include <dirent.h>
#include <unistd.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) static char *cleanup_path(char *path)
{ {
/* Clean it up */ /* Clean it up */
@ -45,18 +21,17 @@ static char *cleanup_path(char *path)
return path; return path;
} }
char *mkpath(const char *fmt, ...) char *mkpath(char *path_buf, size_t sz, const char *fmt, ...)
{ {
va_list args; va_list args;
unsigned len; unsigned len;
char *pathname = get_pathname();
va_start(args, fmt); va_start(args, fmt);
len = vsnprintf(pathname, PATH_MAX, fmt, args); len = vsnprintf(path_buf, sz, fmt, args);
va_end(args); va_end(args);
if (len >= PATH_MAX) if (len >= sz)
return bad_path; strncpy(path_buf, "/bad-path/", sz);
return cleanup_path(pathname); return cleanup_path(path_buf);
} }
int path__join(char *bf, size_t size, const char *path1, const char *path2) int path__join(char *bf, size_t size, const char *path1, const char *path2)