diff mbox series

[3/3] trace-cmd: Do not free tsync from thread

Message ID 20210331230728.350421194@goodmis.org (mailing list archive)
State Accepted
Headers show
Series trace-cmd: Fix the memory mangement of trace-cmd agent. | expand

Commit Message

Steven Rostedt March 31, 2021, 11:06 p.m. UTC
From: "Steven Rostedt (VMware)" <rostedt@goodmis.org>

The freeing of an object should happen in the same context as where it is
created. The tsync object is created in the main thread and passed to the
guest and host handling threads. It should be closed and freed by the thread
that created it, especially since that thread accesses the tsync object
after creating the thread. Having the created thread free it would cause all
sorts of dangerous race conditions.

Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
 lib/trace-cmd/trace-timesync.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
diff mbox series

Patch

diff --git a/lib/trace-cmd/trace-timesync.c b/lib/trace-cmd/trace-timesync.c
index 24984fb17dab..ba877f701c85 100644
--- a/lib/trace-cmd/trace-timesync.c
+++ b/lib/trace-cmd/trace-timesync.c
@@ -535,6 +535,10 @@  void tracecmd_tsync_free(struct tracecmd_time_sync *tsync)
 		tsync_context->sync_count = 0;
 		tsync_context->sync_size = 0;
 	}
+
+	if (tsync->msg_handle)
+		tracecmd_msg_handle_close(tsync->msg_handle);
+
 	pthread_mutex_destroy(&tsync->lock);
 	pthread_cond_destroy(&tsync->cond);
 	pthread_barrier_destroy(&tsync->first_sync);
@@ -703,9 +707,6 @@  static void *tsync_host_thread(void *data)
 	struct tracecmd_time_sync *tsync = data;
 
 	tsync_with_guest(tsync);
-	tracecmd_msg_handle_close(tsync->msg_handle);
-	tsync->msg_handle = NULL;
-
 	pthread_exit(0);
 }
 
@@ -868,6 +869,7 @@  int tracecmd_tsync_with_guest_stop(struct tracecmd_time_sync *tsync)
 static void *tsync_agent_thread(void *data)
 {
 	struct tracecmd_time_sync *tsync = data;
+	long ret = 0;
 	int sd;
 
 	while (true) {
@@ -875,6 +877,7 @@  static void *tsync_agent_thread(void *data)
 		if (sd < 0) {
 			if (errno == EINTR)
 				continue;
+			ret = -1;
 			goto out;
 		}
 		break;
@@ -885,11 +888,7 @@  static void *tsync_agent_thread(void *data)
 	tsync_with_host(tsync);
 
 out:
-	tracecmd_msg_handle_close(tsync->msg_handle);
-	tracecmd_tsync_free(tsync);
-	close(sd);
-
-	pthread_exit(0);
+	pthread_exit((void *)ret);
 }
 
 /**