From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796344387; b=m6ABRLpZGHWd5seP1mBZQhgxg8TYYyj8PU9dxa7ZGU+G+OIxccfs/6lz4U7vd9/oV12u I2h6MhAMVzDXi7Fj/u4TdCtPRtYpS+je5e2wLchbQxGRxYggFzPuIXXXJ2fXO2MtAEMnm qmwab2DFhI41NXaKd50XG9itS03B95hlcw7KBBtWHuo17Mgxcv9zYbGLzuZwGuhIQt2mC 24Q11cxPwqzc9Y31Tixf6y4aY6hZFyeDmss/VVzROfCJMDfBN6+MUY98uuzv8qfoD037D bYIcNF2MSfbKVA915ZgkO0s049HfZix98BVeg3lg6c3AUKDJZ8SEfdwPkriLMHphq+Q== 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=2o5TrU2F+qUmzxxbfxPdD4rNQj8X71DLz3jAt8iq5QTNEq/QXDo15oy8DGazmMcRRWvz 3nRs1a2aodRlYsA+Pa+l8fi7j2hFDvhoMPILwuOBp83AW5xxOHRKAexUCS4fmp905rqom mchnPf0zrcL6UucdhL4DAvwTXmHiMc+VTLbCMFcw8M39gGs9qnr3JBPqT7U5tC/qANCjT AoRmmzGpkmZYPox01xO+zvErCO/VA1BgixqBFzPgYd/Ei+dwEEUFFW+W5/gn/mz9o4kdi DDsoyHV35awkP9Hz+t5FcZaO+GjAStHG8D7UfcCT2YXST+xzu16onz3b7riMczgMTbw== 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.199 Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) (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 17990606A332 for ; Fri, 4 Dec 2026 00:33:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 17990606A332 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-f199.google.com with SMTP id d2e1a72fcca58-8a0592d4539so93418b3a.3 for ; Thu, 03 Dec 2026 16:33:07 -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=bYrFRsWCEeJhRWh9+FbLWy1cPIEprIi2u+DqPsavPOJNaLQVie1uHVsdB2Bua0+xOk CbavmQn2RK0o9DJi7dL08AplspZRrLWV65QBkuFDv6biqq0caT0zOE7dzH57PE3fhh3x QsKhE0z0YRnNtKH56Zh9vfBqZEOdnEaG02pl7g0+O/a82LoTcexEP0nicZyDatOv84jx 6h5hI4ZeWlJ+Vv1jgyrAScVBbD+XFdFVmXBBr/BnSVLwu/05imCng294k0jl6iLY27kF Adw69FvlY5xGhDmQdna/74c56At8UPBpap+mEDlGR1l5RMkglZtPNjJD/EfupuwTUpCK koRw== X-Forwarded-Encrypted: i=1; AL+hErGz/HhgqN3g2YdyPJGq9I68YPSRwKiQoiT42ZcFfuC6RFHVkJAISbwD5sHiRagDF6KrkqisW6tWaTM=@vger.kernel.org X-Gm-Message-State: APuU4Z9iqUlou+82He4RAomE3x6LhizcLGNzReXC351S2orZcauqwMkI SqlvtTDdEbre9BTOvjm2FRry9G4rdPcetyfTOW8mSMqQiAkGNN1e090ElWkGCkuoUG+njLS3uLN +YzcclpkYBw== 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-api@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