Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp7980585rdb; Thu, 4 Jan 2024 14:20:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IEUxST2CvXmnWNT96MX281bMLiGkaZv1CWNgsufdLFYIjzoBV68PNFtuQeTPmKnmLcJ6DxH X-Received: by 2002:a05:6512:3987:b0:50e:7410:d5b0 with SMTP id j7-20020a056512398700b0050e7410d5b0mr793848lfu.28.1704406819569; Thu, 04 Jan 2024 14:20:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704406819; cv=none; d=google.com; s=arc-20160816; b=Fmtjjc8670Xts6v4gigl0ajN754ATukkIuiFNq2PTo5nsuK7T77GxWU219n81nFLDm B0o1p0N8VnMmWnrXcQLjRPNIyfGmy6lX31RgNZUdSp2ATLqH12dKRPszut4Ap0kTpLES B+5anQjXBCFKQncm0UZku5oz80HjVve7UiqiopvPzsmIaDc+4w3uNg2RL0wGVSj9X3QT Yf9qieXF6h8ID+I95Ou3Wg82oQ8o+IZEeQQ6hLNV/1bIS+TDXLWFkk0i4VREmordUpXf MICwtoyYXe8PIpMbdnA3DxyYNETnXORVSx6IiDzbNsee1O1aV3UWU44HR7LdypvVKxJY YLBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:feedback-id:dkim-signature; bh=H30Ea/7n+Kca5VxufaG3WS56g0npATZySdngobj9tDE=; fh=baON6TEuz/Uu+wFn69hOCT7hCu9WIFiIXOHPEdnlk6E=; b=vXU5tAYVR69Zf7scIUryLCaEJ16McSnQAi8MMQVNqVFc0ipY80zDPu8T4dyafeYRiK qw7xC8cggmWbKfSwv9Bx7Hy34lDi59XGo82Qfn84PCzWXqH/5e1sfPn1DwPYlXvkKy93 XonWQWmnUU9hSQ5OJz80d2aRg1YOX+6uISiOUpebD+z4E4DULRJNo54zSvc51tzmR6JE L23EHsrLW1qYkfuH6jgehebfynoEUWZUI4uUSGLAB/2tKBtQikHwy6ioaPDp7vpPP/2d a3c8oXQoa7xMRziQ0uVPc5Hid98IXx3YHFhizUc5xslDZjbP89wGOlMsErbZ9XKCcVL8 1cFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Li830Kao; spf=pass (google.com: domain of linux-kernel+bounces-17280-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-17280-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id j15-20020a1709062a0f00b00a28dc17add2si91787eje.675.2024.01.04.14.20.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jan 2024 14:20:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-17280-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Li830Kao; spf=pass (google.com: domain of linux-kernel+bounces-17280-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-17280-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 2646A1F22617 for ; Thu, 4 Jan 2024 22:20:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A0AAA2D040; Thu, 4 Jan 2024 22:19:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Li830Kao" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-qt1-f177.google.com (mail-qt1-f177.google.com [209.85.160.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 52B1D2C85F; Thu, 4 Jan 2024 22:19:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-qt1-f177.google.com with SMTP id d75a77b69052e-42782601608so4527341cf.2; Thu, 04 Jan 2024 14:19:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704406792; x=1705011592; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:from:to:cc:subject:date :message-id:reply-to; bh=H30Ea/7n+Kca5VxufaG3WS56g0npATZySdngobj9tDE=; b=Li830KaoKGj49A8fY8ORhjh2iypmRgZ+2GXqLCaXbstveV+P1SwDaVK3Ga+2f4RrhQ +2AsB3tlw1v0IUQNLbQb+Hk4YOiCi5bHpRnqfNtyvAAvw0BN6muPD0GNr0E+lMhvvTiR d/YaGhJgC5CJfIv51G47kzIIvKzL08aDKeoQl7PR+olY3dt4QjxgFMAaMjEk3331ABF+ BchT6td1sl7aTCOh+mQbkakJwmUjSyNqz13M+XSsxc7ebXsBxZCyhvhg2A4AYqKwtlHe AtH2ZwGlRxqxNDuVdoGf2dDiWH1Gj+pQOUsDa7SHAdcbK+xorbgYzULCdMPSGBZlpf6K PYeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704406792; x=1705011592; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=H30Ea/7n+Kca5VxufaG3WS56g0npATZySdngobj9tDE=; b=oGE/G+LO2VG4d+2rpj+eTVZm1gLxh/UiaHHPgF3Pd84o7HLx5XMu8fNltwzbPqJye0 v9jnRrdyXj6aK9EPfE6B4cre/Xdhw3l3Q6iqG+Nalk5G1t7QsU0VL903iuy5cA9wuMRh n8Z3J7ObOK+zvTHx3gV/GCYAJUt5aHYUfX9GhtuEcH3mApc4rqAEshEnNiplRVax88qI MbWXNtXMhEPa4oe4J8GSaU61Tt+HyzQrWAYZy3disU91b5jNiFW7RSqBqmBydzHCfZqg qzniyshPWohiCpo6KQLasSMOA3QaSeDjUlODSx1X3Cl/dL0xt76fPHwBYy16LIi1nPXo dNVw== X-Gm-Message-State: AOJu0Ywv0jrEh7494ykjJnvBGhwKmlMCPS4e81hc/sLjm420BAVy1ElO lB9uVCp/+2veq0wQ05D/ljA= X-Received: by 2002:ac8:59d6:0:b0:425:4043:8d38 with SMTP id f22-20020ac859d6000000b0042540438d38mr1441031qtf.83.1704406791981; Thu, 04 Jan 2024 14:19:51 -0800 (PST) Received: from auth1-smtp.messagingengine.com (auth1-smtp.messagingengine.com. [66.111.4.227]) by smtp.gmail.com with ESMTPSA id kr10-20020ac861ca000000b00428319369aasm177715qtb.12.2024.01.04.14.19.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jan 2024 14:19:51 -0800 (PST) Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailauth.nyi.internal (Postfix) with ESMTP id D14D227C0064; Thu, 4 Jan 2024 17:19:50 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Thu, 04 Jan 2024 17:19:50 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrvdegjedgudeitdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpeffhffvvefukfhfgggtuggjsehttdertddttddvnecuhfhrohhmpeeuohhq uhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrf grthhtvghrnhepjeeihfdtuedvgedvtddufffggeefhefgtdeivdevveelvefhkeehffdt keeihedvnecuffhomhgrihhnpehruhhsthdqlhgrnhhgrdhorhhgnecuvehluhhsthgvrh fuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepsghoqhhunhdomhgvshhmthhp rghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedtieegqddujeejkeehheehvddqsg hoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfhhigihmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 4 Jan 2024 17:19:50 -0500 (EST) Date: Thu, 4 Jan 2024 14:18:42 -0800 From: Boqun Feng To: Alice Ryhl Cc: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , Andreas Hindborg , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Tiago Lam , Thomas Gleixner , Martin Rodriguez Reboredo , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3 3/4] rust: sync: add `CondVar::wait_timeout` Message-ID: References: <20240104-rb-new-condvar-methods-v3-0-70b514fcbe52@google.com> <20240104-rb-new-condvar-methods-v3-3-70b514fcbe52@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240104-rb-new-condvar-methods-v3-3-70b514fcbe52@google.com> On Thu, Jan 04, 2024 at 02:02:43PM +0000, Alice Ryhl wrote: > Sleep on a condition variable with a timeout. > > This is used by Rust Binder for process freezing. There, we want to > sleep until the freeze operation completes, but we want to be able to > abort the process freezing if it doesn't complete within some timeout. > > Reviewed-by: Martin Rodriguez Reboredo > Reviewed-by: Tiago Lam > Signed-off-by: Alice Ryhl Reviewed-by: Boqun Feng That said, I want to hear from Thomas on the usage of jiffies, see below: > --- [...] > - fn wait_internal(&self, wait_state: u32, guard: &mut Guard<'_, T, B>) { > + fn wait_internal( > + &self, > + wait_state: u32, > + guard: &mut Guard<'_, T, B>, > + timeout_in_jiffies: c_long, This is an internal function, and it makes sense we use the same type for durations as the C function we rely on. > + ) -> c_long { > let wait = Opaque::::uninit(); > > // SAFETY: `wait` points to valid memory. > @@ -113,11 +122,13 @@ fn wait_internal(&self, wait_state: u32, guard: &mut Guar > bindings::prepare_to_wait_exclusive(self.wait_list.get(), wait.get(), wait_state as _) > }; > > - // SAFETY: No arguments, switches to another thread. > - guard.do_unlocked(|| unsafe { bindings::schedule() }); > + // SAFETY: Switches to another thread. The timeout can be any number. > + let ret = guard.do_unlocked(|| unsafe { bindings::schedule_timeout(timeout_in_jiffies) }); > > // SAFETY: Both `wait` and `wait_list` point to valid memory. > unsafe { bindings::finish_wait(self.wait_list.get(), wait.get()) }; > + > + ret > } > [...] > + /// 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 > + /// 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. > + #[must_use = "wait_interruptible_timeout returns if a signal is pending, so the caller must check the return value"] > + pub fn wait_interruptible_timeout( > + &self, > + guard: &mut Guard<'_, T, B>, > + jiffies: Jiffies, This is a public interface, so it may make sense use a HZ-independent type for durations, e.g. core::time::Duration: https://doc.rust-lang.org/core/time/struct.Duration.html but we don't have enough users to see whether there would be a need for HZ-dependent durations, so I think it's fine that we stick with a simple solution in Alice's patchset to get the ball rolling, we can always remove a public interface with HZ-dependent durations whenever we want. Thoughts? Regards, Boqun > + ) -> CondVarTimeoutResult { > + let jiffies = jiffies.try_into().unwrap_or(MAX_SCHEDULE_TIMEOUT); > + let res = self.wait_internal(bindings::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 }, > + } > + } > + [...] > + > +/// The return type of `wait_timeout`. > +pub enum CondVarTimeoutResult { > + /// The timeout was reached. > + Timeout, > + /// Somebody woke us up. > + Woken { > + /// Remaining sleep duration. > + jiffies: Jiffies, > + }, > + /// A signal occurred. > + Signal { > + /// Remaining sleep duration. > + jiffies: Jiffies, > + }, > +} > diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs > index f12a684bc957..149a5259d431 100644 > --- a/rust/kernel/sync/lock.rs > +++ b/rust/kernel/sync/lock.rs > @@ -139,7 +139,7 @@ pub struct Guard<'a, T: ?Sized, B: Backend> { > unsafe impl Sync for Guard<'_, T, B> {} > > impl Guard<'_, T, B> { > - pub(crate) fn do_unlocked(&mut self, cb: impl FnOnce()) { > + pub(crate) fn do_unlocked(&mut self, cb: impl FnOnce() -> U) -> U { > // SAFETY: The caller owns the lock, so it is safe to unlock it. > unsafe { B::unlock(self.lock.state.get(), &self.state) }; > > @@ -147,7 +147,7 @@ pub(crate) fn do_unlocked(&mut self, cb: impl FnOnce()) { > let _relock = > ScopeGuard::new(|| unsafe { B::relock(self.lock.state.get(), &mut self.state) }); > > - cb(); > + cb() > } > } > > diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs > index 9451932d5d86..ffb4a51eb898 100644 > --- a/rust/kernel/task.rs > +++ b/rust/kernel/task.rs > @@ -7,6 +7,9 @@ > use crate::{bindings, types::Opaque}; > use core::{marker::PhantomData, ops::Deref, ptr}; > > +/// A sentinal value used for infinite timeouts. > +pub const MAX_SCHEDULE_TIMEOUT: c_long = c_long::MAX; > + > /// Returns the currently running task. > #[macro_export] > macro_rules! current { > > -- > 2.43.0.472.g3155946c3a-goog >