Commit 67dec926 authored by Yonatan Goldschmidt's avatar Yonatan Goldschmidt Committed by Arnaldo Carvalho de Melo

perf inject jit: Add namespaces support

This patch fixes "perf inject --jit" to properly operate on
namespaced/containerized processes:

* jitdump files are generated by the process, thus they should be
  looked up in its mount NS.

* DSOs of injected MMAP events will later be looked up in the process
  mount NS, so write them into its NS.

* PIDs & TIDs from jitdump events need to be translated to the PID as
  seen by "perf record" before written into MMAP events.

For a process in a different PID NS, the TID & PID given in the jitdump
event are actually ignored; I use the TID & PID of the thread which
mmap()ed the jitdump file. This is simplified and won't do for forks of
the initial process, if they continue using the same jitdump file.
Future patches might improve it.

This was tested by recording a NodeJS process running with
"--perf-prof", inside a Docker container, and by recording another
NodeJS process running in the same namespaces as perf itself, to make
sure it's not broken for non-containerized processes.
Signed-off-by: default avatarYonatan Goldschmidt <yonatan.goldschmidt@granulate.io>
Acked-by: default avatarJiri Olsa <jolsa@redhat.com>
Acked-by: default avatarNamhyung Kim <namhyung@kernel.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20201105015604.1726943-1-yonatan.goldschmidt@granulate.ioSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 2b51c71b
...@@ -313,7 +313,7 @@ static int perf_event__jit_repipe_mmap(struct perf_tool *tool, ...@@ -313,7 +313,7 @@ static int perf_event__jit_repipe_mmap(struct perf_tool *tool,
* if jit marker, then inject jit mmaps and generate ELF images * if jit marker, then inject jit mmaps and generate ELF images
*/ */
ret = jit_process(inject->session, &inject->output, machine, ret = jit_process(inject->session, &inject->output, machine,
event->mmap.filename, event->mmap.pid, &n); event->mmap.filename, event->mmap.pid, event->mmap.tid, &n);
if (ret < 0) if (ret < 0)
return ret; return ret;
if (ret) { if (ret) {
...@@ -413,7 +413,7 @@ static int perf_event__jit_repipe_mmap2(struct perf_tool *tool, ...@@ -413,7 +413,7 @@ static int perf_event__jit_repipe_mmap2(struct perf_tool *tool,
* if jit marker, then inject jit mmaps and generate ELF images * if jit marker, then inject jit mmaps and generate ELF images
*/ */
ret = jit_process(inject->session, &inject->output, machine, ret = jit_process(inject->session, &inject->output, machine,
event->mmap2.filename, event->mmap2.pid, &n); event->mmap2.filename, event->mmap2.pid, event->mmap2.tid, &n);
if (ret < 0) if (ret < 0)
return ret; return ret;
if (ret) { if (ret) {
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
#include <data.h> #include <data.h>
int jit_process(struct perf_session *session, struct perf_data *output, int jit_process(struct perf_session *session, struct perf_data *output,
struct machine *machine, char *filename, pid_t pid, u64 *nbytes); struct machine *machine, char *filename, pid_t pid, pid_t tid, u64 *nbytes);
int jit_inject_record(const char *filename); int jit_inject_record(const char *filename);
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "event.h" #include "event.h"
#include "debug.h" #include "debug.h"
#include "evlist.h" #include "evlist.h"
#include "namespaces.h"
#include "symbol.h" #include "symbol.h"
#include <elf.h> #include <elf.h>
...@@ -35,6 +36,7 @@ struct jit_buf_desc { ...@@ -35,6 +36,7 @@ struct jit_buf_desc {
struct perf_data *output; struct perf_data *output;
struct perf_session *session; struct perf_session *session;
struct machine *machine; struct machine *machine;
struct nsinfo *nsi;
union jr_entry *entry; union jr_entry *entry;
void *buf; void *buf;
uint64_t sample_type; uint64_t sample_type;
...@@ -72,7 +74,8 @@ struct jit_tool { ...@@ -72,7 +74,8 @@ struct jit_tool {
#define get_jit_tool(t) (container_of(tool, struct jit_tool, tool)) #define get_jit_tool(t) (container_of(tool, struct jit_tool, tool))
static int static int
jit_emit_elf(char *filename, jit_emit_elf(struct jit_buf_desc *jd,
char *filename,
const char *sym, const char *sym,
uint64_t code_addr, uint64_t code_addr,
const void *code, const void *code,
...@@ -83,14 +86,18 @@ jit_emit_elf(char *filename, ...@@ -83,14 +86,18 @@ jit_emit_elf(char *filename,
uint32_t unwinding_header_size, uint32_t unwinding_header_size,
uint32_t unwinding_size) uint32_t unwinding_size)
{ {
int ret, fd; int ret, fd, saved_errno;
struct nscookie nsc;
if (verbose > 0) if (verbose > 0)
fprintf(stderr, "write ELF image %s\n", filename); fprintf(stderr, "write ELF image %s\n", filename);
nsinfo__mountns_enter(jd->nsi, &nsc);
fd = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0644); fd = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0644);
saved_errno = errno;
nsinfo__mountns_exit(&nsc);
if (fd == -1) { if (fd == -1) {
pr_warning("cannot create jit ELF %s: %s\n", filename, strerror(errno)); pr_warning("cannot create jit ELF %s: %s\n", filename, strerror(saved_errno));
return -1; return -1;
} }
...@@ -99,8 +106,11 @@ jit_emit_elf(char *filename, ...@@ -99,8 +106,11 @@ jit_emit_elf(char *filename,
close(fd); close(fd);
if (ret) if (ret) {
nsinfo__mountns_enter(jd->nsi, &nsc);
unlink(filename); unlink(filename);
nsinfo__mountns_exit(&nsc);
}
return ret; return ret;
} }
...@@ -134,12 +144,15 @@ static int ...@@ -134,12 +144,15 @@ static int
jit_open(struct jit_buf_desc *jd, const char *name) jit_open(struct jit_buf_desc *jd, const char *name)
{ {
struct jitheader header; struct jitheader header;
struct nscookie nsc;
struct jr_prefix *prefix; struct jr_prefix *prefix;
ssize_t bs, bsz = 0; ssize_t bs, bsz = 0;
void *n, *buf = NULL; void *n, *buf = NULL;
int ret, retval = -1; int ret, retval = -1;
nsinfo__mountns_enter(jd->nsi, &nsc);
jd->in = fopen(name, "r"); jd->in = fopen(name, "r");
nsinfo__mountns_exit(&nsc);
if (!jd->in) if (!jd->in)
return -1; return -1;
...@@ -367,6 +380,20 @@ jit_inject_event(struct jit_buf_desc *jd, union perf_event *event) ...@@ -367,6 +380,20 @@ jit_inject_event(struct jit_buf_desc *jd, union perf_event *event)
return 0; return 0;
} }
static pid_t jr_entry_pid(struct jit_buf_desc *jd, union jr_entry *jr)
{
if (jd->nsi && jd->nsi->in_pidns)
return jd->nsi->tgid;
return jr->load.pid;
}
static pid_t jr_entry_tid(struct jit_buf_desc *jd, union jr_entry *jr)
{
if (jd->nsi && jd->nsi->in_pidns)
return jd->nsi->pid;
return jr->load.tid;
}
static uint64_t convert_timestamp(struct jit_buf_desc *jd, uint64_t timestamp) static uint64_t convert_timestamp(struct jit_buf_desc *jd, uint64_t timestamp)
{ {
struct perf_tsc_conversion tc; struct perf_tsc_conversion tc;
...@@ -402,14 +429,15 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr) ...@@ -402,14 +429,15 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr)
const char *sym; const char *sym;
uint64_t count; uint64_t count;
int ret, csize, usize; int ret, csize, usize;
pid_t pid, tid; pid_t nspid, pid, tid;
struct { struct {
u32 pid, tid; u32 pid, tid;
u64 time; u64 time;
} *id; } *id;
pid = jr->load.pid; nspid = jr->load.pid;
tid = jr->load.tid; pid = jr_entry_pid(jd, jr);
tid = jr_entry_tid(jd, jr);
csize = jr->load.code_size; csize = jr->load.code_size;
usize = jd->unwinding_mapped_size; usize = jd->unwinding_mapped_size;
addr = jr->load.code_addr; addr = jr->load.code_addr;
...@@ -425,14 +453,14 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr) ...@@ -425,14 +453,14 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr)
filename = event->mmap2.filename; filename = event->mmap2.filename;
size = snprintf(filename, PATH_MAX, "%s/jitted-%d-%" PRIu64 ".so", size = snprintf(filename, PATH_MAX, "%s/jitted-%d-%" PRIu64 ".so",
jd->dir, jd->dir,
pid, nspid,
count); count);
size++; /* for \0 */ size++; /* for \0 */
size = PERF_ALIGN(size, sizeof(u64)); size = PERF_ALIGN(size, sizeof(u64));
uaddr = (uintptr_t)code; uaddr = (uintptr_t)code;
ret = jit_emit_elf(filename, sym, addr, (const void *)uaddr, csize, jd->debug_data, jd->nr_debug_entries, ret = jit_emit_elf(jd, filename, sym, addr, (const void *)uaddr, csize, jd->debug_data, jd->nr_debug_entries,
jd->unwinding_data, jd->eh_frame_hdr_size, jd->unwinding_size); jd->unwinding_data, jd->eh_frame_hdr_size, jd->unwinding_size);
if (jd->debug_data && jd->nr_debug_entries) { if (jd->debug_data && jd->nr_debug_entries) {
...@@ -451,7 +479,7 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr) ...@@ -451,7 +479,7 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr)
free(event); free(event);
return -1; return -1;
} }
if (stat(filename, &st)) if (nsinfo__stat(filename, &st, jd->nsi))
memset(&st, 0, sizeof(st)); memset(&st, 0, sizeof(st));
event->mmap2.header.type = PERF_RECORD_MMAP2; event->mmap2.header.type = PERF_RECORD_MMAP2;
...@@ -515,14 +543,15 @@ static int jit_repipe_code_move(struct jit_buf_desc *jd, union jr_entry *jr) ...@@ -515,14 +543,15 @@ static int jit_repipe_code_move(struct jit_buf_desc *jd, union jr_entry *jr)
int usize; int usize;
u16 idr_size; u16 idr_size;
int ret; int ret;
pid_t pid, tid; pid_t nspid, pid, tid;
struct { struct {
u32 pid, tid; u32 pid, tid;
u64 time; u64 time;
} *id; } *id;
pid = jr->move.pid; nspid = jr->load.pid;
tid = jr->move.tid; pid = jr_entry_pid(jd, jr);
tid = jr_entry_tid(jd, jr);
usize = jd->unwinding_mapped_size; usize = jd->unwinding_mapped_size;
idr_size = jd->machine->id_hdr_size; idr_size = jd->machine->id_hdr_size;
...@@ -536,12 +565,12 @@ static int jit_repipe_code_move(struct jit_buf_desc *jd, union jr_entry *jr) ...@@ -536,12 +565,12 @@ static int jit_repipe_code_move(struct jit_buf_desc *jd, union jr_entry *jr)
filename = event->mmap2.filename; filename = event->mmap2.filename;
size = snprintf(filename, PATH_MAX, "%s/jitted-%d-%" PRIu64 ".so", size = snprintf(filename, PATH_MAX, "%s/jitted-%d-%" PRIu64 ".so",
jd->dir, jd->dir,
pid, nspid,
jr->move.code_index); jr->move.code_index);
size++; /* for \0 */ size++; /* for \0 */
if (stat(filename, &st)) if (nsinfo__stat(filename, &st, jd->nsi))
memset(&st, 0, sizeof(st)); memset(&st, 0, sizeof(st));
size = PERF_ALIGN(size, sizeof(u64)); size = PERF_ALIGN(size, sizeof(u64));
...@@ -700,7 +729,7 @@ jit_inject(struct jit_buf_desc *jd, char *path) ...@@ -700,7 +729,7 @@ jit_inject(struct jit_buf_desc *jd, char *path)
* as captured in the RECORD_MMAP record * as captured in the RECORD_MMAP record
*/ */
static int static int
jit_detect(char *mmap_name, pid_t pid) jit_detect(char *mmap_name, pid_t pid, struct nsinfo *nsi)
{ {
char *p; char *p;
char *end = NULL; char *end = NULL;
...@@ -740,7 +769,7 @@ jit_detect(char *mmap_name, pid_t pid) ...@@ -740,7 +769,7 @@ jit_detect(char *mmap_name, pid_t pid)
* pid does not match mmap pid * pid does not match mmap pid
* pid==0 in system-wide mode (synthesized) * pid==0 in system-wide mode (synthesized)
*/ */
if (pid && pid2 != pid) if (pid && pid2 != nsi->nstgid)
return -1; return -1;
/* /*
* validate suffix * validate suffix
...@@ -782,16 +811,30 @@ jit_process(struct perf_session *session, ...@@ -782,16 +811,30 @@ jit_process(struct perf_session *session,
struct machine *machine, struct machine *machine,
char *filename, char *filename,
pid_t pid, pid_t pid,
pid_t tid,
u64 *nbytes) u64 *nbytes)
{ {
struct thread *thread;
struct nsinfo *nsi;
struct evsel *first; struct evsel *first;
struct jit_buf_desc jd; struct jit_buf_desc jd;
int ret; int ret;
thread = machine__findnew_thread(machine, pid, tid);
if (thread == NULL) {
pr_err("problem processing JIT mmap event, skipping it.\n");
return 0;
}
nsi = nsinfo__get(thread->nsinfo);
thread__put(thread);
/* /*
* first, detect marker mmap (i.e., the jitdump mmap) * first, detect marker mmap (i.e., the jitdump mmap)
*/ */
if (jit_detect(filename, pid)) { if (jit_detect(filename, pid, nsi)) {
nsinfo__put(nsi);
// Strip //anon* mmaps if we processed a jitdump for this pid // Strip //anon* mmaps if we processed a jitdump for this pid
if (jit_has_pid(machine, pid) && (strncmp(filename, "//anon", 6) == 0)) if (jit_has_pid(machine, pid) && (strncmp(filename, "//anon", 6) == 0))
return 1; return 1;
...@@ -804,6 +847,7 @@ jit_process(struct perf_session *session, ...@@ -804,6 +847,7 @@ jit_process(struct perf_session *session,
jd.session = session; jd.session = session;
jd.output = output; jd.output = output;
jd.machine = machine; jd.machine = machine;
jd.nsi = nsi;
/* /*
* track sample_type to compute id_all layout * track sample_type to compute id_all layout
...@@ -821,5 +865,7 @@ jit_process(struct perf_session *session, ...@@ -821,5 +865,7 @@ jit_process(struct perf_session *session,
ret = 1; ret = 1;
} }
nsinfo__put(jd.nsi);
return ret; return ret;
} }
...@@ -287,3 +287,15 @@ char *nsinfo__realpath(const char *path, struct nsinfo *nsi) ...@@ -287,3 +287,15 @@ char *nsinfo__realpath(const char *path, struct nsinfo *nsi)
return rpath; return rpath;
} }
int nsinfo__stat(const char *filename, struct stat *st, struct nsinfo *nsi)
{
int ret;
struct nscookie nsc;
nsinfo__mountns_enter(nsi, &nsc);
ret = stat(filename, st);
nsinfo__mountns_exit(&nsc);
return ret;
}
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#define __PERF_NAMESPACES_H #define __PERF_NAMESPACES_H
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h>
#include <linux/stddef.h> #include <linux/stddef.h>
#include <linux/perf_event.h> #include <linux/perf_event.h>
#include <linux/refcount.h> #include <linux/refcount.h>
...@@ -56,6 +57,7 @@ void nsinfo__mountns_enter(struct nsinfo *nsi, struct nscookie *nc); ...@@ -56,6 +57,7 @@ void nsinfo__mountns_enter(struct nsinfo *nsi, struct nscookie *nc);
void nsinfo__mountns_exit(struct nscookie *nc); void nsinfo__mountns_exit(struct nscookie *nc);
char *nsinfo__realpath(const char *path, struct nsinfo *nsi); char *nsinfo__realpath(const char *path, struct nsinfo *nsi);
int nsinfo__stat(const char *filename, struct stat *st, struct nsinfo *nsi);
static inline void __nsinfo__zput(struct nsinfo **nsip) static inline void __nsinfo__zput(struct nsinfo **nsip)
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment