Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
370ce164 430952e6

+14 -34
+3 -1
tools/perf/builtin-config.c
··· 12 12 #include "util/debug.h" 13 13 #include "util/config.h" 14 14 #include <linux/string.h> 15 + #include <limits.h> 15 16 #include <stdio.h> 16 17 #include <stdlib.h> 17 18 ··· 158 157 { 159 158 int i, ret = -1; 160 159 struct perf_config_set *set; 161 - char *user_config = mkpath("%s/.perfconfig", getenv("HOME")); 160 + char path[PATH_MAX]; 161 + char *user_config = mkpath(path, sizeof(path), "%s/.perfconfig", getenv("HOME")); 162 162 const char *config_filename; 163 163 bool changed = false; 164 164
+3 -1
tools/perf/builtin-help.c
··· 19 19 #include <linux/string.h> 20 20 #include <linux/zalloc.h> 21 21 #include <errno.h> 22 + #include <limits.h> 22 23 #include <stdio.h> 23 24 #include <stdlib.h> 24 25 #include <string.h> ··· 390 389 { 391 390 struct stat st; 392 391 const char *html_path = system_path(PERF_HTML_PATH); 392 + char path[PATH_MAX]; 393 393 394 394 /* Check that we have a perf documentation directory. */ 395 - if (stat(mkpath("%s/perf.html", html_path), &st) 395 + if (stat(mkpath(path, sizeof(path), "%s/perf.html", html_path), &st) 396 396 || !S_ISREG(st.st_mode)) { 397 397 pr_err("'%s': not a documentation directory.", html_path); 398 398 return -1;
+1 -1
tools/perf/util/cache.h
··· 26 26 return path[0] == '/'; 27 27 } 28 28 29 - char *mkpath(const char *fmt, ...) __printf(1, 2); 29 + char *mkpath(char *path_buf, size_t sz, const char *fmt, ...) __printf(3, 4); 30 30 31 31 #endif /* __PERF_CACHE_H */
+2 -1
tools/perf/util/config.c
··· 543 543 const char *home = NULL; 544 544 char *config; 545 545 struct stat st; 546 + char path[PATH_MAX]; 546 547 547 548 home = getenv("HOME"); 548 549 ··· 555 554 if (!home || !*home || !perf_config_global()) 556 555 return NULL; 557 556 558 - config = strdup(mkpath("%s/.perfconfig", home)); 557 + config = strdup(mkpath(path, sizeof(path), "%s/.perfconfig", home)); 559 558 if (config == NULL) { 560 559 pr_warning("Not enough memory to process %s/.perfconfig, ignoring it.\n", home); 561 560 return NULL;
+5 -30
tools/perf/util/path.c
··· 1 1 // 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 - */ 14 2 #include "path.h" 15 3 #include "cache.h" 16 4 #include <linux/kernel.h> ··· 9 21 #include <sys/stat.h> 10 22 #include <dirent.h> 11 23 #include <unistd.h> 12 - 13 - static char bad_path[] = "/bad-path/"; 14 - /* 15 - * One hack: 16 - */ 17 - static char *get_pathname(void) 18 - { 19 - static char pathname_array[4][PATH_MAX]; 20 - static int idx; 21 - 22 - return pathname_array[3 & ++idx]; 23 - } 24 24 25 25 static char *cleanup_path(char *path) 26 26 { ··· 21 45 return path; 22 46 } 23 47 24 - char *mkpath(const char *fmt, ...) 48 + char *mkpath(char *path_buf, size_t sz, const char *fmt, ...) 25 49 { 26 50 va_list args; 27 51 unsigned len; 28 - char *pathname = get_pathname(); 29 52 30 53 va_start(args, fmt); 31 - len = vsnprintf(pathname, PATH_MAX, fmt, args); 54 + len = vsnprintf(path_buf, sz, fmt, args); 32 55 va_end(args); 33 - if (len >= PATH_MAX) 34 - return bad_path; 35 - return cleanup_path(pathname); 56 + if (len >= sz) 57 + strncpy(path_buf, "/bad-path/", sz); 58 + return cleanup_path(path_buf); 36 59 } 37 60 38 61 int path__join(char *bf, size_t size, const char *path1, const char *path2)