aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/util/db-export.c
diff options
context:
space:
mode:
authorArnaldo Carvalho de Melo <acme@redhat.com>2015-04-06 19:43:22 -0400
committerArnaldo Carvalho de Melo <acme@redhat.com>2015-05-08 15:19:27 -0400
commitb91fc39f4ad7503419dd617df78401fa36266cb3 (patch)
tree370d4454766d031be63ac06a9407055b528954da /tools/perf/util/db-export.c
parente1ed3a5b87ed6759e16ec93f16aae83d2cc77ca2 (diff)
perf machine: Protect the machine->threads with a rwlock
In addition to using refcounts for the struct thread lifetime management, we need to protect access to machine->threads from concurrent access. That happens in 'perf top', where a thread processes events, inserting and deleting entries from that rb_tree while another thread decays hist_entries, that end up dropping references and ultimately deleting threads from the rb_tree and releasing its resources when no further hist_entry (or other data structures, like in 'perf sched') references it. So the rule is the same for refcounts + protected trees in the kernel, get the tree lock, find object, bump the refcount, drop the tree lock, return, use object, drop the refcount if no more use of it is needed, keep it if storing it in some other data structure, drop when releasing that data structure. I.e. pair "t = machine__find(new)_thread()" with a "thread__put(t)", and "perf_event__preprocess_sample(&al)" with "addr_location__put(&al)". The addr_location__put() one is because as we return references to several data structures, we may end up adding more reference counting for the other data structures and then we'll drop it at addr_location__put() time. Acked-by: David Ahern <dsahern@gmail.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Borislav Petkov <bp@suse.de> Cc: Don Zickus <dzickus@redhat.com> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Stephane Eranian <eranian@google.com> Link: http://lkml.kernel.org/n/tip-bs9rt4n0jw3hi9f3zxyy3xln@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf/util/db-export.c')
-rw-r--r--tools/perf/util/db-export.c14
1 files changed, 10 insertions, 4 deletions
diff --git a/tools/perf/util/db-export.c b/tools/perf/util/db-export.c
index bb39a3ffc70b..eb7a2acb973b 100644
--- a/tools/perf/util/db-export.c
+++ b/tools/perf/util/db-export.c
@@ -122,6 +122,7 @@ int db_export__machine(struct db_export *dbe, struct machine *machine)
122int db_export__thread(struct db_export *dbe, struct thread *thread, 122int db_export__thread(struct db_export *dbe, struct thread *thread,
123 struct machine *machine, struct comm *comm) 123 struct machine *machine, struct comm *comm)
124{ 124{
125 struct thread *main_thread;
125 u64 main_thread_db_id = 0; 126 u64 main_thread_db_id = 0;
126 int err; 127 int err;
127 128
@@ -131,8 +132,6 @@ int db_export__thread(struct db_export *dbe, struct thread *thread,
131 thread->db_id = ++dbe->thread_last_db_id; 132 thread->db_id = ++dbe->thread_last_db_id;
132 133
133 if (thread->pid_ != -1) { 134 if (thread->pid_ != -1) {
134 struct thread *main_thread;
135
136 if (thread->pid_ == thread->tid) { 135 if (thread->pid_ == thread->tid) {
137 main_thread = thread; 136 main_thread = thread;
138 } else { 137 } else {
@@ -144,14 +143,16 @@ int db_export__thread(struct db_export *dbe, struct thread *thread,
144 err = db_export__thread(dbe, main_thread, machine, 143 err = db_export__thread(dbe, main_thread, machine,
145 comm); 144 comm);
146 if (err) 145 if (err)
147 return err; 146 goto out_put;
148 if (comm) { 147 if (comm) {
149 err = db_export__comm_thread(dbe, comm, thread); 148 err = db_export__comm_thread(dbe, comm, thread);
150 if (err) 149 if (err)
151 return err; 150 goto out_put;
152 } 151 }
153 } 152 }
154 main_thread_db_id = main_thread->db_id; 153 main_thread_db_id = main_thread->db_id;
154 if (main_thread != thread)
155 thread__put(main_thread);
155 } 156 }
156 157
157 if (dbe->export_thread) 158 if (dbe->export_thread)
@@ -159,6 +160,10 @@ int db_export__thread(struct db_export *dbe, struct thread *thread,
159 machine); 160 machine);
160 161
161 return 0; 162 return 0;
163
164out_put:
165 thread__put(main_thread);
166 return err;
162} 167}
163 168
164int db_export__comm(struct db_export *dbe, struct comm *comm, 169int db_export__comm(struct db_export *dbe, struct comm *comm,
@@ -303,6 +308,7 @@ int db_export__sample(struct db_export *dbe, union perf_event *event,
303 if (err) 308 if (err)
304 return err; 309 return err;
305 310
311 /* FIXME: check refcounting for get_main_thread, that calls machine__find_thread... */
306 main_thread = get_main_thread(al->machine, thread); 312 main_thread = get_main_thread(al->machine, thread);
307 if (main_thread) 313 if (main_thread)
308 comm = machine__thread_exec_comm(al->machine, main_thread); 314 comm = machine__thread_exec_comm(al->machine, main_thread);