Kill a spilled owner whose helper exits still tracing it, and check stale spill requests leave no acknowledgement

This commit is contained in:
Kurt Himebauch committed 2026-12-05 05:38:01 -05:00
1 parent f68a78c577
commit 0cd42b7217
2 files changed
+72 -3

No files matched your search

+14 -1
View File
@@ -47,6 +47,8 @@
#define ZR_PTRACE_DETACH 17
#define ZR_PTRACE_SEIZE 0x4206
#define ZR_PTRACE_INTERRUPT 0x4207
#define ZR_PTRACE_SETOPTIONS 0x4200
#define ZR_PTRACE_O_EXITKILL (1 << 20)
#define ZR_SPILL_CHUNK (8UL << 20)
#define ZR_SPILL_RUNS ((16UL << 20) / sizeof(struct zr_run))
#define ZR_SPILL_TIDS 8192
@@ -762,9 +764,20 @@ static int zr_resumed(void) {
}
// Runs in a process that shares the owner's memory, so it keeps running while every owner thread is stopped.
// Once pages are dropped, the owner must not outlive a helper that ends without detaching: the kernel kills
// every thread still traced when its tracer exits. Only a stopped tracee takes options, which all are here.
static int zr_exit_kill(void) {
for (int i = 0; i < zr_seized; i++) {
if (zr_stopped[i] <= 0) continue;
long r = zr_sys(SYS_ptrace, ZR_PTRACE_SETOPTIONS, zr_tids[i], 0, ZR_PTRACE_O_EXITKILL);
if (r < 0 && r != -ESRCH) return 0;
}
return 1;
}
static void zr_helper(void) {
zr_sys(SYS_prctl, PR_SET_NAME, "zr-spill");
if (!zr_seize_all() || zr_resumed()) {
if (!zr_seize_all() || zr_resumed() || !zr_exit_kill()) {
zr_detach_all();
return;
}
+58 -2
View File
@@ -10,6 +10,7 @@ PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload"
PROGRAM = r"""
#define _GNU_SOURCE
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
@@ -122,6 +123,30 @@ static long wait_for(const char *suffix, pid_t pid, long ms) {
return -3;
}
static pid_t find_helper(pid_t owner) {
DIR *proc = opendir("/proc");
struct dirent *entry;
while (proc && (entry = readdir(proc)) != NULL) {
pid_t pid = (pid_t)atoi(entry->d_name);
if (pid <= 0) continue;
char path[64], text[512];
snprintf(path, sizeof(path), "/proc/%d/stat", pid);
FILE *f = fopen(path, "r");
if (!f) continue;
size_t n = fread(text, 1, sizeof(text) - 1, f);
fclose(f);
text[n] = 0;
char *close = strrchr(text, ')');
int ppid = 0;
if (strstr(text, "(zr-spill)") && close && sscanf(close + 2, "%*c %d", &ppid) == 1 && ppid == owner) {
closedir(proc);
return pid;
}
}
if (proc) closedir(proc);
return -1;
}
int main(int argc, char **argv) {
if (argc > 1 && !strcmp(argv[1], "probe")) {
// whether a child may seize its parent, as the spill helper does (Yama's ptrace_scope 1 refuses)
@@ -185,7 +210,12 @@ int main(int argc, char **argv) {
put(".resume", "1"); // resumed already: this request comes too late
put(".hold", "4 0");
kill(child, SIGURG);
stale_ack = wait_for(".ack.", child, 2000);
for (long waited = 0; waited < 2000 && !stale_ack; waited += 20) {
char path[512];
snprintf(path, sizeof(path), "%s.ack.%d", ctl, child);
stale_ack = access(path, F_OK) == 0; // any acknowledgement, even an empty or cancelled one
nap(20);
}
long at = shared->progress;
nap(500);
stale_ran = shared->progress - at;
@@ -194,6 +224,24 @@ int main(int argc, char **argv) {
put(".hold", stale ? "4 1" : "4 0");
kill(child, SIGURG);
long spilled = wait_for(".ack.", child, 20000);
if (argc > 3 && !strcmp(argv[3], "killhelper")) {
// the helper dies holding pages it dropped: the owner must die with it rather than run without them
pid_t helper = find_helper(child);
if (helper > 0) kill(helper, SIGKILL);
int status = 0, waited = 0;
while (waitpid(child, &status, WNOHANG) == 0 && waited < 5000) {
nap(10);
waited += 10;
}
if (waited >= 5000) {
shared->stop = 1;
kill(child, SIGKILL);
waitpid(child, &status, 0);
}
printf("spilled=%ld helper=%d waited=%d exit=%d\n", spilled, (int)helper, waited,
WIFEXITED(status) ? WEXITSTATUS(status) : 128 + WTERMSIG(status));
return 0;
}
long after = rss_kb(child);
shared->quiet = 0;
long frozen_at = shared->progress;
@@ -275,13 +323,21 @@ class ZramSpillPreloadTest(unittest.TestCase):
def test_a_request_that_arrives_after_the_resume_is_dropped(self):
r = self.run_spill("stale")
self.assertEqual(r["exit"], 0)
self.assertLess(r["stale"], 0) # no answer: nothing was held or written
self.assertEqual(r["stale"], 0) # no acknowledgement of any kind: nothing was held or written
self.assertGreater(r["stale_ran"], 0) # it kept running throughout
# the next request, asked under the current count, is answered by the same handler
self.assertGreaterEqual(r["spilled"], 0)
self.assertGreater(r["resumed"], 0)
self.assertEqual(r["errors"], 0)
def test_an_owner_dies_with_a_helper_that_dies_holding_its_pages(self):
self.need_tracing()
r = self.run_spill("killhelper")
self.assertGreater(r["spilled"], 0)
self.assertGreater(r["helper"], 0)
self.assertLess(r["waited"], 5000)
self.assertEqual(r["exit"], 128 + 9)
def test_a_process_that_cannot_be_traced_falls_back_to_zram(self):
r = self.run_spill("undumpable")
self.assertEqual(r["exit"], 0)