From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796344387; b=GEg8gnRfmvwJiYPTyZrOrEgahkCdV2Z0IxdLuTvpXYbr2wCZb98vMa9M6Wkqb5t01cOd C24KfBp7feQEjvxjyf2l27Vdlw7ulgPXgl4snvvc4w9Q8uOFo/NN8l2Ph5y/MHw4vuhPH h7xvIEt2h/EcBvbmmtFUwoa8cEyss0UDdqHSs1a7xFFXzFRjw4ngS0m6ZOdBeahRoQQpd EV61ibvASuztmsXtiP3ChrlS6UYvyioG6GyoEmRUj2a2KpYwbhf6O5Vs9Ox3Br8/wNCIM kM8EH+PFl9cEO5T+7yRsKqxhOaSpBF0vocjqD+1gXfGqCEwPbk2J33SpUcWIiMx7q/A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796344387; 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=GP9k57UDzPhZASRbKd63fKNrQQOBL5Rsnz8pE+zaklo=; b=S6ggT1V14/OV+hGn9kt9APbbrR+OL0D+Ua604YRxh89nSilPz9xoL7R6QbggCLkozEmt ok5ZV7dC2pjJpRETqAvl3Za8kWrgrS3v1wUa+VlPN2OovEs5y3P7fS5PKDFm9d8odqUz/ It3ZDcfT6L9WFxcRxFp+NZJpo+X/OppkXHHzHWCgBGkt57E0DlNUpIOGZlogEY4wzIljs uJJxGxuN0vG5oVH7f7n33iW7sreckgCTQzb8Z4/nkvTywZLiYzb60wutxVBAyGl/DpAgN eLWx8sPJRd0Qjc0FbKKPK6JiJG4qA6w26bRV0zsI2Q21hyv4SqfFVkoiYGjJJOer+2w== 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=Q59PLQiV; arc=none smtp.remote-ip=209.85.210.198 Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.198]) (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 207B7606A335 for ; Fri, 4 Dec 2026 00:33:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 207B7606A335 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=Q59PLQiV Received: by mail-pf1-f198.google.com with SMTP id d2e1a72fcca58-8b691cf0eeaso113729b3a.1 for ; Thu, 03 Dec 2026 16:33:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796344386; x=1796949186; 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=GP9k57UDzPhZASRbKd63fKNrQQOBL5Rsnz8pE+zaklo=; b=Q59PLQiVIjcayQDimXq2wvoKAXrxKscv/bFfC9mhgeQn0jWc3/rGgT1lm8k+8BeQLx uArGeVzO6gt5rn0hiDaPj4ffvia+Kjp1PKL8vKFVQ0fdDTIgbOGgFIZ/woBDALUnWaw2 d2tP/ygq7FUYlW9Tc8mc4CxvdIdzyVXM2lFfwIzRFKcJIffSLw5hs6rQLlw43IvCrb3p chlUo+D95LHGCMWOwnDEB+1tAdwrOwQt6ZPCFdEmDcTp60D0qYcxxiWV4dtbCSPTKs8d c95RN/AlHO2gRCrPSSU7NuMDjlIvHRQ5sEh6egCGEkHuG/69SgXjakYApHC76DnYXzdj JN+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796344386; x=1796949186; 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=GP9k57UDzPhZASRbKd63fKNrQQOBL5Rsnz8pE+zaklo=; b=C6O9h/p6qVQEM2CWydPe9xJQ7L37zTefFmGduwn6FXl1Knr75mDyr7tYvUa5fildrz 9uvlttv9/ytgXSGSef0J3UCbvOQJoyLUf8OhtMlEMgkOVfx144lrVrb+qvIo5tRMvOvn p3mnZ7wdcvUHsb5EoJBqYeEmm14jVpv+y07HNO5Y1xFzofV/SpKgGltthsJNEuF+scjt eFyztxHYpAPzKOFKIAUJJ3DdPuTFLoFIMh7qG3tWw6pnXy4M4MCmzot4383Y1N0ZDcHQ MkqtvCOVLEt0+eJwVWUNE3gtRq7JzOepmD5HlI/WJ4VUvUTy3NQ1vJe0XdICifCa8Pci 0UDg== X-Forwarded-Encrypted: i=1; AL+hErHEQkgVlgTVi9LQU+al6Z0DFmsZW1mGdFrTohxJ+P7Allv612+9Mrg1a7yN/mIvgWUoLw5pT5jMUaqmAtU=@vger.kernel.org X-Gm-Message-State: APuU4Z+jBcmHL526aSh0VUY4ELbvLOg/iEYzX1hlHDaM9/lYQeZYt5Ag JaVfmfugor0Gf3U95p227E6WOyK9wqFQ0KWvDCXAzJPX2CQ/tTQkUTdWyZjIjUf3KJz1pbtuM1D FD6dIkZSk0w== X-Received: from pfbmt10.prod.google.com ([2002:a05:6a00:6e6a:b0:8a0:4085:d831]) (user=grahalt job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:b96:b0:89e:f8e9:205f with SMTP id d2e1a72fcca58-8de2d426bc4mr478256b3a.32.1796344385994; Thu, 03 Dec 2026 16:33:05 -0800 (PST) Date: Fri, 04 Dec 2026 00:32:46 +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=3803; i=grahalt@google.com; s=20261202; h=from:subject:message-id; bh=u9rlL+wj0jYgXYhflQvhfXGjeqIMYQhzIjgUlyHg3bs=; b=qUJeEeydi4F04dSpIt9x8j0+klZy/GnPONAkfBf0VGyCgaaGU3MpIAOH2qvxClqM7nepCJY/t DCfsNUdVduKA37JoK7tQDWEx4KdO63PPg5REYfSpoz24kpemaFTgrmf X-Mailer: b4 0.14.3 Message-ID: <20261204-rust-binder-thread-spindown-v1-1-8702c42d4437@google.com> Subject: [PATCH 1/4] rust: sync: condvar: add wait_interruptible_freezable_timeout() 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" To support waiting on a condition variable with a timeout as a freezable task, add CondVar::wait_interruptible_freezable_timeout(). Rust Binder requires this functionality to allow idle looper threads to time out and spin down while still allowing processes to be frozen when their threads are blocked in the binder driver. Assisted-by: Gemini Signed-off-by: Nicholas Giunta --- rust/kernel/sync/condvar.rs | 47 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/rust/kernel/sync/condvar.rs b/rust/kernel/sync/condvar.rs index 69d58dfbad7b..e5ddf7427eea 100644 --- a/rust/kernel/sync/condvar.rs +++ b/rust/kernel/sync/condvar.rs @@ -177,6 +177,22 @@ pub fn wait_interruptible_freezable( crate::current!().signal_pending() } + fn wait_interruptible_timeout_internal( + &self, + wait_state: c_int, + guard: &mut Guard<'_, T, B>, + jiffies: Jiffies, + ) -> CondVarTimeoutResult { + let jiffies = jiffies.try_into().unwrap_or(MAX_SCHEDULE_TIMEOUT); + let res = self.wait_internal(wait_state, guard, jiffies); + + match (res as Jiffies, crate::current!().signal_pending()) { + (jiffies, true) => CondVarTimeoutResult::Signal { jiffies }, + (0, false) => CondVarTimeoutResult::Timeout, + (jiffies, false) => CondVarTimeoutResult::Woken { jiffies }, + } + } + /// Releases the lock and waits for a notification in interruptible mode. /// /// Atomically releases the given lock (whose ownership is proven by the guard) and puts the @@ -188,14 +204,31 @@ pub fn wait_interruptible_timeout( guard: &mut Guard<'_, T, B>, jiffies: Jiffies, ) -> CondVarTimeoutResult { - let jiffies = jiffies.try_into().unwrap_or(MAX_SCHEDULE_TIMEOUT); - let res = self.wait_internal(TASK_INTERRUPTIBLE, guard, jiffies); + self.wait_interruptible_timeout_internal(TASK_INTERRUPTIBLE, guard, jiffies) + } - match (res as Jiffies, crate::current!().signal_pending()) { - (jiffies, true) => CondVarTimeoutResult::Signal { jiffies }, - (0, false) => CondVarTimeoutResult::Timeout, - (jiffies, false) => CondVarTimeoutResult::Woken { jiffies }, - } + /// Releases the lock and waits for a notification in interruptible and freezable mode. + /// + /// Atomically releases the given lock (whose ownership is proven by the guard) and puts the + /// thread to sleep. It wakes up when notified by [`CondVar::notify_one`] or + /// [`CondVar::notify_all`], or when a timeout occurs, or when the thread receives a signal. + /// + /// The process is allowed to be frozen during this sleep. No lock should be held when calling + /// this function, and there is a lockdep assertion for this. Freezing a task that holds a lock + /// can trivially deadlock vs another task that needs that lock to complete before it too can + /// hit freezable. + #[must_use = "wait_interruptible_freezable_timeout returns if a signal is pending, \ + so the caller must check the return value"] + pub fn wait_interruptible_freezable_timeout( + &self, + guard: &mut Guard<'_, T, B>, + jiffies: Jiffies, + ) -> CondVarTimeoutResult { + self.wait_interruptible_timeout_internal( + TASK_INTERRUPTIBLE | TASK_FREEZABLE, + guard, + jiffies, + ) } /// Calls the kernel function to notify the appropriate number of threads. -- 2.56.1.915.g69d5ec7298-goog