Skip to content

Commit

Permalink
tools api fs: Avoid large static PATH_MAX arrays
Browse files Browse the repository at this point in the history
Change struct fs to have a pointer to a dynamically allocated array
rather than an array. This reduces the size of fs__entries from 24,768
bytes to 240 bytes. Read paths into a stack allocated array and
strdup. Fix off-by-1 fscanf %<num>s in fs__read_mounts caught by
address sanitizer.

Signed-off-by: Ian Rogers <irogers@google.com>
Link: https://lore.kernel.org/r/20230526183401.2326121-7-irogers@google.com
Cc: K Prateek Nayak <kprateek.nayak@amd.com>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Ross Zwisler <zwisler@chromium.org>
Cc: Steven Rostedt (Google) <rostedt@goodmis.org>
Cc: Sean Christopherson <seanjc@google.com>
Cc: Yang Jihong <yangjihong1@huawei.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Tiezhu Yang <yangtiezhu@loongson.cn>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: linux-kernel@vger.kernel.org
Cc: linux-perf-users@vger.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
  • Loading branch information
captain5050 authored and acmel committed May 28, 2023
1 parent 1fc88e5 commit 89df62c
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions tools/lib/api/fs/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ static const char * const bpf_fs__known_mountpoints[] = {
struct fs {
const char *name;
const char * const *mounts;
char path[PATH_MAX];
char *path;
bool found;
bool checked;
long magic;
Expand Down Expand Up @@ -151,17 +151,23 @@ static bool fs__read_mounts(struct fs *fs)
bool found = false;
char type[100];
FILE *fp;
char path[PATH_MAX + 1];

fp = fopen("/proc/mounts", "r");
if (fp == NULL)
return NULL;
return false;

while (!found &&
fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
fs->path, type) == 2) {
path, type) == 2) {

if (strcmp(type, fs->name) == 0)
if (strcmp(type, fs->name) == 0) {
free(fs->path);
fs->path = strdup(path);
if (!fs->path)
return false;
found = true;
}
}

fclose(fp);
Expand All @@ -188,8 +194,11 @@ static bool fs__check_mounts(struct fs *fs)
ptr = fs->mounts;
while (*ptr) {
if (fs__valid_mount(*ptr, fs->magic) == 0) {
free(fs->path);
fs->path = strdup(*ptr);
if (!fs->path)
return false;
fs->found = true;
strcpy(fs->path, *ptr);
return true;
}
ptr++;
Expand Down Expand Up @@ -227,10 +236,12 @@ static bool fs__env_override(struct fs *fs)
if (!override_path)
return false;

free(fs->path);
fs->path = strdup(override_path);
if (!fs->path)
return false;
fs->found = true;
fs->checked = true;
strncpy(fs->path, override_path, sizeof(fs->path) - 1);
fs->path[sizeof(fs->path) - 1] = '\0';
return true;
}

Expand Down

0 comments on commit 89df62c

Please sign in to comment.