Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,12 @@ $(BUILD_DIR)/test-exec-handoff: tests/test-exec-handoff.c | $(BUILD_DIR)
@echo " CROSS $< (with -lpthread)"
$(Q)$(CROSS_COMPILE)gcc $(CROSS_TEST_CFLAGS) -o $@ $< -lpthread

# test-sigchld-after-wait's late cases include a thread that blocks SIGCHLD,
# and one default case arms a POSIX timer.
$(BUILD_DIR)/test-sigchld-after-wait: tests/test-sigchld-after-wait.c | $(BUILD_DIR)
@echo " CROSS $< (with -lrt -lpthread)"
$(Q)$(CROSS_COMPILE)gcc $(CROSS_TEST_CFLAGS) -o $@ $< -lrt -lpthread

# test-cntvct-thread verifies cloned vCPUs inherit EL0 timer access.
$(BUILD_DIR)/test-cntvct-thread: tests/test-cntvct-thread.c | $(BUILD_DIR)
@echo " CROSS $< (with -lpthread)"
Expand Down
6 changes: 6 additions & 0 deletions docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,11 @@ environment hook, read once and with no effect at all when unset:
| `ELFUSE_DIR_BACKING_FAULT=N` | the union drain fails with `ENOMEM` once it has buffered N names | `test-dir-backing-drain-error` |
| `ELFUSE_DIR_PRIMARY_READ_FAULT=N` | `readdir` on the synthetic stream fails with `EIO` after N entries | `test-dir-primary-read-error` |
| `ELFUSE_DIR_UNION_BACKING_DELAY_US=N` | widens the window between pinning a directory stream and looking its backing up | `test-dir-union-fd-reuse` |
| `ELFUSE_EXIT_SKIP_REGISTRY=1` | sends each exit's SIGCHLD record without the lifecycle registry, as when it cannot be opened | `test-sigchld-exit-fallback` |
| `ELFUSE_FD_IDENTITY_WINDOW_US=N` | widens the window between reading a descriptor's stamp and pinning its host fd | `test-fstatfs-fd-identity` |
| `ELFUSE_FUSE_OPEN_LOCK_DELAY_US=N` | holds a FUSE open inside its session lock after the descriptor is allocated, where a concurrent lookup waits on that lock with `fuse_lock` held | `test-fuse-open-lock` |
| `ELFUSE_SIGCHLD_DECIDE_DELAY_US=N` | holds an exiting child N microseconds between reading its parent's and adopter's flag files and appending its SIGCHLD records, N parsed as for `ELFUSE_SIGNAL_DRAIN_DELAY_US` | `test-sigchld-exit-race` |
| `ELFUSE_SIGNAL_DRAIN_DELAY_US=N` | holds each drain of the signal transport that a SIGUSR2 starts for N microseconds, N being the value's leading decimal number (no effect unless it is from 1 to 999999), as if the sender's SIGUSR2 arrived late | `test-sigchld-late-drain` |
Comment thread
xalestar marked this conversation as resolved.
Resolved
| `ELFUSE_USBDEV_OPEN_FAULT=info\|blob\|pipe` | fails one step of a usbdevfs open: the model lookup or the descriptor copy with `ENOMEM`, the readiness pipe with `ENFILE` | `test-usbdev-faults` |
| `ELFUSE_USBDEV_PUBLISH_DELAY_US=N` | widens the window between `fd_alloc` publishing a usbdevfs fd and the side table binding it, where a close finds no entry | `test-usbdev-faults` |
| `ELFUSE_USBDEV_RETIRE_DELAY_US=N` | widens the window between the side table binding a usbdevfs fd and the open's recheck, where a close can reap the entry and a sibling open can take its slot | `test-usbdev-faults` |
Expand Down Expand Up @@ -681,6 +684,9 @@ The lanes these drive:
| `test-dir-fd-budget-union` | a union directory fd costs one host descriptor, like a plain one |
| `test-fstatfs-fd-identity` | `fstatfs` answers for the descriptor it pinned, not for the fd number |
| `test-fuse-open-lock` | a full FUSE file-slot table reports ENFILE before anything reaches the daemon, every failed open releases what it took, and an open and a lookup on one mount do not deadlock |
| `test-sigchld-late-drain` | a SIGCHLD that the parent discards at the child's exit stays discarded when the parent installs a handler before it drains the record: with nothing before the exit, after an `SA_RESETHAND` run, the exit of a thread that blocked SIGCHLD, the reap of a `CLONE_VM` child that blocked it, and for an adopted child that had already exited; a SIGCHLD kept pending under a handler with SIGCHLD blocked stays dropped when `SIG_IGN` or `SIG_DFL` replaces the handler while the child's record is still undrained, and a SIGCHLD kept pending under `SIG_DFL` with SIGCHLD blocked, which the parent's unblock discards, does not reach a handler installed after a further `SIG_DFL` while the record is still undrained; a SIGCHLD that another process sent with `kill` stays dropped after `SIG_DFL`, and one sent with `kill` under `SIG_DFL` with SIGCHLD unblocked does not reach a handler installed after it (the sender decides it from the flag file, by the rule for a `kill`); a SIGCHLD sent with `kill` to a fork child that has not yet changed its SIGCHLD action or mask, which has no flag file to decide from, stays dropped after the child installs `SIG_IGN` (`rt_sigaction` drains the record first); and a kept SIGCHLD is pending when `waitpid` or `waitid` returns the child (the wait drains the record) |
| `test-sigchld-exit-race` | a SIGCHLD stays dropped when the parent changes the action after the child has read the parent's flag file but before it has appended its record: `SIG_IGN` or `SIG_DFL` over a handler with SIGCHLD blocked, or an unblock and `SIG_DFL` over `SIG_DFL` with SIGCHLD blocked (the record carries the count of drops the child read, and the drain discards it because the `SIG_IGN` or `SIG_DFL` has raised the count since) |
| `test-sigchld-exit-fallback` | a child that exits while its parent ignores SIGCHLD with SIGCHLD blocked, and that sends its record without the lifecycle registry, still has its SIGCHLD discarded: the child decides it from the parent's flag file by the rule for an exit |
| `test-usbdev-faults` | an interface number wider than the table that indexes it, each open-time failure reported as itself, a close inside the fd publish window leaking nothing, and a reap that answers for the description it snapshotted rather than the fd number |
| `test-usbdev-ioctl-departed` | every usbdevfs ioctl this layer names, on a device that has gone: the return, the `errno`, the stamp on the fd that asked and the stamp on a peer fd, against the Linux answer recorded for that request |

