From: Tzvetomir Stoyanov <tstoyanov@vmware.com>
To: rostedt@goodmis.org
Cc: linux-trace-devel@vger.kernel.org
Subject: [PATCH v2 2/2] trace-cmd: Fix a possible race condition and deadlock in trace-cmd
Date: Wed, 5 Jun 2019 14:19:37 +0300 [thread overview]
Message-ID: <20190605111937.17894-2-tstoyanov@vmware.com> (raw)
In-Reply-To: <20190605111937.17894-1-tstoyanov@vmware.com>
When pipes are used for communication between trace-cmd main
thread and per-cpu recorder threads, there is a possible race
condition in stop_threads(), which can cause a deadlock between
the main thread and cpu recorder thread:
In trace_stream_read(), the select() call can return 0 if threads
have no data to send. This will force stop_threads() to stop reading
the thread's pipes and enter a waitpid() loop, to wait for all threads
to be terminated. However, there is a case when some threads are still
flushing its data - tracecmd_flush_recording() tries a blocking write()
to the pipe. A dead lock appears - the cpu thread is blocked in write(),
as its buffer is full and no one is reading it. The main thread is blocked
in waitpid(), to wait the same thread to exit.
The deadlock can be (randomly) observed with the command
"trace-cmd profile -p function -F sleep 10"
The proposed fix removes the select timeout, makes the call blocking,
to ensure the threads are flushed its data before going in waitpid() loop.
Signed-off-by: Tzvetomir Stoyanov <tstoyanov@vmware.com>
---
tracecmd/trace-record.c | 3 +--
tracecmd/trace-stream.c | 6 ++++++
2 files changed, 7 insertions(+), 2 deletions(-)
diff --git a/tracecmd/trace-record.c b/tracecmd/trace-record.c
index 4523128..2f5fbd9 100644
--- a/tracecmd/trace-record.c
+++ b/tracecmd/trace-record.c
@@ -626,7 +626,6 @@ static void delete_thread_data(void)
static void stop_threads(enum trace_type type)
{
- struct timeval tv = { 0, 0 };
int ret;
int i;
@@ -643,7 +642,7 @@ static void stop_threads(enum trace_type type)
/* Flush out the pipes */
if (type & TRACE_TYPE_STREAM) {
do {
- ret = trace_stream_read(pids, recorder_threads, &tv);
+ ret = trace_stream_read(pids, recorder_threads, NULL);
} while (ret > 0);
}
diff --git a/tracecmd/trace-stream.c b/tracecmd/trace-stream.c
index dad3466..3814a35 100644
--- a/tracecmd/trace-stream.c
+++ b/tracecmd/trace-stream.c
@@ -92,6 +92,7 @@ int trace_stream_read(struct pid_record_data *pids, int nr_pids, struct timeval
struct pid_record_data *last_pid;
fd_set rfds;
int top_rfd = 0;
+ int nr_fd;
int ret;
int i;
@@ -119,18 +120,23 @@ int trace_stream_read(struct pid_record_data *pids, int nr_pids, struct timeval
return 1;
}
+ nr_fd = 0;
FD_ZERO(&rfds);
for (i = 0; i < nr_pids; i++) {
/* Do not process closed pipes */
if (pids[i].closed)
continue;
+ nr_fd++;
if (pids[i].brass[0] > top_rfd)
top_rfd = pids[i].brass[0];
FD_SET(pids[i].brass[0], &rfds);
}
+ if (!nr_fd)
+ return 0;
+
ret = select(top_rfd + 1, &rfds, NULL, NULL, tv);
if (ret > 0)
--
2.21.0
next prev parent reply other threads:[~2019-06-05 11:19 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-06-05 11:19 [PATCH v2 1/2] trace-cmd: Fix crash when trace-cmd is executed with args "profile -F sleep 1" Tzvetomir Stoyanov
2019-06-05 11:19 ` Tzvetomir Stoyanov [this message]
2019-06-07 6:48 ` [PATCH v2 2/2] trace-cmd: Fix a possible race condition and deadlock in trace-cmd Slavomir Kaslev
2019-06-18 16:20 ` [PATCH v2 1/2] trace-cmd: Fix crash when trace-cmd is executed with args "profile -F sleep 1" Steven Rostedt
2019-06-18 17:54 ` Johannes Berg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190605111937.17894-2-tstoyanov@vmware.com \
--to=tstoyanov@vmware.com \
--cc=linux-trace-devel@vger.kernel.org \
--cc=rostedt@goodmis.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).