License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 22:07:57 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-08-13 12:12:10 +08:00
|
|
|
#include <sys/sysmacros.h>
|
2015-11-30 17:02:21 +08:00
|
|
|
#include <sys/types.h>
|
2017-04-18 21:46:11 +08:00
|
|
|
#include <errno.h>
|
2019-01-22 21:14:55 +08:00
|
|
|
#include <libgen.h>
|
2015-11-30 17:02:21 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <byteswap.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/mman.h>
|
2017-04-18 02:36:40 +08:00
|
|
|
#include <linux/stringify.h>
|
2015-11-30 17:02:21 +08:00
|
|
|
|
2019-08-30 22:11:01 +08:00
|
|
|
#include "build-id.h"
|
2015-11-30 17:02:21 +08:00
|
|
|
#include "event.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "evlist.h"
|
2020-11-05 09:56:04 +08:00
|
|
|
#include "namespaces.h"
|
2015-11-30 17:02:21 +08:00
|
|
|
#include "symbol.h"
|
|
|
|
#include <elf.h>
|
|
|
|
|
2016-03-08 16:38:50 +08:00
|
|
|
#include "tsc.h"
|
2015-11-30 17:02:21 +08:00
|
|
|
#include "session.h"
|
|
|
|
#include "jit.h"
|
|
|
|
#include "jitdump.h"
|
|
|
|
#include "genelf.h"
|
perf inject jit: Remove //anon mmap events
**perf-<pid>.map and jit-<pid>.dump designs:
When a JIT generates code to be executed, it must allocate memory and
mark it executable using an mmap call.
*** perf-<pid>.map design
The perf-<pid>.map assumes that any sample recorded in an anonymous
memory page is JIT code. It then tries to resolve the symbol name by
looking at the process' perf-<pid>.map.
*** jit-<pid>.dump design
The jit-<pid>.dump mechanism takes a different approach. It requires a
JIT to write a `<path>/jit-<pid>.dump` file. This file must also be
mmapped so that perf inject -jit can find the file. The JIT must also
add JIT_CODE_LOAD records for any functions it generates. The records
are timestamped using a clock which can be correlated to the perf record
clock.
After perf record, the `perf inject -jit` pass parses the recording
looking for a `<path>/jit-<pid>.dump` file. When it finds the file, it
parses it and for each JIT_CODE_LOAD record:
* creates an elf file `<path>/jitted-<pid>-<code_index>.so
* injects a new mmap record mapping the new elf file into the process.
*** Coexistence design
The kernel and perf support both of these mechanisms. We need to make
sure perf works on an app supporting either or both of these mechanisms.
Both designs rely on mmap records to determine how to resolve an ip
address.
The mmap records of both techniques by definition overlap. When the JIT
compiles a method, it must:
* allocate memory (mmap)
* add execution privilege (mprotect or mmap. either will
generate an mmap event form the kernel to perf)
* compile code into memory
* add a function record to perf-<pid>.map and/or jit-<pid>.dump
Because the jit-<pid>.dump mechanism supports greater capabilities, perf
prefers the symbols from jit-<pid>.dump. It implements this based on
timestamp ordering of events. There is an implicit ASSUMPTION that the
JIT_CODE_LOAD record timestamp will be after the // anon mmap event that
was generated during memory allocation or adding the execution privilege setting.
*** Problems with the ASSUMPTION
The ASSUMPTION made in the Coexistence design section above is violated
in the following scenario.
*** Scenario
While a JIT is jitting code it will eventually need to commit more
pages and change these pages to executable permissions. Typically the
JIT will want these collocated to minimize branch displacements.
The kernel will coalesce these anonymous mapping with identical
permissions before sending an MMAP event for the new pages. The address
range of the new mmap will not be just the most recently mmap pages.
It will include the entire coalesced mmap region.
See mm/mmap.c
unsigned long mmap_region(struct file *file, unsigned long addr,
unsigned long len, vm_flags_t vm_flags, unsigned long pgoff,
struct list_head *uf)
{
...
/*
* Can we just expand an old mapping?
*/
...
perf_event_mmap(vma);
...
}
*** Symptoms
The coalesced // anon mmap event will be timestamped after the
JIT_CODE_LOAD records. This means it will be used as the most recent
mapping for that entire address range. For remaining events it will look
at the inferior perf-<pid>.map for symbols.
If both mechanisms are supported, the symbol will appear twice with
different module names. This causes weird behavior in reporting.
If only jit-<pid>.dump is supported, the symbol will no longer be resolved.
** Implemented solution
This patch solves the issue by removing // anon mmap events for any
process which has a valid jit-<pid>.dump file.
It tracks on a per process basis to handle the case where some running
apps support jit-<pid>.dump, but some only support perf-<pid>.map.
It adds new assumptions:
* // anon mmap events are only required for perf-<pid>.map support.
* An app that uses jit-<pid>.dump, no longer needs
perf-<pid>.map support. It assumes that any perf-<pid>.map info is
inferior.
*** Details
Use thread->priv to store whether a jitdump file has been processed
During "perf inject --jit", discard "//anon*" mmap events for any pid which
has sucessfully processed a jitdump file.
** Testing:
// jitdump case
perf record <app with jitdump>
perf inject --jit --input perf.data --output perfjit.data
// verify mmap "//anon" events present initially
perf script --input perf.data --show-mmap-events | grep '//anon'
// verify mmap "//anon" events removed
perf script --input perfjit.data --show-mmap-events | grep '//anon'
// no jitdump case
perf record <app without jitdump>
perf inject --jit --input perf.data --output perfjit.data
// verify mmap "//anon" events present initially
perf script --input perf.data --show-mmap-events | grep '//anon'
// verify mmap "//anon" events not removed
perf script --input perfjit.data --show-mmap-events | grep '//anon'
** Repro:
This issue was discovered while testing the initial CoreCLR jitdump
implementation. https://github.com/dotnet/coreclr/pull/26897.
** Alternate solutions considered
These were also briefly considered:
* Change kernel to not coalesce mmap regions.
* Change kernel reporting of coalesced mmap regions to perf. Only
include newly mapped memory.
* Only strip parts of // anon mmap events overlapping existing
jitted-<pid>-<code_index>.so mmap events.
Signed-off-by: Steve MacLean <Steve.MacLean@Microsoft.com>
Acked-by: Ian Rogers <irogers@google.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lore.kernel.org/lkml/1590544271-125795-1-git-send-email-steve.maclean@linux.microsoft.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2020-05-27 09:51:11 +08:00
|
|
|
#include "thread.h"
|
2015-11-30 17:02:21 +08:00
|
|
|
|
tools perf: Move from sane_ctype.h obtained from git to the Linux's original
We got the sane_ctype.h headers from git and kept using it so far, but
since that code originally came from the kernel sources to the git
sources, perhaps its better to just use the one in the kernel, so that
we can leverage tools/perf/check_headers.sh to be notified when our copy
gets out of sync, i.e. when fixes or goodies are added to the code we've
copied.
This will help with things like tools/lib/string.c where we want to have
more things in common with the kernel, such as strim(), skip_spaces(),
etc so as to go on removing the things that we have in tools/perf/util/
and instead using the code in the kernel, indirectly and removing things
like EXPORT_SYMBOL(), etc, getting notified when fixes and improvements
are made to the original code.
Hopefully this also should help with reducing the difference of code
hosted in tools/ to the one in the kernel proper.
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: https://lkml.kernel.org/n/tip-7k9868l713wqtgo01xxygn12@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2019-06-26 04:27:31 +08:00
|
|
|
#include <linux/ctype.h>
|
2019-07-04 23:06:20 +08:00
|
|
|
#include <linux/zalloc.h>
|
2017-04-18 03:10:49 +08:00
|
|
|
|
2015-11-30 17:02:21 +08:00
|
|
|
struct jit_buf_desc {
|
2017-01-24 05:07:59 +08:00
|
|
|
struct perf_data *output;
|
2015-11-30 17:02:21 +08:00
|
|
|
struct perf_session *session;
|
|
|
|
struct machine *machine;
|
2020-11-05 09:56:04 +08:00
|
|
|
struct nsinfo *nsi;
|
2015-11-30 17:02:21 +08:00
|
|
|
union jr_entry *entry;
|
|
|
|
void *buf;
|
|
|
|
uint64_t sample_type;
|
|
|
|
size_t bufsize;
|
|
|
|
FILE *in;
|
2018-12-03 18:22:00 +08:00
|
|
|
bool needs_bswap; /* handles cross-endianness */
|
2016-03-08 16:38:50 +08:00
|
|
|
bool use_arch_timestamp;
|
2015-11-30 17:02:21 +08:00
|
|
|
void *debug_data;
|
2016-10-13 18:59:40 +08:00
|
|
|
void *unwinding_data;
|
|
|
|
uint64_t unwinding_size;
|
|
|
|
uint64_t unwinding_mapped_size;
|
|
|
|
uint64_t eh_frame_hdr_size;
|
2015-11-30 17:02:21 +08:00
|
|
|
size_t nr_debug_entries;
|
|
|
|
uint32_t code_load_count;
|
|
|
|
u64 bytes_written;
|
|
|
|
struct rb_root code_root;
|
|
|
|
char dir[PATH_MAX];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct jit_tool {
|
|
|
|
struct perf_tool tool;
|
2017-01-24 05:07:59 +08:00
|
|
|
struct perf_data output;
|
|
|
|
struct perf_data input;
|
2015-11-30 17:02:21 +08:00
|
|
|
u64 bytes_written;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define hmax(a, b) ((a) > (b) ? (a) : (b))
|
|
|
|
#define get_jit_tool(t) (container_of(tool, struct jit_tool, tool))
|
|
|
|
|
|
|
|
static int
|
2020-11-05 09:56:04 +08:00
|
|
|
jit_emit_elf(struct jit_buf_desc *jd,
|
|
|
|
char *filename,
|
2015-11-30 17:02:21 +08:00
|
|
|
const char *sym,
|
|
|
|
uint64_t code_addr,
|
|
|
|
const void *code,
|
2015-11-30 17:02:23 +08:00
|
|
|
int csize,
|
|
|
|
void *debug,
|
2016-10-13 18:59:41 +08:00
|
|
|
int nr_debug_entries,
|
|
|
|
void *unwinding,
|
|
|
|
uint32_t unwinding_header_size,
|
|
|
|
uint32_t unwinding_size)
|
2015-11-30 17:02:21 +08:00
|
|
|
{
|
2020-11-05 09:56:04 +08:00
|
|
|
int ret, fd, saved_errno;
|
|
|
|
struct nscookie nsc;
|
2015-11-30 17:02:21 +08:00
|
|
|
|
|
|
|
if (verbose > 0)
|
|
|
|
fprintf(stderr, "write ELF image %s\n", filename);
|
|
|
|
|
2020-11-05 09:56:04 +08:00
|
|
|
nsinfo__mountns_enter(jd->nsi, &nsc);
|
2015-11-30 17:02:21 +08:00
|
|
|
fd = open(filename, O_CREAT|O_TRUNC|O_WRONLY, 0644);
|
2020-11-05 09:56:04 +08:00
|
|
|
saved_errno = errno;
|
|
|
|
nsinfo__mountns_exit(&nsc);
|
2015-11-30 17:02:21 +08:00
|
|
|
if (fd == -1) {
|
2020-11-05 09:56:04 +08:00
|
|
|
pr_warning("cannot create jit ELF %s: %s\n", filename, strerror(saved_errno));
|
2015-11-30 17:02:21 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-10-13 18:59:41 +08:00
|
|
|
ret = jit_write_elf(fd, code_addr, sym, (const void *)code, csize, debug, nr_debug_entries,
|
|
|
|
unwinding, unwinding_header_size, unwinding_size);
|
2015-11-30 17:02:21 +08:00
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
2020-11-05 09:56:04 +08:00
|
|
|
if (ret) {
|
|
|
|
nsinfo__mountns_enter(jd->nsi, &nsc);
|
|
|
|
unlink(filename);
|
|
|
|
nsinfo__mountns_exit(&nsc);
|
|
|
|
}
|
2015-11-30 17:02:21 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
jit_close(struct jit_buf_desc *jd)
|
|
|
|
{
|
|
|
|
if (!(jd && jd->in))
|
|
|
|
return;
|
|
|
|
funlockfile(jd->in);
|
|
|
|
fclose(jd->in);
|
|
|
|
jd->in = NULL;
|
|
|
|
}
|
|
|
|
|
2016-03-08 03:44:41 +08:00
|
|
|
static int
|
|
|
|
jit_validate_events(struct perf_session *session)
|
|
|
|
{
|
2019-07-21 19:23:51 +08:00
|
|
|
struct evsel *evsel;
|
2016-03-08 03:44:41 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* check that all events use CLOCK_MONOTONIC
|
|
|
|
*/
|
2016-06-23 22:26:15 +08:00
|
|
|
evlist__for_each_entry(session->evlist, evsel) {
|
2019-07-21 19:24:29 +08:00
|
|
|
if (evsel->core.attr.use_clockid == 0 || evsel->core.attr.clockid != CLOCK_MONOTONIC)
|
2016-03-08 03:44:41 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-30 17:02:21 +08:00
|
|
|
static int
|
|
|
|
jit_open(struct jit_buf_desc *jd, const char *name)
|
|
|
|
{
|
|
|
|
struct jitheader header;
|
2020-11-05 09:56:04 +08:00
|
|
|
struct nscookie nsc;
|
2015-11-30 17:02:21 +08:00
|
|
|
struct jr_prefix *prefix;
|
|
|
|
ssize_t bs, bsz = 0;
|
|
|
|
void *n, *buf = NULL;
|
|
|
|
int ret, retval = -1;
|
|
|
|
|
2020-11-05 09:56:04 +08:00
|
|
|
nsinfo__mountns_enter(jd->nsi, &nsc);
|
2015-11-30 17:02:21 +08:00
|
|
|
jd->in = fopen(name, "r");
|
2020-11-05 09:56:04 +08:00
|
|
|
nsinfo__mountns_exit(&nsc);
|
2015-11-30 17:02:21 +08:00
|
|
|
if (!jd->in)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
bsz = hmax(sizeof(header), sizeof(*prefix));
|
|
|
|
|
|
|
|
buf = malloc(bsz);
|
|
|
|
if (!buf)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* protect from writer modifying the file while we are reading it
|
|
|
|
*/
|
|
|
|
flockfile(jd->in);
|
|
|
|
|
|
|
|
ret = fread(buf, sizeof(header), 1, jd->in);
|
|
|
|
if (ret != 1)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
memcpy(&header, buf, sizeof(header));
|
|
|
|
|
|
|
|
if (header.magic != JITHEADER_MAGIC) {
|
|
|
|
if (header.magic != JITHEADER_MAGIC_SW)
|
|
|
|
goto error;
|
|
|
|
jd->needs_bswap = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (jd->needs_bswap) {
|
|
|
|
header.version = bswap_32(header.version);
|
|
|
|
header.total_size = bswap_32(header.total_size);
|
|
|
|
header.pid = bswap_32(header.pid);
|
|
|
|
header.elf_mach = bswap_32(header.elf_mach);
|
|
|
|
header.timestamp = bswap_64(header.timestamp);
|
|
|
|
header.flags = bswap_64(header.flags);
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:38:50 +08:00
|
|
|
jd->use_arch_timestamp = header.flags & JITDUMP_FLAGS_ARCH_TIMESTAMP;
|
|
|
|
|
2015-11-30 17:02:21 +08:00
|
|
|
if (verbose > 2)
|
2016-03-08 16:38:50 +08:00
|
|
|
pr_debug("version=%u\nhdr.size=%u\nts=0x%llx\npid=%d\nelf_mach=%d\nuse_arch_timestamp=%d\n",
|
2015-11-30 17:02:21 +08:00
|
|
|
header.version,
|
|
|
|
header.total_size,
|
|
|
|
(unsigned long long)header.timestamp,
|
|
|
|
header.pid,
|
2016-03-08 16:38:50 +08:00
|
|
|
header.elf_mach,
|
|
|
|
jd->use_arch_timestamp);
|
2015-11-30 17:02:21 +08:00
|
|
|
|
2016-10-13 18:59:42 +08:00
|
|
|
if (header.version > JITHEADER_VERSION) {
|
2017-04-18 02:36:40 +08:00
|
|
|
pr_err("wrong jitdump version %u, expected " __stringify(JITHEADER_VERSION),
|
2016-10-13 18:59:42 +08:00
|
|
|
header.version);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2015-11-30 17:02:21 +08:00
|
|
|
if (header.flags & JITDUMP_FLAGS_RESERVED) {
|
|
|
|
pr_err("jitdump file contains invalid or unsupported flags 0x%llx\n",
|
|
|
|
(unsigned long long)header.flags & JITDUMP_FLAGS_RESERVED);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:38:50 +08:00
|
|
|
if (jd->use_arch_timestamp && !jd->session->time_conv.time_mult) {
|
|
|
|
pr_err("jitdump file uses arch timestamps but there is no timestamp conversion\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2016-03-08 03:44:41 +08:00
|
|
|
/*
|
|
|
|
* validate event is using the correct clockid
|
|
|
|
*/
|
2016-03-08 16:38:50 +08:00
|
|
|
if (!jd->use_arch_timestamp && jit_validate_events(jd->session)) {
|
2016-03-08 03:44:41 +08:00
|
|
|
pr_err("error, jitted code must be sampled with perf record -k 1\n");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2015-11-30 17:02:21 +08:00
|
|
|
bs = header.total_size - sizeof(header);
|
|
|
|
|
|
|
|
if (bs > bsz) {
|
|
|
|
n = realloc(buf, bs);
|
|
|
|
if (!n)
|
|
|
|
goto error;
|
|
|
|
bsz = bs;
|
|
|
|
buf = n;
|
|
|
|
/* read extra we do not know about */
|
|
|
|
ret = fread(buf, bs - bsz, 1, jd->in);
|
|
|
|
if (ret != 1)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* keep dirname for generating files and mmap records
|
|
|
|
*/
|
|
|
|
strcpy(jd->dir, name);
|
|
|
|
dirname(jd->dir);
|
2023-04-04 04:35:45 +08:00
|
|
|
free(buf);
|
2015-11-30 17:02:21 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
error:
|
2023-04-04 04:35:45 +08:00
|
|
|
free(buf);
|
2015-11-30 17:02:21 +08:00
|
|
|
funlockfile(jd->in);
|
|
|
|
fclose(jd->in);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static union jr_entry *
|
|
|
|
jit_get_next_entry(struct jit_buf_desc *jd)
|
|
|
|
{
|
|
|
|
struct jr_prefix *prefix;
|
|
|
|
union jr_entry *jr;
|
|
|
|
void *addr;
|
|
|
|
size_t bs, size;
|
|
|
|
int id, ret;
|
|
|
|
|
|
|
|
if (!(jd && jd->in))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (jd->buf == NULL) {
|
|
|
|
size_t sz = getpagesize();
|
|
|
|
if (sz < sizeof(*prefix))
|
|
|
|
sz = sizeof(*prefix);
|
|
|
|
|
|
|
|
jd->buf = malloc(sz);
|
|
|
|
if (jd->buf == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
jd->bufsize = sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix = jd->buf;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* file is still locked at this point
|
|
|
|
*/
|
|
|
|
ret = fread(prefix, sizeof(*prefix), 1, jd->in);
|
|
|
|
if (ret != 1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (jd->needs_bswap) {
|
|
|
|
prefix->id = bswap_32(prefix->id);
|
|
|
|
prefix->total_size = bswap_32(prefix->total_size);
|
|
|
|
prefix->timestamp = bswap_64(prefix->timestamp);
|
|
|
|
}
|
|
|
|
id = prefix->id;
|
|
|
|
size = prefix->total_size;
|
|
|
|
|
|
|
|
bs = (size_t)size;
|
|
|
|
if (bs < sizeof(*prefix))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (id >= JIT_CODE_MAX) {
|
2016-10-13 18:59:38 +08:00
|
|
|
pr_warning("next_entry: unknown record type %d, skipping\n", id);
|
2015-11-30 17:02:21 +08:00
|
|
|
}
|
|
|
|
if (bs > jd->bufsize) {
|
|
|
|
void *n;
|
|
|
|
n = realloc(jd->buf, bs);
|
|
|
|
if (!n)
|
|
|
|
return NULL;
|
|
|
|
jd->buf = n;
|
|
|
|
jd->bufsize = bs;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = ((void *)jd->buf) + sizeof(*prefix);
|
|
|
|
|
|
|
|
ret = fread(addr, bs - sizeof(*prefix), 1, jd->in);
|
|
|
|
if (ret != 1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
jr = (union jr_entry *)jd->buf;
|
|
|
|
|
|
|
|
switch(id) {
|
|
|
|
case JIT_CODE_DEBUG_INFO:
|
|
|
|
if (jd->needs_bswap) {
|
|
|
|
uint64_t n;
|
|
|
|
jr->info.code_addr = bswap_64(jr->info.code_addr);
|
|
|
|
jr->info.nr_entry = bswap_64(jr->info.nr_entry);
|
|
|
|
for (n = 0 ; n < jr->info.nr_entry; n++) {
|
|
|
|
jr->info.entries[n].addr = bswap_64(jr->info.entries[n].addr);
|
|
|
|
jr->info.entries[n].lineno = bswap_32(jr->info.entries[n].lineno);
|
|
|
|
jr->info.entries[n].discrim = bswap_32(jr->info.entries[n].discrim);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2016-10-13 18:59:40 +08:00
|
|
|
case JIT_CODE_UNWINDING_INFO:
|
|
|
|
if (jd->needs_bswap) {
|
|
|
|
jr->unwinding.unwinding_size = bswap_64(jr->unwinding.unwinding_size);
|
|
|
|
jr->unwinding.eh_frame_hdr_size = bswap_64(jr->unwinding.eh_frame_hdr_size);
|
|
|
|
jr->unwinding.mapped_size = bswap_64(jr->unwinding.mapped_size);
|
|
|
|
}
|
|
|
|
break;
|
2015-11-30 17:02:21 +08:00
|
|
|
case JIT_CODE_CLOSE:
|
|
|
|
break;
|
|
|
|
case JIT_CODE_LOAD:
|
|
|
|
if (jd->needs_bswap) {
|
|
|
|
jr->load.pid = bswap_32(jr->load.pid);
|
|
|
|
jr->load.tid = bswap_32(jr->load.tid);
|
|
|
|
jr->load.vma = bswap_64(jr->load.vma);
|
|
|
|
jr->load.code_addr = bswap_64(jr->load.code_addr);
|
|
|
|
jr->load.code_size = bswap_64(jr->load.code_size);
|
|
|
|
jr->load.code_index= bswap_64(jr->load.code_index);
|
|
|
|
}
|
|
|
|
jd->code_load_count++;
|
|
|
|
break;
|
|
|
|
case JIT_CODE_MOVE:
|
|
|
|
if (jd->needs_bswap) {
|
|
|
|
jr->move.pid = bswap_32(jr->move.pid);
|
|
|
|
jr->move.tid = bswap_32(jr->move.tid);
|
|
|
|
jr->move.vma = bswap_64(jr->move.vma);
|
|
|
|
jr->move.old_code_addr = bswap_64(jr->move.old_code_addr);
|
|
|
|
jr->move.new_code_addr = bswap_64(jr->move.new_code_addr);
|
|
|
|
jr->move.code_size = bswap_64(jr->move.code_size);
|
|
|
|
jr->move.code_index = bswap_64(jr->move.code_index);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case JIT_CODE_MAX:
|
|
|
|
default:
|
2016-10-13 18:59:38 +08:00
|
|
|
/* skip unknown record (we have read them) */
|
|
|
|
break;
|
2015-11-30 17:02:21 +08:00
|
|
|
}
|
|
|
|
return jr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
jit_inject_event(struct jit_buf_desc *jd, union perf_event *event)
|
|
|
|
{
|
|
|
|
ssize_t size;
|
|
|
|
|
2017-01-24 05:07:59 +08:00
|
|
|
size = perf_data__write(jd->output, event, event->header.size);
|
2015-11-30 17:02:21 +08:00
|
|
|
if (size < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
jd->bytes_written += size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-11-05 09:56:04 +08:00
|
|
|
static pid_t jr_entry_pid(struct jit_buf_desc *jd, union jr_entry *jr)
|
|
|
|
{
|
2022-02-11 18:34:06 +08:00
|
|
|
if (jd->nsi && nsinfo__in_pidns(jd->nsi))
|
|
|
|
return nsinfo__tgid(jd->nsi);
|
2020-11-05 09:56:04 +08:00
|
|
|
return jr->load.pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static pid_t jr_entry_tid(struct jit_buf_desc *jd, union jr_entry *jr)
|
|
|
|
{
|
2022-02-11 18:34:06 +08:00
|
|
|
if (jd->nsi && nsinfo__in_pidns(jd->nsi))
|
|
|
|
return nsinfo__pid(jd->nsi);
|
2020-11-05 09:56:04 +08:00
|
|
|
return jr->load.tid;
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:38:50 +08:00
|
|
|
static uint64_t convert_timestamp(struct jit_buf_desc *jd, uint64_t timestamp)
|
|
|
|
{
|
2021-04-28 20:09:13 +08:00
|
|
|
struct perf_tsc_conversion tc = { .time_shift = 0, };
|
|
|
|
struct perf_record_time_conv *time_conv = &jd->session->time_conv;
|
2016-03-08 16:38:50 +08:00
|
|
|
|
|
|
|
if (!jd->use_arch_timestamp)
|
|
|
|
return timestamp;
|
|
|
|
|
2021-04-28 20:09:13 +08:00
|
|
|
tc.time_shift = time_conv->time_shift;
|
|
|
|
tc.time_mult = time_conv->time_mult;
|
|
|
|
tc.time_zero = time_conv->time_zero;
|
2020-09-14 19:53:09 +08:00
|
|
|
|
2021-04-28 20:09:13 +08:00
|
|
|
/*
|
|
|
|
* The event TIME_CONV was extended for the fields from "time_cycles"
|
|
|
|
* when supported cap_user_time_short, for backward compatibility,
|
|
|
|
* checks the event size and assigns these extended fields if these
|
|
|
|
* fields are contained in the event.
|
|
|
|
*/
|
|
|
|
if (event_contains(*time_conv, time_cycles)) {
|
|
|
|
tc.time_cycles = time_conv->time_cycles;
|
|
|
|
tc.time_mask = time_conv->time_mask;
|
|
|
|
tc.cap_user_time_zero = time_conv->cap_user_time_zero;
|
|
|
|
tc.cap_user_time_short = time_conv->cap_user_time_short;
|
|
|
|
|
|
|
|
if (!tc.cap_user_time_zero)
|
|
|
|
return 0;
|
|
|
|
}
|
2016-03-08 16:38:50 +08:00
|
|
|
|
|
|
|
return tsc_to_perf_time(timestamp, &tc);
|
|
|
|
}
|
|
|
|
|
2015-11-30 17:02:21 +08:00
|
|
|
static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr)
|
|
|
|
{
|
|
|
|
struct perf_sample sample;
|
|
|
|
union perf_event *event;
|
|
|
|
struct perf_tool *tool = jd->session->tool;
|
|
|
|
uint64_t code, addr;
|
|
|
|
uintptr_t uaddr;
|
|
|
|
char *filename;
|
|
|
|
struct stat st;
|
|
|
|
size_t size;
|
|
|
|
u16 idr_size;
|
|
|
|
const char *sym;
|
2019-09-28 09:41:18 +08:00
|
|
|
uint64_t count;
|
2016-10-13 18:59:40 +08:00
|
|
|
int ret, csize, usize;
|
2020-11-05 09:56:04 +08:00
|
|
|
pid_t nspid, pid, tid;
|
2015-11-30 17:02:21 +08:00
|
|
|
struct {
|
|
|
|
u32 pid, tid;
|
|
|
|
u64 time;
|
|
|
|
} *id;
|
|
|
|
|
2020-11-05 09:56:04 +08:00
|
|
|
nspid = jr->load.pid;
|
|
|
|
pid = jr_entry_pid(jd, jr);
|
|
|
|
tid = jr_entry_tid(jd, jr);
|
2015-11-30 17:02:21 +08:00
|
|
|
csize = jr->load.code_size;
|
2016-10-13 18:59:40 +08:00
|
|
|
usize = jd->unwinding_mapped_size;
|
2015-11-30 17:02:21 +08:00
|
|
|
addr = jr->load.code_addr;
|
|
|
|
sym = (void *)((unsigned long)jr + sizeof(jr->load));
|
|
|
|
code = (unsigned long)jr + jr->load.p.total_size - csize;
|
|
|
|
count = jr->load.code_index;
|
|
|
|
idr_size = jd->machine->id_hdr_size;
|
|
|
|
|
|
|
|
event = calloc(1, sizeof(*event) + idr_size);
|
|
|
|
if (!event)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
filename = event->mmap2.filename;
|
2019-09-28 09:41:18 +08:00
|
|
|
size = snprintf(filename, PATH_MAX, "%s/jitted-%d-%" PRIu64 ".so",
|
2015-11-30 17:02:21 +08:00
|
|
|
jd->dir,
|
2020-11-05 09:56:04 +08:00
|
|
|
nspid,
|
2015-11-30 17:02:21 +08:00
|
|
|
count);
|
|
|
|
|
|
|
|
size++; /* for \0 */
|
|
|
|
|
|
|
|
size = PERF_ALIGN(size, sizeof(u64));
|
|
|
|
uaddr = (uintptr_t)code;
|
2020-11-05 09:56:04 +08:00
|
|
|
ret = jit_emit_elf(jd, filename, sym, addr, (const void *)uaddr, csize, jd->debug_data, jd->nr_debug_entries,
|
2016-10-13 18:59:41 +08:00
|
|
|
jd->unwinding_data, jd->eh_frame_hdr_size, jd->unwinding_size);
|
2015-11-30 17:02:21 +08:00
|
|
|
|
|
|
|
if (jd->debug_data && jd->nr_debug_entries) {
|
2019-07-04 23:06:20 +08:00
|
|
|
zfree(&jd->debug_data);
|
2015-11-30 17:02:21 +08:00
|
|
|
jd->nr_debug_entries = 0;
|
|
|
|
}
|
|
|
|
|
2016-10-13 18:59:40 +08:00
|
|
|
if (jd->unwinding_data && jd->eh_frame_hdr_size) {
|
2019-07-04 23:06:20 +08:00
|
|
|
zfree(&jd->unwinding_data);
|
2016-10-13 18:59:40 +08:00
|
|
|
jd->eh_frame_hdr_size = 0;
|
|
|
|
jd->unwinding_mapped_size = 0;
|
|
|
|
jd->unwinding_size = 0;
|
|
|
|
}
|
|
|
|
|
2015-11-30 17:02:21 +08:00
|
|
|
if (ret) {
|
|
|
|
free(event);
|
|
|
|
return -1;
|
|
|
|
}
|
2020-11-05 09:56:04 +08:00
|
|
|
if (nsinfo__stat(filename, &st, jd->nsi))
|
perf jit: memset() variable 'st' using the correct size
The current code is memsetting the 'struct stat' variable 'st' with the size of
'stat' (which turns out to be 1 byte) rather than the size of variable 'sz'.
Committer notes:
sizeof(function) isn't valid, the result depends on the compiler used, with
gcc, enabling pedantic warnings we get:
$ cat sizeof_function.c
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
int main(void)
{
printf("sizeof(stat)=%zd, stat=%p\n", sizeof(stat), stat);
return 0;
}
$ readelf -sW sizeof_function | grep -w stat
49: 0000000000400630 16 FUNC WEAK HIDDEN 13 stat
$ cc -pedantic sizeof_function.c -o sizeof_function
sizeof_function.c: In function ‘main’:
sizeof_function.c:8:46: warning: invalid application of ‘sizeof’ to a function type [-Wpointer-arith]
printf("sizeof(stat)=%zd, stat=%p\n", sizeof(stat), stat);
^
$ ./sizeof_function
sizeof(stat)=1, stat=0x400630
$
Standard C, section 6.5.3.4:
"The sizeof operator shall not be applied to an expression that has function
type or an incomplete type, to the parenthesized name of such a type,
or to an expression that designates a bit-field member."
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf
Signed-off-by: Colin Ian King <colin.king@canonical.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Fixes: 9b07e27f88b9 ("perf inject: Add jitdump mmap injection support")
Link: http://lkml.kernel.org/r/1461020838-9260-1-git-send-email-colin.king@canonical.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2016-04-19 07:07:18 +08:00
|
|
|
memset(&st, 0, sizeof(st));
|
2015-11-30 17:02:21 +08:00
|
|
|
|
|
|
|
event->mmap2.header.type = PERF_RECORD_MMAP2;
|
|
|
|
event->mmap2.header.misc = PERF_RECORD_MISC_USER;
|
|
|
|
event->mmap2.header.size = (sizeof(event->mmap2) -
|
|
|
|
(sizeof(event->mmap2.filename) - size) + idr_size);
|
|
|
|
|
|
|
|
event->mmap2.pgoff = GEN_ELF_TEXT_OFFSET;
|
|
|
|
event->mmap2.start = addr;
|
2016-10-13 18:59:40 +08:00
|
|
|
event->mmap2.len = usize ? ALIGN_8(csize) + usize : csize;
|
2015-11-30 17:02:21 +08:00
|
|
|
event->mmap2.pid = pid;
|
|
|
|
event->mmap2.tid = tid;
|
|
|
|
event->mmap2.ino = st.st_ino;
|
|
|
|
event->mmap2.maj = major(st.st_dev);
|
|
|
|
event->mmap2.min = minor(st.st_dev);
|
|
|
|
event->mmap2.prot = st.st_mode;
|
|
|
|
event->mmap2.flags = MAP_SHARED;
|
|
|
|
event->mmap2.ino_generation = 1;
|
|
|
|
|
|
|
|
id = (void *)((unsigned long)event + event->mmap.header.size - idr_size);
|
|
|
|
if (jd->sample_type & PERF_SAMPLE_TID) {
|
|
|
|
id->pid = pid;
|
|
|
|
id->tid = tid;
|
|
|
|
}
|
|
|
|
if (jd->sample_type & PERF_SAMPLE_TIME)
|
2016-03-08 16:38:50 +08:00
|
|
|
id->time = convert_timestamp(jd, jr->load.p.timestamp);
|
2015-11-30 17:02:21 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* create pseudo sample to induce dso hit increment
|
|
|
|
* use first address as sample address
|
|
|
|
*/
|
|
|
|
memset(&sample, 0, sizeof(sample));
|
perf tools: Add missing initialization of perf_sample.cpumode in synthesized samples
In 473398a21d28 ("perf tools: Add cpumode to struct perf_sample"), I
missed some places where perf_sample fields are directly initialized in
addition to what is done in perf_evsel__parse_sample(), namely when
synthesizing PERF_RECORD_{MMAP*,COMM,FORK,EXIT} for pre-existing threads
and also in intel_pt and intel_bts when synthesizing events from
processor trace, the jitdump code also was affected, fix it.
The problem was noticed with running:
# perf record -e intel_pt//u true
# perf script
Where the samples wouldn't get resolved because perf_sample.cpumode
would be left as zero, i.e. PERF_RECORD_MISC_CPUMODE_UNKNOWN, not
resolving as kernel, hypervisor or user cpu modes.
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Fixes: 473398a21d28 ("perf tools: Add cpumode to struct perf_sample")
Link: http://lkml.kernel.org/n/tip-n5sdauxgk24d5nun8kuuu2mh@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2016-03-30 05:46:04 +08:00
|
|
|
sample.cpumode = PERF_RECORD_MISC_USER;
|
2015-11-30 17:02:21 +08:00
|
|
|
sample.pid = pid;
|
|
|
|
sample.tid = tid;
|
|
|
|
sample.time = id->time;
|
|
|
|
sample.ip = addr;
|
|
|
|
|
|
|
|
ret = perf_event__process_mmap2(tool, event, &sample, jd->machine);
|
|
|
|
if (ret)
|
2023-04-04 04:35:45 +08:00
|
|
|
goto out;
|
2015-11-30 17:02:21 +08:00
|
|
|
|
|
|
|
ret = jit_inject_event(jd, event);
|
|
|
|
/*
|
|
|
|
* mark dso as use to generate buildid in the header
|
|
|
|
*/
|
|
|
|
if (!ret)
|
|
|
|
build_id__mark_dso_hit(tool, event, &sample, NULL, jd->machine);
|
|
|
|
|
2023-04-04 04:35:45 +08:00
|
|
|
out:
|
|
|
|
free(event);
|
2015-11-30 17:02:21 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int jit_repipe_code_move(struct jit_buf_desc *jd, union jr_entry *jr)
|
|
|
|
{
|
|
|
|
struct perf_sample sample;
|
|
|
|
union perf_event *event;
|
|
|
|
struct perf_tool *tool = jd->session->tool;
|
|
|
|
char *filename;
|
|
|
|
size_t size;
|
|
|
|
struct stat st;
|
2016-10-13 18:59:40 +08:00
|
|
|
int usize;
|
2015-11-30 17:02:21 +08:00
|
|
|
u16 idr_size;
|
|
|
|
int ret;
|
2020-11-05 09:56:04 +08:00
|
|
|
pid_t nspid, pid, tid;
|
2015-11-30 17:02:21 +08:00
|
|
|
struct {
|
|
|
|
u32 pid, tid;
|
|
|
|
u64 time;
|
|
|
|
} *id;
|
|
|
|
|
2020-11-05 09:56:04 +08:00
|
|
|
nspid = jr->load.pid;
|
|
|
|
pid = jr_entry_pid(jd, jr);
|
|
|
|
tid = jr_entry_tid(jd, jr);
|
2016-10-13 18:59:40 +08:00
|
|
|
usize = jd->unwinding_mapped_size;
|
2015-11-30 17:02:21 +08:00
|
|
|
idr_size = jd->machine->id_hdr_size;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* +16 to account for sample_id_all (hack)
|
|
|
|
*/
|
|
|
|
event = calloc(1, sizeof(*event) + 16);
|
|
|
|
if (!event)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
filename = event->mmap2.filename;
|
2019-09-28 09:41:18 +08:00
|
|
|
size = snprintf(filename, PATH_MAX, "%s/jitted-%d-%" PRIu64 ".so",
|
2015-11-30 17:02:21 +08:00
|
|
|
jd->dir,
|
2020-11-05 09:56:04 +08:00
|
|
|
nspid,
|
2015-11-30 17:02:21 +08:00
|
|
|
jr->move.code_index);
|
|
|
|
|
|
|
|
size++; /* for \0 */
|
|
|
|
|
2020-11-05 09:56:04 +08:00
|
|
|
if (nsinfo__stat(filename, &st, jd->nsi))
|
perf jit: memset() variable 'st' using the correct size
The current code is memsetting the 'struct stat' variable 'st' with the size of
'stat' (which turns out to be 1 byte) rather than the size of variable 'sz'.
Committer notes:
sizeof(function) isn't valid, the result depends on the compiler used, with
gcc, enabling pedantic warnings we get:
$ cat sizeof_function.c
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
int main(void)
{
printf("sizeof(stat)=%zd, stat=%p\n", sizeof(stat), stat);
return 0;
}
$ readelf -sW sizeof_function | grep -w stat
49: 0000000000400630 16 FUNC WEAK HIDDEN 13 stat
$ cc -pedantic sizeof_function.c -o sizeof_function
sizeof_function.c: In function ‘main’:
sizeof_function.c:8:46: warning: invalid application of ‘sizeof’ to a function type [-Wpointer-arith]
printf("sizeof(stat)=%zd, stat=%p\n", sizeof(stat), stat);
^
$ ./sizeof_function
sizeof(stat)=1, stat=0x400630
$
Standard C, section 6.5.3.4:
"The sizeof operator shall not be applied to an expression that has function
type or an incomplete type, to the parenthesized name of such a type,
or to an expression that designates a bit-field member."
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf
Signed-off-by: Colin Ian King <colin.king@canonical.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Fixes: 9b07e27f88b9 ("perf inject: Add jitdump mmap injection support")
Link: http://lkml.kernel.org/r/1461020838-9260-1-git-send-email-colin.king@canonical.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2016-04-19 07:07:18 +08:00
|
|
|
memset(&st, 0, sizeof(st));
|
2015-11-30 17:02:21 +08:00
|
|
|
|
|
|
|
size = PERF_ALIGN(size, sizeof(u64));
|
|
|
|
|
|
|
|
event->mmap2.header.type = PERF_RECORD_MMAP2;
|
|
|
|
event->mmap2.header.misc = PERF_RECORD_MISC_USER;
|
|
|
|
event->mmap2.header.size = (sizeof(event->mmap2) -
|
|
|
|
(sizeof(event->mmap2.filename) - size) + idr_size);
|
|
|
|
event->mmap2.pgoff = GEN_ELF_TEXT_OFFSET;
|
|
|
|
event->mmap2.start = jr->move.new_code_addr;
|
2016-10-13 18:59:40 +08:00
|
|
|
event->mmap2.len = usize ? ALIGN_8(jr->move.code_size) + usize
|
|
|
|
: jr->move.code_size;
|
2015-11-30 17:02:21 +08:00
|
|
|
event->mmap2.pid = pid;
|
|
|
|
event->mmap2.tid = tid;
|
|
|
|
event->mmap2.ino = st.st_ino;
|
|
|
|
event->mmap2.maj = major(st.st_dev);
|
|
|
|
event->mmap2.min = minor(st.st_dev);
|
|
|
|
event->mmap2.prot = st.st_mode;
|
|
|
|
event->mmap2.flags = MAP_SHARED;
|
|
|
|
event->mmap2.ino_generation = 1;
|
|
|
|
|
|
|
|
id = (void *)((unsigned long)event + event->mmap.header.size - idr_size);
|
|
|
|
if (jd->sample_type & PERF_SAMPLE_TID) {
|
|
|
|
id->pid = pid;
|
|
|
|
id->tid = tid;
|
|
|
|
}
|
|
|
|
if (jd->sample_type & PERF_SAMPLE_TIME)
|
2016-03-08 16:38:50 +08:00
|
|
|
id->time = convert_timestamp(jd, jr->load.p.timestamp);
|
2015-11-30 17:02:21 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* create pseudo sample to induce dso hit increment
|
|
|
|
* use first address as sample address
|
|
|
|
*/
|
|
|
|
memset(&sample, 0, sizeof(sample));
|
perf tools: Add missing initialization of perf_sample.cpumode in synthesized samples
In 473398a21d28 ("perf tools: Add cpumode to struct perf_sample"), I
missed some places where perf_sample fields are directly initialized in
addition to what is done in perf_evsel__parse_sample(), namely when
synthesizing PERF_RECORD_{MMAP*,COMM,FORK,EXIT} for pre-existing threads
and also in intel_pt and intel_bts when synthesizing events from
processor trace, the jitdump code also was affected, fix it.
The problem was noticed with running:
# perf record -e intel_pt//u true
# perf script
Where the samples wouldn't get resolved because perf_sample.cpumode
would be left as zero, i.e. PERF_RECORD_MISC_CPUMODE_UNKNOWN, not
resolving as kernel, hypervisor or user cpu modes.
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Fixes: 473398a21d28 ("perf tools: Add cpumode to struct perf_sample")
Link: http://lkml.kernel.org/n/tip-n5sdauxgk24d5nun8kuuu2mh@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2016-03-30 05:46:04 +08:00
|
|
|
sample.cpumode = PERF_RECORD_MISC_USER;
|
2015-11-30 17:02:21 +08:00
|
|
|
sample.pid = pid;
|
|
|
|
sample.tid = tid;
|
|
|
|
sample.time = id->time;
|
|
|
|
sample.ip = jr->move.new_code_addr;
|
|
|
|
|
|
|
|
ret = perf_event__process_mmap2(tool, event, &sample, jd->machine);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = jit_inject_event(jd, event);
|
|
|
|
if (!ret)
|
|
|
|
build_id__mark_dso_hit(tool, event, &sample, NULL, jd->machine);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int jit_repipe_debug_info(struct jit_buf_desc *jd, union jr_entry *jr)
|
|
|
|
{
|
|
|
|
void *data;
|
|
|
|
size_t sz;
|
|
|
|
|
|
|
|
if (!(jd && jr))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
sz = jr->prefix.total_size - sizeof(jr->info);
|
|
|
|
data = malloc(sz);
|
|
|
|
if (!data)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memcpy(data, &jr->info.entries, sz);
|
|
|
|
|
|
|
|
jd->debug_data = data;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we must use nr_entry instead of size here because
|
|
|
|
* we cannot distinguish actual entry from padding otherwise
|
|
|
|
*/
|
|
|
|
jd->nr_debug_entries = jr->info.nr_entry;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-13 18:59:40 +08:00
|
|
|
static int
|
|
|
|
jit_repipe_unwinding_info(struct jit_buf_desc *jd, union jr_entry *jr)
|
|
|
|
{
|
|
|
|
void *unwinding_data;
|
|
|
|
uint32_t unwinding_data_size;
|
|
|
|
|
|
|
|
if (!(jd && jr))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
unwinding_data_size = jr->prefix.total_size - sizeof(jr->unwinding);
|
|
|
|
unwinding_data = malloc(unwinding_data_size);
|
|
|
|
if (!unwinding_data)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memcpy(unwinding_data, &jr->unwinding.unwinding_data,
|
|
|
|
unwinding_data_size);
|
|
|
|
|
|
|
|
jd->eh_frame_hdr_size = jr->unwinding.eh_frame_hdr_size;
|
|
|
|
jd->unwinding_size = jr->unwinding.unwinding_size;
|
|
|
|
jd->unwinding_mapped_size = jr->unwinding.mapped_size;
|
|
|
|
jd->unwinding_data = unwinding_data;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-30 17:02:21 +08:00
|
|
|
static int
|
|
|
|
jit_process_dump(struct jit_buf_desc *jd)
|
|
|
|
{
|
|
|
|
union jr_entry *jr;
|
perf jit: Avoid returning garbage for a ret variable
When the loop body isn't executed at all, then the 'ret' local variable,
that is uninitialized will be used as the return value.
This triggers this error on Alpine Linux:
CC /tmp/build/perf/util/demangle-java.o
CC /tmp/build/perf/util/demangle-rust.o
CC /tmp/build/perf/util/jitdump.o
CC /tmp/build/perf/util/genelf.o
util/jitdump.c: In function 'jit_process':
util/jitdump.c:622:3: error: 'ret' may be used uninitialized in this function [-Werror=maybe-uninitialized]
fprintf(stderr, "injected: %s (%d)\n", path, ret);
^
util/jitdump.c:584:6: note: 'ret' was declared here
int ret;
^
FLEX /tmp/build/perf/util/parse-events-flex.c
/ $ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-alpine-linux-musl/5.3.0/lto-wrapper
Target: x86_64-alpine-linux-musl
Configured with: /home/buildozer/aports/main/gcc/src/gcc-5.3.0/configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info
+--build=x86_64-alpine-linux-musl --host=x86_64-alpine-linux-musl --target=x86_64-alpine-linux-musl --with-pkgversion='Alpine 5.3.0' --enable-checking=release
+--disable-fixed-point --disable-libstdcxx-pch --disable-multilib --disable-nls --disable-werror --disable-symvers --enable-__cxa_atexit --enable-esp
+--enable-cloog-backend --enable-languages=c,c++,objc,java,fortran,ada --disable-libssp --disable-libmudflap --disable-libsanitizer --enable-shared
+--enable-threads --enable-tls --with-system-zlib
Thread model: posix
gcc version 5.3.0 (Alpine 5.3.0)
But this so far got under the radar, not causing any build problem, till the
"perf jit: enable jitdump support without dwarf" gets applied, when the above
problem takes place, some combination of inlining or whatever, the problem
is real, so fix it by initializing the variable to zero.
Cc: Anton Blanchard <anton@ozlabs.org>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Maciej Debski <maciejd@google.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lkml.kernel.org/r/20161013200437.GA12815@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2016-10-14 04:12:35 +08:00
|
|
|
int ret = 0;
|
2015-11-30 17:02:21 +08:00
|
|
|
|
|
|
|
while ((jr = jit_get_next_entry(jd))) {
|
|
|
|
switch(jr->prefix.id) {
|
|
|
|
case JIT_CODE_LOAD:
|
|
|
|
ret = jit_repipe_code_load(jd, jr);
|
|
|
|
break;
|
|
|
|
case JIT_CODE_MOVE:
|
|
|
|
ret = jit_repipe_code_move(jd, jr);
|
|
|
|
break;
|
|
|
|
case JIT_CODE_DEBUG_INFO:
|
|
|
|
ret = jit_repipe_debug_info(jd, jr);
|
|
|
|
break;
|
2016-10-13 18:59:40 +08:00
|
|
|
case JIT_CODE_UNWINDING_INFO:
|
|
|
|
ret = jit_repipe_unwinding_info(jd, jr);
|
|
|
|
break;
|
2015-11-30 17:02:21 +08:00
|
|
|
default:
|
|
|
|
ret = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
jit_inject(struct jit_buf_desc *jd, char *path)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (verbose > 0)
|
|
|
|
fprintf(stderr, "injecting: %s\n", path);
|
|
|
|
|
|
|
|
ret = jit_open(jd, path);
|
|
|
|
if (ret)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
ret = jit_process_dump(jd);
|
|
|
|
|
|
|
|
jit_close(jd);
|
|
|
|
|
|
|
|
if (verbose > 0)
|
|
|
|
fprintf(stderr, "injected: %s (%d)\n", path, ret);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* File must be with pattern .../jit-XXXX.dump
|
|
|
|
* where XXXX is the PID of the process which did the mmap()
|
|
|
|
* as captured in the RECORD_MMAP record
|
|
|
|
*/
|
|
|
|
static int
|
2020-11-05 09:56:04 +08:00
|
|
|
jit_detect(char *mmap_name, pid_t pid, struct nsinfo *nsi)
|
2015-11-30 17:02:21 +08:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
char *end = NULL;
|
|
|
|
pid_t pid2;
|
|
|
|
|
|
|
|
if (verbose > 2)
|
|
|
|
fprintf(stderr, "jit marker trying : %s\n", mmap_name);
|
|
|
|
/*
|
|
|
|
* get file name
|
|
|
|
*/
|
|
|
|
p = strrchr(mmap_name, '/');
|
|
|
|
if (!p)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* match prefix
|
|
|
|
*/
|
|
|
|
if (strncmp(p, "/jit-", 5))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* skip prefix
|
|
|
|
*/
|
|
|
|
p += 5;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* must be followed by a pid
|
|
|
|
*/
|
|
|
|
if (!isdigit(*p))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pid2 = (int)strtol(p, &end, 10);
|
|
|
|
if (!end)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pid does not match mmap pid
|
|
|
|
* pid==0 in system-wide mode (synthesized)
|
|
|
|
*/
|
2022-02-11 18:34:06 +08:00
|
|
|
if (pid && pid2 != nsinfo__nstgid(nsi))
|
2015-11-30 17:02:21 +08:00
|
|
|
return -1;
|
|
|
|
/*
|
|
|
|
* validate suffix
|
|
|
|
*/
|
|
|
|
if (strcmp(end, ".dump"))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (verbose > 0)
|
|
|
|
fprintf(stderr, "jit marker found: %s\n", mmap_name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
perf inject jit: Remove //anon mmap events
**perf-<pid>.map and jit-<pid>.dump designs:
When a JIT generates code to be executed, it must allocate memory and
mark it executable using an mmap call.
*** perf-<pid>.map design
The perf-<pid>.map assumes that any sample recorded in an anonymous
memory page is JIT code. It then tries to resolve the symbol name by
looking at the process' perf-<pid>.map.
*** jit-<pid>.dump design
The jit-<pid>.dump mechanism takes a different approach. It requires a
JIT to write a `<path>/jit-<pid>.dump` file. This file must also be
mmapped so that perf inject -jit can find the file. The JIT must also
add JIT_CODE_LOAD records for any functions it generates. The records
are timestamped using a clock which can be correlated to the perf record
clock.
After perf record, the `perf inject -jit` pass parses the recording
looking for a `<path>/jit-<pid>.dump` file. When it finds the file, it
parses it and for each JIT_CODE_LOAD record:
* creates an elf file `<path>/jitted-<pid>-<code_index>.so
* injects a new mmap record mapping the new elf file into the process.
*** Coexistence design
The kernel and perf support both of these mechanisms. We need to make
sure perf works on an app supporting either or both of these mechanisms.
Both designs rely on mmap records to determine how to resolve an ip
address.
The mmap records of both techniques by definition overlap. When the JIT
compiles a method, it must:
* allocate memory (mmap)
* add execution privilege (mprotect or mmap. either will
generate an mmap event form the kernel to perf)
* compile code into memory
* add a function record to perf-<pid>.map and/or jit-<pid>.dump
Because the jit-<pid>.dump mechanism supports greater capabilities, perf
prefers the symbols from jit-<pid>.dump. It implements this based on
timestamp ordering of events. There is an implicit ASSUMPTION that the
JIT_CODE_LOAD record timestamp will be after the // anon mmap event that
was generated during memory allocation or adding the execution privilege setting.
*** Problems with the ASSUMPTION
The ASSUMPTION made in the Coexistence design section above is violated
in the following scenario.
*** Scenario
While a JIT is jitting code it will eventually need to commit more
pages and change these pages to executable permissions. Typically the
JIT will want these collocated to minimize branch displacements.
The kernel will coalesce these anonymous mapping with identical
permissions before sending an MMAP event for the new pages. The address
range of the new mmap will not be just the most recently mmap pages.
It will include the entire coalesced mmap region.
See mm/mmap.c
unsigned long mmap_region(struct file *file, unsigned long addr,
unsigned long len, vm_flags_t vm_flags, unsigned long pgoff,
struct list_head *uf)
{
...
/*
* Can we just expand an old mapping?
*/
...
perf_event_mmap(vma);
...
}
*** Symptoms
The coalesced // anon mmap event will be timestamped after the
JIT_CODE_LOAD records. This means it will be used as the most recent
mapping for that entire address range. For remaining events it will look
at the inferior perf-<pid>.map for symbols.
If both mechanisms are supported, the symbol will appear twice with
different module names. This causes weird behavior in reporting.
If only jit-<pid>.dump is supported, the symbol will no longer be resolved.
** Implemented solution
This patch solves the issue by removing // anon mmap events for any
process which has a valid jit-<pid>.dump file.
It tracks on a per process basis to handle the case where some running
apps support jit-<pid>.dump, but some only support perf-<pid>.map.
It adds new assumptions:
* // anon mmap events are only required for perf-<pid>.map support.
* An app that uses jit-<pid>.dump, no longer needs
perf-<pid>.map support. It assumes that any perf-<pid>.map info is
inferior.
*** Details
Use thread->priv to store whether a jitdump file has been processed
During "perf inject --jit", discard "//anon*" mmap events for any pid which
has sucessfully processed a jitdump file.
** Testing:
// jitdump case
perf record <app with jitdump>
perf inject --jit --input perf.data --output perfjit.data
// verify mmap "//anon" events present initially
perf script --input perf.data --show-mmap-events | grep '//anon'
// verify mmap "//anon" events removed
perf script --input perfjit.data --show-mmap-events | grep '//anon'
// no jitdump case
perf record <app without jitdump>
perf inject --jit --input perf.data --output perfjit.data
// verify mmap "//anon" events present initially
perf script --input perf.data --show-mmap-events | grep '//anon'
// verify mmap "//anon" events not removed
perf script --input perfjit.data --show-mmap-events | grep '//anon'
** Repro:
This issue was discovered while testing the initial CoreCLR jitdump
implementation. https://github.com/dotnet/coreclr/pull/26897.
** Alternate solutions considered
These were also briefly considered:
* Change kernel to not coalesce mmap regions.
* Change kernel reporting of coalesced mmap regions to perf. Only
include newly mapped memory.
* Only strip parts of // anon mmap events overlapping existing
jitted-<pid>-<code_index>.so mmap events.
Signed-off-by: Steve MacLean <Steve.MacLean@Microsoft.com>
Acked-by: Ian Rogers <irogers@google.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lore.kernel.org/lkml/1590544271-125795-1-git-send-email-steve.maclean@linux.microsoft.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2020-05-27 09:51:11 +08:00
|
|
|
static void jit_add_pid(struct machine *machine, pid_t pid)
|
|
|
|
{
|
|
|
|
struct thread *thread = machine__findnew_thread(machine, pid, pid);
|
|
|
|
|
|
|
|
if (!thread) {
|
|
|
|
pr_err("%s: thread %d not found or created\n", __func__, pid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-09 07:28:00 +08:00
|
|
|
thread__set_priv(thread, (void *)true);
|
2023-06-09 07:28:12 +08:00
|
|
|
thread__put(thread);
|
perf inject jit: Remove //anon mmap events
**perf-<pid>.map and jit-<pid>.dump designs:
When a JIT generates code to be executed, it must allocate memory and
mark it executable using an mmap call.
*** perf-<pid>.map design
The perf-<pid>.map assumes that any sample recorded in an anonymous
memory page is JIT code. It then tries to resolve the symbol name by
looking at the process' perf-<pid>.map.
*** jit-<pid>.dump design
The jit-<pid>.dump mechanism takes a different approach. It requires a
JIT to write a `<path>/jit-<pid>.dump` file. This file must also be
mmapped so that perf inject -jit can find the file. The JIT must also
add JIT_CODE_LOAD records for any functions it generates. The records
are timestamped using a clock which can be correlated to the perf record
clock.
After perf record, the `perf inject -jit` pass parses the recording
looking for a `<path>/jit-<pid>.dump` file. When it finds the file, it
parses it and for each JIT_CODE_LOAD record:
* creates an elf file `<path>/jitted-<pid>-<code_index>.so
* injects a new mmap record mapping the new elf file into the process.
*** Coexistence design
The kernel and perf support both of these mechanisms. We need to make
sure perf works on an app supporting either or both of these mechanisms.
Both designs rely on mmap records to determine how to resolve an ip
address.
The mmap records of both techniques by definition overlap. When the JIT
compiles a method, it must:
* allocate memory (mmap)
* add execution privilege (mprotect or mmap. either will
generate an mmap event form the kernel to perf)
* compile code into memory
* add a function record to perf-<pid>.map and/or jit-<pid>.dump
Because the jit-<pid>.dump mechanism supports greater capabilities, perf
prefers the symbols from jit-<pid>.dump. It implements this based on
timestamp ordering of events. There is an implicit ASSUMPTION that the
JIT_CODE_LOAD record timestamp will be after the // anon mmap event that
was generated during memory allocation or adding the execution privilege setting.
*** Problems with the ASSUMPTION
The ASSUMPTION made in the Coexistence design section above is violated
in the following scenario.
*** Scenario
While a JIT is jitting code it will eventually need to commit more
pages and change these pages to executable permissions. Typically the
JIT will want these collocated to minimize branch displacements.
The kernel will coalesce these anonymous mapping with identical
permissions before sending an MMAP event for the new pages. The address
range of the new mmap will not be just the most recently mmap pages.
It will include the entire coalesced mmap region.
See mm/mmap.c
unsigned long mmap_region(struct file *file, unsigned long addr,
unsigned long len, vm_flags_t vm_flags, unsigned long pgoff,
struct list_head *uf)
{
...
/*
* Can we just expand an old mapping?
*/
...
perf_event_mmap(vma);
...
}
*** Symptoms
The coalesced // anon mmap event will be timestamped after the
JIT_CODE_LOAD records. This means it will be used as the most recent
mapping for that entire address range. For remaining events it will look
at the inferior perf-<pid>.map for symbols.
If both mechanisms are supported, the symbol will appear twice with
different module names. This causes weird behavior in reporting.
If only jit-<pid>.dump is supported, the symbol will no longer be resolved.
** Implemented solution
This patch solves the issue by removing // anon mmap events for any
process which has a valid jit-<pid>.dump file.
It tracks on a per process basis to handle the case where some running
apps support jit-<pid>.dump, but some only support perf-<pid>.map.
It adds new assumptions:
* // anon mmap events are only required for perf-<pid>.map support.
* An app that uses jit-<pid>.dump, no longer needs
perf-<pid>.map support. It assumes that any perf-<pid>.map info is
inferior.
*** Details
Use thread->priv to store whether a jitdump file has been processed
During "perf inject --jit", discard "//anon*" mmap events for any pid which
has sucessfully processed a jitdump file.
** Testing:
// jitdump case
perf record <app with jitdump>
perf inject --jit --input perf.data --output perfjit.data
// verify mmap "//anon" events present initially
perf script --input perf.data --show-mmap-events | grep '//anon'
// verify mmap "//anon" events removed
perf script --input perfjit.data --show-mmap-events | grep '//anon'
// no jitdump case
perf record <app without jitdump>
perf inject --jit --input perf.data --output perfjit.data
// verify mmap "//anon" events present initially
perf script --input perf.data --show-mmap-events | grep '//anon'
// verify mmap "//anon" events not removed
perf script --input perfjit.data --show-mmap-events | grep '//anon'
** Repro:
This issue was discovered while testing the initial CoreCLR jitdump
implementation. https://github.com/dotnet/coreclr/pull/26897.
** Alternate solutions considered
These were also briefly considered:
* Change kernel to not coalesce mmap regions.
* Change kernel reporting of coalesced mmap regions to perf. Only
include newly mapped memory.
* Only strip parts of // anon mmap events overlapping existing
jitted-<pid>-<code_index>.so mmap events.
Signed-off-by: Steve MacLean <Steve.MacLean@Microsoft.com>
Acked-by: Ian Rogers <irogers@google.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lore.kernel.org/lkml/1590544271-125795-1-git-send-email-steve.maclean@linux.microsoft.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2020-05-27 09:51:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool jit_has_pid(struct machine *machine, pid_t pid)
|
|
|
|
{
|
|
|
|
struct thread *thread = machine__find_thread(machine, pid, pid);
|
2023-06-09 07:28:00 +08:00
|
|
|
void *priv;
|
perf inject jit: Remove //anon mmap events
**perf-<pid>.map and jit-<pid>.dump designs:
When a JIT generates code to be executed, it must allocate memory and
mark it executable using an mmap call.
*** perf-<pid>.map design
The perf-<pid>.map assumes that any sample recorded in an anonymous
memory page is JIT code. It then tries to resolve the symbol name by
looking at the process' perf-<pid>.map.
*** jit-<pid>.dump design
The jit-<pid>.dump mechanism takes a different approach. It requires a
JIT to write a `<path>/jit-<pid>.dump` file. This file must also be
mmapped so that perf inject -jit can find the file. The JIT must also
add JIT_CODE_LOAD records for any functions it generates. The records
are timestamped using a clock which can be correlated to the perf record
clock.
After perf record, the `perf inject -jit` pass parses the recording
looking for a `<path>/jit-<pid>.dump` file. When it finds the file, it
parses it and for each JIT_CODE_LOAD record:
* creates an elf file `<path>/jitted-<pid>-<code_index>.so
* injects a new mmap record mapping the new elf file into the process.
*** Coexistence design
The kernel and perf support both of these mechanisms. We need to make
sure perf works on an app supporting either or both of these mechanisms.
Both designs rely on mmap records to determine how to resolve an ip
address.
The mmap records of both techniques by definition overlap. When the JIT
compiles a method, it must:
* allocate memory (mmap)
* add execution privilege (mprotect or mmap. either will
generate an mmap event form the kernel to perf)
* compile code into memory
* add a function record to perf-<pid>.map and/or jit-<pid>.dump
Because the jit-<pid>.dump mechanism supports greater capabilities, perf
prefers the symbols from jit-<pid>.dump. It implements this based on
timestamp ordering of events. There is an implicit ASSUMPTION that the
JIT_CODE_LOAD record timestamp will be after the // anon mmap event that
was generated during memory allocation or adding the execution privilege setting.
*** Problems with the ASSUMPTION
The ASSUMPTION made in the Coexistence design section above is violated
in the following scenario.
*** Scenario
While a JIT is jitting code it will eventually need to commit more
pages and change these pages to executable permissions. Typically the
JIT will want these collocated to minimize branch displacements.
The kernel will coalesce these anonymous mapping with identical
permissions before sending an MMAP event for the new pages. The address
range of the new mmap will not be just the most recently mmap pages.
It will include the entire coalesced mmap region.
See mm/mmap.c
unsigned long mmap_region(struct file *file, unsigned long addr,
unsigned long len, vm_flags_t vm_flags, unsigned long pgoff,
struct list_head *uf)
{
...
/*
* Can we just expand an old mapping?
*/
...
perf_event_mmap(vma);
...
}
*** Symptoms
The coalesced // anon mmap event will be timestamped after the
JIT_CODE_LOAD records. This means it will be used as the most recent
mapping for that entire address range. For remaining events it will look
at the inferior perf-<pid>.map for symbols.
If both mechanisms are supported, the symbol will appear twice with
different module names. This causes weird behavior in reporting.
If only jit-<pid>.dump is supported, the symbol will no longer be resolved.
** Implemented solution
This patch solves the issue by removing // anon mmap events for any
process which has a valid jit-<pid>.dump file.
It tracks on a per process basis to handle the case where some running
apps support jit-<pid>.dump, but some only support perf-<pid>.map.
It adds new assumptions:
* // anon mmap events are only required for perf-<pid>.map support.
* An app that uses jit-<pid>.dump, no longer needs
perf-<pid>.map support. It assumes that any perf-<pid>.map info is
inferior.
*** Details
Use thread->priv to store whether a jitdump file has been processed
During "perf inject --jit", discard "//anon*" mmap events for any pid which
has sucessfully processed a jitdump file.
** Testing:
// jitdump case
perf record <app with jitdump>
perf inject --jit --input perf.data --output perfjit.data
// verify mmap "//anon" events present initially
perf script --input perf.data --show-mmap-events | grep '//anon'
// verify mmap "//anon" events removed
perf script --input perfjit.data --show-mmap-events | grep '//anon'
// no jitdump case
perf record <app without jitdump>
perf inject --jit --input perf.data --output perfjit.data
// verify mmap "//anon" events present initially
perf script --input perf.data --show-mmap-events | grep '//anon'
// verify mmap "//anon" events not removed
perf script --input perfjit.data --show-mmap-events | grep '//anon'
** Repro:
This issue was discovered while testing the initial CoreCLR jitdump
implementation. https://github.com/dotnet/coreclr/pull/26897.
** Alternate solutions considered
These were also briefly considered:
* Change kernel to not coalesce mmap regions.
* Change kernel reporting of coalesced mmap regions to perf. Only
include newly mapped memory.
* Only strip parts of // anon mmap events overlapping existing
jitted-<pid>-<code_index>.so mmap events.
Signed-off-by: Steve MacLean <Steve.MacLean@Microsoft.com>
Acked-by: Ian Rogers <irogers@google.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lore.kernel.org/lkml/1590544271-125795-1-git-send-email-steve.maclean@linux.microsoft.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2020-05-27 09:51:11 +08:00
|
|
|
|
|
|
|
if (!thread)
|
2023-06-09 07:28:00 +08:00
|
|
|
return false;
|
perf inject jit: Remove //anon mmap events
**perf-<pid>.map and jit-<pid>.dump designs:
When a JIT generates code to be executed, it must allocate memory and
mark it executable using an mmap call.
*** perf-<pid>.map design
The perf-<pid>.map assumes that any sample recorded in an anonymous
memory page is JIT code. It then tries to resolve the symbol name by
looking at the process' perf-<pid>.map.
*** jit-<pid>.dump design
The jit-<pid>.dump mechanism takes a different approach. It requires a
JIT to write a `<path>/jit-<pid>.dump` file. This file must also be
mmapped so that perf inject -jit can find the file. The JIT must also
add JIT_CODE_LOAD records for any functions it generates. The records
are timestamped using a clock which can be correlated to the perf record
clock.
After perf record, the `perf inject -jit` pass parses the recording
looking for a `<path>/jit-<pid>.dump` file. When it finds the file, it
parses it and for each JIT_CODE_LOAD record:
* creates an elf file `<path>/jitted-<pid>-<code_index>.so
* injects a new mmap record mapping the new elf file into the process.
*** Coexistence design
The kernel and perf support both of these mechanisms. We need to make
sure perf works on an app supporting either or both of these mechanisms.
Both designs rely on mmap records to determine how to resolve an ip
address.
The mmap records of both techniques by definition overlap. When the JIT
compiles a method, it must:
* allocate memory (mmap)
* add execution privilege (mprotect or mmap. either will
generate an mmap event form the kernel to perf)
* compile code into memory
* add a function record to perf-<pid>.map and/or jit-<pid>.dump
Because the jit-<pid>.dump mechanism supports greater capabilities, perf
prefers the symbols from jit-<pid>.dump. It implements this based on
timestamp ordering of events. There is an implicit ASSUMPTION that the
JIT_CODE_LOAD record timestamp will be after the // anon mmap event that
was generated during memory allocation or adding the execution privilege setting.
*** Problems with the ASSUMPTION
The ASSUMPTION made in the Coexistence design section above is violated
in the following scenario.
*** Scenario
While a JIT is jitting code it will eventually need to commit more
pages and change these pages to executable permissions. Typically the
JIT will want these collocated to minimize branch displacements.
The kernel will coalesce these anonymous mapping with identical
permissions before sending an MMAP event for the new pages. The address
range of the new mmap will not be just the most recently mmap pages.
It will include the entire coalesced mmap region.
See mm/mmap.c
unsigned long mmap_region(struct file *file, unsigned long addr,
unsigned long len, vm_flags_t vm_flags, unsigned long pgoff,
struct list_head *uf)
{
...
/*
* Can we just expand an old mapping?
*/
...
perf_event_mmap(vma);
...
}
*** Symptoms
The coalesced // anon mmap event will be timestamped after the
JIT_CODE_LOAD records. This means it will be used as the most recent
mapping for that entire address range. For remaining events it will look
at the inferior perf-<pid>.map for symbols.
If both mechanisms are supported, the symbol will appear twice with
different module names. This causes weird behavior in reporting.
If only jit-<pid>.dump is supported, the symbol will no longer be resolved.
** Implemented solution
This patch solves the issue by removing // anon mmap events for any
process which has a valid jit-<pid>.dump file.
It tracks on a per process basis to handle the case where some running
apps support jit-<pid>.dump, but some only support perf-<pid>.map.
It adds new assumptions:
* // anon mmap events are only required for perf-<pid>.map support.
* An app that uses jit-<pid>.dump, no longer needs
perf-<pid>.map support. It assumes that any perf-<pid>.map info is
inferior.
*** Details
Use thread->priv to store whether a jitdump file has been processed
During "perf inject --jit", discard "//anon*" mmap events for any pid which
has sucessfully processed a jitdump file.
** Testing:
// jitdump case
perf record <app with jitdump>
perf inject --jit --input perf.data --output perfjit.data
// verify mmap "//anon" events present initially
perf script --input perf.data --show-mmap-events | grep '//anon'
// verify mmap "//anon" events removed
perf script --input perfjit.data --show-mmap-events | grep '//anon'
// no jitdump case
perf record <app without jitdump>
perf inject --jit --input perf.data --output perfjit.data
// verify mmap "//anon" events present initially
perf script --input perf.data --show-mmap-events | grep '//anon'
// verify mmap "//anon" events not removed
perf script --input perfjit.data --show-mmap-events | grep '//anon'
** Repro:
This issue was discovered while testing the initial CoreCLR jitdump
implementation. https://github.com/dotnet/coreclr/pull/26897.
** Alternate solutions considered
These were also briefly considered:
* Change kernel to not coalesce mmap regions.
* Change kernel reporting of coalesced mmap regions to perf. Only
include newly mapped memory.
* Only strip parts of // anon mmap events overlapping existing
jitted-<pid>-<code_index>.so mmap events.
Signed-off-by: Steve MacLean <Steve.MacLean@Microsoft.com>
Acked-by: Ian Rogers <irogers@google.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lore.kernel.org/lkml/1590544271-125795-1-git-send-email-steve.maclean@linux.microsoft.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2020-05-27 09:51:11 +08:00
|
|
|
|
2023-06-09 07:28:00 +08:00
|
|
|
priv = thread__priv(thread);
|
2023-06-09 07:28:12 +08:00
|
|
|
thread__put(thread);
|
2023-06-09 07:28:00 +08:00
|
|
|
return (bool)priv;
|
perf inject jit: Remove //anon mmap events
**perf-<pid>.map and jit-<pid>.dump designs:
When a JIT generates code to be executed, it must allocate memory and
mark it executable using an mmap call.
*** perf-<pid>.map design
The perf-<pid>.map assumes that any sample recorded in an anonymous
memory page is JIT code. It then tries to resolve the symbol name by
looking at the process' perf-<pid>.map.
*** jit-<pid>.dump design
The jit-<pid>.dump mechanism takes a different approach. It requires a
JIT to write a `<path>/jit-<pid>.dump` file. This file must also be
mmapped so that perf inject -jit can find the file. The JIT must also
add JIT_CODE_LOAD records for any functions it generates. The records
are timestamped using a clock which can be correlated to the perf record
clock.
After perf record, the `perf inject -jit` pass parses the recording
looking for a `<path>/jit-<pid>.dump` file. When it finds the file, it
parses it and for each JIT_CODE_LOAD record:
* creates an elf file `<path>/jitted-<pid>-<code_index>.so
* injects a new mmap record mapping the new elf file into the process.
*** Coexistence design
The kernel and perf support both of these mechanisms. We need to make
sure perf works on an app supporting either or both of these mechanisms.
Both designs rely on mmap records to determine how to resolve an ip
address.
The mmap records of both techniques by definition overlap. When the JIT
compiles a method, it must:
* allocate memory (mmap)
* add execution privilege (mprotect or mmap. either will
generate an mmap event form the kernel to perf)
* compile code into memory
* add a function record to perf-<pid>.map and/or jit-<pid>.dump
Because the jit-<pid>.dump mechanism supports greater capabilities, perf
prefers the symbols from jit-<pid>.dump. It implements this based on
timestamp ordering of events. There is an implicit ASSUMPTION that the
JIT_CODE_LOAD record timestamp will be after the // anon mmap event that
was generated during memory allocation or adding the execution privilege setting.
*** Problems with the ASSUMPTION
The ASSUMPTION made in the Coexistence design section above is violated
in the following scenario.
*** Scenario
While a JIT is jitting code it will eventually need to commit more
pages and change these pages to executable permissions. Typically the
JIT will want these collocated to minimize branch displacements.
The kernel will coalesce these anonymous mapping with identical
permissions before sending an MMAP event for the new pages. The address
range of the new mmap will not be just the most recently mmap pages.
It will include the entire coalesced mmap region.
See mm/mmap.c
unsigned long mmap_region(struct file *file, unsigned long addr,
unsigned long len, vm_flags_t vm_flags, unsigned long pgoff,
struct list_head *uf)
{
...
/*
* Can we just expand an old mapping?
*/
...
perf_event_mmap(vma);
...
}
*** Symptoms
The coalesced // anon mmap event will be timestamped after the
JIT_CODE_LOAD records. This means it will be used as the most recent
mapping for that entire address range. For remaining events it will look
at the inferior perf-<pid>.map for symbols.
If both mechanisms are supported, the symbol will appear twice with
different module names. This causes weird behavior in reporting.
If only jit-<pid>.dump is supported, the symbol will no longer be resolved.
** Implemented solution
This patch solves the issue by removing // anon mmap events for any
process which has a valid jit-<pid>.dump file.
It tracks on a per process basis to handle the case where some running
apps support jit-<pid>.dump, but some only support perf-<pid>.map.
It adds new assumptions:
* // anon mmap events are only required for perf-<pid>.map support.
* An app that uses jit-<pid>.dump, no longer needs
perf-<pid>.map support. It assumes that any perf-<pid>.map info is
inferior.
*** Details
Use thread->priv to store whether a jitdump file has been processed
During "perf inject --jit", discard "//anon*" mmap events for any pid which
has sucessfully processed a jitdump file.
** Testing:
// jitdump case
perf record <app with jitdump>
perf inject --jit --input perf.data --output perfjit.data
// verify mmap "//anon" events present initially
perf script --input perf.data --show-mmap-events | grep '//anon'
// verify mmap "//anon" events removed
perf script --input perfjit.data --show-mmap-events | grep '//anon'
// no jitdump case
perf record <app without jitdump>
perf inject --jit --input perf.data --output perfjit.data
// verify mmap "//anon" events present initially
perf script --input perf.data --show-mmap-events | grep '//anon'
// verify mmap "//anon" events not removed
perf script --input perfjit.data --show-mmap-events | grep '//anon'
** Repro:
This issue was discovered while testing the initial CoreCLR jitdump
implementation. https://github.com/dotnet/coreclr/pull/26897.
** Alternate solutions considered
These were also briefly considered:
* Change kernel to not coalesce mmap regions.
* Change kernel reporting of coalesced mmap regions to perf. Only
include newly mapped memory.
* Only strip parts of // anon mmap events overlapping existing
jitted-<pid>-<code_index>.so mmap events.
Signed-off-by: Steve MacLean <Steve.MacLean@Microsoft.com>
Acked-by: Ian Rogers <irogers@google.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lore.kernel.org/lkml/1590544271-125795-1-git-send-email-steve.maclean@linux.microsoft.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2020-05-27 09:51:11 +08:00
|
|
|
}
|
|
|
|
|
2015-11-30 17:02:21 +08:00
|
|
|
int
|
|
|
|
jit_process(struct perf_session *session,
|
2017-01-24 05:07:59 +08:00
|
|
|
struct perf_data *output,
|
2015-11-30 17:02:21 +08:00
|
|
|
struct machine *machine,
|
|
|
|
char *filename,
|
|
|
|
pid_t pid,
|
2020-11-05 09:56:04 +08:00
|
|
|
pid_t tid,
|
2015-11-30 17:02:21 +08:00
|
|
|
u64 *nbytes)
|
|
|
|
{
|
2020-11-05 09:56:04 +08:00
|
|
|
struct thread *thread;
|
|
|
|
struct nsinfo *nsi;
|
2019-07-21 19:23:51 +08:00
|
|
|
struct evsel *first;
|
2015-11-30 17:02:21 +08:00
|
|
|
struct jit_buf_desc jd;
|
|
|
|
int ret;
|
|
|
|
|
2020-11-05 09:56:04 +08:00
|
|
|
thread = machine__findnew_thread(machine, pid, tid);
|
|
|
|
if (thread == NULL) {
|
|
|
|
pr_err("problem processing JIT mmap event, skipping it.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-09 07:28:00 +08:00
|
|
|
nsi = nsinfo__get(thread__nsinfo(thread));
|
2020-11-05 09:56:04 +08:00
|
|
|
thread__put(thread);
|
|
|
|
|
2015-11-30 17:02:21 +08:00
|
|
|
/*
|
|
|
|
* first, detect marker mmap (i.e., the jitdump mmap)
|
|
|
|
*/
|
2020-11-05 09:56:04 +08:00
|
|
|
if (jit_detect(filename, pid, nsi)) {
|
|
|
|
nsinfo__put(nsi);
|
|
|
|
|
2022-08-06 06:06:45 +08:00
|
|
|
/*
|
|
|
|
* Strip //anon*, [anon:* and /memfd:* mmaps if we processed a jitdump for this pid
|
|
|
|
*/
|
|
|
|
if (jit_has_pid(machine, pid) &&
|
|
|
|
((strncmp(filename, "//anon", 6) == 0) ||
|
|
|
|
(strncmp(filename, "[anon:", 6) == 0) ||
|
|
|
|
(strncmp(filename, "/memfd:", 7) == 0)))
|
perf inject jit: Remove //anon mmap events
**perf-<pid>.map and jit-<pid>.dump designs:
When a JIT generates code to be executed, it must allocate memory and
mark it executable using an mmap call.
*** perf-<pid>.map design
The perf-<pid>.map assumes that any sample recorded in an anonymous
memory page is JIT code. It then tries to resolve the symbol name by
looking at the process' perf-<pid>.map.
*** jit-<pid>.dump design
The jit-<pid>.dump mechanism takes a different approach. It requires a
JIT to write a `<path>/jit-<pid>.dump` file. This file must also be
mmapped so that perf inject -jit can find the file. The JIT must also
add JIT_CODE_LOAD records for any functions it generates. The records
are timestamped using a clock which can be correlated to the perf record
clock.
After perf record, the `perf inject -jit` pass parses the recording
looking for a `<path>/jit-<pid>.dump` file. When it finds the file, it
parses it and for each JIT_CODE_LOAD record:
* creates an elf file `<path>/jitted-<pid>-<code_index>.so
* injects a new mmap record mapping the new elf file into the process.
*** Coexistence design
The kernel and perf support both of these mechanisms. We need to make
sure perf works on an app supporting either or both of these mechanisms.
Both designs rely on mmap records to determine how to resolve an ip
address.
The mmap records of both techniques by definition overlap. When the JIT
compiles a method, it must:
* allocate memory (mmap)
* add execution privilege (mprotect or mmap. either will
generate an mmap event form the kernel to perf)
* compile code into memory
* add a function record to perf-<pid>.map and/or jit-<pid>.dump
Because the jit-<pid>.dump mechanism supports greater capabilities, perf
prefers the symbols from jit-<pid>.dump. It implements this based on
timestamp ordering of events. There is an implicit ASSUMPTION that the
JIT_CODE_LOAD record timestamp will be after the // anon mmap event that
was generated during memory allocation or adding the execution privilege setting.
*** Problems with the ASSUMPTION
The ASSUMPTION made in the Coexistence design section above is violated
in the following scenario.
*** Scenario
While a JIT is jitting code it will eventually need to commit more
pages and change these pages to executable permissions. Typically the
JIT will want these collocated to minimize branch displacements.
The kernel will coalesce these anonymous mapping with identical
permissions before sending an MMAP event for the new pages. The address
range of the new mmap will not be just the most recently mmap pages.
It will include the entire coalesced mmap region.
See mm/mmap.c
unsigned long mmap_region(struct file *file, unsigned long addr,
unsigned long len, vm_flags_t vm_flags, unsigned long pgoff,
struct list_head *uf)
{
...
/*
* Can we just expand an old mapping?
*/
...
perf_event_mmap(vma);
...
}
*** Symptoms
The coalesced // anon mmap event will be timestamped after the
JIT_CODE_LOAD records. This means it will be used as the most recent
mapping for that entire address range. For remaining events it will look
at the inferior perf-<pid>.map for symbols.
If both mechanisms are supported, the symbol will appear twice with
different module names. This causes weird behavior in reporting.
If only jit-<pid>.dump is supported, the symbol will no longer be resolved.
** Implemented solution
This patch solves the issue by removing // anon mmap events for any
process which has a valid jit-<pid>.dump file.
It tracks on a per process basis to handle the case where some running
apps support jit-<pid>.dump, but some only support perf-<pid>.map.
It adds new assumptions:
* // anon mmap events are only required for perf-<pid>.map support.
* An app that uses jit-<pid>.dump, no longer needs
perf-<pid>.map support. It assumes that any perf-<pid>.map info is
inferior.
*** Details
Use thread->priv to store whether a jitdump file has been processed
During "perf inject --jit", discard "//anon*" mmap events for any pid which
has sucessfully processed a jitdump file.
** Testing:
// jitdump case
perf record <app with jitdump>
perf inject --jit --input perf.data --output perfjit.data
// verify mmap "//anon" events present initially
perf script --input perf.data --show-mmap-events | grep '//anon'
// verify mmap "//anon" events removed
perf script --input perfjit.data --show-mmap-events | grep '//anon'
// no jitdump case
perf record <app without jitdump>
perf inject --jit --input perf.data --output perfjit.data
// verify mmap "//anon" events present initially
perf script --input perf.data --show-mmap-events | grep '//anon'
// verify mmap "//anon" events not removed
perf script --input perfjit.data --show-mmap-events | grep '//anon'
** Repro:
This issue was discovered while testing the initial CoreCLR jitdump
implementation. https://github.com/dotnet/coreclr/pull/26897.
** Alternate solutions considered
These were also briefly considered:
* Change kernel to not coalesce mmap regions.
* Change kernel reporting of coalesced mmap regions to perf. Only
include newly mapped memory.
* Only strip parts of // anon mmap events overlapping existing
jitted-<pid>-<code_index>.so mmap events.
Signed-off-by: Steve MacLean <Steve.MacLean@Microsoft.com>
Acked-by: Ian Rogers <irogers@google.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lore.kernel.org/lkml/1590544271-125795-1-git-send-email-steve.maclean@linux.microsoft.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2020-05-27 09:51:11 +08:00
|
|
|
return 1;
|
|
|
|
|
2016-03-08 03:44:40 +08:00
|
|
|
return 0;
|
perf inject jit: Remove //anon mmap events
**perf-<pid>.map and jit-<pid>.dump designs:
When a JIT generates code to be executed, it must allocate memory and
mark it executable using an mmap call.
*** perf-<pid>.map design
The perf-<pid>.map assumes that any sample recorded in an anonymous
memory page is JIT code. It then tries to resolve the symbol name by
looking at the process' perf-<pid>.map.
*** jit-<pid>.dump design
The jit-<pid>.dump mechanism takes a different approach. It requires a
JIT to write a `<path>/jit-<pid>.dump` file. This file must also be
mmapped so that perf inject -jit can find the file. The JIT must also
add JIT_CODE_LOAD records for any functions it generates. The records
are timestamped using a clock which can be correlated to the perf record
clock.
After perf record, the `perf inject -jit` pass parses the recording
looking for a `<path>/jit-<pid>.dump` file. When it finds the file, it
parses it and for each JIT_CODE_LOAD record:
* creates an elf file `<path>/jitted-<pid>-<code_index>.so
* injects a new mmap record mapping the new elf file into the process.
*** Coexistence design
The kernel and perf support both of these mechanisms. We need to make
sure perf works on an app supporting either or both of these mechanisms.
Both designs rely on mmap records to determine how to resolve an ip
address.
The mmap records of both techniques by definition overlap. When the JIT
compiles a method, it must:
* allocate memory (mmap)
* add execution privilege (mprotect or mmap. either will
generate an mmap event form the kernel to perf)
* compile code into memory
* add a function record to perf-<pid>.map and/or jit-<pid>.dump
Because the jit-<pid>.dump mechanism supports greater capabilities, perf
prefers the symbols from jit-<pid>.dump. It implements this based on
timestamp ordering of events. There is an implicit ASSUMPTION that the
JIT_CODE_LOAD record timestamp will be after the // anon mmap event that
was generated during memory allocation or adding the execution privilege setting.
*** Problems with the ASSUMPTION
The ASSUMPTION made in the Coexistence design section above is violated
in the following scenario.
*** Scenario
While a JIT is jitting code it will eventually need to commit more
pages and change these pages to executable permissions. Typically the
JIT will want these collocated to minimize branch displacements.
The kernel will coalesce these anonymous mapping with identical
permissions before sending an MMAP event for the new pages. The address
range of the new mmap will not be just the most recently mmap pages.
It will include the entire coalesced mmap region.
See mm/mmap.c
unsigned long mmap_region(struct file *file, unsigned long addr,
unsigned long len, vm_flags_t vm_flags, unsigned long pgoff,
struct list_head *uf)
{
...
/*
* Can we just expand an old mapping?
*/
...
perf_event_mmap(vma);
...
}
*** Symptoms
The coalesced // anon mmap event will be timestamped after the
JIT_CODE_LOAD records. This means it will be used as the most recent
mapping for that entire address range. For remaining events it will look
at the inferior perf-<pid>.map for symbols.
If both mechanisms are supported, the symbol will appear twice with
different module names. This causes weird behavior in reporting.
If only jit-<pid>.dump is supported, the symbol will no longer be resolved.
** Implemented solution
This patch solves the issue by removing // anon mmap events for any
process which has a valid jit-<pid>.dump file.
It tracks on a per process basis to handle the case where some running
apps support jit-<pid>.dump, but some only support perf-<pid>.map.
It adds new assumptions:
* // anon mmap events are only required for perf-<pid>.map support.
* An app that uses jit-<pid>.dump, no longer needs
perf-<pid>.map support. It assumes that any perf-<pid>.map info is
inferior.
*** Details
Use thread->priv to store whether a jitdump file has been processed
During "perf inject --jit", discard "//anon*" mmap events for any pid which
has sucessfully processed a jitdump file.
** Testing:
// jitdump case
perf record <app with jitdump>
perf inject --jit --input perf.data --output perfjit.data
// verify mmap "//anon" events present initially
perf script --input perf.data --show-mmap-events | grep '//anon'
// verify mmap "//anon" events removed
perf script --input perfjit.data --show-mmap-events | grep '//anon'
// no jitdump case
perf record <app without jitdump>
perf inject --jit --input perf.data --output perfjit.data
// verify mmap "//anon" events present initially
perf script --input perf.data --show-mmap-events | grep '//anon'
// verify mmap "//anon" events not removed
perf script --input perfjit.data --show-mmap-events | grep '//anon'
** Repro:
This issue was discovered while testing the initial CoreCLR jitdump
implementation. https://github.com/dotnet/coreclr/pull/26897.
** Alternate solutions considered
These were also briefly considered:
* Change kernel to not coalesce mmap regions.
* Change kernel reporting of coalesced mmap regions to perf. Only
include newly mapped memory.
* Only strip parts of // anon mmap events overlapping existing
jitted-<pid>-<code_index>.so mmap events.
Signed-off-by: Steve MacLean <Steve.MacLean@Microsoft.com>
Acked-by: Ian Rogers <irogers@google.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lore.kernel.org/lkml/1590544271-125795-1-git-send-email-steve.maclean@linux.microsoft.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2020-05-27 09:51:11 +08:00
|
|
|
}
|
2015-11-30 17:02:21 +08:00
|
|
|
|
|
|
|
memset(&jd, 0, sizeof(jd));
|
|
|
|
|
|
|
|
jd.session = session;
|
|
|
|
jd.output = output;
|
|
|
|
jd.machine = machine;
|
2020-11-05 09:56:04 +08:00
|
|
|
jd.nsi = nsi;
|
2015-11-30 17:02:21 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* track sample_type to compute id_all layout
|
|
|
|
* perf sets the same sample type to all events as of now
|
|
|
|
*/
|
2019-09-03 16:39:52 +08:00
|
|
|
first = evlist__first(session->evlist);
|
2019-07-21 19:24:29 +08:00
|
|
|
jd.sample_type = first->core.attr.sample_type;
|
2015-11-30 17:02:21 +08:00
|
|
|
|
|
|
|
*nbytes = 0;
|
|
|
|
|
|
|
|
ret = jit_inject(&jd, filename);
|
2016-03-08 03:44:40 +08:00
|
|
|
if (!ret) {
|
perf inject jit: Remove //anon mmap events
**perf-<pid>.map and jit-<pid>.dump designs:
When a JIT generates code to be executed, it must allocate memory and
mark it executable using an mmap call.
*** perf-<pid>.map design
The perf-<pid>.map assumes that any sample recorded in an anonymous
memory page is JIT code. It then tries to resolve the symbol name by
looking at the process' perf-<pid>.map.
*** jit-<pid>.dump design
The jit-<pid>.dump mechanism takes a different approach. It requires a
JIT to write a `<path>/jit-<pid>.dump` file. This file must also be
mmapped so that perf inject -jit can find the file. The JIT must also
add JIT_CODE_LOAD records for any functions it generates. The records
are timestamped using a clock which can be correlated to the perf record
clock.
After perf record, the `perf inject -jit` pass parses the recording
looking for a `<path>/jit-<pid>.dump` file. When it finds the file, it
parses it and for each JIT_CODE_LOAD record:
* creates an elf file `<path>/jitted-<pid>-<code_index>.so
* injects a new mmap record mapping the new elf file into the process.
*** Coexistence design
The kernel and perf support both of these mechanisms. We need to make
sure perf works on an app supporting either or both of these mechanisms.
Both designs rely on mmap records to determine how to resolve an ip
address.
The mmap records of both techniques by definition overlap. When the JIT
compiles a method, it must:
* allocate memory (mmap)
* add execution privilege (mprotect or mmap. either will
generate an mmap event form the kernel to perf)
* compile code into memory
* add a function record to perf-<pid>.map and/or jit-<pid>.dump
Because the jit-<pid>.dump mechanism supports greater capabilities, perf
prefers the symbols from jit-<pid>.dump. It implements this based on
timestamp ordering of events. There is an implicit ASSUMPTION that the
JIT_CODE_LOAD record timestamp will be after the // anon mmap event that
was generated during memory allocation or adding the execution privilege setting.
*** Problems with the ASSUMPTION
The ASSUMPTION made in the Coexistence design section above is violated
in the following scenario.
*** Scenario
While a JIT is jitting code it will eventually need to commit more
pages and change these pages to executable permissions. Typically the
JIT will want these collocated to minimize branch displacements.
The kernel will coalesce these anonymous mapping with identical
permissions before sending an MMAP event for the new pages. The address
range of the new mmap will not be just the most recently mmap pages.
It will include the entire coalesced mmap region.
See mm/mmap.c
unsigned long mmap_region(struct file *file, unsigned long addr,
unsigned long len, vm_flags_t vm_flags, unsigned long pgoff,
struct list_head *uf)
{
...
/*
* Can we just expand an old mapping?
*/
...
perf_event_mmap(vma);
...
}
*** Symptoms
The coalesced // anon mmap event will be timestamped after the
JIT_CODE_LOAD records. This means it will be used as the most recent
mapping for that entire address range. For remaining events it will look
at the inferior perf-<pid>.map for symbols.
If both mechanisms are supported, the symbol will appear twice with
different module names. This causes weird behavior in reporting.
If only jit-<pid>.dump is supported, the symbol will no longer be resolved.
** Implemented solution
This patch solves the issue by removing // anon mmap events for any
process which has a valid jit-<pid>.dump file.
It tracks on a per process basis to handle the case where some running
apps support jit-<pid>.dump, but some only support perf-<pid>.map.
It adds new assumptions:
* // anon mmap events are only required for perf-<pid>.map support.
* An app that uses jit-<pid>.dump, no longer needs
perf-<pid>.map support. It assumes that any perf-<pid>.map info is
inferior.
*** Details
Use thread->priv to store whether a jitdump file has been processed
During "perf inject --jit", discard "//anon*" mmap events for any pid which
has sucessfully processed a jitdump file.
** Testing:
// jitdump case
perf record <app with jitdump>
perf inject --jit --input perf.data --output perfjit.data
// verify mmap "//anon" events present initially
perf script --input perf.data --show-mmap-events | grep '//anon'
// verify mmap "//anon" events removed
perf script --input perfjit.data --show-mmap-events | grep '//anon'
// no jitdump case
perf record <app without jitdump>
perf inject --jit --input perf.data --output perfjit.data
// verify mmap "//anon" events present initially
perf script --input perf.data --show-mmap-events | grep '//anon'
// verify mmap "//anon" events not removed
perf script --input perfjit.data --show-mmap-events | grep '//anon'
** Repro:
This issue was discovered while testing the initial CoreCLR jitdump
implementation. https://github.com/dotnet/coreclr/pull/26897.
** Alternate solutions considered
These were also briefly considered:
* Change kernel to not coalesce mmap regions.
* Change kernel reporting of coalesced mmap regions to perf. Only
include newly mapped memory.
* Only strip parts of // anon mmap events overlapping existing
jitted-<pid>-<code_index>.so mmap events.
Signed-off-by: Steve MacLean <Steve.MacLean@Microsoft.com>
Acked-by: Ian Rogers <irogers@google.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lore.kernel.org/lkml/1590544271-125795-1-git-send-email-steve.maclean@linux.microsoft.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2020-05-27 09:51:11 +08:00
|
|
|
jit_add_pid(machine, pid);
|
2015-11-30 17:02:21 +08:00
|
|
|
*nbytes = jd.bytes_written;
|
2016-03-08 03:44:40 +08:00
|
|
|
ret = 1;
|
|
|
|
}
|
2015-11-30 17:02:21 +08:00
|
|
|
|
2020-11-05 09:56:04 +08:00
|
|
|
nsinfo__put(jd.nsi);
|
2023-04-04 04:35:45 +08:00
|
|
|
free(jd.buf);
|
2020-11-05 09:56:04 +08:00
|
|
|
|
2015-11-30 17:02:21 +08:00
|
|
|
return ret;
|
|
|
|
}
|