mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2024-12-09 12:04:15 +08:00
41d5854113
I got several memory leak reports from Asan with a simple command. It was because VDSO is not released due to the refcount. Like in __dsos_addnew_id(), it should put the refcount after adding to the list. $ perf record true [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.030 MB perf.data (10 samples) ] ================================================================= ==692599==ERROR: LeakSanitizer: detected memory leaks Direct leak of 439 byte(s) in 1 object(s) allocated from: #0 0x7fea52341037 in __interceptor_calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:154 #1 0x559bce4aa8ee in dso__new_id util/dso.c:1256 #2 0x559bce59245a in __machine__addnew_vdso util/vdso.c:132 #3 0x559bce59245a in machine__findnew_vdso util/vdso.c:347 #4 0x559bce50826c in map__new util/map.c:175 #5 0x559bce503c92 in machine__process_mmap2_event util/machine.c:1787 #6 0x559bce512f6b in machines__deliver_event util/session.c:1481 #7 0x559bce515107 in perf_session__deliver_event util/session.c:1551 #8 0x559bce51d4d2 in do_flush util/ordered-events.c:244 #9 0x559bce51d4d2 in __ordered_events__flush util/ordered-events.c:323 #10 0x559bce519bea in __perf_session__process_events util/session.c:2268 #11 0x559bce519bea in perf_session__process_events util/session.c:2297 #12 0x559bce2e7a52 in process_buildids /home/namhyung/project/linux/tools/perf/builtin-record.c:1017 #13 0x559bce2e7a52 in record__finish_output /home/namhyung/project/linux/tools/perf/builtin-record.c:1234 #14 0x559bce2ed4f6 in __cmd_record /home/namhyung/project/linux/tools/perf/builtin-record.c:2026 #15 0x559bce2ed4f6 in cmd_record /home/namhyung/project/linux/tools/perf/builtin-record.c:2858 #16 0x559bce422db4 in run_builtin /home/namhyung/project/linux/tools/perf/perf.c:313 #17 0x559bce2acac8 in handle_internal_command /home/namhyung/project/linux/tools/perf/perf.c:365 #18 0x559bce2acac8 in run_argv /home/namhyung/project/linux/tools/perf/perf.c:409 #19 0x559bce2acac8 in main /home/namhyung/project/linux/tools/perf/perf.c:539 #20 0x7fea51e76d09 in __libc_start_main ../csu/libc-start.c:308 Indirect leak of 32 byte(s) in 1 object(s) allocated from: #0 0x7fea52341037 in __interceptor_calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:154 #1 0x559bce520907 in nsinfo__copy util/namespaces.c:169 #2 0x559bce50821b in map__new util/map.c:168 #3 0x559bce503c92 in machine__process_mmap2_event util/machine.c:1787 #4 0x559bce512f6b in machines__deliver_event util/session.c:1481 #5 0x559bce515107 in perf_session__deliver_event util/session.c:1551 #6 0x559bce51d4d2 in do_flush util/ordered-events.c:244 #7 0x559bce51d4d2 in __ordered_events__flush util/ordered-events.c:323 #8 0x559bce519bea in __perf_session__process_events util/session.c:2268 #9 0x559bce519bea in perf_session__process_events util/session.c:2297 #10 0x559bce2e7a52 in process_buildids /home/namhyung/project/linux/tools/perf/builtin-record.c:1017 #11 0x559bce2e7a52 in record__finish_output /home/namhyung/project/linux/tools/perf/builtin-record.c:1234 #12 0x559bce2ed4f6 in __cmd_record /home/namhyung/project/linux/tools/perf/builtin-record.c:2026 #13 0x559bce2ed4f6 in cmd_record /home/namhyung/project/linux/tools/perf/builtin-record.c:2858 #14 0x559bce422db4 in run_builtin /home/namhyung/project/linux/tools/perf/perf.c:313 #15 0x559bce2acac8 in handle_internal_command /home/namhyung/project/linux/tools/perf/perf.c:365 #16 0x559bce2acac8 in run_argv /home/namhyung/project/linux/tools/perf/perf.c:409 #17 0x559bce2acac8 in main /home/namhyung/project/linux/tools/perf/perf.c:539 #18 0x7fea51e76d09 in __libc_start_main ../csu/libc-start.c:308 SUMMARY: AddressSanitizer: 471 byte(s) leaked in 2 allocation(s). Signed-off-by: Namhyung Kim <namhyung@kernel.org> Acked-by: Jiri Olsa <jolsa@redhat.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Ian Rogers <irogers@google.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lore.kernel.org/lkml/20210315045641.700430-1-namhyung@kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
364 lines
7.2 KiB
C
364 lines
7.2 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
#include <errno.h>
|
|
#include <unistd.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <fcntl.h>
|
|
#include <stdlib.h>
|
|
#include <linux/kernel.h>
|
|
|
|
#include "vdso.h"
|
|
#include "dso.h"
|
|
#include <internal/lib.h>
|
|
#include "map.h"
|
|
#include "symbol.h"
|
|
#include "machine.h"
|
|
#include "thread.h"
|
|
#include "linux/string.h"
|
|
#include <linux/zalloc.h>
|
|
#include "debug.h"
|
|
|
|
/*
|
|
* Include definition of find_map() also used in perf-read-vdso.c for
|
|
* building perf-read-vdso32 and perf-read-vdsox32.
|
|
*/
|
|
#include "find-map.c"
|
|
|
|
#define VDSO__TEMP_FILE_NAME "/tmp/perf-vdso.so-XXXXXX"
|
|
|
|
struct vdso_file {
|
|
bool found;
|
|
bool error;
|
|
char temp_file_name[sizeof(VDSO__TEMP_FILE_NAME)];
|
|
const char *dso_name;
|
|
const char *read_prog;
|
|
};
|
|
|
|
struct vdso_info {
|
|
struct vdso_file vdso;
|
|
#if BITS_PER_LONG == 64
|
|
struct vdso_file vdso32;
|
|
struct vdso_file vdsox32;
|
|
#endif
|
|
};
|
|
|
|
static struct vdso_info *vdso_info__new(void)
|
|
{
|
|
static const struct vdso_info vdso_info_init = {
|
|
.vdso = {
|
|
.temp_file_name = VDSO__TEMP_FILE_NAME,
|
|
.dso_name = DSO__NAME_VDSO,
|
|
},
|
|
#if BITS_PER_LONG == 64
|
|
.vdso32 = {
|
|
.temp_file_name = VDSO__TEMP_FILE_NAME,
|
|
.dso_name = DSO__NAME_VDSO32,
|
|
.read_prog = "perf-read-vdso32",
|
|
},
|
|
.vdsox32 = {
|
|
.temp_file_name = VDSO__TEMP_FILE_NAME,
|
|
.dso_name = DSO__NAME_VDSOX32,
|
|
.read_prog = "perf-read-vdsox32",
|
|
},
|
|
#endif
|
|
};
|
|
|
|
return memdup(&vdso_info_init, sizeof(vdso_info_init));
|
|
}
|
|
|
|
static char *get_file(struct vdso_file *vdso_file)
|
|
{
|
|
char *vdso = NULL;
|
|
char *buf = NULL;
|
|
void *start, *end;
|
|
size_t size;
|
|
int fd;
|
|
|
|
if (vdso_file->found)
|
|
return vdso_file->temp_file_name;
|
|
|
|
if (vdso_file->error || find_map(&start, &end, VDSO__MAP_NAME))
|
|
return NULL;
|
|
|
|
size = end - start;
|
|
|
|
buf = memdup(start, size);
|
|
if (!buf)
|
|
return NULL;
|
|
|
|
fd = mkstemp(vdso_file->temp_file_name);
|
|
if (fd < 0)
|
|
goto out;
|
|
|
|
if (size == (size_t) write(fd, buf, size))
|
|
vdso = vdso_file->temp_file_name;
|
|
|
|
close(fd);
|
|
|
|
out:
|
|
free(buf);
|
|
|
|
vdso_file->found = (vdso != NULL);
|
|
vdso_file->error = !vdso_file->found;
|
|
return vdso;
|
|
}
|
|
|
|
void machine__exit_vdso(struct machine *machine)
|
|
{
|
|
struct vdso_info *vdso_info = machine->vdso_info;
|
|
|
|
if (!vdso_info)
|
|
return;
|
|
|
|
if (vdso_info->vdso.found)
|
|
unlink(vdso_info->vdso.temp_file_name);
|
|
#if BITS_PER_LONG == 64
|
|
if (vdso_info->vdso32.found)
|
|
unlink(vdso_info->vdso32.temp_file_name);
|
|
if (vdso_info->vdsox32.found)
|
|
unlink(vdso_info->vdsox32.temp_file_name);
|
|
#endif
|
|
|
|
zfree(&machine->vdso_info);
|
|
}
|
|
|
|
static struct dso *__machine__addnew_vdso(struct machine *machine, const char *short_name,
|
|
const char *long_name)
|
|
{
|
|
struct dso *dso;
|
|
|
|
dso = dso__new(short_name);
|
|
if (dso != NULL) {
|
|
__dsos__add(&machine->dsos, dso);
|
|
dso__set_long_name(dso, long_name, false);
|
|
/* Put dso here because __dsos_add already got it */
|
|
dso__put(dso);
|
|
}
|
|
|
|
return dso;
|
|
}
|
|
|
|
static enum dso_type machine__thread_dso_type(struct machine *machine,
|
|
struct thread *thread)
|
|
{
|
|
enum dso_type dso_type = DSO__TYPE_UNKNOWN;
|
|
struct map *map;
|
|
|
|
maps__for_each_entry(thread->maps, map) {
|
|
struct dso *dso = map->dso;
|
|
if (!dso || dso->long_name[0] != '/')
|
|
continue;
|
|
dso_type = dso__type(dso, machine);
|
|
if (dso_type != DSO__TYPE_UNKNOWN)
|
|
break;
|
|
}
|
|
|
|
return dso_type;
|
|
}
|
|
|
|
#if BITS_PER_LONG == 64
|
|
|
|
static int vdso__do_copy_compat(FILE *f, int fd)
|
|
{
|
|
char buf[4096];
|
|
size_t count;
|
|
|
|
while (1) {
|
|
count = fread(buf, 1, sizeof(buf), f);
|
|
if (ferror(f))
|
|
return -errno;
|
|
if (feof(f))
|
|
break;
|
|
if (count && writen(fd, buf, count) != (ssize_t)count)
|
|
return -errno;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int vdso__copy_compat(const char *prog, int fd)
|
|
{
|
|
FILE *f;
|
|
int err;
|
|
|
|
f = popen(prog, "r");
|
|
if (!f)
|
|
return -errno;
|
|
|
|
err = vdso__do_copy_compat(f, fd);
|
|
|
|
if (pclose(f) == -1)
|
|
return -errno;
|
|
|
|
return err;
|
|
}
|
|
|
|
static int vdso__create_compat_file(const char *prog, char *temp_name)
|
|
{
|
|
int fd, err;
|
|
|
|
fd = mkstemp(temp_name);
|
|
if (fd < 0)
|
|
return -errno;
|
|
|
|
err = vdso__copy_compat(prog, fd);
|
|
|
|
if (close(fd) == -1)
|
|
return -errno;
|
|
|
|
return err;
|
|
}
|
|
|
|
static const char *vdso__get_compat_file(struct vdso_file *vdso_file)
|
|
{
|
|
int err;
|
|
|
|
if (vdso_file->found)
|
|
return vdso_file->temp_file_name;
|
|
|
|
if (vdso_file->error)
|
|
return NULL;
|
|
|
|
err = vdso__create_compat_file(vdso_file->read_prog,
|
|
vdso_file->temp_file_name);
|
|
if (err) {
|
|
pr_err("%s failed, error %d\n", vdso_file->read_prog, err);
|
|
vdso_file->error = true;
|
|
return NULL;
|
|
}
|
|
|
|
vdso_file->found = true;
|
|
|
|
return vdso_file->temp_file_name;
|
|
}
|
|
|
|
static struct dso *__machine__findnew_compat(struct machine *machine,
|
|
struct vdso_file *vdso_file)
|
|
{
|
|
const char *file_name;
|
|
struct dso *dso;
|
|
|
|
dso = __dsos__find(&machine->dsos, vdso_file->dso_name, true);
|
|
if (dso)
|
|
goto out;
|
|
|
|
file_name = vdso__get_compat_file(vdso_file);
|
|
if (!file_name)
|
|
goto out;
|
|
|
|
dso = __machine__addnew_vdso(machine, vdso_file->dso_name, file_name);
|
|
out:
|
|
return dso;
|
|
}
|
|
|
|
static int __machine__findnew_vdso_compat(struct machine *machine,
|
|
struct thread *thread,
|
|
struct vdso_info *vdso_info,
|
|
struct dso **dso)
|
|
{
|
|
enum dso_type dso_type;
|
|
|
|
dso_type = machine__thread_dso_type(machine, thread);
|
|
|
|
#ifndef HAVE_PERF_READ_VDSO32
|
|
if (dso_type == DSO__TYPE_32BIT)
|
|
return 0;
|
|
#endif
|
|
#ifndef HAVE_PERF_READ_VDSOX32
|
|
if (dso_type == DSO__TYPE_X32BIT)
|
|
return 0;
|
|
#endif
|
|
|
|
switch (dso_type) {
|
|
case DSO__TYPE_32BIT:
|
|
*dso = __machine__findnew_compat(machine, &vdso_info->vdso32);
|
|
return 1;
|
|
case DSO__TYPE_X32BIT:
|
|
*dso = __machine__findnew_compat(machine, &vdso_info->vdsox32);
|
|
return 1;
|
|
case DSO__TYPE_UNKNOWN:
|
|
case DSO__TYPE_64BIT:
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
#endif
|
|
|
|
static struct dso *machine__find_vdso(struct machine *machine,
|
|
struct thread *thread)
|
|
{
|
|
struct dso *dso = NULL;
|
|
enum dso_type dso_type;
|
|
|
|
dso_type = machine__thread_dso_type(machine, thread);
|
|
switch (dso_type) {
|
|
case DSO__TYPE_32BIT:
|
|
dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO32, true);
|
|
if (!dso) {
|
|
dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO,
|
|
true);
|
|
if (dso && dso_type != dso__type(dso, machine))
|
|
dso = NULL;
|
|
}
|
|
break;
|
|
case DSO__TYPE_X32BIT:
|
|
dso = __dsos__find(&machine->dsos, DSO__NAME_VDSOX32, true);
|
|
break;
|
|
case DSO__TYPE_64BIT:
|
|
case DSO__TYPE_UNKNOWN:
|
|
default:
|
|
dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO, true);
|
|
break;
|
|
}
|
|
|
|
return dso;
|
|
}
|
|
|
|
struct dso *machine__findnew_vdso(struct machine *machine,
|
|
struct thread *thread)
|
|
{
|
|
struct vdso_info *vdso_info;
|
|
struct dso *dso = NULL;
|
|
|
|
down_write(&machine->dsos.lock);
|
|
if (!machine->vdso_info)
|
|
machine->vdso_info = vdso_info__new();
|
|
|
|
vdso_info = machine->vdso_info;
|
|
if (!vdso_info)
|
|
goto out_unlock;
|
|
|
|
dso = machine__find_vdso(machine, thread);
|
|
if (dso)
|
|
goto out_unlock;
|
|
|
|
#if BITS_PER_LONG == 64
|
|
if (__machine__findnew_vdso_compat(machine, thread, vdso_info, &dso))
|
|
goto out_unlock;
|
|
#endif
|
|
|
|
dso = __dsos__find(&machine->dsos, DSO__NAME_VDSO, true);
|
|
if (!dso) {
|
|
char *file;
|
|
|
|
file = get_file(&vdso_info->vdso);
|
|
if (file)
|
|
dso = __machine__addnew_vdso(machine, DSO__NAME_VDSO, file);
|
|
}
|
|
|
|
out_unlock:
|
|
dso__get(dso);
|
|
up_write(&machine->dsos.lock);
|
|
return dso;
|
|
}
|
|
|
|
bool dso__is_vdso(struct dso *dso)
|
|
{
|
|
return !strcmp(dso->short_name, DSO__NAME_VDSO) ||
|
|
!strcmp(dso->short_name, DSO__NAME_VDSO32) ||
|
|
!strcmp(dso->short_name, DSO__NAME_VDSOX32);
|
|
}
|