Keep intercepted signals, discard checks and owner exit in the spill helper, and answer a cancelled spill

This commit is contained in:
Kurt Himebauch committed 2026-12-05 05:16:03 -05:00
1 parent c6333350ae
commit 6638a7dac8
2 files changed
+72 -18

No files matched your search

+50 -10
View File
@@ -42,6 +42,7 @@
#define ZR_INLINE_KB (32UL << 10)
#define ZR_INLINE_CHUNKS 512UL
#define ZR_FAILED (~0UL)
#define ZR_CANCELLED (-2L)
#define ZR_FUTEX_WAIT 0
#define ZR_PTRACE_DETACH 17
#define ZR_PTRACE_SEIZE 0x4206
@@ -226,8 +227,11 @@ static long zr_reaper;
static int zr_seized;
static int zr_tids[ZR_SPILL_TIDS];
static signed char zr_stopped[ZR_SPILL_TIDS];
static unsigned char zr_signals[ZR_SPILL_TIDS];
static char *zr_helper_stack;
static int zr_helper_done;
static int zr_helper_discarded;
static int zr_helper_released;
static unsigned long zr_helper_spilled;
static unsigned long zr_baseline; // the resume count the spill was asked under
@@ -467,8 +471,13 @@ static void zr_ack(long value, const char *suffix) {
path[n] = 0;
int fd = (int)zr_sys(SYS_openat, AT_FDCWD, path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
if (fd < 0) return;
n = 0;
if (value < -1) {
path[n++] = '-';
value = -value;
}
for (d = 0; value >= 0 && (d == 0 || value > 0); value /= 10) digits[d++] = (char)('0' + value % 10);
for (n = 0; d > 0;) path[n++] = digits[--d];
while (d > 0) path[n++] = digits[--d];
if (n > 0) zr_sys(SYS_write, fd, path, n);
zr_sys(SYS_close, fd);
}
@@ -536,6 +545,7 @@ static long zr_seize_new(void) {
break;
}
zr_tids[zr_seized] = (int)tid;
zr_signals[zr_seized] = 0;
zr_stopped[zr_seized++] = 0;
zr_sys(SYS_ptrace, ZR_PTRACE_INTERRUPT, tid, 0, 0);
added++;
@@ -548,7 +558,7 @@ static long zr_seize_new(void) {
static void zr_detach_all(void) {
for (int i = 0; i < zr_seized; i++)
if (zr_stopped[i] >= 0) zr_sys(SYS_ptrace, ZR_PTRACE_DETACH, zr_tids[i], 0, 0);
if (zr_stopped[i] >= 0) zr_sys(SYS_ptrace, ZR_PTRACE_DETACH, zr_tids[i], 0, zr_signals[i]);
zr_seized = 0;
}
@@ -566,7 +576,11 @@ static int zr_seize_all(void) {
long tid;
while ((tid = zr_sys(SYS_wait4, -1, &status, __WALL | WNOHANG, NULL)) > 0) {
int i = zr_seized_index(tid);
if (i >= 0) zr_stopped[i] = WIFSTOPPED(status) ? 1 : -1;
if (i < 0) continue;
zr_stopped[i] = WIFSTOPPED(status) ? 1 : -1;
// A signal that reached the thread before the interrupt stops it in that signal's delivery, which a
// detach without the signal would drop.
if (WIFSTOPPED(status) && (status >> 16) == 0) zr_signals[i] = (unsigned char)WSTOPSIG(status);
}
for (int i = 0; i < zr_seized; i++) waiting += zr_stopped[i] == 0;
if (waiting == 0) break;
@@ -670,10 +684,12 @@ static unsigned long zr_spill_range(int pagemap, int fd, unsigned long start, un
*full = 1;
break;
}
zr_sys(SYS_madvise, from, len, MADV_DONTNEED);
zr_runs[zr_run_count++] = (struct zr_run){from, len, *off, (unsigned long)writable};
*off += len;
spilled += len;
zr_helper_discarded = 1;
if (zr_sys(SYS_madvise, from, len, MADV_DONTNEED) == 0) {
zr_runs[zr_run_count++] = (struct zr_run){from, len, *off, (unsigned long)writable};
*off += len;
spilled += len;
}
i = j;
}
addr += pages * zr_page;
@@ -728,7 +744,10 @@ static int zr_spill_file(void) {
while (d > 0) path[n++] = digits[--d];
path[n] = 0;
int fd = (int)zr_sys(SYS_openat, AT_FDCWD, path, O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC | O_DIRECT, 0600);
if (fd >= 0) zr_sys(SYS_unlinkat, AT_FDCWD, path, 0);
if (fd >= 0 && zr_sys(SYS_unlinkat, AT_FDCWD, path, 0) < 0) {
zr_sys(SYS_close, fd);
return -1;
}
return fd;
}
@@ -762,7 +781,14 @@ static void zr_helper(void) {
// nothing written (a full volume): the owner pages out to zram instead and acknowledges that
if (spilled > 0) {
zr_ack((long)(spilled / 1024), ".ack.");
while (!zr_resumed()) zr_nap(&idle, 0, ZR_RESUME_POLL_MS);
while (!zr_resumed()) {
// reparented: the owner is gone and there is nothing to read back into
if (zr_sys(SYS_getppid) != zr_owner) {
zr_sys(SYS_close, fd);
return;
}
zr_nap(&idle, 0, ZR_RESUME_POLL_MS);
}
long start = zr_now_ms();
char path[48] = "/proc/";
size_t n = 6 + zr_digits(path + 6, zr_owner);
@@ -777,8 +803,14 @@ static void zr_helper(void) {
}
}
if (mem >= 0) zr_sys(SYS_close, mem);
zr_sys(SYS_close, fd);
zr_helper_released = restored;
zr_detach_all();
// after the detach, so a controller that sees the marker finds the process out of its tracing stop
if (restored) zr_ack(zr_now_ms() - start, ".restored.");
return;
}
zr_helper_released = 1;
zr_sys(SYS_close, fd);
zr_detach_all();
}
@@ -798,6 +830,8 @@ static int zr_spill(void) {
zr_baseline = zr_request_arg;
zr_helper_done = 0;
zr_helper_spilled = 0;
zr_helper_discarded = 0;
zr_helper_released = 0;
zr_reaper = zr_sys(SYS_gettid);
__atomic_store_n(&zr_owner, zr_sys(SYS_getpid), __ATOMIC_RELEASE);
long child = zr_clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SYSVSEM, zr_helper_stack + ZR_STACK, NULL, NULL, NULL);
@@ -805,6 +839,8 @@ static int zr_spill(void) {
int status;
while (zr_sys(SYS_wait4, child, &status, __WALL, NULL) == -EINTR) {
}
// A helper that ended between dropping pages and reading them back took them with it.
if (zr_helper_discarded && !zr_helper_released) zr_sys(SYS_kill, zr_sys(SYS_getpid), SIGKILL);
}
__atomic_store_n(&zr_owner, 0, __ATOMIC_RELEASE);
return child > 0 && zr_helper_done && zr_helper_spilled > 0;
@@ -825,7 +861,11 @@ static void zr_request(int worker) {
zr_ack(-1, ".ack.");
#if defined(__aarch64__)
if (worker && zr_spill()) return;
if (zr_resumed()) return;
// it acknowledged the start, so the controller waits for a final answer
if (zr_resumed()) {
zr_ack(ZR_CANCELLED, ".ack.");
return;
}
#endif
zr_visits = worker ? ZR_FAILED : ZR_INLINE_CHUNKS;
zr_walk(1, 1, 0, 0);
+22 -8
View File
@@ -180,16 +180,26 @@ int main(int argc, char **argv) {
}
}
int stale = argc > 3 && !strcmp(argv[3], "stale");
if (stale) put(".resume", "1"); // resumed already: the request below comes too late
put(".hold", "4 0");
long stale_ack = 0, stale_ran = 0;
if (stale) {
put(".resume", "1"); // resumed already: this request comes too late
put(".hold", "4 0");
kill(child, SIGURG);
stale_ack = wait_for(".ack.", child, 2000);
long at = shared->progress;
nap(500);
stale_ran = shared->progress - at;
}
// a request asked under the current count, which the same handler answers
put(".hold", stale ? "4 1" : "4 0");
kill(child, SIGURG);
long spilled = wait_for(".ack.", child, stale ? 2000 : 20000);
long spilled = wait_for(".ack.", child, 20000);
long after = rss_kb(child);
shared->quiet = 0;
long frozen_at = shared->progress;
nap(500);
long frozen_now = shared->progress;
put(".resume", "1");
put(".resume", stale ? "2" : "1");
long restored = spilled > 0 ? wait_for(".restored.", child, 20000) : 0;
for (int waited = 0; shared->progress == frozen_now && waited < 10000; waited += 10) nap(10);
long resumed = shared->progress;
@@ -197,9 +207,10 @@ int main(int argc, char **argv) {
shared->stop = 1;
int status = 0;
waitpid(child, &status, 0);
printf("before=%ld after=%ld back=%ld spilled=%ld restored=%ld frozen=%ld resumed=%ld errors=%ld exit=%d\n",
printf("before=%ld after=%ld back=%ld spilled=%ld restored=%ld frozen=%ld resumed=%ld errors=%ld exit=%d "
"stale=%ld stale_ran=%ld\n",
before, after, back, spilled, restored, frozen_now - frozen_at, resumed - frozen_now, shared->errors,
WIFEXITED(status) ? WEXITSTATUS(status) : 128 + WTERMSIG(status));
WIFEXITED(status) ? WEXITSTATUS(status) : 128 + WTERMSIG(status), stale_ack, stale_ran);
return 0;
}
"""
@@ -264,8 +275,11 @@ 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["spilled"], 0) # no answer: nothing was held or written
self.assertGreater(r["frozen"], 0) # it kept running throughout
self.assertLess(r["stale"], 0) # no answer: 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_a_process_that_cannot_be_traced_falls_back_to_zram(self):