Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1980621rwd; Thu, 25 May 2023 23:07:50 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6kz5vCzcmjIaaGl6wsoMbCrxoyQGjkvp3K09DJn/oboP+4Q8UZLyb5+JorsNe02dWeQxHa X-Received: by 2002:a05:6a00:2e26:b0:643:4d69:efb8 with SMTP id fc38-20020a056a002e2600b006434d69efb8mr2135270pfb.6.1685081270308; Thu, 25 May 2023 23:07:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685081270; cv=none; d=google.com; s=arc-20160816; b=N1YeBQ+JZEelFUlx+s09ve79OD5wKxzCUPZ8uSXG9PEGCOcP8NnBRnRHUE7M5Swaa/ K1yDa7VLKm81xM4jyztEW5xpuDzZLh+ByXtmr0yJBc+C+H6cb0mmY/SGmNmeCaNobjKj Ye3MwYYSL0AO5mVdLvcKw9J8r9g0jhf11bEFKz8F0iwR+m+Wqkq37ZC1OBwn9ysYk5w6 1g+KnnWvW8WD4c/0c3F0SG+6q6f5yAww/wsvdrFSV80GtHOdiBn/cXM4I3meiiU5Sdc7 0WPORFEBeqaxm1x3lyXnukM6b2xgSydDm0NhdM8vE222TvSn+xhDWgT/VvU6Ri2GKPqR WToQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version; bh=Gacq5P0IJ01X+RbhoROwJTZ5NElVYJMYyjcnLhlhkpI=; b=y16MFaTh/3UfZlrdzofkzYnJY8H1x8dcH4X0obyXcCKG5qgitEIeNP6+kNYsdQCcBr LYlLm39tjzatMEuplC/6OacT36QewJHYo9O8prxnxWvCUhIfes54mMEeg9WQr6b8mRbV 7Gvj7AJ4L8D+EbGGxBlwv5/XAIEO7t2T7xDv+QHNrwoAfHZVRXQCdcXdHVgYt3zgRWbY ElOOUnNEUNgGV2tqISjwEMlm8D//La3ls/celZVSrjm5rHia2JVVvn3GKYj8NbXimWwX rkzMyv0989zz68kFbebUnx3fVb8RrnPwrcZlTdyg4LqKBUvaoEg0J8UA+AdS+kWMkvFv 8ctQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c28-20020a056a00009c00b00640f04eb325si3288891pfj.229.2023.05.25.23.07.36; Thu, 25 May 2023 23:07:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236687AbjEZGBL convert rfc822-to-8bit (ORCPT + 99 others); Fri, 26 May 2023 02:01:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51396 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235137AbjEZGBJ (ORCPT ); Fri, 26 May 2023 02:01:09 -0400 Received: from mail-vk1-f182.google.com (mail-vk1-f182.google.com [209.85.221.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6330F125; Thu, 25 May 2023 23:01:07 -0700 (PDT) Received: by mail-vk1-f182.google.com with SMTP id 71dfb90a1353d-456d241fcdcso90337e0c.0; Thu, 25 May 2023 23:01:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685080866; x=1687672866; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gyyvopx0qx2J/HSG6Wcv+o30ueEsw+4dnHLmLntqWO0=; b=bNy5AALy57U9ftCANNuw6TDyUEnVrSGCKhjt1M0OJ9PbbkFCpgikMUHZIh7tY2I4U9 225p8L9qcfBi59Ofe3v/59LkTeF6ZHtLHzslM9aTUUO8Atc5/jz4+JWkW/ydQpgkhTRb loMALBPlQE+ibgV72DNdHliGsS7VMkSmYLWybORlxQWv9jNHrvESdK8BSt+gx9kUDt/c 1NImG3zJKtae1pITh8c265GMQfvhh/9ZGOxmoUFt+zItLAormv9RcN7TjDhh4ecTaNuo SFP31cSaB9tUWgMD+wXpDIyPVZ72O++gT7ccuILsUp0JDsDsTy5amVWS5/kuGtcXA1H7 u5lw== X-Gm-Message-State: AC+VfDxxDuQgxC8IiGpHP9jbAuePZ00zrPkpAErdoY/MDVt97bgBuqai VFx2TjpHRCDbv4iIP2ydDNHc32Kho3JLO9oaeWQ= X-Received: by 2002:a1f:3d17:0:b0:3ea:7af1:9ea4 with SMTP id k23-20020a1f3d17000000b003ea7af19ea4mr156594vka.12.1685080866267; Thu, 25 May 2023 23:01:06 -0700 (PDT) MIME-Version: 1.0 References: <20230525071133.2066610-1-irogers@google.com> <20230525071133.2066610-2-irogers@google.com> In-Reply-To: From: Namhyung Kim Date: Thu, 25 May 2023 23:00:54 -0700 Message-ID: Subject: Re: [PATCH v1 01/16] perf header: Make nodes dynamic in write_mem_topology To: Ian Rogers Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , "Masami Hiramatsu (Google)" , "Steven Rostedt (Google)" , Ross Zwisler , Leo Yan , Tiezhu Yang , Yang Jihong , Andi Kleen , Kan Liang , Ravi Bangoria , Sean Christopherson , K Prateek Nayak , Paolo Bonzini , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, May 25, 2023 at 7:39 PM Ian Rogers wrote: > > On Thu, May 25, 2023 at 12:15 PM Namhyung Kim wrote: > > > > On Thu, May 25, 2023 at 12:12 AM Ian Rogers wrote: > > > > > > Avoid a large static array, dynamically allocate the nodes avoiding a > > > hard coded limited as well. > > > > > > Signed-off-by: Ian Rogers > > > --- > > > tools/perf/util/header.c | 33 +++++++++++++++++++++------------ > > > 1 file changed, 21 insertions(+), 12 deletions(-) > > > > > > diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c > > > index 2dde3ca20de5..80593ed8c79b 100644 > > > --- a/tools/perf/util/header.c > > > +++ b/tools/perf/util/header.c > > > @@ -24,6 +24,7 @@ > > > #include > > > #endif > > > #include > > > +#include // reallocarray > > > > > > #include "dso.h" > > > #include "evlist.h" > > > @@ -1396,13 +1397,14 @@ static int memory_node__sort(const void *a, const void *b) > > > return na->node - nb->node; > > > } > > > > > > -static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp) > > > +static int build_mem_topology(struct memory_node **nodesp, u64 *cntp) > > > { > > > char path[PATH_MAX]; > > > struct dirent *ent; > > > DIR *dir; > > > - u64 cnt = 0; > > > int ret = 0; > > > + size_t cnt = 0, size = 0; > > > + struct memory_node *nodes = NULL; > > > > > > scnprintf(path, PATH_MAX, "%s/devices/system/node/", > > > sysfs__mountpoint()); > > > @@ -1426,16 +1428,24 @@ static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp) > > > if (r != 1) > > > continue; > > > > > > - if (WARN_ONCE(cnt >= size, > > > - "failed to write MEM_TOPOLOGY, way too many nodes\n")) { > > > - closedir(dir); > > > - return -1; > > > - } > > > + if (cnt >= size) { > > > + struct memory_node *new_nodes = > > > + reallocarray(nodes, cnt + 4, sizeof(*nodes)); > > > > > > + if (!new_nodes) { > > > + pr_err("Failed to write MEM_TOPOLOGY, size %zd nodes\n", size); > > > + free(nodes); > > > + closedir(dir); > > > + return -ENOMEM; > > > + } > > > + nodes = new_nodes; > > > + size += 4; > > > + } > > > ret = memory_node__read(&nodes[cnt++], idx); > > > > I think you need to handle error cases here. > > > > Thanks, > > Namhyung > > Not sure I follow. The reallocarray tests for failure, frees nodes and > returns -ENOMEM on error. Right, but I think it would leak the nodes when the memory_node__read() fails. Thanks, Namhyung