Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp1471304rwd; Thu, 25 May 2023 13:09:04 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6CBdV98xBFoC21NPgGnbfiqGUB9Co6JRAqzQ8pmHb8RBgdcwinOtBiBI+9z2ZP1h6k3VBl X-Received: by 2002:a17:90b:149:b0:253:971b:dd1a with SMTP id em9-20020a17090b014900b00253971bdd1amr2944323pjb.9.1685045344394; Thu, 25 May 2023 13:09:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685045344; cv=none; d=google.com; s=arc-20160816; b=gZCpUuon57CSeZK9SvIkigYWOr1JnppVLbi9mm2nfxEAcIIdTXHlcWBHJj3LCqXXOI RAUUSJDnUWZ+tc71egT2BVDpSy56i4BTGQ9lICS8HmjglSiBnFEIwXqy8pPN+EvOLs4L pCms9Cbuq852AJWAMuCFacapS+RHHBGnB8W9MhEOMzL1qQIHj8N0YJTzAV8hs5bgemP+ F2/yr/lDnoXLQrlntPORJStlmEzynkDpe3QIl5hcv24O7tV9LTX9+NPCbSzkEL7JkQEB R3H9KMIM9IorMyhf7EM/lk8RFRrjUKa0ZIS6nJJsHu7jwU9t3Lq9qPKrzm1AZMclcLoL QI0Q== 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=Vj9GWghmdoP7sOvYVP6dfgFuR1CxN0pwXTCWKjaqzUk=; b=VVtIyDZNQd51XOe8mTwVQiEZf/Z59PRIMZmukizwD5gFFWhN3uwnwV/a+kY9QJkDGa B2Q/6VJNmXeQyVgsqrUnyIchfcEXiRBhjwEltMi2a4b8vEzQN5vHpExMZB9nW2H1KZzQ aqw4J6xRcnrQC8jhqTjL/2amEVQj/dLchkdnzZK8eUfEll4e5kK1JIb7X0mrlmuQB5cT wSLO2AaDgm/vP7uriprQdoZGwg8hCRpjw+569x6rRmLNS2k0nkWLPwzyD0dqFoH7xzlZ S3NpR4Flcttxo2Kph1RXvAhDrhrgTj9Wt/ymZG5UTxaXs6XZG3wN7LAf4PkJgqr6Vh7Y nlBQ== 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 z194-20020a6333cb000000b0053468421910si1787636pgz.620.2023.05.25.13.08.51; Thu, 25 May 2023 13:09:04 -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 S243646AbjEYT17 convert rfc822-to-8bit (ORCPT + 99 others); Thu, 25 May 2023 15:27:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42914 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243670AbjEYT1l (ORCPT ); Thu, 25 May 2023 15:27:41 -0400 Received: from mail-yb1-f176.google.com (mail-yb1-f176.google.com [209.85.219.176]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46DBB10E3; Thu, 25 May 2023 12:26:33 -0700 (PDT) Received: by mail-yb1-f176.google.com with SMTP id 3f1490d57ef6-ba8afcc82c0so28122276.2; Thu, 25 May 2023 12:26:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685042691; x=1687634691; 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=ps2nba1CZQSLfcCNY2djZFhOfLDj/kiQp3GO8gySDB4=; b=FCxUaapCUGb5NNZDCdsk7mtNpettchXvWswpbcHTbMX/5CEmR2n/XUhxMOuzMVC5mO 7fw8xQ9lQJe8vng0R4hquINa05vgmh6yocGJVnnMUCFZdI7k05DizuaiAeJI7uxwUwok 1v4l/yx65FW3Cz94vx7HgeYfb7LduSypZknTp2lrsiFYyV0NCsheW5GrbycZFXzUwD8P pK4YmGBerooI+I/w1++uxdwYulxryiixdM7aUz6mmvnZaXWkv/X00GHDjOQVfYAfEmLQ kN2tsxsBEW/xvI2R2MaFbrIzUoH4w8zrayLeI9AvoPkjtZ752hiZtQx5TEHbwfWHeeEP TnnQ== X-Gm-Message-State: AC+VfDwy1PoDq6L54wVNvetx4tHQ6jJsM9hF7qSFBwPqEeGlDQhlZpIb gg3Qljy4TUrGLZl4UerVWa+P6GD0OOv/xQNGalPk2QfF X-Received: by 2002:a67:e205:0:b0:439:31df:f097 with SMTP id g5-20020a67e205000000b0043931dff097mr6127786vsa.24.1685042105094; Thu, 25 May 2023 12:15:05 -0700 (PDT) MIME-Version: 1.0 References: <20230525071133.2066610-1-irogers@google.com> <20230525071133.2066610-2-irogers@google.com> In-Reply-To: <20230525071133.2066610-2-irogers@google.com> From: Namhyung Kim Date: Thu, 25 May 2023 12:14:53 -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 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 > } > > *cntp = cnt; > + *nodesp = nodes; > closedir(dir); > > if (!ret) > @@ -1444,8 +1454,6 @@ static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp) > return ret; > } > > -#define MAX_MEMORY_NODES 2000 > - > /* > * The MEM_TOPOLOGY holds physical memory map for every > * node in system. The format of data is as follows: > @@ -1464,8 +1472,8 @@ static int build_mem_topology(struct memory_node *nodes, u64 size, u64 *cntp) > static int write_mem_topology(struct feat_fd *ff __maybe_unused, > struct evlist *evlist __maybe_unused) > { > - static struct memory_node nodes[MAX_MEMORY_NODES]; > - u64 bsize, version = 1, i, nr; > + struct memory_node *nodes = NULL; > + u64 bsize, version = 1, i, nr = 0; > int ret; > > ret = sysfs__read_xll("devices/system/memory/block_size_bytes", > @@ -1473,7 +1481,7 @@ static int write_mem_topology(struct feat_fd *ff __maybe_unused, > if (ret) > return ret; > > - ret = build_mem_topology(&nodes[0], MAX_MEMORY_NODES, &nr); > + ret = build_mem_topology(&nodes, &nr); > if (ret) > return ret; > > @@ -1508,6 +1516,7 @@ static int write_mem_topology(struct feat_fd *ff __maybe_unused, > } > > out: > + free(nodes); > return ret; > } > > -- > 2.40.1.698.g37aff9b760-goog >