diff mbox series

[1/2] kernel-shark-qt: Avoid race condition when reading data

Message ID 20190104200559.24471-2-ykaradzhov@vmware.com (mailing list archive)
State Accepted
Headers show
Series Improve/debug the searching logic | expand

Commit Message

Yordan Karadzhov Jan. 4, 2019, 8:06 p.m. UTC
We know that the data reading operations are not thread-safe.
A permanent solution for the problem is being worked on. For
the time being, this patch provides a naive temporary fix by
slapping mutexes all over the code, which will slow things down
a bit.

Signed-off-by: Yordan Karadzhov <ykaradzhov@vmware.com>
---
 kernel-shark-qt/src/libkshark.c            | 74 +++++++++++++---------
 kernel-shark-qt/src/libkshark.h            |  3 -
 kernel-shark-qt/src/plugins/sched_events.c |  6 +-
 3 files changed, 47 insertions(+), 36 deletions(-)
diff mbox series

Patch

diff --git a/kernel-shark-qt/src/libkshark.c b/kernel-shark-qt/src/libkshark.c
index 598ea52..23dc813 100644
--- a/kernel-shark-qt/src/libkshark.c
+++ b/kernel-shark-qt/src/libkshark.c
@@ -960,31 +960,6 @@  ssize_t kshark_load_data_records(struct kshark_context *kshark_ctx,
 	return -ENOMEM;
 }
 
-/**
- * @brief A thread-safe read of a record from a specific offset.
- *
- * @param kshark_ctx: Input location for the session context pointer.
- * @param offset: the offset into the file to find the record.
- *
- * @returns The returned pevent_record must be freed.
- */
-struct tep_record *kshark_read_at(struct kshark_context *kshark_ctx,
-				  uint64_t offset)
-{
-	/*
-	 * Calling tracecmd_read_at() is not thread-safe. Use a mutex to
-	 * protect the access.
-	 */
-	pthread_mutex_lock(&kshark_ctx->input_mutex);
-
-	struct tep_record *data = tracecmd_read_at(kshark_ctx->handle,
-						      offset, NULL);
-
-	pthread_mutex_unlock(&kshark_ctx->input_mutex);
-
-	return data;
-}
-
 static const char *kshark_get_latency(struct tep_handle *pe,
 				      struct tep_record *record)
 {
@@ -1046,10 +1021,18 @@  int kshark_get_pid_easy(struct kshark_entry *entry)
 		/*
 		 * The entry has been touched by a plugin callback function.
 		 * Because of this we do not trust the value of "entry->pid".
+		 *
+		 * Currently the data reading operations are not thread-safe.
+		 * Use a mutex to protect the access.
 		 */
-		data = kshark_read_at(kshark_ctx, entry->offset);
+		pthread_mutex_lock(&kshark_ctx->input_mutex);
+
+		data = tracecmd_read_at(kshark_ctx->handle, entry->offset,
+					NULL);
 		pid = tep_data_pid(kshark_ctx->pevent, data);
 		free_record(data);
+
+		pthread_mutex_unlock(&kshark_ctx->input_mutex);
 	}
 
 	return pid;
@@ -1106,10 +1089,18 @@  const char *kshark_get_latency_easy(struct kshark_entry *entry)
 	if (entry->event_id < 0)
 		return NULL;
 
-	data = kshark_read_at(kshark_ctx, entry->offset);
+	/*
+	 * Currently the data reading operations are not thread-safe.
+	 * Use a mutex to protect the access.
+	 */
+	pthread_mutex_lock(&kshark_ctx->input_mutex);
+
+	data = tracecmd_read_at(kshark_ctx->handle, entry->offset, NULL);
 	lat = kshark_get_latency(kshark_ctx->pevent, data);
 	free_record(data);
 
+	pthread_mutex_unlock(&kshark_ctx->input_mutex);
+
 	return lat;
 }
 
@@ -1142,10 +1133,18 @@  int kshark_get_event_id_easy(struct kshark_entry *entry)
 		 * The entry has been touched by a plugin callback function.
 		 * Because of this we do not trust the value of
 		 * "entry->event_id".
+		 *
+		 * Currently the data reading operations are not thread-safe.
+		 * Use a mutex to protect the access.
 		 */
-		data = kshark_read_at(kshark_ctx, entry->offset);
+		pthread_mutex_lock(&kshark_ctx->input_mutex);
+
+		data = tracecmd_read_at(kshark_ctx->handle, entry->offset,
+					NULL);
 		event_id = tep_data_type(kshark_ctx->pevent, data);
 		free_record(data);
+
+		pthread_mutex_unlock(&kshark_ctx->input_mutex);
 	}
 
 	return (event_id == -1)? -EFAULT : event_id;
