[2/6] perf thread: Allow tools to register a thread->priv destructor

Message ID 20230719202951.534582-3-acme@kernel.org
State New
Headers
Series Fix some 'perf trace' leaks found with -fsanitize=address. |

Commit Message

Arnaldo Carvalho de Melo July 19, 2023, 8:29 p.m. UTC
  From: Arnaldo Carvalho de Melo <acme@redhat.com>

So that when thread__delete() runs it can be called and free stuff tools
stashed into thread->priv, like 'perf trace' does and will use this
new facility to plug some leaks.

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/util/thread.c | 11 +++++++++++
 tools/perf/util/thread.h |  2 ++
 2 files changed, 13 insertions(+)
  

Comments

Ian Rogers July 19, 2023, 10:31 p.m. UTC | #1
On Wed, Jul 19, 2023 at 1:30 PM Arnaldo Carvalho de Melo
<acme@kernel.org> wrote:
>
> From: Arnaldo Carvalho de Melo <acme@redhat.com>
>
> So that when thread__delete() runs it can be called and free stuff tools
> stashed into thread->priv, like 'perf trace' does and will use this
> new facility to plug some leaks.
>
> Cc: Adrian Hunter <adrian.hunter@intel.com>
> Cc: Ian Rogers <irogers@google.com>
> Cc: Jiri Olsa <jolsa@kernel.org>
> Cc: Namhyung Kim <namhyung@kernel.org>
> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
> ---
>  tools/perf/util/thread.c | 11 +++++++++++
>  tools/perf/util/thread.h |  2 ++
>  2 files changed, 13 insertions(+)
>
> diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
> index 0b166404c5c365cf..35dd4e716e411da9 100644
> --- a/tools/perf/util/thread.c
> +++ b/tools/perf/util/thread.c
> @@ -80,6 +80,13 @@ struct thread *thread__new(pid_t pid, pid_t tid)
>         return NULL;
>  }
>
> +static void (*thread__priv_destructor)(void *priv);
> +
> +void thread__set_priv_destructor(void (*destructor)(void *priv))
> +{

Perhaps:
assert(thread__priv_destructor == NULL);

To make it clear that there should never be >1 currently.

> +       thread__priv_destructor = destructor;
> +}
> +
>  void thread__delete(struct thread *thread)
>  {
>         struct namespaces *namespaces, *tmp_namespaces;
> @@ -112,6 +119,10 @@ void thread__delete(struct thread *thread)
>         exit_rwsem(thread__namespaces_lock(thread));
>         exit_rwsem(thread__comm_lock(thread));
>         thread__free_stitch_list(thread);
> +
> +       if (thread__priv_destructor)
> +               thread__priv_destructor(thread__priv(thread));
> +
>         RC_CHK_FREE(thread);
>  }
>
> diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h
> index 9068a21ce0fa1b0f..e79225a0ea46b789 100644
> --- a/tools/perf/util/thread.h
> +++ b/tools/perf/util/thread.h
> @@ -71,6 +71,8 @@ struct thread *thread__new(pid_t pid, pid_t tid);
>  int thread__init_maps(struct thread *thread, struct machine *machine);
>  void thread__delete(struct thread *thread);
>
> +void thread__set_priv_destructor(void (*destructor)(void *priv));
> +
>  struct thread *thread__get(struct thread *thread);
>  void thread__put(struct thread *thread);
>
> --
> 2.41.0
>
  
