fix(handoff): fork records matched by the child's start time, never removed

This commit is contained in:
Kurt Himebauch committed 2026-11-28 09:16:28 -05:00
1 parent 7221782940
commit 63f5ac5b5e
1 file changed
+32 -35
+32 -35
View File
@@ -110,18 +110,21 @@ static __thread pid_t t_ddh_only_thread;
// it, in this process's own memory: the child may exec wrappers before it claims an entry, and those
// cannot carry it. One record per pid, at the pid's own index: two live children never share a pid,
// so nothing is ever full or pushed out, and a fork that gets a pid replaces whatever record a gone
// child left there. Mapped when first needed, and only the pages of pids used are ever touched.
// child << 32 | thread in one word: a record is only ever whole. A child not in here (a raw clone())
// is no thread's for a __WNOTHREAD wait.
// child left there; a record is never removed, and the child's start time in it tells it from a
// later child's. Mapped when first needed, and only the pages of pids used are ever touched. One
// word: a record is only ever whole. A child not in here (a raw clone()) is no thread's for a
// __WNOTHREAD wait.
#define DDH_FORKS (1u << 22) /* PID_MAX_LIMIT */
static uint64_t *g_ddh_forks;
// child << 42 | thread << 20 | generation: pids and thread ids are below 2^22 (PID_MAX_LIMIT), and
// the generation (this process's fork count) tells a record from an identical one made after it
static uint32_t g_ddh_fork_generation;
static uint64_t ddh_fork_record(pid_t child, pid_t thread) {
uint64_t generation = __atomic_add_fetch(&g_ddh_fork_generation, 1, __ATOMIC_RELAXED) & 0xfffff;
return (uint64_t)((uint32_t)child & 0x3fffff) << 42 | (uint64_t)((uint32_t)thread & 0x3fffff) << 20 | generation;
// child << 42 | thread << 20 | the child's start time's low 20 bits: pids and thread ids are below
// 2^22 (PID_MAX_LIMIT). The start time tells the record of one child from that of a later one given
// the same pid: a handoff entry holds its forked child's start time (child_start), and a record that
// does not match it is not that child's. 0: not read (any).
static long ddh_start_time(pid_t pid);
static uint64_t ddh_fork_record(pid_t child, pid_t thread, long start) {
uint64_t when = start > 0 ? (uint64_t)start & 0xfffff : 0;
return (uint64_t)((uint32_t)child & 0x3fffff) << 42 | (uint64_t)((uint32_t)thread & 0x3fffff) << 20 | when;
}
static pid_t ddh_record_child(uint64_t r) { return (pid_t)(r >> 42); }
static pid_t ddh_record_thread(uint64_t r) { return (pid_t)((r >> 20) & 0x3fffff); }
@@ -141,28 +144,22 @@ static uint64_t *ddh_forks(int create) {
static void ddh_forked_by(pid_t child, pid_t thread) {
uint64_t *map = ddh_forks(1);
if (map && child > 0) __atomic_store_n(&map[(uint32_t)child % DDH_FORKS], ddh_fork_record(child, thread), __ATOMIC_RELEASE);
if (!map || child <= 0) return;
int saved = errno;
long start = ddh_start_time(child);
errno = saved;
__atomic_store_n(&map[(uint32_t)child % DDH_FORKS], ddh_fork_record(child, thread, start), __ATOMIC_RELEASE);
}
static pid_t ddh_forking_thread(pid_t child) {
// The thread that forked child, by the record of the fork whose child started at child_start (-1:
// any); 0 when there is none.
static pid_t ddh_forking_thread(pid_t child, long child_start) {
uint64_t *map = ddh_forks(0);
uint64_t r = map && child > 0 ? __atomic_load_n(&map[(uint32_t)child % DDH_FORKS], __ATOMIC_ACQUIRE) : 0;
return r && ddh_record_child(r) == child ? ddh_record_thread(r) : 0;
}
// The child's end has been taken: its record goes (unless a new child of the pid has one already).
static void ddh_forget(pid_t child) {
uint64_t *map = ddh_forks(0);
if (!map || child <= 0) return;
uint64_t *slot = &map[(uint32_t)child % DDH_FORKS];
uint64_t seen = __atomic_load_n(slot, __ATOMIC_ACQUIRE);
if (!seen || ddh_record_child(seen) != child) return;
// The child is reaped: a process that has the pid now is a later one, and a fork of ours that
// got it has recorded itself (or will, replacing this): only the record read here goes.
int saved = errno;
int again = syscall(SYS_kill, child, 0) == 0 || errno != ESRCH;
errno = saved;
if (!again) __atomic_compare_exchange_n(slot, &seen, 0, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
if (!r || ddh_record_child(r) != child) return 0;
uint64_t when = r & 0xfffff;
if (when && child_start > 0 && when != ((uint64_t)child_start & 0xfffff)) return 0; // a later child's
return ddh_record_thread(r);
}
// In a forked child: the thread making the table did not come along, so the child may make its own.
@@ -1413,8 +1410,8 @@ static pid_t ddh_first_live_thread(void) {
// The thread a child's exit goes to now: the one that forked it while that lives, then the one the
// kernel moved it to, the group's first live thread. Only an exit /proc confirms moves it: when /proc
// cannot say, the forking thread keeps it.
static pid_t ddh_child_owner(pid_t child) {
pid_t thread = ddh_forking_thread(child);
static pid_t ddh_child_owner(const struct ddh_entry *e) {
pid_t thread = ddh_forking_thread(e->child, (long)e->child_start);
if (thread <= 0 || ddh_thread_alive(thread) != 0) return thread;
pid_t heir = ddh_first_live_thread();
return heir > 0 ? heir : thread;
@@ -1424,7 +1421,7 @@ static pid_t ddh_child_owner(pid_t child) {
static int ddh_selects(const struct ddh_entry *e, pid_t pid) {
if (t_ddh_only_thread) {
// Not this thread's, or not known yet (the fork is still being recorded): not for this wait.
if (ddh_child_owner(e->child) != t_ddh_only_thread) return 0;
if (ddh_child_owner(e) != t_ddh_only_thread) return 0;
}
if (pid > 0) return e->child == pid;
if (pid == -1) return 1;
@@ -1557,7 +1554,6 @@ static int ddh_status(const struct ddh_entry *e) {
static void ddh_release(struct ddh_entry *e, int *status) {
if (status) *status = ddh_status(e);
ddh_forget(e->child);
memset(e, 0, offsetof(struct ddh_entry, state));
e->status = 0;
e->reaped = 0;
@@ -1685,7 +1681,6 @@ static pid_t ddh_wait(pid_t pid, int *status, int options, struct rusage *ru) {
}
// A forked child that died while handing its program off (a signal): its entry is done.
if (!h && (h = ddh_claiming(r))) ddh_release(h, NULL);
if (WIFEXITED(wstatus) || WIFSIGNALED(wstatus)) ddh_forget(r);
if (status) *status = wstatus;
return r;
}
@@ -1802,7 +1797,6 @@ static int ddh_waitid(idtype_t idtype, id_t id, siginfo_t *info, int options) {
// A forked child that died while handing its program off (a signal): its entry is done.
int died = got.si_code == CLD_EXITED || got.si_code == CLD_KILLED || got.si_code == CLD_DUMPED;
if (died && !ddh_find(child) && (h = ddh_claiming(child))) ddh_release(h, NULL);
if (died) ddh_forget(child);
if (info) *info = got;
return 0;
}
@@ -1846,8 +1840,11 @@ static int ddh_app_group(pid_t pgid) {
#define DDH_GROUPS "/run/droiddeck-handoff-groups"
static long ddh_start_time(pid_t pid) {
char path[48], stat[512];
snprintf(path, sizeof(path), "/proc/%d/stat", (int)pid);
char path[64], stat[512];
// its main thread's, which is the process's: an emulator (qemu-user) answers /proc/<own pid>/stat
// itself, with values of its own, but not the task's file, so a process and its parent read the
// same start time
snprintf(path, sizeof(path), "/proc/%d/task/%d/stat", (int)pid, (int)pid);
int fd = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC);
if (fd < 0) return -1;
long n = syscall(SYS_read, fd, stat, sizeof(stat) - 1);