From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796344390; b=5GfgY5pSp42tebVfM77xRqOXLvqMC25ZiqFv8SA4EP8nZoNQHJd4EX7YbFRPzs8Epkec 8tINGoNwdwAHQo3QA98aFsqblH1VKEXJo7ISpjCDwvCPdXtyclIAYgixFAYmCk7K9biou dTo5cDCo3i+j2u/IDA1rZ4JmnNXs9Y37Sc79QnOJ8jTJ/qJeM5sWxTgWcHHy4axuwi5L1 K/J6eNOXF4ZRByYLXQulFVXW9Hl+8ZJJ5yPa+87ZTJ4If6Mck/HzLQo9/CStKuTSYBAYi dNAaWwQEQC/m2fX/J72pHmRBIiSzXpU+2xnQxHiVqRiZDXS5od5k4pyzi3TUYFXvEXw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796344390; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Developer-Key:X-Developer-Signature: X-Mailer:Message-ID:Subject:From:To:Cc:Content-Type; bh=1alnwo6KniN+LFyeSCZs4LvDUiF4QcLbIT5VKZkRiNU=; b=pWEHGHbd7vR+zLgsNBgfAAgMbcRWMFZaiBIDnD59yMLAwrVXPEoptEyeeJ1X3AbupnjX YDF/o9Cf6szfvbOKVcnkT0yGH2lowrGAAX5ogbR8mfHmt577RsQPNfjMx5DYQ7E92MLA3 9CSTcoeNPpyrDgqBb+RsFyG0ehpqkMPqn7ytNwFsTdYoKT77eoCFAPErVZo9WA9/TAsYl VmLdp2N4PAXaHRAwvl4/lmtnoJPrEaUs3I9QHi8vEBkXZzSeQN9llnB0SGreDJac19ExZ HyQKTlRkeddsTiX7kWQKRAI08f4X7SSQWMAsxDe4/63vK0rfdpyRYTBOApHPCH8WeHA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--grahalt.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=m2sZXfB4; arc=none smtp.remote-ip=209.85.215.197 Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.197]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C19B8606A6BD for ; Fri, 4 Dec 2026 00:33:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C19B8606A6BD Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--grahalt.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=m2sZXfB4 Received: by mail-pg1-f197.google.com with SMTP id 41be03b00d2f7-cd5fd34fee0so253615a12.3 for ; Thu, 03 Dec 2026 16:33:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796344389; x=1796949189; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=1alnwo6KniN+LFyeSCZs4LvDUiF4QcLbIT5VKZkRiNU=; b=m2sZXfB4QqiQqGBczm/Xtuqxe4xtCrK1mT7JYnfxSexYhtpNGxdyLpcwdRF3DOSAtv m2D3diyd10O44ktedqD1d0utrpFFdmrPLXriF+xAVDGigxQfm3p6CpE11VCjPPUO1Eij tyx+1ugQWm5TpB+xKDOzugVzVnq3TBVh7QfDgJmwlxbQuAwc+IpT5DUDZI7hvmm766oh YL8LWmI7quQth+od/tkSrFc2f3y5GaPdzC8RBDbRVi5TCK30JMaKKYF/EZI12ut1rDQB 7yGVhKsaE8bWmt0jejkCF1yGNvneILlrciXoKXOBc/HS6mkhq34AWlqLi13pmPB6yxCq b1ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796344389; x=1796949189; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=1alnwo6KniN+LFyeSCZs4LvDUiF4QcLbIT5VKZkRiNU=; b=EASGgbsICz3urp986hy0hX8lCiwa989yrU7qKi9O9Uw8GSQpFJopwdWLqdLtrEQswj 9Zh+0ElJ/xk9i3qGhuKNZI5OPcKYdy9yosrUJF13oB4+NVfQXLXqhT+W5MQs7zWvZsuw dSXomQ5BC4s162qz055OVdIwnDMURmnRMYyhAmWaJoMo2TGYNwMS9+Klp7KvOSvLLOSm Gx2sVWxbht18JbED0OXuFtIncaYP6i3OB7fgXd0pe/Prrjw5RpdGPejCYNqe+IyeKq7L tA7QH/+nFetIwzst3ndKWXblRs+13lbOfvGQ/OH1S9w2bjKrdFg3hJ/HZ6/z49nt5t5m MiGg== X-Forwarded-Encrypted: i=1; AL+hErFlDtONLmkFfKqZ2pHa8H0JC6TPIhsu+sJScfh7nrZqAPvN8GNZxAd8GzAztuwQesGlIqq6+7LpZo69RDM=@vger.kernel.org X-Gm-Message-State: APuU4Z+IdhBsvDhCv7wpyoS4Cjj0uCfTwHFsADRIkxnnH94tIHa5Tr/a rbEG9z+PAWSCPWbP195jVC0TRLDsp5JW6sZqt6RiIzd3bWXUEpj8QN+//q9Dvf08yoJtbUVCmcy hrtHanbI6yg== X-Received: from plhz24.prod.google.com ([2002:a17:902:d9d8:b0:2eb:337a:cac]) (user=grahalt job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1111:b0:2f0:ea76:1a19 with SMTP id d9443c01a7336-2f1ba203259mr2167315ad.54.1796344388658; Thu, 03 Dec 2026 16:33:08 -0800 (PST) Date: Fri, 04 Dec 2026 00:32:49 +0000 In-Reply-To: <20261204-rust-binder-thread-spindown-v1-0-8702c42d4437@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261204-rust-binder-thread-spindown-v1-0-8702c42d4437@google.com> X-Developer-Key: i=grahalt@google.com; a=ed25519; pk=Fdl8UB2iAFT3+RcdOulskhAYycOow7vcBsXiopOEuZQ= X-Developer-Signature: v=1; a=ed25519-sha256; t=1796344384; l=16308; i=grahalt@google.com; s=20261202; h=from:subject:message-id; bh=dImEYZM0pmdpezR+LTKqE4s1zjHlOIQMAM2uod2wi5w=; b=bBXAlevL5LbLwAfjyPPwri6o9MWr9pItyVMbdJNvlcqM4cBaNAkH2z2r9gF80bPi6s6Lxc6yu DH1uHkwzNEzDnfoQKaXQVzHeOYfZb/OA8UF3yJBvLSYQix0xErXLwgN X-Mailer: b4 0.14.3 Message-ID: <20261204-rust-binder-thread-spindown-v1-4-8702c42d4437@google.com> Subject: [PATCH 4/4] selftests/binderfs: add idle timeout tests From: Nicholas Giunta To: Greg Kroah-Hartman , Carlos Llamas , Alice Ryhl , "=?utf-8?q?Arve_Hj=C3=B8nnev=C3=A5g?=" , Todd Kjos , Christian Brauner , Boqun Feng , Gary Guo , Miguel Ojeda , Shuah Khan Cc: Lyude Paul , Daniel Almeida , "=?utf-8?q?Onur_=C3=96zkan?=" , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Trevor Gross , Danilo Krummrich , Tamir Duberstein , Alexandre Courbot , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-api@vger.kernel.org, Nicholas Giunta Content-Type: text/plain; charset="utf-8" Add kselftest coverage under FIXTURE(binderfs_idle) for Rust binder's per-process idle thread timeout support (BINDER_SET_IDLE_TIMEOUT and BR_FINISHED): - ioctl_and_default_disabled: verify a fresh binder fd defaults to timeout=0 (disabled), validate BINDER_SET_IDLE_TIMEOUT argument bounds (-1 and INT64_MIN return EINVAL; INT64_MAX, 100, and 0 succeed), and verify resetting 100 -> 0 disables spin-down at runtime. - spindown_and_respawn: verify BC_ENTER_LOOPER threads remain immune even with a peer in ready_threads, BC_REGISTER_LOOPER threads spin down with BR_FINISHED (without BR_SPAWN_LOOPER), and BINDER_THREAD_EXIT decrements started_thread_count so subsequent cycles spawn fresh looper threads. - sole_ready_thread: verify the last remaining thread in ready_threads defers its idle timeout until a peer joins (then exits with BR_FINISHED), delivers BR_TRANSACTION rather than BR_FINISHED when woken by incoming work, and stays alive when BINDER_SET_IDLE_TIMEOUT(0) disables spin-down before a peer joins. Probe BINDER_SET_IDLE_TIMEOUT(0) on a throwaway fd during fixture setup so the tests skip cleanly on kernels without idle timeout support. Assisted-by: Gemini Signed-off-by: Nicholas Giunta --- .../selftests/filesystems/binderfs/binderfs_test.c | 487 +++++++++++++++++++++ 1 file changed, 487 insertions(+) diff --git a/tools/testing/selftests/filesystems/binderfs/binderfs_test.c b/tools/testing/selftests/filesystems/binderfs/binderfs_test.c index 7059bc9d25bd..39510117e395 100644 --- a/tools/testing/selftests/filesystems/binderfs/binderfs_test.c +++ b/tools/testing/selftests/filesystems/binderfs/binderfs_test.c @@ -6,13 +6,16 @@ #include #include #include +#include #include #include #include #include #include +#include #include #include +#include #include #include #include @@ -431,4 +434,488 @@ TEST(binderfs_test_unprivileged) __do_binderfs_test(_metadata); } +#define BINDER_MMAP_SIZE (128 * 1024) + +enum { + BR_SEEN_SPAWN = 1, + BR_SEEN_FINISHED = 2, + BR_SEEN_TXN = 4, + BR_SEEN_COMPLETE = 8, +}; + +struct looper_worker { + int fd; + __u32 cmd; + int tid; + int done; + int seen; +}; + +static int binder_wr(int fd, const void *wbuf, size_t wlen, + void *rbuf, size_t rlen) +{ + struct binder_write_read bwr = { + .write_buffer = (binder_uintptr_t)(uintptr_t)wbuf, + .write_size = wlen, + .read_buffer = (binder_uintptr_t)(uintptr_t)rbuf, + .read_size = rlen, + }; + int ret; + + do { + ret = ioctl(fd, BINDER_WRITE_READ, &bwr); + } while (ret < 0 && errno == EINTR); + + return ret < 0 ? -1 : (int)bwr.read_consumed; +} + +static int binder_parse_read(const __u8 *buf, int len, + binder_uintptr_t *txn_buf) +{ + size_t off = 0; + int seen = 0; + + if (len < 0) + return -1; + + while (off + sizeof(__u32) <= (size_t)len) { + __u32 cmd; + + memcpy(&cmd, buf + off, sizeof(cmd)); + off += sizeof(cmd); + if (cmd == BR_NOOP) { + continue; + } else if (cmd == BR_TRANSACTION_COMPLETE) { + seen |= BR_SEEN_COMPLETE; + } else if (cmd == BR_SPAWN_LOOPER) { + seen |= BR_SEEN_SPAWN; + } else if (cmd == BR_FINISHED) { + seen |= BR_SEEN_FINISHED; + } else if (cmd == BR_TRANSACTION) { + struct binder_transaction_data tr; + + if (off + sizeof(tr) > (size_t)len) + return -1; + memcpy(&tr, buf + off, sizeof(tr)); + off += sizeof(tr); + if (txn_buf) + *txn_buf = tr.data.ptr.buffer; + seen |= BR_SEEN_TXN; + } else { + return -1; + } + } + + return off == (size_t)len ? seen : -1; +} + +static int binder_free_buffer(int fd, binder_uintptr_t ptr) +{ + __u32 cmd = BC_FREE_BUFFER; + __u8 buf[sizeof(cmd) + sizeof(ptr)]; + + memcpy(buf, &cmd, sizeof(cmd)); + memcpy(buf + sizeof(cmd), &ptr, sizeof(ptr)); + return binder_wr(fd, buf, sizeof(buf), NULL, 0); +} + +static int binder_send_oneway(int cfd) +{ + __u32 cmd = BC_TRANSACTION; + struct binder_transaction_data txn = { .flags = TF_ONE_WAY }; + __u8 wbuf[sizeof(cmd) + sizeof(txn)], rbuf[128]; + int n, seen; + + memcpy(wbuf, &cmd, sizeof(cmd)); + memcpy(wbuf + sizeof(cmd), &txn, sizeof(txn)); + n = binder_wr(cfd, wbuf, sizeof(wbuf), rbuf, sizeof(rbuf)); + seen = binder_parse_read(rbuf, n, NULL); + return (seen > 0 && (seen & BR_SEEN_COMPLETE)) ? 0 : -1; +} + +static int binder_trigger_spawn(int sfd, int cfd) +{ + __u8 rbuf[512]; + binder_uintptr_t txn_buf = 0; + int n, seen; + + if (binder_send_oneway(cfd) < 0) + return -1; + n = binder_wr(sfd, NULL, 0, rbuf, sizeof(rbuf)); + seen = binder_parse_read(rbuf, n, &txn_buf); + if (seen < 0 || !(seen & BR_SEEN_SPAWN)) + return -1; + if ((seen & BR_SEEN_TXN) && binder_free_buffer(sfd, txn_buf) < 0) + return -1; + return 0; +} + +static void *looper_worker_fn(void *arg) +{ + struct looper_worker *w = arg; + __u8 rbuf[512]; + binder_uintptr_t txn_buf = 0; + __u32 exit_cmd = BC_EXIT_LOOPER; + __s32 zero = 0; + int n, seen; + + __atomic_store_n(&w->tid, (int)syscall(SYS_gettid), __ATOMIC_RELEASE); + n = binder_wr(w->fd, &w->cmd, sizeof(w->cmd), rbuf, sizeof(rbuf)); + + seen = binder_parse_read(rbuf, n, &txn_buf); + if (seen > 0 && (seen & BR_SEEN_TXN) && + binder_free_buffer(w->fd, txn_buf) < 0) + seen = -1; + if (binder_wr(w->fd, &exit_cmd, sizeof(exit_cmd), NULL, 0) < 0 || + ioctl(w->fd, BINDER_THREAD_EXIT, &zero) < 0) + seen = -1; + + w->seen = seen; + __atomic_store_n(&w->done, 1, __ATOMIC_RELEASE); + return NULL; +} + +static int proc_read_file(const char *path, char *buf, size_t len) +{ + int pfd; + ssize_t n; + + pfd = open(path, O_RDONLY | O_CLOEXEC); + if (pfd < 0) + return -1; + do { + n = read(pfd, buf, len - 1); + } while (n < 0 && errno == EINTR); + close(pfd); + if (n < 0) + return -1; + buf[n] = '\0'; + return (int)n; +} + +static int looper_start(pthread_t *thr, struct looper_worker *w, + int fd, __u32 cmd) +{ + char wpath[64], spath[64], buf[256], *p; + int i, tid, n; + + memset(w, 0, sizeof(*w)); + w->fd = fd; + w->cmd = cmd; + if (pthread_create(thr, NULL, looper_worker_fn, w) != 0) + return -1; + + for (i = 0; i < 5000; i++) { + tid = __atomic_load_n(&w->tid, __ATOMIC_ACQUIRE); + if (tid || __atomic_load_n(&w->done, __ATOMIC_ACQUIRE)) + break; + usleep(1000); + } + if (__atomic_load_n(&w->done, __ATOMIC_ACQUIRE)) + return 0; + if (!tid) + return -1; + + snprintf(wpath, sizeof(wpath), "/proc/self/task/%d/wchan", tid); + snprintf(spath, sizeof(spath), "/proc/self/task/%d/stat", tid); + for (i = 0; i < 5000; i++) { + if (__atomic_load_n(&w->done, __ATOMIC_ACQUIRE)) + return 0; + n = proc_read_file(wpath, buf, sizeof(buf)); + if (n > 0) { + if (strcmp(buf, "rust_binder_wait") == 0) + return 0; + if (strcmp(buf, "0") == 0 || strstr(buf, "futex")) { + usleep(1000); + continue; + } + } + n = proc_read_file(spath, buf, sizeof(buf)); + if (n < 0) + return __atomic_load_n(&w->done, __ATOMIC_ACQUIRE) ? 0 : -1; + p = strrchr(buf, ')'); + if (p && p[1] == ' ' && p[2] == 'S') + return 0; + usleep(1000); + } + return -1; +} + +static int looper_join(pthread_t thr, struct looper_worker *w) +{ + int i; + + for (i = 0; i < 5000; i++) { + if (__atomic_load_n(&w->done, __ATOMIC_ACQUIRE)) + return pthread_join(thr, NULL); + usleep(1000); + } + return -1; +} + +struct binderfs_idle_ctx { + char mntpt[sizeof(P_tmpdir "/binderfs_XXXXXX")]; + int sfd; + int cfd; + void *smap; +}; + +FIXTURE(binderfs_idle) { + struct binderfs_idle_ctx ctx; +}; + +static void idle_cleanup(struct binderfs_idle_ctx *ctx) +{ + if (ctx->cfd >= 0) { + close(ctx->cfd); + ctx->cfd = -1; + } + if (ctx->smap && ctx->smap != MAP_FAILED) { + munmap(ctx->smap, BINDER_MMAP_SIZE); + ctx->smap = MAP_FAILED; + } + if (ctx->sfd >= 0) { + close(ctx->sfd); + ctx->sfd = -1; + } + binderfs_umount(ctx->mntpt); +} + +FIXTURE_SETUP(binderfs_idle) +{ + char path[sizeof(self->ctx.mntpt) + 1 + BINDERFS_MAX_NAME]; + __u32 max_threads = 1, enter = BC_ENTER_LOOPER; + __s64 probe_timeout = 0; + __s32 zero = 0; + int probe_fd, ret; + + self->ctx.mntpt[0] = '\0'; + self->ctx.sfd = -1; + self->ctx.cfd = -1; + self->ctx.smap = MAP_FAILED; + + if (geteuid() != 0) { + ret = enter_userns(); + if (ret < 0) + SKIP(return, "enter_userns() unsupported: %s", + strerror(-ret)); + } + + change_mountns(_metadata); + + ret = binderfs_mount(self->ctx.mntpt, sizeof(self->ctx.mntpt)); + if (ret == -ENODEV) + SKIP(return, "binderfs missing"); + ASSERT_EQ(ret, 0); + + ASSERT_EQ(binderfs_add_device(self->ctx.mntpt, "my-binder", NULL), 0) { + idle_cleanup(&self->ctx); + } + + snprintf(path, sizeof(path), "%s/my-binder", self->ctx.mntpt); + + /* Probe ioctl on a throwaway fd so sfd keeps its default timeout. */ + probe_fd = open(path, O_RDWR | O_CLOEXEC); + ASSERT_GE(probe_fd, 0) { + idle_cleanup(&self->ctx); + } + ret = ioctl(probe_fd, BINDER_SET_IDLE_TIMEOUT, &probe_timeout); + if (ret < 0 && (errno == EINVAL || errno == ENOTTY)) { + close(probe_fd); + idle_cleanup(&self->ctx); + SKIP(return, "BINDER_SET_IDLE_TIMEOUT not supported"); + } + close(probe_fd); + ASSERT_EQ(ret, 0) { + idle_cleanup(&self->ctx); + } + + self->ctx.sfd = open(path, O_RDWR | O_CLOEXEC); + ASSERT_GE(self->ctx.sfd, 0) { + idle_cleanup(&self->ctx); + } + self->ctx.smap = mmap(NULL, BINDER_MMAP_SIZE, PROT_READ, MAP_PRIVATE, + self->ctx.sfd, 0); + ASSERT_NE(self->ctx.smap, MAP_FAILED) { + idle_cleanup(&self->ctx); + } + ASSERT_EQ(ioctl(self->ctx.sfd, BINDER_SET_MAX_THREADS, &max_threads), 0) { + idle_cleanup(&self->ctx); + } + ASSERT_EQ(ioctl(self->ctx.sfd, BINDER_SET_CONTEXT_MGR, &zero), 0) { + idle_cleanup(&self->ctx); + } + ASSERT_GE(binder_wr(self->ctx.sfd, &enter, sizeof(enter), NULL, 0), 0) { + idle_cleanup(&self->ctx); + } + + self->ctx.cfd = open(path, O_RDWR | O_CLOEXEC); + ASSERT_GE(self->ctx.cfd, 0) { + idle_cleanup(&self->ctx); + } + ASSERT_EQ(binder_trigger_spawn(self->ctx.sfd, self->ctx.cfd), 0) { + idle_cleanup(&self->ctx); + } +} + +FIXTURE_TEARDOWN(binderfs_idle) +{ + idle_cleanup(&self->ctx); +} + +static void assert_no_spindown(struct __test_metadata *_metadata, + int sfd, int cfd, useconds_t wait_us, + bool *spawned) +{ + struct looper_worker w_reg, w_enter; + pthread_t t_reg, t_enter; + + ASSERT_EQ(looper_start(&t_reg, &w_reg, sfd, BC_REGISTER_LOOPER), 0); + ASSERT_EQ(looper_start(&t_enter, &w_enter, sfd, BC_ENTER_LOOPER), 0); + + usleep(wait_us); + EXPECT_FALSE(__atomic_load_n(&w_reg.done, __ATOMIC_ACQUIRE)); + EXPECT_FALSE(__atomic_load_n(&w_enter.done, __ATOMIC_ACQUIRE)); + + ASSERT_EQ(binder_send_oneway(cfd), 0); + ASSERT_EQ(binder_send_oneway(cfd), 0); + ASSERT_EQ(looper_join(t_reg, &w_reg), 0); + ASSERT_EQ(looper_join(t_enter, &w_enter), 0); + + EXPECT_GT(w_reg.seen, 0); + EXPECT_EQ(w_reg.seen & (BR_SEEN_TXN | BR_SEEN_FINISHED), BR_SEEN_TXN); + EXPECT_GT(w_enter.seen, 0); + EXPECT_EQ(w_enter.seen & (BR_SEEN_TXN | BR_SEEN_FINISHED), BR_SEEN_TXN); + + if (spawned) + *spawned = (w_reg.seen > 0 && (w_reg.seen & BR_SEEN_SPAWN)) || + (w_enter.seen > 0 && (w_enter.seen & BR_SEEN_SPAWN)); +} + +TEST_F(binderfs_idle, ioctl_and_default_disabled) +{ + bool spawned = false; + __s64 timeout; + + /* Default timeout is disabled (0); verify idle threads stay parked. */ + assert_no_spindown(_metadata, self->ctx.sfd, self->ctx.cfd, 600 * 1000, + &spawned); + + timeout = -1; + EXPECT_EQ(ioctl(self->ctx.sfd, BINDER_SET_IDLE_TIMEOUT, &timeout), -1); + EXPECT_EQ(errno, EINVAL); + + timeout = INT64_MIN; + EXPECT_EQ(ioctl(self->ctx.sfd, BINDER_SET_IDLE_TIMEOUT, &timeout), -1); + EXPECT_EQ(errno, EINVAL); + + timeout = INT64_MAX; + EXPECT_EQ(ioctl(self->ctx.sfd, BINDER_SET_IDLE_TIMEOUT, &timeout), 0); + + timeout = 100; + ASSERT_EQ(ioctl(self->ctx.sfd, BINDER_SET_IDLE_TIMEOUT, &timeout), 0); + + /* Resetting 100 -> 0 must disable spin-down at runtime. */ + timeout = 0; + ASSERT_EQ(ioctl(self->ctx.sfd, BINDER_SET_IDLE_TIMEOUT, &timeout), 0); + if (!spawned) + ASSERT_EQ(binder_trigger_spawn(self->ctx.sfd, self->ctx.cfd), 0); + assert_no_spindown(_metadata, self->ctx.sfd, self->ctx.cfd, 250 * 1000, + NULL); +} + +TEST_F(binderfs_idle, spindown_and_respawn) +{ + __s64 timeout = 100; + int cycle; + + ASSERT_EQ(ioctl(self->ctx.sfd, BINDER_SET_IDLE_TIMEOUT, &timeout), 0); + + /* + * Two cycles with max_threads=1 prove: + * 1. BC_ENTER_LOOPER is immune even with a peer (lead-wait on cycle 0 + * would trigger deferred timeout + BR_FINISHED on w_reg arrival if + * Entered could time out). + * 2. BC_REGISTER_LOOPER exits with BR_FINISHED (no BR_SPAWN_LOOPER). + * 3. BINDER_THREAD_EXIT decrements started_thread_count so waking + * w_enter emits a fresh BR_SPAWN_LOOPER for the next cycle. + */ + for (cycle = 0; cycle < 2; cycle++) { + struct looper_worker w_enter, w_reg; + pthread_t t_enter, t_reg; + + ASSERT_EQ(looper_start(&t_enter, &w_enter, self->ctx.sfd, + BC_ENTER_LOOPER), 0); + if (cycle == 0) + usleep(200 * 1000); + ASSERT_EQ(looper_start(&t_reg, &w_reg, self->ctx.sfd, + BC_REGISTER_LOOPER), 0); + + ASSERT_EQ(looper_join(t_reg, &w_reg), 0); + EXPECT_EQ(w_reg.seen, BR_SEEN_FINISHED); + EXPECT_FALSE(__atomic_load_n(&w_enter.done, __ATOMIC_ACQUIRE)); + + ASSERT_EQ(binder_send_oneway(self->ctx.cfd), 0); + ASSERT_EQ(looper_join(t_enter, &w_enter), 0); + EXPECT_EQ(w_enter.seen, BR_SEEN_TXN | BR_SEEN_SPAWN); + } +} + +TEST_F(binderfs_idle, sole_ready_thread) +{ + struct looper_worker w_reg, w_enter; + pthread_t t_reg, t_enter; + __s64 timeout = 100; + + ASSERT_EQ(ioctl(self->ctx.sfd, BINDER_SET_IDLE_TIMEOUT, &timeout), 0); + + /* Sole ready thread defers timeout, then exits once a peer joins. */ + ASSERT_EQ(looper_start(&t_reg, &w_reg, self->ctx.sfd, + BC_REGISTER_LOOPER), 0); + usleep(250 * 1000); + EXPECT_FALSE(__atomic_load_n(&w_reg.done, __ATOMIC_ACQUIRE)); + + ASSERT_EQ(looper_start(&t_enter, &w_enter, self->ctx.sfd, + BC_ENTER_LOOPER), 0); + ASSERT_EQ(looper_join(t_reg, &w_reg), 0); + EXPECT_EQ(w_reg.seen, BR_SEEN_FINISHED); + + ASSERT_EQ(binder_send_oneway(self->ctx.cfd), 0); + ASSERT_EQ(looper_join(t_enter, &w_enter), 0); + EXPECT_EQ(w_enter.seen, BR_SEEN_TXN | BR_SEEN_SPAWN); + + /* Deferred timeout woken by work delivers BR_TRANSACTION, not BR_FINISHED. */ + ASSERT_EQ(looper_start(&t_reg, &w_reg, self->ctx.sfd, + BC_REGISTER_LOOPER), 0); + usleep(250 * 1000); + EXPECT_FALSE(__atomic_load_n(&w_reg.done, __ATOMIC_ACQUIRE)); + + ASSERT_EQ(binder_send_oneway(self->ctx.cfd), 0); + ASSERT_EQ(looper_join(t_reg, &w_reg), 0); + EXPECT_EQ(w_reg.seen, BR_SEEN_TXN); + + /* Disabling timeout while deferred prevents BR_FINISHED when a peer joins. */ + ASSERT_EQ(binder_trigger_spawn(self->ctx.sfd, self->ctx.cfd), 0); + ASSERT_EQ(looper_start(&t_reg, &w_reg, self->ctx.sfd, + BC_REGISTER_LOOPER), 0); + usleep(250 * 1000); + EXPECT_FALSE(__atomic_load_n(&w_reg.done, __ATOMIC_ACQUIRE)); + + timeout = 0; + ASSERT_EQ(ioctl(self->ctx.sfd, BINDER_SET_IDLE_TIMEOUT, &timeout), 0); + ASSERT_EQ(looper_start(&t_enter, &w_enter, self->ctx.sfd, + BC_ENTER_LOOPER), 0); + usleep(150 * 1000); + EXPECT_FALSE(__atomic_load_n(&w_reg.done, __ATOMIC_ACQUIRE)); + + ASSERT_EQ(binder_send_oneway(self->ctx.cfd), 0); + ASSERT_EQ(binder_send_oneway(self->ctx.cfd), 0); + ASSERT_EQ(looper_join(t_reg, &w_reg), 0); + ASSERT_EQ(looper_join(t_enter, &w_enter), 0); + EXPECT_GT(w_reg.seen, 0); + EXPECT_EQ(w_reg.seen & (BR_SEEN_TXN | BR_SEEN_FINISHED), BR_SEEN_TXN); + EXPECT_GT(w_enter.seen, 0); + EXPECT_EQ(w_enter.seen & (BR_SEEN_TXN | BR_SEEN_FINISHED), BR_SEEN_TXN); +} + TEST_HARNESS_MAIN -- 2.56.1.915.g69d5ec7298-goog