Skip to contentSkip to conversation

linux-user: make process_pending_signals thread-safe

PATCH In review Hamza Mahfooz 2 participants 5 messages May 24, 2021 – May 27, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: make process_pending_signals thread-safe

Use pthread_sigmask instead of sigprocmask inside process_pending_signals to ensure that race conditions aren't possible.

---
 linux-user/signal.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
linux-user/signal.c +1 -2
index 7eecec46c4..81ff753c01 100644
@@ -1005,9 +1005,8 @@ void process_pending_signals(CPUArchState *cpu_env)
     sigset_t *blocked_set;
 
     while (qatomic_read(&ts->signal_pending)) {
-        /* FIXME: This is not threadsafe.  */
         sigfillset(&set);
-        sigprocmask(SIG_SETMASK, &set, 0);
+        pthread_sigmask(SIG_SETMASK, &set, 0);
 
     restart_scan:
         sig = ts->sync_signal.pending;
Signature · 1 line
2.31.1
#1 lore raw
Quoted 14 lines · Hamza Mahfooz

Use pthread_sigmask instead of sigprocmask inside process_pending_signals to ensure that race conditions aren't possible.

---
 linux-user/signal.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
linux-user/signal.c +0 -0
index 7eecec46c4..81ff753c01 100644

linux-user/signal.c void process_pending_signals(CPUArchState *cpu_env) · line 1009

Quoted 7 lines
linux-user/signal.c +1 -2
@@ -1005,9 +1005,8 @@ void process_pending_signals(CPUArchState *cpu_env)
     sigset_t *blocked_set;
 
     while (qatomic_read(&ts->signal_pending)) {
-        /* FIXME: This is not threadsafe.  */
         sigfillset(&set);
-        sigprocmask(SIG_SETMASK, &set, 0);
+        pthread_sigmask(SIG_SETMASK, &set, 0);

We use sigprocmask() in plenty more places than this one in linux-user, so it seems unlikely that the FIXME comment is simply noting that we've used sigprocmask() rather than pthread_sigmask(). Indeed, the comment dates back to before this function called sigprocmask() at all (the sigprocmask() call was added in commit 3d3efba020da which just preserves the FIXME comment.

So I think we cannot remove this FIXME comment like this: we need to more carefully analyze the code/dig through the history to identify what race condition/threadsafety issue the comment is attempting to point out, because it's not "we didn't use pthread_sigmask()".

(As it happens, on Linux/glibc sigprocmask() is implemented as simply calling pthread_sigmask():
https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/unix/sysv/linux/sigprocmask.c;h=9dfd8076d12aff9014fa40f7e93111760a1a8bad;hb=HEAD
If we do want to change from sigprocmask() to pthread_sigmask(), we should be consistent about doing that, not just change this call only.)

thanks
-- PMM

#2 lore raw
Quoted 2 lines · Peter Maydell

If we do want to change from sigprocmask() to pthread_sigmask(), we should be consistent about doing that, not just change this call only.)

On that note, do you think it would worthwhile to have a Coccinelle script replace all instances of sigprocmask with pthread_sigmask (in linux-user, of course)?

#3 lore raw
Quoted 11 lines · Hamza Mahfooz
Quoted 3 lines · Peter Maydell

If we do want to change from sigprocmask() to pthread_sigmask(), we should be consistent about doing that, not just change this call only.)

On that note, do you think it would worthwhile to have a Coccinelle script replace all instances of sigprocmask with pthread_sigmask (in linux-user, of course)?

What issue are we trying to fix by making this change ?

There are only 7 calls so a coccinelle script seems like overkill.

thanks
-- PMM

#4 lore raw
Quoted 1 line · Peter Maydell

What issue are we trying to fix by making this change ?

I suppose that it wouldn't fix any issues in the current state of affairs, maybe it is something to reconsider if glibc ever changes such that, sigprocmask is no longer MT-safe (which is within reason since the MT behavior of sigprocmask is undefined by POSIX).