Expand Down
29 changes: 28 additions & 1 deletion mk/tests.mk
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ ELFUSE_HOST_NOFILE_MIN ?= $(shell bash "$(CURDIR)/tests/test-config.sh" --host-n
test-usbdev-ioctl-departed \
test-dir-fd-budget-union \
test-dir-backing-drain-error test-dir-union-fd-reuse \
test-fstatfs-fd-identity test-fuse-open-lock \
test-fstatfs-fd-identity test-fuse-open-lock test-sigchld-late-drain \
test-sigchld-exit-race test-sigchld-exit-fallback \
test-dir-union-alias test-dir-primary-read-error \
test-getdents64-small-buf \
test-sysroot-symlink-target \
Expand Down Expand Up @@ -305,6 +306,9 @@ $(call run-lane,test-dir-backing-drain-error,a lost union listing is reported no
$(call run-lane,test-dir-union-fd-reuse,a union walk answers for the directory it pinned)
$(call run-lane,test-fstatfs-fd-identity,fstatfs answers for the descriptor it pinned)
$(call run-lane,test-fuse-open-lock,a FUSE open returns its slot and keeps the lock order)
$(call run-lane,test-sigchld-late-drain,SIGCHLD decided at exit or kill survives a late drain)
$(call run-lane,test-sigchld-exit-race,SIG_IGN or SIG_DFL during an exit drops its SIGCHLD)
$(call run-lane,test-sigchld-exit-fallback,an exit without the registry decides its SIGCHLD)
$(call run-lane,test-dir-union-alias,a dup shares one listing position and one union)
$(call run-lane,test-dir-primary-read-error,a failed synthetic read is reported not ended)
$(call run-lane,test-getdents64-small-buf,a getdents64 buffer too small for one entry)
Expand Down Expand Up @@ -2043,6 +2047,29 @@ test-fuse-open-lock: $(ELFUSE_BIN) $(TEST_DIR)/test-fuse-open-lock
$(ELFUSE_BIN) $(TEST_DIR)/test-fuse-open-lock slots
ELFUSE_FUSE_OPEN_LOCK_DELAY_US=200000 \
$(ELFUSE_BIN) $(TEST_DIR)/test-fuse-open-lock order
## A SIGCHLD decided at the child's exit or at a kill survives a late drain
# ELFUSE_SIGNAL_DRAIN_DELAY_US keeps the record undrained until the test, 100
# ms after the exit or the kill, installs its handler, or SIG_IGN or SIG_DFL,
# or until its wait returns. Without the delay, a build that lacks the part a
# case covers passes that case, except a build that never drops a pending
# SIGCHLD.
test-sigchld-late-drain: $(ELFUSE_BIN) $(TEST_DIR)/test-sigchld-after-wait
ELFUSE_SIGNAL_DRAIN_DELAY_US=250000 \
$(ELFUSE_BIN) $(TEST_DIR)/test-sigchld-after-wait late
## SIG_IGN or SIG_DFL during a child's exit drops the SIGCHLD of that exit
# ELFUSE_SIGCHLD_DECIDE_DELAY_US holds the exiting child between reading the
# parent's flag file and appending its record, so the test's action change,
# 100 ms after the child's last write, lands in between. Without the delay, a
# build whose drain ignores the drop count in the record passes.
test-sigchld-exit-race: $(ELFUSE_BIN) $(TEST_DIR)/test-sigchld-after-wait
ELFUSE_SIGCHLD_DECIDE_DELAY_US=300000 \
$(ELFUSE_BIN) $(TEST_DIR)/test-sigchld-after-wait exiting
## An exit that cannot take the lifecycle registry decides its SIGCHLD too
# ELFUSE_EXIT_SKIP_REGISTRY sends each exit's record without the registry, as
# when it cannot be opened.
test-sigchld-exit-fallback: $(ELFUSE_BIN) $(TEST_DIR)/test-sigchld-after-wait
ELFUSE_EXIT_SKIP_REGISTRY=1 \
$(ELFUSE_BIN) $(TEST_DIR)/test-sigchld-after-wait fallback
## A union directory fd must cost one host descriptor, like a plain one
# The measurement only means something with the host limit at exactly what
# elfuse asks for -- the same squeeze tests/manifest.txt puts on
Expand Down
6 changes: 3 additions & 3 deletions scripts/check-eintr-contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,14 @@
"restartable",
"The queue is empty by the loop condition; no request is dequeued.",
),
"syscall/proc.c::sys_wait4": (
"syscall/proc.c::wait4_once": (
"restartable",
"Every reapable outcome returns earlier; no child is removed and no "
"status or rusage is written.",
),
"syscall/proc.c::sys_waitid": (
"syscall/proc.c::waitid_once": (
"restartable",
"Same shape as sys_wait4: the reap and WNOHANG answers precede this.",
"Same shape as wait4_once: the reap and WNOHANG answers precede this.",
),
"runtime/futex.c::futex_lock_pi_inner": (
"restartable",
Expand Down
14 changes: 7 additions & 7 deletions src/runtime/forkipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2153,13 +2153,13 @@ int64_t sys_clone(hv_vcpu_t vcpu,
* barrier, so without the lock its munmap could rewrite the region table
* during the overlay sync, the clone snapshot, the header, or the region
* copy, or close a backing fd before it is sent. Everything taken beneath
* it (fd_lock, pty_keepalive_lock, elf_path_lock, sysroot_lock, sig_lock)
* follows mmap_lock in the order in syscall/internal.h. The IPC writes
* under it cannot deadlock: the child only reads until admission, and a
* child that dies turns the next write into EPIPE (SO_NOSIGPIPE above). The
* cost lands on a straggler waiting in pthread_mutex_lock for the whole
* send, where no teardown wake reaches it, so an exit_group join during a
* slow fork can time out on it.
* it (fd_lock, pty_keepalive_lock, elf_path_lock, sysroot_lock, sig_lock,
* sigchld_pub_lock) follows mmap_lock in the order in syscall/internal.h.
* The IPC writes under it cannot deadlock: the child only reads until
* admission, and a child that dies turns the next write into EPIPE
* (SO_NOSIGPIPE above). The cost lands on a straggler waiting in
* pthread_mutex_lock for the whole send, where no teardown wake reaches it,
* so an exit_group join during a slow fork can time out on it.
*
* The prepare call below converts MAP_SHARED|MAP_ANONYMOUS regions with no
* backing fd into memfd-backed overlays that the child re-installs from the
Expand Down
18 changes: 18 additions & 0 deletions src/runtime/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,9 @@ void thread_deactivate(thread_entry_t *t)
* thread_lock to honor sig_lock (4) before thread_lock (5).
*/
signal_refresh_pending_hint();

/* This thread's mask no longer counts toward SIGCHLD being kept. */
signal_sigchld_republish();
}

thread_entry_t *thread_find(int64_t tid)
Expand Down Expand Up @@ -885,6 +888,16 @@ bool thread_signal_deliverable(uint64_t sigbit)
return false;
}

bool thread_signal_blocked_any(uint64_t sigbit)
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
{
/* Lock-free scan, on the same terms as thread_signal_deliverable above. */
THREAD_FOR_EACH_ACTIVE_RELAXED (t) {
if (sigbit & thread_blocked_load(t))
return true;
}
return false;
}

void thread_kick_futex_waiters(uint64_t shared_pending)
{
/* Lock-free scan, on the same terms as thread_signal_deliverable above. A
Expand Down Expand Up @@ -1844,6 +1857,11 @@ int64_t thread_ptrace_wait(int64_t tracer_tid,
* before thread_lock (5).
*/
signal_refresh_pending_hint();

/* The reaped child's mask no longer counts toward SIGCHLD being
* kept.
*/
signal_sigchld_republish();
return tid;
}
}
Expand Down
23 changes: 14 additions & 9 deletions src/runtime/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@
* signal API without an include cycle.
*/
typedef struct thread_entry {
/* Read without thread_lock. Four scans walk the table lock-free
/* Read without thread_lock. Five scans walk the table lock-free
* (thread_pending_union, thread_tid_alive, thread_signal_deliverable,
* thread_kick_futex_waiters): each loads active, then one or two of
* blocked, guest_tid, and tpending.pending, with nothing in between. A
* scanner that saw active as 1 just before this slot went inactive can
* still be loading one of those when thread_alloc recycles the slot, so
* none of them may be plain-written there. They are grouped here, cleared
* with atomic stores, and deliberately excluded from the memset that clears
* the rest. Keep them ahead of the rest of the struct; thread.c asserts
* that each one sits below where its memset starts.
* thread_signal_blocked_any, thread_kick_futex_waiters): each loads active,
* then one or two of blocked, guest_tid, and tpending.pending, with nothing
* in between. A scanner that saw active as 1 just before this slot went
* inactive can still be loading one of those when thread_alloc recycles the
* slot, so none of them may be plain-written there. They are grouped here,
* cleared with atomic stores, and deliberately excluded from the memset
* that clears the rest. Keep them ahead of the rest of the struct; thread.c
* asserts that each one sits below where its memset starts.
*/
_Atomic int active; /* Non-zero while thread is running. Stays int (not
* bool) because lock-free paths in thread.c use
Expand Down Expand Up @@ -490,6 +490,11 @@ void thread_wake_exit_waiters(void);
*/
bool thread_signal_deliverable(uint64_t sigbit);

/* Check if any active thread blocks sigbit. Same lock-free scan as
* thread_signal_deliverable. Does NOT acquire thread_lock.
*/
bool thread_signal_blocked_any(uint64_t sigbit);

/* Kick every other thread that has a pending signal it does not block, so one
* parked in a futex wait re-checks now rather than when its polling quantum
* ends. @shared_pending is the process-directed pending set. Lock-free scan;
Expand Down
32 changes: 28 additions & 4 deletions src/syscall/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,43 @@
* the slot under mmap_lock deadlocks
* against the leader, which needs that lock
* to run the request that frees the slot
* sig_drain_lock (syscall/proc.c): serializes signal transport drains;
* taken with no lock held; holds the
* transport flock and sig_lock beneath it
* while it reads and queues records, then,
* after releasing the transport flock, the
* lifecycle registry lock, autoreap_lock
* and pid_lock for the work they ask for
* autoreap_lock (syscall/proc.c): serializes the whole no-zombie reap
* sequence against rt_sigaction; holds
* the lifecycle registry lock, pid_lock
* and, through proc_pidfd_notify_exit,
* pidfd_lock
* lifecycle registry lock (syscall/proc.c): a flock; holds pid_lock
* beneath it through
* lifecycle_import_children.
* proc_process_exit holds it while it
* appends to the parent's and the
* adopter's transport, so a transport
* flock, and the sig_lock that a drain
* takes under that flock, nest beneath
* it; no path takes it while holding a
* transport flock or sig_lock
* sig_lock (syscall/signal.c): signal handlers/pending/blocked
* thread_lock (runtime/thread.c): thread table
* sfd_lock (syscall/fd.c): special fd (never held with thread_lock)
* autoreap_lock (syscall/proc.c): serializes the whole no-zombie reap
* sequence against rt_sigaction; holds
* pid_lock and, through
* proc_pidfd_notify_exit, pidfd_lock
* elf_path_lock (syscall/proc-state.c): cached /proc/self/exe path; holds
* cwd_lock beneath it through
* proc_get_cwd, and sysroot_lock through
* the proc_cwd_refresh that runs with
* cwd_lock dropped
* pid_lock (syscall/proc.c): process table / wait state
* sigchld_pub_lock (syscall/signal.c): publishes the sigchld flag file
* word; taken under sig_lock, under
* mmap_lock by sys_clone's
* proc_registry_publish_self, or alone;
* holds only log_mutex beneath it, through
* the flag file's log_warn
* pidfd_lock (syscall/proc-pidfd.c): pidfd registry
* futex bucket (runtime/futex.c): per-bucket, index-ordered if >1. Also
* taken alone by futex_kick, beneath
Expand Down
2 changes: 1 addition & 1 deletion src/syscall/proc-pidfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ int64_t sys_pidfd_send_signal(guest_t *g,

if (pid == proc_get_pid()) {
if (sig != 0)
signal_queue(sig);
signal_queue_kill(sig);
return 0;
}

Expand Down
Loading
Loading