Re: [PATCH 1/3] perf tools: Protect reading thread's namespace

2019-05-22 Thread Namhyung Kim
Hi Arnaldo,

On Wed, May 22, 2019 at 10:18:32AM -0300, Arnaldo Carvalho de Melo wrote:
> Em Wed, May 22, 2019 at 02:32:48PM +0900, Namhyung Kim escreveu:
> > It seems that the current code lacks holding the namespace lock in
> > thread__namespaces().  Otherwise it can see inconsistent results.
> > 
> > Signed-off-by: Namhyung Kim 
> > ---
> >  tools/perf/util/thread.c | 15 +--
> >  1 file changed, 13 insertions(+), 2 deletions(-)
> > 
> > diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
> > index 403045a2bbea..b413ba5b9835 100644
> > --- a/tools/perf/util/thread.c
> > +++ b/tools/perf/util/thread.c
> > @@ -133,7 +133,7 @@ void thread__put(struct thread *thread)
> > }
> >  }
> >  
> > -struct namespaces *thread__namespaces(const struct thread *thread)
> > +static struct namespaces *__thread__namespaces(const struct thread *thread)
> >  {
> > if (list_empty(>namespaces_list))
> > return NULL;
> > @@ -141,10 +141,21 @@ struct namespaces *thread__namespaces(const struct 
> > thread *thread)
> > return list_first_entry(>namespaces_list, struct namespaces, 
> > list);
> >  }
> >  
> > +struct namespaces *thread__namespaces(const struct thread *thread)
> > +{
> > +   struct namespaces *ns;
> > +
> > +   down_read((struct rw_semaphore *)>namespaces_lock);
> > +   ns = __thread__namespaces(thread);
> > +   up_read((struct rw_semaphore *)>namespaces_lock);
> > +
> 
> Humm, so we need to change thread__namespaces() to remove that const
> instead of throwing it away with that cast, right?

Yes, that's possible too.  Note that thread__comm_str() has the same issue
as well.

Thanks,
Namhyung


Re: [PATCH 1/3] perf tools: Protect reading thread's namespace

2019-05-22 Thread Arnaldo Carvalho de Melo
Em Wed, May 22, 2019 at 02:32:48PM +0900, Namhyung Kim escreveu:
> It seems that the current code lacks holding the namespace lock in
> thread__namespaces().  Otherwise it can see inconsistent results.
> 
> Signed-off-by: Namhyung Kim 
> ---
>  tools/perf/util/thread.c | 15 +--
>  1 file changed, 13 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
> index 403045a2bbea..b413ba5b9835 100644
> --- a/tools/perf/util/thread.c
> +++ b/tools/perf/util/thread.c
> @@ -133,7 +133,7 @@ void thread__put(struct thread *thread)
>   }
>  }
>  
> -struct namespaces *thread__namespaces(const struct thread *thread)
> +static struct namespaces *__thread__namespaces(const struct thread *thread)
>  {
>   if (list_empty(>namespaces_list))
>   return NULL;
> @@ -141,10 +141,21 @@ struct namespaces *thread__namespaces(const struct 
> thread *thread)
>   return list_first_entry(>namespaces_list, struct namespaces, 
> list);
>  }
>  
> +struct namespaces *thread__namespaces(const struct thread *thread)
> +{
> + struct namespaces *ns;
> +
> + down_read((struct rw_semaphore *)>namespaces_lock);
> + ns = __thread__namespaces(thread);
> + up_read((struct rw_semaphore *)>namespaces_lock);
> +

Humm, so we need to change thread__namespaces() to remove that const
instead of throwing it away with that cast, right?

- Arnaldo

> + return ns;
> +}
> +
>  static int __thread__set_namespaces(struct thread *thread, u64 timestamp,
>   struct namespaces_event *event)
>  {
> - struct namespaces *new, *curr = thread__namespaces(thread);
> + struct namespaces *new, *curr = __thread__namespaces(thread);
>  
>   new = namespaces__new(event);
>   if (!new)
> -- 
> 2.21.0.1020.gf2820cf01a-goog

-- 

- Arnaldo


[PATCH 1/3] perf tools: Protect reading thread's namespace

2019-05-21 Thread Namhyung Kim
It seems that the current code lacks holding the namespace lock in
thread__namespaces().  Otherwise it can see inconsistent results.

Signed-off-by: Namhyung Kim 
---
 tools/perf/util/thread.c | 15 +--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index 403045a2bbea..b413ba5b9835 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -133,7 +133,7 @@ void thread__put(struct thread *thread)
}
 }
 
-struct namespaces *thread__namespaces(const struct thread *thread)
+static struct namespaces *__thread__namespaces(const struct thread *thread)
 {
if (list_empty(>namespaces_list))
return NULL;
@@ -141,10 +141,21 @@ struct namespaces *thread__namespaces(const struct thread 
*thread)
return list_first_entry(>namespaces_list, struct namespaces, 
list);
 }
 
+struct namespaces *thread__namespaces(const struct thread *thread)
+{
+   struct namespaces *ns;
+
+   down_read((struct rw_semaphore *)>namespaces_lock);
+   ns = __thread__namespaces(thread);
+   up_read((struct rw_semaphore *)>namespaces_lock);
+
+   return ns;
+}
+
 static int __thread__set_namespaces(struct thread *thread, u64 timestamp,
struct namespaces_event *event)
 {
-   struct namespaces *new, *curr = thread__namespaces(thread);
+   struct namespaces *new, *curr = __thread__namespaces(thread);
 
new = namespaces__new(event);
if (!new)
-- 
2.21.0.1020.gf2820cf01a-goog