From nobody Sun Feb 8 08:27:42 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5EF84C77B73 for ; Sat, 27 May 2023 03:43:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230036AbjE0Dnv (ORCPT ); Fri, 26 May 2023 23:43:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58072 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229901AbjE0Dnp (ORCPT ); Fri, 26 May 2023 23:43:45 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8545E4 for ; Fri, 26 May 2023 20:43:43 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id 98e67ed59e1d1-2564d6fe52eso76575a91.3 for ; Fri, 26 May 2023 20:43:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685159023; x=1687751023; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=6LNFgpANWxclZfBQXN09BDTHOQ1Q2FmhmEUjxzQcKoQ=; b=M8gRdZwcYSReKulMtU0M6CnxWVgvH2I6XPogufA7q910crmXUeW0QuwrxlUvUFs7zv +PxA7rcqQbFioYAEvSRzcqGXuCiGMEwBk+M3wV+Xvrrjvgnk5KcowEjcF+Ki5GyPaMBx /iXEdQYbmAAD9kGE7wb1ESERjkC2x69dgmdwlAb3vOvbGi6HW95EuL9oWbeE5M41ngIv 55kfOJso0tbfEIg7HEY5g9wSrr9KAHRj8TjT6lL6+NlB8wzmaTnzSMUsQdzfbPOWK/nr lnaDmVJjgWSfRmQdRdkZfd7JuWUxPrH/zoqJwDePppJBDHzbLUoo4hZijHWDkkOwc4vL vM8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685159023; x=1687751023; 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=6LNFgpANWxclZfBQXN09BDTHOQ1Q2FmhmEUjxzQcKoQ=; b=RR2ZSBHFTuKavCJBEfl1fwwHak8Ro9RjHCgi/dMDzXeMINmul3KBq5+qtZr7Z63KuW 16fjiLwniFd1cv0tJtIObXMa6z3CFTKXiCqhI4+yP9q2EoWSuu4YlSh7DEyAz/zJ+VX5 afmPjcNJM0rEl7WX7QUGftP1qE+7GQL99RyV4D0M4UvkJocx4TuRCb5CjT31OBwHQtT9 8f9WlyiUjW4HplVoDUcp6K3UcHpaLJXG5hzxeb1RUSnNUl2foXfzowViNUueegkkHd8A 6zMF+IqouCX/4QZUopt4RRT53mqJmyizwJE1gP2N9NGlXRpS8siHctdTxANLs0lcVeAF qkgw== X-Gm-Message-State: AC+VfDy8pt8xBRvdX81IXfDXYXQN7CzdsGcJ8XyMm4T1WGzxodfw0EHA QjRXJug5vFWU9v+gCfNIanPbfgaTfoP7 X-Google-Smtp-Source: ACHHUZ6P92HLgMuSv7U9cMBYIS6/tm/9qWP4Dk6tYLuWOhZrZHhNrXMo0E9rCtfGDsRNkUbHuC00vgizK/rc X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:3b4e:312c:644:a642]) (user=irogers job=sendgmr) by 2002:a17:90a:9904:b0:246:6a3a:6aec with SMTP id b4-20020a17090a990400b002466a3a6aecmr929999pjp.4.1685159023392; Fri, 26 May 2023 20:43:43 -0700 (PDT) Date: Fri, 26 May 2023 20:43:19 -0700 In-Reply-To: <20230527034324.2597593-1-irogers@google.com> Message-Id: <20230527034324.2597593-2-irogers@google.com> Mime-Version: 1.0 References: <20230527034324.2597593-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.rc0.172.g3f132b7071-goog Subject: [PATCH v1 1/4] perf sched: Avoid large stack allocations From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Commit 5ded57ac1bdb ("perf inject: Remove static variables") moved static variables to local, however, in this case 3 MAX_CPUS (4096) sized arrays were moved onto the stack making the stack frame quite large. Avoid the stack usage by dynamically allocating the arrays. Signed-off-by: Ian Rogers --- tools/perf/builtin-sched.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c index cc4ba506e119..2aeb3c8ac396 100644 --- a/tools/perf/builtin-sched.c +++ b/tools/perf/builtin-sched.c @@ -193,8 +193,8 @@ struct perf_sched { * weird events, such as a task being switched away that is not current. */ struct perf_cpu max_cpu; - u32 curr_pid[MAX_CPUS]; - struct thread *curr_thread[MAX_CPUS]; + u32 *curr_pid; + struct thread **curr_thread; char next_shortname1; char next_shortname2; unsigned int replay_repeat; @@ -224,7 +224,7 @@ struct perf_sched { u64 run_avg; u64 all_runtime; u64 all_count; - u64 cpu_last_switched[MAX_CPUS]; + u64 *cpu_last_switched; struct rb_root_cached atom_root, sorted_atom_root, merged_atom_root; struct list_head sort_list, cmp_pid; bool force; @@ -3599,7 +3599,22 @@ int cmd_sched(int argc, const char **argv) =20 mutex_init(&sched.start_work_mutex); mutex_init(&sched.work_done_wait_mutex); - for (i =3D 0; i < ARRAY_SIZE(sched.curr_pid); i++) + sched.curr_thread =3D calloc(MAX_CPUS, sizeof(*sched.curr_thread)); + if (!sched.curr_thread) { + ret =3D -ENOMEM; + goto out; + } + sched.cpu_last_switched =3D calloc(MAX_CPUS, sizeof(*sched.cpu_last_switc= hed)); + if (!sched.cpu_last_switched) { + ret =3D -ENOMEM; + goto out; + } + sched.curr_pid =3D malloc(MAX_CPUS * sizeof(*sched.curr_pid)); + if (!sched.curr_pid) { + ret =3D -ENOMEM; + goto out; + } + for (i =3D 0; i < MAX_CPUS; i++) sched.curr_pid[i] =3D -1; =20 argc =3D parse_options_subcommand(argc, argv, sched_options, sched_subcom= mands, @@ -3668,6 +3683,9 @@ int cmd_sched(int argc, const char **argv) } =20 out: + free(sched.curr_pid); + free(sched.cpu_last_switched); + free(sched.curr_thread); mutex_destroy(&sched.start_work_mutex); mutex_destroy(&sched.work_done_wait_mutex); =20 --=20 2.41.0.rc0.172.g3f132b7071-goog From nobody Sun Feb 8 08:27:42 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D0787C77B73 for ; Sat, 27 May 2023 03:44:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230161AbjE0Dn7 (ORCPT ); Fri, 26 May 2023 23:43:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58124 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229947AbjE0Dnt (ORCPT ); Fri, 26 May 2023 23:43:49 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A043510E for ; Fri, 26 May 2023 20:43:46 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5659c7dad06so34190257b3.0 for ; Fri, 26 May 2023 20:43:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685159026; x=1687751026; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=9j0CmaHr2uUosemIHAtUjvq8XY3iHL9ps0luhHrIcCk=; b=gP94xvi6b+zhoRwIWTjg46ZhS6rkzx7v2svFGYNNXWxrKKjrKZkCgerCW+USwJJ5lr DphEbgs/fpN3tL9iFWsEFPBd+QjBekbrjKrxPgtRMJMBINdRoYhtv0HLbHsNnNNjTto4 zMvN9LoQtn8FeVFK5PRMnzt2aRP753TSS7fC6UUkL5JanuPV+CXgQjKFSEiYbAnnMpG9 cnE92FWvxzPhyH5dbrPYcZ42MLhrVpMCSwkBajyldLx8h8j3+VLE9sX6eYCWWtoIkuee 2W+LDEDYaiXbiWzCioLXqN1ZLyZ/Jsg+mkIeRy0Qseny+WqOUKzauWYSRsQGe6BioBKT if5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685159026; x=1687751026; 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=9j0CmaHr2uUosemIHAtUjvq8XY3iHL9ps0luhHrIcCk=; b=IAVoV3cBmu1pvNH3MkMBi8c2ed4+nvOMF7q473VFlT4CpvlBOg9tJVWDJ/rF/PSObO oKh6pcHoST442dCnwtX3nyXCHppATVUV138TUphVT56ig9Ctm+1WfozKCor2oNtAVD7n Wagecq9Vw367xNEkNp0wYbU07RfzRbdEXwnExTwJ3LJVv3Ws1kAm2BlqH63t/EPxluOQ rp6Hw6xz90KYQUHDjG4bi+H74HaFtuDmXIeq/2zUt5JRQl9mUmqPWsrf1LU7/ofNhKtU 74Zjnn2TPGfBwKa8CDNTbCjRCqcbs5jP05lcOZWrNDOpAQrFwDmecacXYJQRhEMDLQBS o81g== X-Gm-Message-State: AC+VfDwkT+maqfNyG04NElfYGJrS5kWCnuC5jm8OBP/72ztDJitFCqKU t69scta69Nn252Rl4b+yT/anA9EGcFtl X-Google-Smtp-Source: ACHHUZ7wKIEQLbYrXocZnASUfMtDn0vBZ80GIpjSaCu532B0ZhkyGJlfAThjscSW670Al1jbCvv3BrlWv1wv X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:3b4e:312c:644:a642]) (user=irogers job=sendgmr) by 2002:a81:4421:0:b0:565:9f59:664f with SMTP id r33-20020a814421000000b005659f59664fmr2275624ywa.6.1685159025953; Fri, 26 May 2023 20:43:45 -0700 (PDT) Date: Fri, 26 May 2023 20:43:20 -0700 In-Reply-To: <20230527034324.2597593-1-irogers@google.com> Message-Id: <20230527034324.2597593-3-irogers@google.com> Mime-Version: 1.0 References: <20230527034324.2597593-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.rc0.172.g3f132b7071-goog Subject: [PATCH v1 2/4] perf script: Remove some large stack allocations From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Some char buffers are stack allocated but in total they come to 24kb. Avoid Wstack-usage warnings by moving the arrays to being dynamically allocated. Signed-off-by: Ian Rogers --- tools/perf/builtin-script.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/tools/perf/builtin-script.c b/tools/perf/builtin-script.c index 029d5a597233..3875c4b72db1 100644 --- a/tools/perf/builtin-script.c +++ b/tools/perf/builtin-script.c @@ -3299,14 +3299,21 @@ static int list_available_scripts(const struct opti= on *opt __maybe_unused, int unset __maybe_unused) { struct dirent *script_dirent, *lang_dirent; - char scripts_path[MAXPATHLEN]; + char *buf, *scripts_path, *script_path, *lang_path, *first_half; DIR *scripts_dir, *lang_dir; - char script_path[MAXPATHLEN]; - char lang_path[MAXPATHLEN]; struct script_desc *desc; - char first_half[BUFSIZ]; char *script_root; =20 + buf =3D malloc(3 * MAXPATHLEN + BUFSIZ); + if (!buf) { + pr_err("malloc failed\n"); + exit(-1); + } + scripts_path =3D buf; + script_path =3D buf + MAXPATHLEN; + lang_path =3D buf + 2 * MAXPATHLEN; + first_half =3D buf + 3 * MAXPATHLEN; + snprintf(scripts_path, MAXPATHLEN, "%s/scripts", get_argv_exec_path()); =20 scripts_dir =3D opendir(scripts_path); @@ -3315,6 +3322,7 @@ static int list_available_scripts(const struct option= *opt __maybe_unused, "open(%s) failed.\n" "Check \"PERF_EXEC_PATH\" env to set scripts dir.\n", scripts_path); + free(buf); exit(-1); } =20 @@ -3345,6 +3353,7 @@ static int list_available_scripts(const struct option= *opt __maybe_unused, desc->half_liner ? desc->half_liner : ""); } =20 + free(buf); exit(0); } =20 --=20 2.41.0.rc0.172.g3f132b7071-goog From nobody Sun Feb 8 08:27:42 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2D831C77B7E for ; Sat, 27 May 2023 03:44:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231192AbjE0DoL (ORCPT ); Fri, 26 May 2023 23:44:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230064AbjE0Dnv (ORCPT ); Fri, 26 May 2023 23:43:51 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA986187 for ; Fri, 26 May 2023 20:43:48 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-56561689700so33279997b3.2 for ; Fri, 26 May 2023 20:43:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685159028; x=1687751028; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=SX/Sd6Hz8SucmgLLqLgFnj2GhAGV//bvgD4WKofd4fE=; b=wq+K9+YK2q2GWtwBQagLJBPoNdupJSt8Trc6Xc2YbvtweMG3pLrGczGpkHj1qdbXEd lRlL/TPPGxzEilLsh6ZsHYBHUwIOWtPVFQeGVPtgKj36wPZddikWy7mVpWMogfxGyQji zd/5bIWv2LrrAaDp3bQiPUj5eT59sb/hl2zNXTzzpc0/ODlX2/izzmNX6jOttB5Gwvc6 xC/f0XAHW4TaGswS4OTMY0pixe0MDGKP3sjB63mSc5h65L8m9Uq5ZtMXjFnMt7V62sUh AXd4mtf/NiIhG8mK5M6EjCjvV2D6c+BWbhiGC82EFDLucc//Kz6Lfof64TCnQRbVguOA K4Fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685159028; x=1687751028; 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=SX/Sd6Hz8SucmgLLqLgFnj2GhAGV//bvgD4WKofd4fE=; b=GCOuN/pJ34qiFL2CfYybMR8j28MU+6u+9FGDo0jIDfTijg5aBD/OgtqXKy0aYR+Iem nhTqrZfPePDztPgQPBwZxWgAoXS6fuu+v5ZltmKFZ5jTklVAi3M28eKgPr5zGg+XGymt ywQKKM6MzkNrFnDNv6IJ5wJBPrfbvWoqdFuxNRacihIHyRlS9qVMR49UEH5JIMmqzMeq +ap/vzngrTmgaVHMBFRsKjgPRMXGP7XvRUZuC03Ma9ZYJYzdr0mtmZq1QC0Zh1lRH/Jn 0RJ1EIxsquXF+IbXxPsVcJrCt4gUStYMn57r+RwZ+1xp0ImdSbpx5hFseR4uAbZ4vtNJ 3jaA== X-Gm-Message-State: AC+VfDwq73SedkZAo2xpAsYi2jUp3lvZEuM9o9R6Fw20tRrchu6COkfC Z+jB71mRc3M2/+70AtYxgEDzTnxsP2Wo X-Google-Smtp-Source: ACHHUZ6oHxaq/Re3GjQR3B8rFQwiDAFpOTBD4/Os9JJh1F7fFYsEUjt4QQ/nKBe1tBvxls9SJIEZ0Mxvpzpk X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:3b4e:312c:644:a642]) (user=irogers job=sendgmr) by 2002:a81:ac07:0:b0:565:b269:5ef7 with SMTP id k7-20020a81ac07000000b00565b2695ef7mr2190130ywh.1.1685159028067; Fri, 26 May 2023 20:43:48 -0700 (PDT) Date: Fri, 26 May 2023 20:43:21 -0700 In-Reply-To: <20230527034324.2597593-1-irogers@google.com> Message-Id: <20230527034324.2597593-4-irogers@google.com> Mime-Version: 1.0 References: <20230527034324.2597593-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.rc0.172.g3f132b7071-goog Subject: [PATCH v1 3/4] perf inject: Lazily allocate event_copy From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The event_copy is 64kb (PERF_SAMPLE_SIZE_MAX) and stack allocated in struct perf_inject. It is used for aux events that may not exist in a file. Make the array allocation lazy to cut down on the stack usage. Signed-off-by: Ian Rogers --- tools/perf/builtin-inject.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c index 61766eead4f4..da8c89fefa3a 100644 --- a/tools/perf/builtin-inject.c +++ b/tools/perf/builtin-inject.c @@ -122,7 +122,7 @@ struct perf_inject { u64 aux_id; struct list_head samples; struct itrace_synth_opts itrace_synth_opts; - char event_copy[PERF_SAMPLE_MAX_SIZE]; + char *event_copy; struct perf_file_section secs[HEADER_FEAT_BITS]; struct guest_session guest_session; struct strlist *known_build_ids; @@ -320,8 +320,14 @@ perf_inject__cut_auxtrace_sample(struct perf_inject *i= nject, { size_t sz1 =3D sample->aux_sample.data - (void *)event; size_t sz2 =3D event->header.size - sample->aux_sample.size - sz1; - union perf_event *ev =3D (union perf_event *)inject->event_copy; + union perf_event *ev; =20 + if (inject->event_copy =3D=3D NULL) { + inject->event_copy =3D malloc(PERF_SAMPLE_MAX_SIZE); + if (!inject->event_copy) + return ERR_PTR(-ENOMEM); + } + ev =3D (union perf_event *)inject->event_copy; if (sz1 > event->header.size || sz2 > event->header.size || sz1 + sz2 > event->header.size || sz1 < sizeof(struct perf_event_header) + sizeof(u64)) @@ -357,8 +363,11 @@ static int perf_event__repipe_sample(struct perf_tool = *tool, =20 build_id__mark_dso_hit(tool, event, sample, evsel, machine); =20 - if (inject->itrace_synth_opts.set && sample->aux_sample.size) + if (inject->itrace_synth_opts.set && sample->aux_sample.size) { event =3D perf_inject__cut_auxtrace_sample(inject, event, sample); + if (IS_ERR(event)) + return PTR_ERR(event); + } =20 return perf_event__repipe_synth(tool, event); } @@ -2389,5 +2398,6 @@ int cmd_inject(int argc, const char **argv) if (!inject.in_place_update) perf_data__close(&inject.output); free(inject.itrace_synth_opts.vm_tm_corr_args); + free(inject.event_copy); return ret; } --=20 2.41.0.rc0.172.g3f132b7071-goog From nobody Sun Feb 8 08:27:42 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CF8EDC77B73 for ; Sat, 27 May 2023 03:44:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230163AbjE0DoF (ORCPT ); Fri, 26 May 2023 23:44:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58162 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229976AbjE0Dnx (ORCPT ); Fri, 26 May 2023 23:43:53 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 315B0198 for ; Fri, 26 May 2023 20:43:51 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-565d6824f2dso1681927b3.0 for ; Fri, 26 May 2023 20:43:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685159030; x=1687751030; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=4yvnrpEe2huDW+hchGpet0pGN4yM12JCmRSaRQwE34o=; b=vdiA+trob+vQwP/l9vkYEO9/z+6U1/nBEc0wU3JdgwguFq/5kezVdXsk02KtLWokic jtJreMGHT9MVlRplIvKdvswFPCDUeXNkYxrFl04Xx1hGU5kHz2z3sLpamee0nh/RrOVI viBOVpD596XUao9fsS4EeOEBtVMwghxosbyEPpZtX4JNhLSEfcL0wOtlv10Kg4p/dXau NkkT0UwPv0AZ2aBwmuokplOO+N00TAIddsDVI3u4RfDaT6FgTuPAzVfa5dzTk8Yfc6wV 029oV8TOze6RTO2N7r1o0vCDMPnWgvQ3bwIbYujMjtLmDtk1Ybas1ucjEbaCOQQnwSxQ tiGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685159030; x=1687751030; 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=4yvnrpEe2huDW+hchGpet0pGN4yM12JCmRSaRQwE34o=; b=M1bcdRFQv4dZqfB1g07kx0yrvORIxUH+M5rsrR8coVFHJEz+Ii34AWEEluWVTNMGUO 2Q5l/rwaqXJED1rLARmMJREJP0SZNzn53xm2S0+T9pNKi+PqUlqHFXZBhi9BqziWuO+x poiIygKP6CtqkVo+MyrcM6MBCMKklSKcU7p8a0i4AbnmfMooR7Ifzz4lMvklsmrX9TSk BrUrOJvu+He6dSW8XGYK0GGOGWfyp9I2ioJWXMzCB4AhMPWSkTvz/dXBMi+NV1xvy5V0 ae+BXIHQfuZSr7IeC6E2ffRdMl0PRLr317SfWvzGaeHKCRrRqxO/au6G00JFrRFsXFkK U/6g== X-Gm-Message-State: AC+VfDy3hvtMN+/ZwW2foR5YX159oW0x0zli4aBq/IxDgeNkL3Ngoql0 AOG4hlGw54x+3yS0A9YFwX0dfobpGUNd X-Google-Smtp-Source: ACHHUZ4Zsr/NqIVSUKSFW44V5X/YdTceRVOHBh7ZeU4ozH2kjIPNTkmdKhb86VBC89V8M7NU9xpcWemAwXDt X-Received: from irogers.svl.corp.google.com ([2620:15c:2d4:203:3b4e:312c:644:a642]) (user=irogers job=sendgmr) by 2002:a05:690c:711:b0:565:cfac:6d15 with SMTP id bs17-20020a05690c071100b00565cfac6d15mr502739ywb.8.1685159030508; Fri, 26 May 2023 20:43:50 -0700 (PDT) Date: Fri, 26 May 2023 20:43:22 -0700 In-Reply-To: <20230527034324.2597593-1-irogers@google.com> Message-Id: <20230527034324.2597593-5-irogers@google.com> Mime-Version: 1.0 References: <20230527034324.2597593-1-irogers@google.com> X-Mailer: git-send-email 2.41.0.rc0.172.g3f132b7071-goog Subject: [PATCH v1 4/4] perf inject: Lazily allocate guest_event event_buf From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The event_buf is 64kb (PERF_SAMPLE_SIZE_MAX) and stack allocated in struct perf_inject. It is used for guest events that may not exist in a file. Make the array allocation lazy to cut down on the stack usage. Signed-off-by: Ian Rogers --- tools/perf/builtin-inject.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c index da8c89fefa3a..d68d25575b6c 100644 --- a/tools/perf/builtin-inject.c +++ b/tools/perf/builtin-inject.c @@ -47,7 +47,7 @@ struct guest_event { struct perf_sample sample; union perf_event *event; - char event_buf[PERF_SAMPLE_MAX_SIZE]; + char *event_buf; }; =20 struct guest_id { @@ -1372,11 +1372,19 @@ static void guest_session__convert_time(struct gues= t_session *gs, u64 guest_time =20 static int guest_session__fetch(struct guest_session *gs) { - void *buf =3D gs->ev.event_buf; - struct perf_event_header *hdr =3D buf; + void *buf; + struct perf_event_header *hdr; size_t hdr_sz =3D sizeof(*hdr); ssize_t ret; =20 + buf =3D gs->ev.event_buf; + if (!buf) { + buf =3D malloc(PERF_SAMPLE_MAX_SIZE); + if (!buf) + return -ENOMEM; + gs->ev.event_buf =3D buf; + } + hdr =3D buf; ret =3D readn(gs->tmp_fd, buf, hdr_sz); if (ret < 0) return ret; @@ -2399,5 +2407,6 @@ int cmd_inject(int argc, const char **argv) perf_data__close(&inject.output); free(inject.itrace_synth_opts.vm_tm_corr_args); free(inject.event_copy); + free(inject.guest_session.ev.event_buf); return ret; } --=20 2.41.0.rc0.172.g3f132b7071-goog