lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <17b837bd-d1b5-48f4-9a16-922b71f195e3@intel.com>
Date: Wed, 11 Sep 2024 11:03:55 +0300
From: Adrian Hunter <adrian.hunter@...el.com>
To: Changbin Du <changbin.du@...wei.com>,
 Peter Zijlstra <peterz@...radead.org>, Ingo Molnar <mingo@...hat.com>,
 Arnaldo Carvalho de Melo <acme@...nel.org>,
 Namhyung Kim <namhyung@...nel.org>, Nathan Chancellor <nathan@...nel.org>
Cc: Mark Rutland <mark.rutland@....com>,
 Alexander Shishkin <alexander.shishkin@...ux.intel.com>,
 Jiri Olsa <jolsa@...nel.org>, Ian Rogers <irogers@...gle.com>,
 "Liang, Kan" <kan.liang@...ux.intel.com>,
 Nick Desaulniers <ndesaulniers@...gle.com>, Bill Wendling
 <morbo@...gle.com>, Justin Stitt <justinstitt@...gle.com>,
 linux-perf-users@...r.kernel.org, linux-kernel@...r.kernel.org,
 llvm@...ts.linux.dev, Hui Wang <hw.huiwang@...wei.com>
Subject: Re: [PATCH v6 4/8] perf: symbol: generalize vmlinux path searching

