linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ian Rogers <irogers@google.com>
To: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>,
	Adrian Hunter <adrian.hunter@intel.com>,
	"Masami Hiramatsu (Google)" <mhiramat@kernel.org>,
	"Steven Rostedt (Google)" <rostedt@goodmis.org>,
	Ross Zwisler <zwisler@chromium.org>, Leo Yan <leo.yan@linaro.org>,
	Tiezhu Yang <yangtiezhu@loongson.cn>,
	Yang Jihong <yangjihong1@huawei.com>,
	Andi Kleen <ak@linux.intel.com>,
	Kan Liang <kan.liang@linux.intel.com>,
	Ravi Bangoria <ravi.bangoria@amd.com>,
	Sean Christopherson <seanjc@google.com>,
	K Prateek Nayak <kprateek.nayak@amd.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v1 01/16] perf header: Make nodes dynamic in write_mem_topology
Date: Thu, 25 May 2023 19:38:57 -0700	[thread overview]
Message-ID: <CAP-5=fX0BnzQacr1NRQfis42osk07BuKJ9BqNTf7pGBMoj4ORA@mail.gmail.com> (raw)
In-Reply-To: <CAM9d7cgaSPbnjoA6bv8fwkpoyc6nCdXOOrbmBm_jGbZbm3a_4w@mail.gmail.com>

On Thu, May 25, 2023 at 12:15 PM Namhyung Kim <namhyung@kernel.org> wrote:
>
> On Thu, May 25, 2023 at 12:12 AM Ian Rogers <irogers@google.com> wrote:
> >
> > Avoid a large static array, dynamically allocate the nodes avoiding a
> > hard coded limited as well.
> >
> > Signed-off-by: Ian Rogers <irogers@google.com>
> > ---
> >  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 <bpf/libbpf.h>
> >  #endif
> >  #include <perf/cpumap.h>
> > +#include <tools/libc_compat.h> // 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.

Thanks,
Ian

>
> >         }
> >
> >         *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
> >

  reply	other threads:[~2023-05-26  2:39 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-25  7:11 [PATCH v1 00/16] Address some perf memory/data size issues Ian Rogers
2023-05-25  7:11 ` [PATCH v1 01/16] perf header: Make nodes dynamic in write_mem_topology Ian Rogers
2023-05-25 19:14   ` Namhyung Kim
2023-05-26  2:38     ` Ian Rogers [this message]
2023-05-26  6:00       ` Namhyung Kim
2023-05-25  7:11 ` [PATCH v1 02/16] perf test x86: insn-x86 test data is immutable so mark it const Ian Rogers
2023-05-25  7:11 ` [PATCH v1 03/16] perf test x86: intel-pt-test " Ian Rogers
2023-05-25  7:11 ` [PATCH v1 04/16] perf trace: Make some large static arrays const Ian Rogers
2023-05-25  7:11 ` [PATCH v1 05/16] perf trace beauty: Make MSR " Ian Rogers
2023-05-25  7:11 ` [PATCH v1 06/16] tools api fs: Avoid large static PATH_MAX arrays Ian Rogers
2023-05-25  7:11 ` [PATCH v1 07/16] tools lib api fs tracing_path: Remove two unused MAX_PATH paths Ian Rogers
2023-05-25  7:11 ` [PATCH v1 08/16] perf daemon: Dynamically allocate path to perf Ian Rogers
2023-05-25 19:17   ` Namhyung Kim
2023-05-26  2:40     ` Ian Rogers
2023-05-25  7:11 ` [PATCH v1 09/16] perf lock: Dynamically allocate lockhash_table Ian Rogers
2023-05-25  7:11 ` [PATCH v1 10/16] perf timechart: Make large arrays dynamic Ian Rogers
2023-05-25  7:11 ` [PATCH v1 11/16] perf probe: Dynamically allocate params memory Ian Rogers
2023-05-25  7:11 ` [PATCH v1 12/16] perf path: Make mkpath thread safe Ian Rogers
2023-05-25  7:11 ` [PATCH v1 13/16] perf scripting-engines: Move static to local variable Ian Rogers
2023-05-25  7:11 ` [PATCH v1 14/16] tools api fs: Dynamically allocate cgroupfs mount point cache Ian Rogers
2023-05-25  7:11 ` [PATCH v1 15/16] perf test pmu: Avoid 2 static path arrays Ian Rogers
2023-05-25  7:11 ` [PATCH v1 16/16] libsubcmd: Avoid two path statics Ian Rogers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAP-5=fX0BnzQacr1NRQfis42osk07BuKJ9BqNTf7pGBMoj4ORA@mail.gmail.com' \
    --to=irogers@google.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=ak@linux.intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=jolsa@kernel.org \
    --cc=kan.liang@linux.intel.com \
    --cc=kprateek.nayak@amd.com \
    --cc=leo.yan@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mhiramat@kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=peterz@infradead.org \
    --cc=ravi.bangoria@amd.com \
    --cc=rostedt@goodmis.org \
    --cc=seanjc@google.com \
    --cc=yangjihong1@huawei.com \
    --cc=yangtiezhu@loongson.cn \
    --cc=zwisler@chromium.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).