1 From d3f7cba32820e241abf2f94ada3ce83ad96b04e4 Mon Sep 17 00:00:00 2001
2 From: Namhyung Kim <namhyung@kernel.org>
3 Date: Wed, 22 May 2019 14:32:48 +0900
4 Subject: perf namespace: Protect reading thread's namespace
6 [ Upstream commit 6584140ba9e6762dd7ec73795243289b914f31f9 ]
8 It seems that the current code lacks holding the namespace lock in
9 thread__namespaces(). Otherwise it can see inconsistent results.
11 Signed-off-by: Namhyung Kim <namhyung@kernel.org>
12 Cc: Hari Bathini <hbathini@linux.vnet.ibm.com>
13 Cc: Jiri Olsa <jolsa@redhat.com>
14 Cc: Krister Johansen <kjlx@templeofstupid.com>
15 Link: http://lkml.kernel.org/r/20190522053250.207156-2-namhyung@kernel.org
16 Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
17 Signed-off-by: Sasha Levin <sashal@kernel.org>
19 tools/perf/util/thread.c | 15 +++++++++++++--
20 1 file changed, 13 insertions(+), 2 deletions(-)
22 diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
23 index 2048d393ece6..56007a7e0b4d 100644
24 --- a/tools/perf/util/thread.c
25 +++ b/tools/perf/util/thread.c
26 @@ -128,7 +128,7 @@ void thread__put(struct thread *thread)
30 -struct namespaces *thread__namespaces(const struct thread *thread)
31 +static struct namespaces *__thread__namespaces(const struct thread *thread)
33 if (list_empty(&thread->namespaces_list))
35 @@ -136,10 +136,21 @@ struct namespaces *thread__namespaces(const struct thread *thread)
36 return list_first_entry(&thread->namespaces_list, struct namespaces, list);
39 +struct namespaces *thread__namespaces(const struct thread *thread)
41 + struct namespaces *ns;
43 + down_read((struct rw_semaphore *)&thread->namespaces_lock);
44 + ns = __thread__namespaces(thread);
45 + up_read((struct rw_semaphore *)&thread->namespaces_lock);
50 static int __thread__set_namespaces(struct thread *thread, u64 timestamp,
51 struct namespaces_event *event)
53 - struct namespaces *new, *curr = thread__namespaces(thread);
54 + struct namespaces *new, *curr = __thread__namespaces(thread);
56 new = namespaces__new(event);