@@ -1184,7 +1183,14 @@  const char *kshark_get_event_name_easy(struct kshark_entry *entry)
 		}
 	}
 
+	/*
+	 * Currently the data reading operations are not thread-safe.
+	 * Use a mutex to protect the access.
+	 */
+	pthread_mutex_lock(&kshark_ctx->input_mutex);
 	event = tep_data_event_from_type(kshark_ctx->pevent, event_id);
+	pthread_mutex_unlock(&kshark_ctx->input_mutex);
+
 	if (event)
 		return event->name;
 
@@ -1223,8 +1229,13 @@  const char *kshark_get_info_easy(struct kshark_entry *entry)
 		}
 	}
 
-	data = kshark_read_at(kshark_ctx, entry->offset);
+	/*
+	 * Currently the data reading operations are not thread-safe.
+	 * Use a mutex to protect the access.
+	 */
+	pthread_mutex_lock(&kshark_ctx->input_mutex);
 
+	data = tracecmd_read_at(kshark_ctx->handle, entry->offset, NULL);
 	event_id = tep_data_type(kshark_ctx->pevent, data);
 	event = tep_data_event_from_type(kshark_ctx->pevent, event_id);
 	if (event)
@@ -1232,6 +1243,8 @@  const char *kshark_get_info_easy(struct kshark_entry *entry)
 
 	free_record(data);
 
+	pthread_mutex_unlock(&kshark_ctx->input_mutex);
+
 	return info;
 }
 
@@ -1315,7 +1328,8 @@  char* kshark_dump_entry(const struct kshark_entry *entry)
 		struct tep_event_format *event;
 		struct tep_record *data;
 
-		data = kshark_read_at(kshark_ctx, entry->offset);
+		data = tracecmd_read_at(kshark_ctx->handle, entry->offset,
+					NULL);
 
 		event = tep_data_event_from_type(kshark_ctx->pevent,
 						 entry->event_id);
diff --git a/kernel-shark-qt/src/libkshark.h b/kernel-shark-qt/src/libkshark.h
index 7d1edfc..a1b1f91 100644
--- a/kernel-shark-qt/src/libkshark.h
+++ b/kernel-shark-qt/src/libkshark.h
@@ -183,9 +183,6 @@  char* kshark_dump_custom_entry(struct kshark_context *kshark_ctx,
 			       const struct kshark_entry *entry,
 			       kshark_custom_info_func info_func);
 
-struct tep_record *kshark_read_at(struct kshark_context *kshark_ctx,
-				  uint64_t offset);
-
 /** Bit masks used to control the visibility of the entry after filtering. */
 enum kshark_filter_masks {
 	/**
diff --git a/kernel-shark-qt/src/plugins/sched_events.c b/kernel-shark-qt/src/plugins/sched_events.c
index 1500110..5409bc6 100644
--- a/kernel-shark-qt/src/plugins/sched_events.c
+++ b/kernel-shark-qt/src/plugins/sched_events.c
@@ -184,7 +184,7 @@  bool plugin_wakeup_match_rec_pid(struct kshark_context *kshark_ctx,
 
 	if (plugin_ctx->sched_wakeup_event &&
 	    e->event_id == plugin_ctx->sched_wakeup_event->id) {
-		record = kshark_read_at(kshark_ctx, e->offset);
+		record = tracecmd_read_at(kshark_ctx->handle, e->offset, NULL);
 
 		/* We only want those that actually woke up the task. */
 		ret = tep_read_number_field(plugin_ctx->sched_wakeup_success_field,
@@ -196,7 +196,7 @@  bool plugin_wakeup_match_rec_pid(struct kshark_context *kshark_ctx,
 
 	if (plugin_ctx->sched_wakeup_new_event &&
 	    e->event_id == plugin_ctx->sched_wakeup_new_event->id) {
-		record = kshark_read_at(kshark_ctx, e->offset);
+		record = tracecmd_read_at(kshark_ctx->handle, e->offset, NULL);
 
 		/* We only want those that actually woke up the task. */
 		ret = tep_read_number_field(plugin_ctx->sched_wakeup_new_success_field,
@@ -238,7 +238,7 @@  bool plugin_switch_match_rec_pid(struct kshark_context *kshark_ctx,
 	    e->event_id == plugin_ctx->sched_switch_event->id) {
 		struct tep_record *record;
 
-		record = kshark_read_at(kshark_ctx, e->offset);
+		record = tracecmd_read_at(kshark_ctx->handle, e->offset, NULL);
 		ret = tep_read_number_field(plugin_ctx->sched_switch_prev_state_field,
 					    record->data, &val);