Arnaldo Carvalho de Melo July 20, 2023, 1:49 p.m. UTC | #2
Em Wed, Jul 19, 2023 at 03:31:41PM -0700, Ian Rogers escreveu:
> On Wed, Jul 19, 2023 at 1:30 PM Arnaldo Carvalho de Melo
> <acme@kernel.org> wrote:
> >
> > From: Arnaldo Carvalho de Melo <acme@redhat.com>
> >
> > So that when thread__delete() runs it can be called and free stuff tools
> > stashed into thread->priv, like 'perf trace' does and will use this
> > new facility to plug some leaks.
> >
> > Cc: Adrian Hunter <adrian.hunter@intel.com>
> > Cc: Ian Rogers <irogers@google.com>
> > Cc: Jiri Olsa <jolsa@kernel.org>
> > Cc: Namhyung Kim <namhyung@kernel.org>
> > Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
> > ---
> >  tools/perf/util/thread.c | 11 +++++++++++
> >  tools/perf/util/thread.h |  2 ++
> >  2 files changed, 13 insertions(+)
> >
> > diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
> > index 0b166404c5c365cf..35dd4e716e411da9 100644
> > --- a/tools/perf/util/thread.c
> > +++ b/tools/perf/util/thread.c
> > @@ -80,6 +80,13 @@ struct thread *thread__new(pid_t pid, pid_t tid)
> >         return NULL;
> >  }
> >
> > +static void (*thread__priv_destructor)(void *priv);
> > +
> > +void thread__set_priv_destructor(void (*destructor)(void *priv))
> > +{
> 
> Perhaps:
> assert(thread__priv_destructor == NULL);

I'll add that.
 
> To make it clear that there should never be >1 currently.
> 
> > +       thread__priv_destructor = destructor;
> > +}
> > +
> >  void thread__delete(struct thread *thread)
> >  {
> >         struct namespaces *namespaces, *tmp_namespaces;
> > @@ -112,6 +119,10 @@ void thread__delete(struct thread *thread)
> >         exit_rwsem(thread__namespaces_lock(thread));
> >         exit_rwsem(thread__comm_lock(thread));
> >         thread__free_stitch_list(thread);
> > +
> > +       if (thread__priv_destructor)
> > +               thread__priv_destructor(thread__priv(thread));
> > +
> >         RC_CHK_FREE(thread);
> >  }
> >
> > diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h
> > index 9068a21ce0fa1b0f..e79225a0ea46b789 100644
> > --- a/tools/perf/util/thread.h
> > +++ b/tools/perf/util/thread.h
> > @@ -71,6 +71,8 @@ struct thread *thread__new(pid_t pid, pid_t tid);
> >  int thread__init_maps(struct thread *thread, struct machine *machine);
> >  void thread__delete(struct thread *thread);
> >
> > +void thread__set_priv_destructor(void (*destructor)(void *priv));
> > +
> >  struct thread *thread__get(struct thread *thread);
> >  void thread__put(struct thread *thread);
> >
> > --
> > 2.41.0
> >
  

Patch

diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index 0b166404c5c365cf..35dd4e716e411da9 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -80,6 +80,13 @@  struct thread *thread__new(pid_t pid, pid_t tid)
 	return NULL;
 }
 
+static void (*thread__priv_destructor)(void *priv);
+
+void thread__set_priv_destructor(void (*destructor)(void *priv))
+{
+	thread__priv_destructor = destructor;
+}
+
 void thread__delete(struct thread *thread)
 {
 	struct namespaces *namespaces, *tmp_namespaces;
@@ -112,6 +119,10 @@  void thread__delete(struct thread *thread)
 	exit_rwsem(thread__namespaces_lock(thread));
 	exit_rwsem(thread__comm_lock(thread));
 	thread__free_stitch_list(thread);
+
+	if (thread__priv_destructor)
+		thread__priv_destructor(thread__priv(thread));
+
 	RC_CHK_FREE(thread);
 }
 
diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h
index 9068a21ce0fa1b0f..e79225a0ea46b789 100644
--- a/tools/perf/util/thread.h
+++ b/tools/perf/util/thread.h
@@ -71,6 +71,8 @@  struct thread *thread__new(pid_t pid, pid_t tid);
 int thread__init_maps(struct thread *thread, struct machine *machine);
 void thread__delete(struct thread *thread);
 
+void thread__set_priv_destructor(void (*destructor)(void *priv));
+
 struct thread *thread__get(struct thread *thread);
 void thread__put(struct thread *thread);