Skip to content

Commit 370ce16

Browse files
captain5050acmel
authored andcommitted
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>
1 parent 430952e commit 370ce16

File tree

5 files changed

+14
-34
lines changed

5 files changed

+14
-34
lines changed

tools/perf/builtin-config.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "util/debug.h"
1313
#include "util/config.h"
1414
#include <linux/string.h>
15+
#include <limits.h>
1516
#include <stdio.h>
1617
#include <stdlib.h>
1718

@@ -157,7 +158,8 @@ int cmd_config(int argc, const char **argv)
157158
{
158159
int i, ret = -1;
159160
struct perf_config_set *set;
160-
char *user_config = mkpath("%s/.perfconfig", getenv("HOME"));
161+
char path[PATH_MAX];
162+
char *user_config = mkpath(path, sizeof(path), "%s/.perfconfig", getenv("HOME"));
161163
const char *config_filename;
162164
bool changed = false;
163165

tools/perf/builtin-help.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/string.h>
2020
#include <linux/zalloc.h>
2121
#include <errno.h>
22+
#include <limits.h>
2223
#include <stdio.h>
2324
#include <stdlib.h>
2425
#include <string.h>
@@ -389,9 +390,10 @@ static int get_html_page_path(char **page_path, const char *page)
389390
{
390391
struct stat st;
391392
const char *html_path = system_path(PERF_HTML_PATH);
393+
char path[PATH_MAX];
392394

393395
/* Check that we have a perf documentation directory. */
394-
if (stat(mkpath("%s/perf.html", html_path), &st)
396+
if (stat(mkpath(path, sizeof(path), "%s/perf.html", html_path), &st)
395397
|| !S_ISREG(st.st_mode)) {
396398
pr_err("'%s': not a documentation directory.", html_path);
397399
return -1;

tools/perf/util/cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ static inline int is_absolute_path(const char *path)
2626
return path[0] == '/';
2727
}
2828

29-
char *mkpath(const char *fmt, ...) __printf(1, 2);
29+
char *mkpath(char *path_buf, size_t sz, const char *fmt, ...) __printf(3, 4);
3030

3131
#endif /* __PERF_CACHE_H */

tools/perf/util/config.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@ static char *home_perfconfig(void)
543543
const char *home = NULL;
544544
char *config;
545545
struct stat st;
546+
char path[PATH_MAX];
546547

547548
home = getenv("HOME");
548549

@@ -554,7 +555,7 @@ static char *home_perfconfig(void)
554555
if (!home || !*home || !perf_config_global())
555556
return NULL;
556557

557-
config = strdup(mkpath("%s/.perfconfig", home));
558+
config = strdup(mkpath(path, sizeof(path), "%s/.perfconfig", home));
558559
if (config == NULL) {
559560
pr_warning("Not enough memory to process %s/.perfconfig, ignoring it.\n", home);
560561
return NULL;

tools/perf/util/path.c

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
11
// SPDX-License-Identifier: GPL-2.0
2-
/*
3-
* I'm tired of doing "vsnprintf()" etc just to open a
4-
* file, so here's a "return static buffer with printf"
5-
* interface for paths.
6-
*
7-
* It's obviously not thread-safe. Sue me. But it's quite
8-
* useful for doing things like
9-
*
10-
* f = open(mkpath("%s/%s.perf", base, name), O_RDONLY);
11-
*
12-
* which is what it's designed for.
13-
*/
142
#include "path.h"
153
#include "cache.h"
164
#include <linux/kernel.h>
@@ -22,18 +10,6 @@
2210
#include <dirent.h>
2311
#include <unistd.h>
2412

25-
static char bad_path[] = "/bad-path/";
26-
/*
27-
* One hack:
28-
*/
29-
static char *get_pathname(void)
30-
{
31-
static char pathname_array[4][PATH_MAX];
32-
static int idx;
33-
34-
return pathname_array[3 & ++idx];
35-
}
36-
3713
static char *cleanup_path(char *path)
3814
{
3915
/* Clean it up */
@@ -45,18 +21,17 @@ static char *cleanup_path(char *path)
4521
return path;
4622
}
4723

48-
char *mkpath(const char *fmt, ...)
24+
char *mkpath(char *path_buf, size_t sz, const char *fmt, ...)
4925
{
5026
va_list args;
5127
unsigned len;
52-
char *pathname = get_pathname();
5328

5429
va_start(args, fmt);
55-
len = vsnprintf(pathname, PATH_MAX, fmt, args);
30+
len = vsnprintf(path_buf, sz, fmt, args);
5631
va_end(args);
57-
if (len >= PATH_MAX)
58-
return bad_path;
59-
return cleanup_path(pathname);
32+
if (len >= sz)
33+
strncpy(path_buf, "/bad-path/", sz);
34+
return cleanup_path(path_buf);
6035
}
6136

6237
int path__join(char *bf, size_t size, const char *path1, const char *path2)

0 commit comments

Comments
 (0)