On 25/07/24 05:15, Changbin Du wrote:
> This generalizes the vmlinux path searching logic. Later we will add
> another instance for vdso.
> 
> The search pattern is described by struct dso_filename_pattern, and the
> formatted paths are hold in struct dso_filename_paths.
> 
> Signed-off-by: Changbin Du <changbin.du@...wei.com>
> ---
>  tools/perf/util/machine.c |   4 +-
>  tools/perf/util/symbol.c  | 112 +++++++++++++++++++++-----------------
>  tools/perf/util/symbol.h  |   8 ++-
>  3 files changed, 70 insertions(+), 54 deletions(-)
> 
> diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c
> index 8477edefc299..68315520f15b 100644
> --- a/tools/perf/util/machine.c
> +++ b/tools/perf/util/machine.c
> @@ -896,9 +896,9 @@ size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
>  			printed += fprintf(fp, "[0] %s\n", filename);
>  	}
>  
> -	for (i = 0; i < vmlinux_path__nr_entries; ++i) {
> +	for (i = 0; i < vmlinux_paths.nr_entries; ++i) {
>  		printed += fprintf(fp, "[%d] %s\n", i + dso__has_build_id(kdso),
> -				   vmlinux_path[i]);
> +				   vmlinux_paths.paths[i]);
>  	}
>  	return printed;
>  }
> diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c
> index ad3b7b929e94..6bf75c98e1f2 100644
> --- a/tools/perf/util/symbol.c
> +++ b/tools/perf/util/symbol.c
> @@ -48,8 +48,7 @@ static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map);
>  static int dso__load_vdso_sym(struct dso *dso, struct map *map);
>  static bool symbol__is_idle(const char *name);
>  
> -int vmlinux_path__nr_entries;
> -char **vmlinux_path;
> +struct dso_filename_paths vmlinux_paths;
>  
>  struct symbol_conf symbol_conf = {
>  	.nanosecs		= false,
> @@ -2042,10 +2041,10 @@ int dso__load_vmlinux_path(struct dso *dso, struct map *map)
>  	char *filename = NULL;
>  
>  	pr_debug("Looking at the vmlinux_path (%d entries long)\n",
> -		 vmlinux_path__nr_entries + 1);
> +		 vmlinux_paths.nr_entries + 1);
>  
> -	for (i = 0; i < vmlinux_path__nr_entries; ++i) {
> -		err = dso__load_vmlinux(dso, map, vmlinux_path[i], false);
> +	for (i = 0; i < vmlinux_paths.nr_entries; ++i) {
> +		err = dso__load_vmlinux(dso, map, vmlinux_paths.paths[i], false);
>  		if (err > 0)
>  			goto out;
>  	}
> @@ -2209,7 +2208,7 @@ static int dso__load_kernel_sym(struct dso *dso, struct map *map)
>  			return err;
>  	}
>  
> -	if (!symbol_conf.ignore_vmlinux && vmlinux_path != NULL) {
> +	if (!symbol_conf.ignore_vmlinux && vmlinux_paths.paths != NULL) {
>  		err = dso__load_vmlinux_path(dso, map);
>  		if (err > 0)
>  			return err;
> @@ -2284,57 +2283,55 @@ static int dso__load_guest_kernel_sym(struct dso *dso, struct map *map)
>  	return err;
>  }
>  
> -static void vmlinux_path__exit(void)
> -{
> -	while (--vmlinux_path__nr_entries >= 0)
> -		zfree(&vmlinux_path[vmlinux_path__nr_entries]);
> -	vmlinux_path__nr_entries = 0;
> -
> -	zfree(&vmlinux_path);
> -}
> -
> -static const char * const vmlinux_paths[] = {
> -	"vmlinux",
> -	"/boot/vmlinux"
> +struct dso_filename_pattern {
> +	const char *pattern;
> +	/*
> +	 * 0 for matching directly,
> +	 * 1 for matching by kernel_version,
> +	 * 2 for matching by kernel_version + arch.
> +	 */
> +	int match_type;
>  };
>  
> -static const char * const vmlinux_paths_upd[] = {
> -	"/boot/vmlinux-%s",
> -	"/usr/lib/debug/boot/vmlinux-%s",
> -	"/lib/modules/%s/build/vmlinux",
> -	"/usr/lib/debug/lib/modules/%s/vmlinux",
> -	"/usr/lib/debug/boot/vmlinux-%s.debug"
> +struct dso_filename_pattern vmlinux_patterns[] = {
> +	{"vmlinux", 0},
> +	{"/boot/vmlinux", 0},
> +	{"/boot/vmlinux-%s", 1},
> +	{"/usr/lib/debug/boot/vmlinux-%s", 1},
> +	{"/lib/modules/%s/build/vmlinux", 1},
> +	{"/usr/lib/debug/lib/modules/%s/vmlinux", 1},
> +	{"/usr/lib/debug/boot/vmlinux-%s.debug", 1},
>  };
>  
> -static int vmlinux_path__add(const char *new_entry)
> +static int dso_filename_path__add(struct dso_filename_paths *paths, const char *new_entry)
>  {
> -	vmlinux_path[vmlinux_path__nr_entries] = strdup(new_entry);
> -	if (vmlinux_path[vmlinux_path__nr_entries] == NULL)
> +	paths->paths[paths->nr_entries] = strdup(new_entry);
> +	if (paths->paths[paths->nr_entries] == NULL)
>  		return -1;
> -	++vmlinux_path__nr_entries;
> +	++paths->nr_entries;
>  
>  	return 0;
>  }
>  
> -static int vmlinux_path__init(struct perf_env *env)
> +static void dso_filename_path__exit(struct dso_filename_paths *paths)
>  {
> -	struct utsname uts;
> -	char bf[PATH_MAX];
> -	char *kernel_version;
> -	unsigned int i;
> +	while (--paths->nr_entries >= 0)
> +		zfree(&paths->paths[paths->nr_entries]);
> +	paths->nr_entries = 0;
>  
> -	vmlinux_path = malloc(sizeof(char *) * (ARRAY_SIZE(vmlinux_paths) +
> -			      ARRAY_SIZE(vmlinux_paths_upd)));
> -	if (vmlinux_path == NULL)
> -		return -1;
> -
> -	for (i = 0; i < ARRAY_SIZE(vmlinux_paths); i++)
> -		if (vmlinux_path__add(vmlinux_paths[i]) < 0)
> -			goto out_fail;
> +	zfree(&paths->paths);
> +}
>  
> -	/* only try kernel version if no symfs was given */
> -	if (symbol_conf.symfs[0] != 0)
> -		return 0;
> +static int dso_filename_path__init(struct dso_filename_paths *paths,
> +				   struct dso_filename_pattern *patterns,
> +				   int nr_patterns,
> +				   struct perf_env *env)
> +{
> +	struct utsname uts;
> +	char bf[PATH_MAX];
> +	const char *kernel_version;
> +	const char *arch = perf_env__arch(env);
> +	int i;
>  
>  	if (env) {
>  		kernel_version = env->os_release;
> @@ -2345,16 +2342,28 @@ static int vmlinux_path__init(struct perf_env *env)
>  		kernel_version = uts.release;
>  	}
>  
> -	for (i = 0; i < ARRAY_SIZE(vmlinux_paths_upd); i++) {
> -		snprintf(bf, sizeof(bf), vmlinux_paths_upd[i], kernel_version);
> -		if (vmlinux_path__add(bf) < 0)
> +	paths->paths = malloc(sizeof(char *) * nr_patterns);
> +	if (paths->paths == NULL)
> +		return -1;
> +
> +	for (i = 0; i < nr_patterns; i++) {
> +		if (patterns[i].match_type == 0)
> +			strlcpy(bf, patterns[i].pattern, sizeof(bf));
> +		else if (symbol_conf.symfs[0] == 0) {
> +			/* only try kernel version if no symfs was given */
> +			if (patterns[i].match_type == 1)
> +				snprintf(bf, sizeof(bf), patterns[i].pattern, kernel_version);
> +			else if (patterns[i].match_type == 2)
> +				snprintf(bf, sizeof(bf), patterns[i].pattern, kernel_version, arch);
> +		}
> +		if (dso_filename_path__add(paths, bf) < 0)
>  			goto out_fail;
>  	}
>  
>  	return 0;
>  
>  out_fail:
> -	vmlinux_path__exit();
> +	dso_filename_path__exit(paths);
>  	return -1;
>  }
>  
> @@ -2550,8 +2559,11 @@ int symbol__init(struct perf_env *env)
>  
>  	symbol__elf_init();
>  
> -	if (symbol_conf.try_vmlinux_path && vmlinux_path__init(env) < 0)
> +	if (symbol_conf.try_vmlinux_path &&
> +	    dso_filename_path__init(&vmlinux_paths, vmlinux_patterns,
> +				    ARRAY_SIZE(vmlinux_patterns), env) < 0) {
>  		return -1;
> +	}
>  
>  	if (symbol_conf.field_sep && *symbol_conf.field_sep == '.') {
>  		pr_err("'.' is the only non valid --field-separator argument\n");
> @@ -2628,7 +2640,7 @@ void symbol__exit(void)
>  	intlist__delete(symbol_conf.tid_list);
>  	intlist__delete(symbol_conf.pid_list);
>  	intlist__delete(symbol_conf.addr_list);
> -	vmlinux_path__exit();
> +	dso_filename_path__exit(&vmlinux_paths);
>  	symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list = NULL;
>  	symbol_conf.bt_stop_list = NULL;
>  	symbol_conf.initialized = false;
> diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h
> index 3fb5d146d9b1..30056884945b 100644
> --- a/tools/perf/util/symbol.h
> +++ b/tools/perf/util/symbol.h
> @@ -101,8 +101,12 @@ static inline int __symbol__join_symfs(char *bf, size_t size, const char *path)
>  
>  #define symbol__join_symfs(bf, path) __symbol__join_symfs(bf, sizeof(bf), path)
>  
> -extern int vmlinux_path__nr_entries;
> -extern char **vmlinux_path;
> +struct dso_filename_paths {
> +	int nr_entries;
> +	char **paths;
> +};

Feels a bit over engineered.  We only need the nth path so
a simpler, more encapsulated API could be just:

const char *vdso_path(size_t i);

Also wonder why the paths cannot just be created by asprint().
e.g. notwithstanding that perf_env__os_release() does not exist:


#define MAX_VDSO_PATHS 8
static char *vdso_paths[MAX_VDSO_PATHS];
static size_t vdso_path__nr_entries;

static int vdso_path__init(struct perf_env *env)
{
	const char *k = perf_env__os_release(env);
	const char *a = perf_env__arch(env);
	int i = 0;

#define PATH_INIT(fmt, ...)						\
	(i < MAX_VDSO_PATHS ? ({					\
		int ret = asprintf(&vdso_paths[i], fmt, ##__VA_ARGS__);	\
		if (ret >= 0) {						\
			i += 1;						\
			ret = 0;					\
		}							\
		ret;							\
	}) : -1)

	if (PATH_INIT("/lib/modules/%s/vdso/vdso.so", k) ||
	    PATH_INIT("/lib/modules/%s/vdso/vdso64.so", k) ||
	    PATH_INIT("/lib/modules/%s/vdso/vdso32.so", k) ||
	    PATH_INIT("/lib/modules/%s/build/arch/%s/vdso/vdso.so.dbg", k, a) ||
	    PATH_INIT("/lib/modules/%s/build/arch/%s/kernel/vdso/vdso.so.dbg", k, a) ||
	    PATH_INIT("/lib/modules/%s/build/arch/%s/entry/vdso/vdso32.so.dbg", k, a) ||
	    PATH_INIT("/lib/modules/%s/build/arch/%s/entry/vdso/vdso64.so.dbg", k, a))
		goto out_err;

#undef PATH_INIT

	vdso_path__nr_entries = i;
	return 0;

out_err:
	while (i)
		zfree(&vdso_paths[--i]);
	return -ENOMEM;
}

static void vdso_path__exit(void)
{
	while (vdso_path__nr_entries)
		zfree(&vdso_paths[--vdso_path__nr_entries]);
}

const char *vdso_path(size_t i)
{
	if (i >= vdso_path__nr_entries)
		return NULL;

	return vdso_paths[i];
}


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