perf trace: Preallocate the syscall table
authorArnaldo Carvalho de Melo <acme@redhat.com>
Thu, 18 Jul 2019 23:19:30 +0000 (20:19 -0300)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Mon, 29 Jul 2019 21:34:42 +0000 (18:34 -0300)
We'll continue reading its details from tracefs as we need it, but
preallocate the whole thing otherwise we may realloc and end up with
pointers to the previous buffer.

I.e. in an upcoming algorithm we'll look for syscalls that have function
signatures that are similar to a given syscall to see if we can reuse
its BPF augmenter, so we may be at syscall 42, having a 'struct syscall'
pointing to that slot in trace->syscalls.table[] and try to read the
slot for an yet unread syscall, which would realloc that table to read
the info for syscall 43, say, which would trigger a realoc of
trace->syscalls.table[], and then the pointer we had for syscall 42
would be pointing to the previous block of memory. b00m.

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Luis Cláudio Gonçalves <lclaudio@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: https://lkml.kernel.org/n/tip-m3cjzzifibs13imafhkk77a0@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/builtin-trace.c
tools/perf/util/syscalltbl.c
tools/perf/util/syscalltbl.h

index 765b998755ce9a8628f3fa313cccd955ecc7181d..d8565c9a18a256bfb3ee3a394d8228af7b0e2912 100644 (file)
@@ -79,7 +79,6 @@ struct trace {
        struct perf_tool        tool;
        struct syscalltbl       *sctbl;
        struct {
-               int             max;
                struct syscall  *table;
                struct bpf_map  *map;
                struct { // per syscall BPF_MAP_TYPE_PROG_ARRAY
@@ -1493,21 +1492,10 @@ static int trace__read_syscall_info(struct trace *trace, int id)
        struct syscall *sc;
        const char *name = syscalltbl__name(trace->sctbl, id);
 
-       if (id > trace->syscalls.max) {
-               struct syscall *nsyscalls = realloc(trace->syscalls.table, (id + 1) * sizeof(*sc));
-
-               if (nsyscalls == NULL)
+       if (trace->syscalls.table == NULL) {
+               trace->syscalls.table = calloc(trace->sctbl->syscalls.nr_entries, sizeof(*sc));
+               if (trace->syscalls.table == NULL)
                        return -ENOMEM;
-
-               if (trace->syscalls.max != -1) {
-                       memset(nsyscalls + trace->syscalls.max + 1, 0,
-                              (id - trace->syscalls.max) * sizeof(*sc));
-               } else {
-                       memset(nsyscalls, 0, (id + 1) * sizeof(*sc));
-               }
-
-               trace->syscalls.table = nsyscalls;
-               trace->syscalls.max   = id;
        }
 
        sc = trace->syscalls.table + id;
@@ -1819,11 +1807,11 @@ static struct syscall *trace__syscall_info(struct trace *trace,
 
        err = -EINVAL;
 
-       if ((id > trace->syscalls.max || trace->syscalls.table[id].name == NULL) &&
-           (err = trace__read_syscall_info(trace, id)) != 0)
+       if (id > trace->sctbl->syscalls.max_id)
                goto out_cant_read;
 
-       if (id > trace->syscalls.max)
+       if ((trace->syscalls.table == NULL || trace->syscalls.table[id].name == NULL) &&
+           (err = trace__read_syscall_info(trace, id)) != 0)
                goto out_cant_read;
 
        if (trace->syscalls.table[id].name == NULL) {
@@ -1838,7 +1826,7 @@ out_cant_read:
        if (verbose > 0) {
                char sbuf[STRERR_BUFSIZE];
                fprintf(trace->output, "Problems reading syscall %d: %d (%s)", id, -err, str_error_r(-err, sbuf, sizeof(sbuf)));
-               if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
+               if (id <= trace->sctbl->syscalls.max_id && trace->syscalls.table[id].name != NULL)
                        fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
                fputs(" information\n", trace->output);
        }
@@ -3922,9 +3910,6 @@ int cmd_trace(int argc, const char **argv)
                NULL
        };
        struct trace trace = {
-               .syscalls = {
-                       . max = -1,
-               },
                .opts = {
                        .target = {
                                .uid       = UINT_MAX,
index 022a9c6703385c4f089a899a64077f8fc3da752e..820fceeb19a9a5df5dd80a9195f21eff0f5acbcf 100644 (file)
@@ -79,6 +79,7 @@ static int syscalltbl__init_native(struct syscalltbl *tbl)
 
        qsort(tbl->syscalls.entries, nr_entries, sizeof(struct syscall), syscallcmp);
        tbl->syscalls.nr_entries = nr_entries;
+       tbl->syscalls.max_id     = syscalltbl_native_max_id;
        return 0;
 }
 
index c8e7e9ce0f01e12e2b0b8825cca5c83c7d901662..9172613028d00f4ccca3a836f8585dfe7b8fcf9b 100644 (file)
@@ -6,6 +6,7 @@ struct syscalltbl {
        union {
                int audit_machine;
                struct {
+                       int max_id;
                        int nr_entries;
                        void *entries;
                } syscalls;