[PATCH v1 08/16] perf daemon: Dynamically allocate path to perf

Ian Rogers posted 16 patches 2 years, 8 months ago
There is a newer version of this series
[PATCH v1 08/16] perf daemon: Dynamically allocate path to perf
Posted by Ian Rogers 2 years, 8 months ago
Avoid a PATH_MAX array in __daemon (the .data section) by dynamically
allocating the memory.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/builtin-daemon.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/tools/perf/builtin-daemon.c b/tools/perf/builtin-daemon.c
index 34cbe3e959aa..adb5751c3ed0 100644
--- a/tools/perf/builtin-daemon.c
+++ b/tools/perf/builtin-daemon.c
@@ -90,7 +90,7 @@ struct daemon {
 	char			*base;
 	struct list_head	 sessions;
 	FILE			*out;
-	char			 perf[PATH_MAX];
+	char			*perf;
 	int			 signal_fd;
 	time_t			 start;
 };
@@ -1490,6 +1490,15 @@ static int __cmd_ping(struct daemon *daemon, struct option parent_options[],
 	return send_cmd(daemon, &cmd);
 }
 
+static char *set_perf_exe(void)
+{
+	char path[PATH_MAX];
+
+	perf_exe(path, sizeof(path));
+	__daemon.perf = strdup(path);
+	return __daemon.perf;
+}
+
 int cmd_daemon(int argc, const char **argv)
 {
 	struct option daemon_options[] = {
@@ -1503,7 +1512,9 @@ int cmd_daemon(int argc, const char **argv)
 		OPT_END()
 	};
 
-	perf_exe(__daemon.perf, sizeof(__daemon.perf));
+	if (!set_perf_exe())
+		return -ENOMEM;
+
 	__daemon.out = stdout;
 
 	argc = parse_options(argc, argv, daemon_options, daemon_usage,
-- 
2.40.1.698.g37aff9b760-goog
Re: [PATCH v1 08/16] perf daemon: Dynamically allocate path to perf
Posted by Namhyung Kim 2 years, 8 months ago
On Thu, May 25, 2023 at 12:12 AM Ian Rogers <irogers@google.com> wrote:
>
> Avoid a PATH_MAX array in __daemon (the .data section) by dynamically
> allocating the memory.
>
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
>  tools/perf/builtin-daemon.c | 15 +++++++++++++--
>  1 file changed, 13 insertions(+), 2 deletions(-)
>
> diff --git a/tools/perf/builtin-daemon.c b/tools/perf/builtin-daemon.c
> index 34cbe3e959aa..adb5751c3ed0 100644
> --- a/tools/perf/builtin-daemon.c
> +++ b/tools/perf/builtin-daemon.c
> @@ -90,7 +90,7 @@ struct daemon {
>         char                    *base;
>         struct list_head         sessions;
>         FILE                    *out;
> -       char                     perf[PATH_MAX];
> +       char                    *perf;
>         int                      signal_fd;
>         time_t                   start;
>  };
> @@ -1490,6 +1490,15 @@ static int __cmd_ping(struct daemon *daemon, struct option parent_options[],
>         return send_cmd(daemon, &cmd);
>  }
>
> +static char *set_perf_exe(void)
> +{
> +       char path[PATH_MAX];
> +
> +       perf_exe(path, sizeof(path));
> +       __daemon.perf = strdup(path);
> +       return __daemon.perf;

Then you need to free it somewhere.

Thanks,
Namhyung


> +}
> +
>  int cmd_daemon(int argc, const char **argv)
>  {
>         struct option daemon_options[] = {
> @@ -1503,7 +1512,9 @@ int cmd_daemon(int argc, const char **argv)
>                 OPT_END()
>         };
>
> -       perf_exe(__daemon.perf, sizeof(__daemon.perf));
> +       if (!set_perf_exe())
> +               return -ENOMEM;
> +
>         __daemon.out = stdout;
>
>         argc = parse_options(argc, argv, daemon_options, daemon_usage,
> --
> 2.40.1.698.g37aff9b760-goog
>
Re: [PATCH v1 08/16] perf daemon: Dynamically allocate path to perf
Posted by Ian Rogers 2 years, 8 months ago
On Thu, May 25, 2023 at 12:17 PM Namhyung Kim <namhyung@kernel.org> wrote:
>
> On Thu, May 25, 2023 at 12:12 AM Ian Rogers <irogers@google.com> wrote:
> >
> > Avoid a PATH_MAX array in __daemon (the .data section) by dynamically
> > allocating the memory.
> >
> > Signed-off-by: Ian Rogers <irogers@google.com>
> > ---
> >  tools/perf/builtin-daemon.c | 15 +++++++++++++--
> >  1 file changed, 13 insertions(+), 2 deletions(-)
> >
> > diff --git a/tools/perf/builtin-daemon.c b/tools/perf/builtin-daemon.c
> > index 34cbe3e959aa..adb5751c3ed0 100644
> > --- a/tools/perf/builtin-daemon.c
> > +++ b/tools/perf/builtin-daemon.c
> > @@ -90,7 +90,7 @@ struct daemon {
> >         char                    *base;
> >         struct list_head         sessions;
> >         FILE                    *out;
> > -       char                     perf[PATH_MAX];
> > +       char                    *perf;
> >         int                      signal_fd;
> >         time_t                   start;
> >  };
> > @@ -1490,6 +1490,15 @@ static int __cmd_ping(struct daemon *daemon, struct option parent_options[],
> >         return send_cmd(daemon, &cmd);
> >  }
> >
> > +static char *set_perf_exe(void)
> > +{
> > +       char path[PATH_MAX];
> > +
> > +       perf_exe(path, sizeof(path));
> > +       __daemon.perf = strdup(path);
> > +       return __daemon.perf;
>
> Then you need to free it somewhere.

For leak sanitizer, if an allocation is reachable from a global then
it isn't considered a leak. I'll address this in v2.

Thanks,
Ian

> Thanks,
> Namhyung
>
>
> > +}
> > +
> >  int cmd_daemon(int argc, const char **argv)
> >  {
> >         struct option daemon_options[] = {
> > @@ -1503,7 +1512,9 @@ int cmd_daemon(int argc, const char **argv)
> >                 OPT_END()
> >         };
> >
> > -       perf_exe(__daemon.perf, sizeof(__daemon.perf));
> > +       if (!set_perf_exe())
> > +               return -ENOMEM;
> > +
> >         __daemon.out = stdout;
> >
> >         argc = parse_options(argc, argv, daemon_options, daemon_usage,
> > --
> > 2.40.1.698.g37aff9b760-goog
> >