public inbox for intel-gfx@lists.freedesktop.org
 help / color / mirror / Atom feed
From: Tvrtko Ursulin <tvrtko.ursulin@linux.intel.com>
To: Intel-gfx@lists.freedesktop.org
Subject: [PATCH 2/2] drmtest: Avoid wrong PID/TID after clone races
Date: Tue,  3 Dec 2013 16:44:55 +0000	[thread overview]
Message-ID: <1386089095-9966-3-git-send-email-tvrtko.ursulin@linux.intel.com> (raw)
In-Reply-To: <1386089095-9966-1-git-send-email-tvrtko.ursulin@linux.intel.com>

From: Tvrtko Ursulin <tvrtko.ursulin@intel.com>

Various C library implementations have various races with regards
to caching getpid() or TID inside pthread_kill() implementations.

For example see clone(2) glibc man page and pthread_kill
Bionic C library source.

Work around that by making sure correct PID/TGID and TID values
are retrieved from the kernel when re-raising the signal. It
can be delivered immediately after the clone system call while C
library cached copies have not yet been updated.

Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
---
 lib/drmtest.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/lib/drmtest.c b/lib/drmtest.c
index dcacd3b..139eb82 100644
--- a/lib/drmtest.c
+++ b/lib/drmtest.c
@@ -43,6 +43,8 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <sys/wait.h>
+#include <sys/types.h>
+#include <sys/syscall.h>
 
 #include "drmtest.h"
 #include "i915_drm.h"
@@ -1473,6 +1475,8 @@ static void igt_atexit_handler(void)
 
 static void fatal_sig_handler(int sig)
 {
+	pid_t pid, tid;
+
 	restore_all_sig_handler();
 
 	/*
@@ -1481,7 +1485,11 @@ static void fatal_sig_handler(int sig)
 	 */
 	call_exit_handlers(sig);
 
-	raise(sig);
+	/* Workaround cached PID and TID races on glibc and Bionic libc. */
+	pid = syscall(SYS_getpid);
+	tid = syscall(SYS_gettid);
+
+	syscall(SYS_tgkill, pid, tid, sig);
 }
 
 /*
-- 
1.8.4.3

  parent reply	other threads:[~2013-12-03 16:45 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-03 16:44 [PATCH 0/2] drmtest: Fix hanging child processes Tvrtko Ursulin
2013-12-03 16:44 ` [PATCH 1/2] Revert "lib/drmtest: ducttape over fork race" Tvrtko Ursulin
2013-12-03 16:44 ` Tvrtko Ursulin [this message]
2013-12-03 16:59 ` [PATCH 0/2] drmtest: Fix hanging child processes Daniel Vetter

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=1386089095-9966-3-git-send-email-tvrtko.ursulin@linux.intel.com \
    --to=tvrtko.ursulin@linux.intel.com \
    --cc=Intel-gfx@lists.freedesktop.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