linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] perf config: fix caching and memory leak in perf_home_perfconfig
@ 2021-08-20 13:08 Riccardo Mancini
  2021-08-20 14:13 ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 4+ messages in thread
From: Riccardo Mancini @ 2021-08-20 13:08 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Ian Rogers, Namhyung Kim, Riccardo Mancini, Jiri Olsa,
	Peter Zijlstra, Ingo Molnar, Mark Rutland, Alexander Shishkin,
	Jiri Olsa, Jin Yao, Song Liu, linux-perf-users, linux-kernel

Acaict, perf_home_perfconfig is supposed to cache the result of
home_perfconfig, which returns the default location of perfconfig for
the user, given the HOME environment variable.
However, the current implementation calls home_perfconfig every time
perf_home_perfconfig is called (so no caching is actually performed),
replacing the previous pointer, thus also causing a memory leak.

This patch adds a check of whether either config or failed is set and,
in that case, directly returns config without calling home_perfconfig at
each invocation.

Cc: Jiri Olsa <jolsa@kernel.org>
Fixes: f5f03e19ce14fc31 ("perf config: Add perf_home_perfconfig function")
Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/util/config.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/tools/perf/util/config.c b/tools/perf/util/config.c
index 63d472b336de21d4..6ab670cdf512507e 100644
--- a/tools/perf/util/config.c
+++ b/tools/perf/util/config.c
@@ -581,6 +581,9 @@ const char *perf_home_perfconfig(void)
 	static const char *config;
 	static bool failed;
 
+	if (config || failed)
+		return config;
+
 	config = failed ? NULL : home_perfconfig();
 	if (!config)
 		failed = true;
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] perf config: fix caching and memory leak in perf_home_perfconfig
  2021-08-20 13:08 [PATCH] perf config: fix caching and memory leak in perf_home_perfconfig Riccardo Mancini
@ 2021-08-20 14:13 ` Arnaldo Carvalho de Melo
  2021-08-20 19:58   ` Riccardo Mancini
  0 siblings, 1 reply; 4+ messages in thread
From: Arnaldo Carvalho de Melo @ 2021-08-20 14:13 UTC (permalink / raw)
  To: Riccardo Mancini
  Cc: Ian Rogers, Namhyung Kim, Jiri Olsa, Peter Zijlstra, Ingo Molnar,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Jin Yao, Song Liu,
	linux-perf-users, linux-kernel

Em Fri, Aug 20, 2021 at 03:08:17PM +0200, Riccardo Mancini escreveu:
> Acaict, perf_home_perfconfig is supposed to cache the result of
> home_perfconfig, which returns the default location of perfconfig for
> the user, given the HOME environment variable.
> However, the current implementation calls home_perfconfig every time
> perf_home_perfconfig is called (so no caching is actually performed),
> replacing the previous pointer, thus also causing a memory leak.
> 
> This patch adds a check of whether either config or failed is set and,
> in that case, directly returns config without calling home_perfconfig at
> each invocation.
> 
> Cc: Jiri Olsa <jolsa@kernel.org>
> Fixes: f5f03e19ce14fc31 ("perf config: Add perf_home_perfconfig function")
> Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> ---
>  tools/perf/util/config.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/tools/perf/util/config.c b/tools/perf/util/config.c
> index 63d472b336de21d4..6ab670cdf512507e 100644
> --- a/tools/perf/util/config.c
> +++ b/tools/perf/util/config.c
> @@ -581,6 +581,9 @@ const char *perf_home_perfconfig(void)
>  	static const char *config;
>  	static bool failed;
>  
> +	if (config || failed)
> +		return config;
> +
>  	config = failed ? NULL : home_perfconfig();

humm, why keep the above failed test then?

>  	if (!config)
>  		failed = true;

I.e. please check this:


diff --git a/tools/perf/util/config.c b/tools/perf/util/config.c
index 63d472b336de21d4..4fb5e90d7a57ae48 100644
--- a/tools/perf/util/config.c
+++ b/tools/perf/util/config.c
@@ -581,7 +581,10 @@ const char *perf_home_perfconfig(void)
 	static const char *config;
 	static bool failed;
 
-	config = failed ? NULL : home_perfconfig();
+	if (failed || config)
+		return config;
+
+	config = home_perfconfig();
 	if (!config)
 		failed = true;
 

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] perf config: fix caching and memory leak in perf_home_perfconfig
  2021-08-20 14:13 ` Arnaldo Carvalho de Melo
@ 2021-08-20 19:58   ` Riccardo Mancini
  2021-08-24 19:26     ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 4+ messages in thread
