fix: suspend page-out waits for each handler's ack and can be cancelled

This commit is contained in:
xXJsonDeruloXx committed 2026-10-22 10:01:49 -04:00
1 parent 240b7fcce6
commit 1b14939731
5 files changed
+130 -24

No files matched your search

+21
View File
@@ -126,6 +126,25 @@ static void zr_walk(int compress) {
zr_sys(SYS_close, maps);
}
// Before it stops a session for suspend, the app waits for <ZR_CTL>.ack.<pid> to appear (the handler
// runs here, not one a program installed over it) and then to hold a byte (the page-out finished).
static void zr_ack(int done) {
static const char suffix[] = ".ack.";
char path[sizeof(ZR_CTL) + sizeof(suffix) + 20];
char digits[20];
size_t n = 0, d = 0;
for (const char *p = ZR_CTL; *p; p++) path[n++] = *p;
for (const char *p = suffix; *p; p++) path[n++] = *p;
long pid = zr_sys(SYS_getpid);
do digits[d++] = (char)('0' + pid % 10); while ((pid /= 10) > 0);
while (d > 0) path[n++] = digits[--d];
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;
if (done) zr_sys(SYS_write, fd, "1", 1UL);
zr_sys(SYS_close, fd);
}
static void zr_handler(int sig, siginfo_t *info, void *context) {
(void)sig;
(void)context;
@@ -136,7 +155,9 @@ static void zr_handler(int sig, siginfo_t *info, void *context) {
&& !__atomic_exchange_n(&zr_busy, 1, __ATOMIC_SEQ_CST)) {
__atomic_store_n(&zr_pending, 0, __ATOMIC_SEQ_CST);
int mode = zr_mode();
if (mode == 1) zr_ack(0);
if (mode >= 0) zr_walk(mode);
if (mode == 1) zr_ack(1);
__atomic_store_n(&zr_busy, 0, __ATOMIC_SEQ_CST);
}
errno = saved;
+6 -2
View File
@@ -129,6 +129,8 @@ class ZramPreloadTest(unittest.TestCase):
def setUp(self):
self.ctl.write_text("0")
for ack in self.dir.glob("ctl.ack.*"):
ack.unlink()
def run_program(self, program=None, mb=48, cycles=1, *extra):
environment = dict(os.environ, LD_PRELOAD=str(self.lib))
@@ -140,9 +142,10 @@ class ZramPreloadTest(unittest.TestCase):
out.setdefault(key, []).append(int(value))
return out
def test_only_the_client_takes_the_signal(self):
def test_every_process_takes_the_signal(self):
self.assertEqual([1], self.run_program()["handler"])
self.assertEqual([0], self.run_program(self.game)["handler"])
self.assertEqual([1], self.run_program(self.game)["handler"])
self.assertEqual(2, len(list(self.dir.glob("ctl.ack.*"))))
def test_nothing_is_installed_without_the_control_file(self):
self.ctl.unlink()
@@ -157,6 +160,7 @@ class ZramPreloadTest(unittest.TestCase):
self.assertGreater(out["out"][0], 32 * 1024)
self.assertLess(out["back"][0], out["out"][0] // 4)
self.assertEqual([1], out["intact"])
self.assertEqual(["1"], [ack.read_text() for ack in self.dir.glob("ctl.ack.*")])
@unittest.skipUnless(swap_total_kb() > 256 * 1024, "needs swap")
def test_reads_back_what_the_kernel_swapped_elsewhere(self):