From nobody Wed Dec 17 13:56:58 2025 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 50C301DDC10 for ; Tue, 18 Mar 2025 03:32:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742268747; cv=none; b=UPMXoYViffGsoXG/GnVA2kODD2FYbyeKLQj2/El/96q1PbPFny4+7Rhp1Dgu2Plv8GNlLIEBWSnP/WEEK6War9XgRGgTdYMyeCA3Q34UsQKDeIeSInt7zRRrKA+cjJ5ocqFkZusvP4L6uGCyz3BR4a3QfkUpu4Q4WU17LpR5rJg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742268747; c=relaxed/simple; bh=GbxsG9MTzjhI/k4irKic5R/3koEfGE1mTaI8yVw4hYk=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=Oe6Ta9Nxfr1UkWPMIHKb5/0FXWvxJ9mWhB49CHGl/m+U2yORiFqzZjV8VasyIBVYYEtgiiZc8tBQgqArtHPQHOh5TYEBhkfR2DwvogIr/u1Wz4mOqvFQ1spSb1db3T1uOlmWWWI6My4ps2FGPWgd1i6lsZ/yy6RBye/bJWcyA5M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=PAsA60xr; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="PAsA60xr" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e582bfcada6so9346432276.1 for ; Mon, 17 Mar 2025 20:32:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1742268743; x=1742873543; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=L7xBPeAI1ZGK/na4YhBc+S5M0v2o7F6+AZ1yRtBTaOg=; b=PAsA60xrlBjpqBfbkCgNCrfPF5eGpHt+BHpxB7YpbnIYmaOLgCTofQPsvuzAe5jlDk vvpjVw1e9uSTXLy9UxZmbIMKsCBZuCPV9lb6B4GPILvw1Egq3EpNDN4YvVIQD2GD1Y0f SpdbRs2fIvInURUuMbVcUqzP/0tSKcaCqk6p2JWXzFzlDx2z95KlnsGQuQ0PK7l6BqBx VJ0GHvrVNvuSlbDCCKqGfFNgXtweFJIGxWb7G8pelLVCblhalgCOWFeg6H08Wh7KP2gV oyY1y0F0pduzdi23PVuOGma9Ldb9ceC8P4dwXcuS1AQDAu3Z6Hf9+WZfkCW4g7EDvpAQ r2GQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742268743; x=1742873543; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=L7xBPeAI1ZGK/na4YhBc+S5M0v2o7F6+AZ1yRtBTaOg=; b=K6MYHzJ2CebznqeJ+DeUL8j1QWkCr9lKh55cJmBCT8ssaDxwYCotO5//mlQGaGXzGM sj3sN1CPHxx0LNYlIX+XHTgFtaEncagZW6PjzwsBaSbKJjsyyC59ZKS5KKTUI4wxxzSa ykFuaU8gg/qtcZ1CntZG1ScznAWkDLV3+ZVLPEDJw3fIFUzosd1xQA95UDc5+WmPzJ4M cslqelZNHucKn59HJC49AxGvSaalKD7sUKJpVioprmLYSgarQcBRXc+3cqxjYqgsBTFc ErM7WCpsMnS/XmBo+k+Nb3y67c1Ivx98gwMO1ipzalVbN0j6dN6cdyRztGT3qxfFbEEv IRYw== X-Forwarded-Encrypted: i=1; AJvYcCXlAJgaeInZciYp8R6gsl+hhBjkOPIMl9YbsJ02GBl+Wpwg3+yZFmYHCm6GFD8ZejlLLCUrHXkPisgTXOo=@vger.kernel.org X-Gm-Message-State: AOJu0YxC9jSsfBIkkbnicGqrc/QOFnbNvHf0mN2pLYsyTt/QYW+tvq66 EbsSTYhFFcTDyaT/uWpVDwHcpX7xwhfaWZXFi9cmyt4SksgCUPFeOLZ+ivf4zSCkTE0825wsqN7 DcRqeYA== X-Google-Smtp-Source: AGHT+IFesFgROp1XE2mNJD85/+gXB9dKm0RtUVOiKrqT+K9I0LL2YHgQxyTSTsOCO8NGiyj2C//m84F0vwxS X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:ef29:15ce:a42a:b69f]) (user=irogers job=sendgmr) by 2002:a25:b803:0:b0:e63:65c4:798a with SMTP id 3f1490d57ef6-e63f65df420mr8224276.7.1742268743473; Mon, 17 Mar 2025 20:32:23 -0700 (PDT) Date: Mon, 17 Mar 2025 20:31:49 -0700 In-Reply-To: <20250318033150.119174-1-irogers@google.com> Message-Id: <20250318033150.119174-13-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250318033150.119174-1-irogers@google.com> X-Mailer: git-send-email 2.49.0.rc1.451.g8f38331e32-goog Subject: [PATCH v6 12/13] perf trace: Make syscall table stable From: Ian Rogers To: Ian Rogers , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , Kan Liang , John Garry , Will Deacon , James Clark , Mike Leach , Leo Yan , guoren , Paul Walmsley , Palmer Dabbelt , Albert Ou , Charlie Jenkins , Bibo Mao , Huacai Chen , Catalin Marinas , Jiri Slaby , "=?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?=" , Howard Chu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-arm-kernel@lists.infradead.org, "linux-csky@vger.kernel.org" , linux-riscv@lists.infradead.org, Arnd Bergmann Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Namhyung fixed the syscall table being reallocated and moving by reloading the system call pointer after a move: https://lore.kernel.org/lkml/Z9YHCzINiu4uBQ8B@google.com/ This could be brittle so this patch changes the syscall table to be an array of pointers of "struct syscall" that don't move. Remove unnecessary copies and searches with this change. Signed-off-by: Ian Rogers --- tools/perf/builtin-trace.c | 87 +++++++++++++++++++++++--------------- 1 file changed, 53 insertions(+), 34 deletions(-) diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index 1c080d95c1e2..a5f31472980b 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c @@ -151,7 +151,7 @@ struct trace { struct perf_tool tool; struct { /** Sorted sycall numbers used by the trace. */ - struct syscall *table; + struct syscall **table; /** Size of table. */ size_t table_size; struct { @@ -2473,24 +2473,41 @@ static size_t syscall__scnprintf_args(struct syscal= l *sc, char *bf, size_t size, return printed; } =20 -static void syscall__init(struct syscall *sc, int e_machine, int id) +static struct syscall *syscall__new(int e_machine, int id) { - memset(sc, 0, sizeof(*sc)); + struct syscall *sc =3D zalloc(sizeof(*sc)); + + if (!sc) + return NULL; + sc->e_machine =3D e_machine; sc->id =3D id; + return sc; } =20 -static void syscall__exit(struct syscall *sc) +static void syscall__delete(struct syscall *sc) { if (!sc) return; =20 - zfree(&sc->arg_fmt); + free(sc->arg_fmt); + free(sc); +} + +static int syscall__bsearch_cmp(const void *key, const void *entry) +{ + const struct syscall *a =3D key, *b =3D *((const struct syscall **)entry); + + if (a->e_machine !=3D b->e_machine) + return a->e_machine - b->e_machine; + + return a->id - b->id; } =20 static int syscall__cmp(const void *va, const void *vb) { - const struct syscall *a =3D va, *b =3D vb; + const struct syscall *a =3D *((const struct syscall **)va); + const struct syscall *b =3D *((const struct syscall **)vb); =20 if (a->e_machine !=3D b->e_machine) return a->e_machine - b->e_machine; @@ -2504,27 +2521,33 @@ static struct syscall *trace__find_syscall(struct t= race *trace, int e_machine, i .e_machine =3D e_machine, .id =3D id, }; - struct syscall *sc, *tmp; + struct syscall *sc, **tmp; =20 if (trace->syscalls.table) { - sc =3D bsearch(&key, trace->syscalls.table, trace->syscalls.table_size, - sizeof(struct syscall), syscall__cmp); - if (sc) - return sc; + struct syscall **sc_entry =3D bsearch(&key, trace->syscalls.table, + trace->syscalls.table_size, + sizeof(trace->syscalls.table[0]), + syscall__bsearch_cmp); + + if (sc_entry) + return *sc_entry; } =20 + sc =3D syscall__new(e_machine, id); + if (!sc) + return NULL; + tmp =3D reallocarray(trace->syscalls.table, trace->syscalls.table_size + = 1, - sizeof(struct syscall)); - if (!tmp) + sizeof(trace->syscalls.table[0])); + if (!tmp) { + syscall__delete(sc); return NULL; + } =20 trace->syscalls.table =3D tmp; - sc =3D &trace->syscalls.table[trace->syscalls.table_size++]; - syscall__init(sc, e_machine, id); - qsort(trace->syscalls.table, trace->syscalls.table_size, sizeof(struct sy= scall), + trace->syscalls.table[trace->syscalls.table_size++] =3D sc; + qsort(trace->syscalls.table, trace->syscalls.table_size, sizeof(trace->sy= scalls.table[0]), syscall__cmp); - sc =3D bsearch(&key, trace->syscalls.table, trace->syscalls.table_size, - sizeof(struct syscall), syscall__cmp); return sc; } =20 @@ -3855,14 +3878,14 @@ static int trace__bpf_sys_enter_beauty_map(struct t= race *trace, int e_machine, i return -1; } =20 -static struct bpf_program *trace__find_usable_bpf_prog_entry(struct trace = *trace, struct syscall *_sc) +static struct bpf_program *trace__find_usable_bpf_prog_entry(struct trace = *trace, + struct syscall *sc) { - struct syscall sc =3D *_sc; /* Copy as trace__syscall_info may invalidate= pointer. */ struct tep_format_field *field, *candidate_field; /* * We're only interested in syscalls that have a pointer: */ - for (field =3D sc.args; field; field =3D field->next) { + for (field =3D sc->args; field; field =3D field->next) { if (field->flags & TEP_FIELD_IS_POINTER) goto try_to_find_pair; } @@ -3870,18 +3893,17 @@ static struct bpf_program *trace__find_usable_bpf_p= rog_entry(struct trace *trace return NULL; =20 try_to_find_pair: - for (int i =3D 0, num_idx =3D syscalltbl__num_idx(sc.e_machine); i < num_= idx; ++i) { - int id =3D syscalltbl__id_at_idx(sc.e_machine, i); - /* calling trace__syscall_info() may invalidate '_sc' */ - struct syscall *pair =3D trace__syscall_info(trace, NULL, sc.e_machine, = id); + for (int i =3D 0, num_idx =3D syscalltbl__num_idx(sc->e_machine); i < num= _idx; ++i) { + int id =3D syscalltbl__id_at_idx(sc->e_machine, i); + struct syscall *pair =3D trace__syscall_info(trace, NULL, sc->e_machine,= id); struct bpf_program *pair_prog; bool is_candidate =3D false; =20 - if (pair =3D=3D NULL || pair->id =3D=3D sc.id || + if (pair =3D=3D NULL || pair->id =3D=3D sc->id || pair->bpf_prog.sys_enter =3D=3D trace->skel->progs.syscall_unaugment= ed) continue; =20 - for (field =3D sc.args, candidate_field =3D pair->args; + for (field =3D sc->args, candidate_field =3D pair->args; field && candidate_field; field =3D field->next, candidate_field = =3D candidate_field->next) { bool is_pointer =3D field->flags & TEP_FIELD_IS_POINTER, candidate_is_pointer =3D candidate_field->flags & TEP_FIELD_IS_POI= NTER; @@ -3948,7 +3970,8 @@ static struct bpf_program *trace__find_usable_bpf_pro= g_entry(struct trace *trace goto next_candidate; } =20 - pr_debug("Reusing \"%s\" BPF sys_enter augmenter for \"%s\"\n", pair->na= me, sc.name); + pr_debug("Reusing \"%s\" BPF sys_enter augmenter for \"%s\"\n", pair->na= me, + sc->name); return pair_prog; next_candidate: continue; @@ -4044,11 +4067,7 @@ static int trace__init_syscalls_bpf_prog_array_maps(= struct trace *trace, int e_m pair_prog =3D trace__find_usable_bpf_prog_entry(trace, sc); if (pair_prog =3D=3D NULL) continue; - /* - * Get syscall info again as find usable entry above might - * modify the syscall table and shuffle it. - */ - sc =3D trace__syscall_info(trace, NULL, e_machine, key); + sc->bpf_prog.sys_enter =3D pair_prog; =20 /* @@ -5316,7 +5335,7 @@ static void trace__exit(struct trace *trace) zfree(&trace->ev_qualifier_ids.entries); if (trace->syscalls.table) { for (size_t i =3D 0; i < trace->syscalls.table_size; i++) - syscall__exit(&trace->syscalls.table[i]); + syscall__delete(trace->syscalls.table[i]); zfree(&trace->syscalls.table); } zfree(&trace->perfconfig_events); --=20 2.49.0.rc1.451.g8f38331e32-goog