From: Riccardo Mancini @ 2021-08-20 19:58 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Ian Rogers, Namhyung Kim, Jiri Olsa, Peter Zijlstra, Ingo Molnar,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Jin Yao, Song Liu,
	linux-perf-users, linux-kernel

Hi,

On Fri, 2021-08-20 at 11:13 -0300, Arnaldo Carvalho de Melo wrote:
> Em Fri, Aug 20, 2021 at 03:08:17PM +0200, Riccardo Mancini escreveu:
> > Acaict, perf_home_perfconfig is supposed to cache the result of
> > home_perfconfig, which returns the default location of perfconfig for
> > the user, given the HOME environment variable.
> > However, the current implementation calls home_perfconfig every time
> > perf_home_perfconfig is called (so no caching is actually performed),
> > replacing the previous pointer, thus also causing a memory leak.
> > 
> > This patch adds a check of whether either config or failed is set and,
> > in that case, directly returns config without calling home_perfconfig at
> > each invocation.
> > 
> > Cc: Jiri Olsa <jolsa@kernel.org>
> > Fixes: f5f03e19ce14fc31 ("perf config: Add perf_home_perfconfig function")
> > Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
> > ---
> >  tools/perf/util/config.c | 3 +++
> >  1 file changed, 3 insertions(+)
> > 
> > diff --git a/tools/perf/util/config.c b/tools/perf/util/config.c
> > index 63d472b336de21d4..6ab670cdf512507e 100644
> > --- a/tools/perf/util/config.c
> > +++ b/tools/perf/util/config.c
> > @@ -581,6 +581,9 @@ const char *perf_home_perfconfig(void)
> >         static const char *config;
> >         static bool failed;
> >  
> > +       if (config || failed)
> > +               return config;
> > +
> >         config = failed ? NULL : home_perfconfig();
> 
> humm, why keep the above failed test then?

Sorry, I forgot about that, it should be removed.

> 
> >         if (!config)
> >                 failed = true;
> 
> I.e. please check this:
> 
> 
> diff --git a/tools/perf/util/config.c b/tools/perf/util/config.c
> index 63d472b336de21d4..4fb5e90d7a57ae48 100644
> --- a/tools/perf/util/config.c
> +++ b/tools/perf/util/config.c
> @@ -581,7 +581,10 @@ const char *perf_home_perfconfig(void)
>         static const char *config;
>         static bool failed;
>  
> -       config = failed ? NULL : home_perfconfig();
> +       if (failed || config)
> +               return config;
> +
> +       config = home_perfconfig();
>         if (!config)
>                 failed = true;
>  

Looks good to me.
Shall I resend it fixed?

Thanks,
Riccardo


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] perf config: fix caching and memory leak in perf_home_perfconfig
  2021-08-20 19:58   ` Riccardo Mancini
@ 2021-08-24 19:26     ` Arnaldo Carvalho de Melo
  0 siblings, 0 replies; 4+ messages in thread
From: Arnaldo Carvalho de Melo @ 2021-08-24 19:26 UTC (permalink / raw)
  To: Riccardo Mancini
  Cc: Arnaldo Carvalho de Melo, Ian Rogers, Namhyung Kim, Jiri Olsa,
	Peter Zijlstra, Ingo Molnar, Mark Rutland, Alexander Shishkin,
	Jiri Olsa, Jin Yao, Song Liu, linux-perf-users, linux-kernel

Em Fri, Aug 20, 2021 at 09:58:16PM +0200, Riccardo Mancini escreveu:
> On Fri, 2021-08-20 at 11:13 -0300, Arnaldo Carvalho de Melo wrote:
> > diff --git a/tools/perf/util/config.c b/tools/perf/util/config.c
> > index 63d472b336de21d4..4fb5e90d7a57ae48 100644
> > --- a/tools/perf/util/config.c
> > +++ b/tools/perf/util/config.c
> > @@ -581,7 +581,10 @@ const char *perf_home_perfconfig(void)
> >         static const char *config;
> >         static bool failed;
> >  
> > -       config = failed ? NULL : home_perfconfig();
> > +       if (failed || config)
> > +               return config;
> > +
> > +       config = home_perfconfig();
> >         if (!config)
> >                 failed = true;
> >  
> 
> Looks good to me.
> Shall I resend it fixed?

No need, I'll fix it and apply, thanks for checking!

- Arnaldo

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-08-24 19:26 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-20 13:08 [PATCH] perf config: fix caching and memory leak in perf_home_perfconfig Riccardo Mancini
2021-08-20 14:13 ` Arnaldo Carvalho de Melo
2021-08-20 19:58   ` Riccardo Mancini
2021-08-24 19:26     ` Arnaldo Carvalho de Melo

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).