From nobody Sat Feb 7 17:48:36 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5966B26FA6E for ; Tue, 29 Apr 2025 21:48:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745963292; cv=none; b=Un3Zc51XS2e0n4E+rt14t7OP3IOjoU9k4srtp0Kg0CXwQY57V710P/zk/H1EJUdIH5WoeZm1/pvlQ7slovQMJX/PPxr/2hescpfM7cM4IGvJfTxH53ELVqgbpGS5dgp8YQj6nWcKR3l0leU9fiGCEVrOLZWmGTx0KYBVS9qvTuI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745963292; c=relaxed/simple; bh=nTqe0PDL/gL9YiXXxz5bNPPj0SodUqvokk2yftri5Rg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eUbNcn2bWatcPzOwP4VMUkG9Qhq6E7sueZ/O4HDSENsGm+aWZmDMHqlo5QbCecW7pfKwkU+riPMN1SdqA5T+Y/qSlthJsdSGIJsMdxfpv8/yPDt4YqWkQvGHwCC5IfTXqd7LYmchZMhtOZ3jA84jd0yVdf/hxf7j/ZkNXCzkOWw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=KC2pj+Jg; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="KC2pj+Jg" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1745963290; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=yX5+ZjQPENf74ftxxnt80TaBhjIdXfKiwqjkj6Gc0n8=; b=KC2pj+JgIP9gT39Hp0sRCuoGeHNzIAgyswIUA7mIWLr9Pdp6/9x11Is/sdIeyXVFWaw4a3 gc1Y4Gy+7ERJOwvRXZXileeAp+veNcta7we5LG7J+S6qZrDYWrERPRCeIWUkzq8GvxBVtu X8UkT1pXSozpiv7+FR5KuDg/TSMhDfw= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-205-E6X-vOuOMyKkbN9YtiS_AA-1; Tue, 29 Apr 2025 17:48:05 -0400 X-MC-Unique: E6X-vOuOMyKkbN9YtiS_AA-1 X-Mimecast-MFC-AGG-ID: E6X-vOuOMyKkbN9YtiS_AA_1745963283 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 13BC61956087; Tue, 29 Apr 2025 21:48:03 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.88.132]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C749919560A3; Tue, 29 Apr 2025 21:47:58 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, Andreas Hindborg , linux-kernel@vger.kernel.org Cc: Boqun Feng , FUJITA Tomonori , Frederic Weisbecker , Thomas Gleixner , Anna-Maria Behnsen , John Stultz , Stephen Boyd , Miguel Ojeda , Alex Gaynor , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich Subject: [PATCH v4 1/7] rust: hrtimer: Document the return value for HrTimerHandle::cancel() Date: Tue, 29 Apr 2025 17:44:37 -0400 Message-ID: <20250429214752.1637859-2-lyude@redhat.com> In-Reply-To: <20250429214752.1637859-1-lyude@redhat.com> References: <20250429214752.1637859-1-lyude@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 Content-Type: text/plain; charset="utf-8" Just a drive-by fix I noticed: we don't actually document what the return value from cancel() does, so do that. Signed-off-by: Lyude Paul --- V4: * Reword to "Returns `true` if the timer was running." Signed-off-by: Lyude Paul --- rust/kernel/time/hrtimer.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index 27243eaaf8ed7..ef96c7e2c21ff 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -304,6 +304,8 @@ pub unsafe trait HrTimerHandle { /// Note that the timer might be started by a concurrent start operati= on. If /// so, the timer might not be in the **stopped** state when this func= tion /// returns. + /// + /// Returns `true` if the timer was running. fn cancel(&mut self) -> bool; } =20 --=20 2.48.1 From nobody Sat Feb 7 17:48:36 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6F409264609 for ; Tue, 29 Apr 2025 21:48:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745963296; cv=none; b=MXVRzZhh2UwQtki3DWSxRf3oa8wbi+/pVOtV/QvQBG6zWq15mE/3LdexZduch5yiiXnBCxXaJ97oYGCber/00Au9VuLA8ck1vJ108yWwwTNKeNczdLE7lyrUpJDWcdlkqaH3wpkhCC13JuarVbTzywxy4Kdx4cfC/yk6up4SVHA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745963296; c=relaxed/simple; bh=MSF0qwmCf6ZWXrtsVfIP/yBIfGWSyTA2Js4oFfyZz3M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jC+RC3CfJR2jXII2qF5PxoDyF5R9X3EKizdPC9OZmf0eicgQArkCNnKUjDAxrnuKN7nHGVBKPxtWpb8s+DrAZ2Sbpcij0+dkbBfkT8Xem/xUxi8loz0J1URv6id3SrAZTHxPuKy4ECkqtIol2ZCBZRO+Ab5cuGVo3YJLjmYbTvw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=dGszl4de; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="dGszl4de" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1745963293; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gZb82PbyGYSBKPulXkIr1Fg1XkWaJSRhe1YdKH7R7kE=; b=dGszl4deY0bQl/Gi1Eg8Pln2wl6lASi3ww034mjpYQ7gVgVvUH1QklYTW9MvGWZ5vHeReG oMi5CO7VKrU4Y7IK97/TS1STx6ag3SU9nLxFa1qjscDW4mY8wvmZ7Cs4fl05eca1p/pl3x F/goWV6SkeUQf5wt0Ca0nu9r9bG/1bw= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-662-5sbtlesFPNCQxhQ6mYB1hw-1; Tue, 29 Apr 2025 17:48:10 -0400 X-MC-Unique: 5sbtlesFPNCQxhQ6mYB1hw-1 X-Mimecast-MFC-AGG-ID: 5sbtlesFPNCQxhQ6mYB1hw_1745963287 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3A1391956094; Tue, 29 Apr 2025 21:48:07 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.88.132]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 6767E19560A3; Tue, 29 Apr 2025 21:48:03 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, Andreas Hindborg , linux-kernel@vger.kernel.org Cc: Boqun Feng , FUJITA Tomonori , Frederic Weisbecker , Thomas Gleixner , Anna-Maria Behnsen , John Stultz , Stephen Boyd , Miguel Ojeda , Alex Gaynor , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich Subject: [PATCH v4 2/7] rust: hrtimer: Add HrTimer::raw_forward() and forward() Date: Tue, 29 Apr 2025 17:44:38 -0400 Message-ID: <20250429214752.1637859-3-lyude@redhat.com> In-Reply-To: <20250429214752.1637859-1-lyude@redhat.com> References: <20250429214752.1637859-1-lyude@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 Content-Type: text/plain; charset="utf-8" Within the hrtimer API there are quite a number of functions that can only be safely called from one of two contexts: * When we have exclusive access to the hrtimer and the timer is not active. * When we're within the hrtimer's callback context as it is being executed. This commit adds bindings for hrtimer_forward() for the first such context, along with HrTimer::raw_forward() for later use in implementing the hrtimer_forward() in the latter context. Signed-off-by: Lyude Paul --- V4: * Fix the safety contract for raw_forward() * Require Pin<&mut Self>, not &mut self * Drop incorrect UniquePin example * Rewrite documentation a bit (re: Andreas) Signed-off-by: Lyude Paul --- rust/kernel/time/hrtimer.rs | 44 ++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index ef96c7e2c21ff..76caf9fae8170 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -68,7 +68,11 @@ //! `start` operation. =20 use super::ClockId; -use crate::{prelude::*, time::Instant, types::Opaque}; +use crate::{ + prelude::*, + time::{Delta, Instant}, + types::Opaque, +}; use core::marker::PhantomData; use pin_init::PinInit; =20 @@ -164,6 +168,44 @@ pub(crate) unsafe fn raw_cancel(this: *const Self) -> = bool { // handled on the C side. unsafe { bindings::hrtimer_cancel(c_timer_ptr) !=3D 0 } } + + /// Forward the timer expiry for a given timer pointer. + /// + /// # Safety + /// + /// - `self_ptr` must point to a valid `Self`. + /// - The caller must either have exclusive access to the data pointed= at by `self_ptr`, or be + /// within the context of the timer callback. + #[inline] + unsafe fn raw_forward(self_ptr: *mut Self, now: Instant, interval: Del= ta) -> u64 { + // SAFETY: + // * The C API requirements for this function are fulfilled by our= safety contract. + // * `self_ptr` is guaranteed to point to a valid `Self` via our s= afety contract + unsafe { + bindings::hrtimer_forward(Self::raw_get(self_ptr), now.as_nano= s(), interval.as_nanos()) + } + } + + /// Conditionally forward the timer. + /// + /// If the timer expires after `now`, this function does nothing and r= eturns 0. If the timer + /// expired at or before `now`, this function forwards the timer by `i= nterval` until the timer + /// expires after `now` and then returns the number of times the timer= was forwarded by + /// `interval`. + /// + /// This function is mainly useful for timer types which can provide e= xclusive access to the + /// timer when the timer is not running. For forwarding the timer from= within the timer callback + /// context, see [`HrTimerCallbackContext::forward()`]. + /// + /// Returns the number of overruns that occurred as a result of the ti= mer expiry change. + pub fn forward(self: Pin<&mut Self>, now: Instant, interval: Delta) ->= u64 { + // SAFETY: + // - `raw_forward` does not move `self`. + // - Self is a mutable reference and thus always points to a valid= `HrTimer` + // - The only way that we could hold a mutable reference to `HrTim= er` is if we have + // exclusive access to it - fulfilling the requirements of the C= API. + unsafe { Self::raw_forward(self.get_unchecked_mut(), now, interval= ) } + } } =20 /// Implemented by pointer types that point to structs that contain a [`Hr= Timer`]. --=20 2.48.1 From nobody Sat Feb 7 17:48:36 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9ADC1257452 for ; Tue, 29 Apr 2025 21:48:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745963310; cv=none; b=Q0i+03TWw6nr4PREhzfPmC0LVaTaavScQiPbl2C6d3IvUzqjdJsVrELhM/s9PIPaIwGtqQDSi1Mie844Q4PYP7Y07+u6tOr2xwZqlz21zX9Er0yODT89z773mpzDlFacz9I6EsLsB0OtZ8l+vT60JJkE4ucI1TJZoWhcUYMeJ9k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745963310; c=relaxed/simple; bh=SZLfsXPRDLV4F+4hb6ap0CmSiJ6OyHg1dcvc8VeYBF8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=n9Y27NtAZ3GzkYeTh1xR2PkYRAL42BXA4QohwdScr7ccbwtfzRGDhttFiXU7s4Xrae+ESvFOTOjkhQdfFa9V6zceAnFCZcihtHZBL0XZDNsQtEUjG9pry0ktZhb3isQu2q+dLoyq9mp0FDGgpGaUlhMfBZZJ1BN+K4sotky3ku4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=MSfd+lIg; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="MSfd+lIg" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1745963307; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IznTkOaPFCfAvRQt1OhWZrGExipCFPEqSVkrVHQdx+Y=; b=MSfd+lIgA9j4RdDj6jADsHDScmntO/MlI+YPxiB/0ueSApEaK6j1/L6nwvxQbRRzbEwJtz lPQ02NkaIg7G4gTtEtiOpdWrTGnZ1b/M1JLCpJCwBPWvs35INJ9laLXCFtetw5yOHDTzJ5 Uk4g+GmCDlhgTwVxf7npGoeCWDp2fyU= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-99-b2L_gP_UPuyXyBuQ_6Cl5w-1; Tue, 29 Apr 2025 17:48:23 -0400 X-MC-Unique: b2L_gP_UPuyXyBuQ_6Cl5w-1 X-Mimecast-MFC-AGG-ID: b2L_gP_UPuyXyBuQ_6Cl5w_1745963300 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D0EAF180048E; Tue, 29 Apr 2025 21:48:19 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.88.132]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 6C7E619560A3; Tue, 29 Apr 2025 21:48:16 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, Andreas Hindborg , linux-kernel@vger.kernel.org Cc: Boqun Feng , FUJITA Tomonori , Frederic Weisbecker , Thomas Gleixner , Anna-Maria Behnsen , John Stultz , Stephen Boyd , Miguel Ojeda , Alex Gaynor , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Fiona Behrens Subject: [PATCH v4 3/7] rust: hrtimer: Add HrTimerCallbackContext and ::forward() Date: Tue, 29 Apr 2025 17:44:39 -0400 Message-ID: <20250429214752.1637859-4-lyude@redhat.com> In-Reply-To: <20250429214752.1637859-1-lyude@redhat.com> References: <20250429214752.1637859-1-lyude@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 Content-Type: text/plain; charset="utf-8" With Linux's hrtimer API, there's a number of methods that can only be called in two situations: * When we have exclusive access to the hrtimer and it is not currently active * When we're within the context of an hrtimer callback context This commit handles the second situation and implements hrtimer_forward() support in the context of a timer callback. We do this by introducing a HrTimerCallbackContext type which is provided to users during the RawHrTimerCallback::run() callback, and then add a forward() function to the type. Signed-off-by: Lyude Paul --- V2: * Improve SAFETY comments for HrTimerCallbackContext uses (I forgot to mention that we're within RawHrTimerCallback::run() * Split forward into forward() and raw_forward() since we're going to have two contexts that we can call forward() from now. * Clarify contexts in which certain hrtimer methods can be called. * Make sure that we use a mutable reference for forward() here - just in case :). * Rename interval to duration V3: * Rename duration -back- to interval (now that I actually have read hrtimer_forward's source, interval does make more sense than duration considering the fact we return the number of overruns that occurred according to the given interval). * Rewrite documentation a bit (re: Andreas) Signed-off-by: Lyude Paul --- rust/kernel/time/hrtimer.rs | 59 ++++++++++++++++++++++++++++- rust/kernel/time/hrtimer/arc.rs | 9 ++++- rust/kernel/time/hrtimer/pin.rs | 9 ++++- rust/kernel/time/hrtimer/pin_mut.rs | 11 +++++- rust/kernel/time/hrtimer/tbox.rs | 9 ++++- 5 files changed, 90 insertions(+), 7 deletions(-) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index 76caf9fae8170..b0304b2cf2da9 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -73,7 +73,7 @@ time::{Delta, Instant}, types::Opaque, }; -use core::marker::PhantomData; +use core::{marker::PhantomData, ptr::NonNull}; use pin_init::PinInit; =20 /// A timer backed by a C `struct hrtimer`. @@ -322,7 +322,10 @@ pub trait HrTimerCallback { type Pointer<'a>: RawHrTimerCallback; =20 /// Called by the timer logic when the timer fires. - fn run(this: as RawHrTimerCallback>::CallbackTarget= <'_>) -> HrTimerRestart + fn run( + this: as RawHrTimerCallback>::CallbackTarget<'_= >, + ctx: HrTimerCallbackContext<'_, T>, + ) -> HrTimerRestart where Self: Sized; } @@ -515,6 +518,58 @@ fn into_c(self) -> bindings::hrtimer_mode { } } =20 +/// Privileged smart-pointer for a [`HrTimer`] callback context. +/// +/// Many [`HrTimer`] methods can only be called in two situations: +/// +/// * When the caller has exclusive access to the `HrTimer` and the `HrTim= er` is guaranteed not to +/// be running. +/// * From within the context of an `HrTimer`'s callback method. +/// +/// This type provides access to said methods from within a timer callback= context. +/// +/// # Invariants +/// +/// * The existence of this type means the caller is currently within the = callback for an +/// [`HrTimer`]. +/// * `self.0` always points to a live instance of [`HrTimer`]. +pub struct HrTimerCallbackContext<'a, T>(NonNull>, PhantomData<= &'a ()>); + +impl<'a, T> HrTimerCallbackContext<'a, T> { + /// Create a new [`HrTimerCallbackContext`]. + /// + /// # Safety + /// + /// This function relies on the caller being within the context of a t= imer callback, so it must + /// not be used anywhere except for within implementations of [`RawHrT= imerCallback::run`]. The + /// caller promises that `timer` points to a valid initialized instanc= e of + /// [`bindings::hrtimer`]. + pub(crate) unsafe fn from_raw(timer: *mut HrTimer) -> Self { + // SAFETY: The caller guarantees `timer` is a valid pointer to an = initialized + // `bindings::hrtimer` + Self(unsafe { NonNull::new_unchecked(timer) }, PhantomData) + } + + /// Conditionally forward the timer. + /// + /// If the timer expires after `now`, this function does nothing and r= eturns 0. If the timer + /// expired at or before `now`, this function forwards the timer by `i= nterval` until the timer + /// expires after `now` and then returns the number of times the timer= was forwarded by + /// `interval`. + /// + /// This function is mainly useful for timer types which can provide e= xclusive access to the + /// timer when the timer is not running. For forwarding the timer when= you have exclusive access + /// to the timer, see [`HrTimer::forward()`]. + /// + /// Returns the number of overruns that occurred as a result of the ti= mer expiry change. + pub fn forward(&mut self, now: Instant, interval: Delta) -> u64 { + // SAFETY: + // - We are guaranteed to be within the context of a timer callbac= k by our type invariants + // - By our type invariants, `self.0` always points to a valid `Hr= Timer` + unsafe { HrTimer::::raw_forward(self.0.as_ptr(), now, interval)= } + } +} + /// Use to implement the [`HasHrTimer`] trait. /// /// See [`module`] documentation for an example. diff --git a/rust/kernel/time/hrtimer/arc.rs b/rust/kernel/time/hrtimer/arc= .rs index acc70a0ea1be5..f30efd2d1094d 100644 --- a/rust/kernel/time/hrtimer/arc.rs +++ b/rust/kernel/time/hrtimer/arc.rs @@ -3,6 +3,7 @@ use super::HasHrTimer; use super::HrTimer; use super::HrTimerCallback; +use super::HrTimerCallbackContext; use super::HrTimerHandle; use super::HrTimerPointer; use super::RawHrTimerCallback; @@ -95,6 +96,12 @@ impl RawHrTimerCallback for Arc // allocation from other `Arc` clones. let receiver =3D unsafe { ArcBorrow::from_raw(data_ptr) }; =20 - T::run(receiver).into_c() + // SAFETY: + // - By C API contract `timer_ptr` is the pointer that we passed w= hen queuing the timer, so + // it is a valid pointer to a `HrTimer` embedded in a `T`. + // - We are within `RawHrTimerCallback::run` + let context =3D unsafe { HrTimerCallbackContext::from_raw(timer_pt= r) }; + + T::run(receiver, context).into_c() } } diff --git a/rust/kernel/time/hrtimer/pin.rs b/rust/kernel/time/hrtimer/pin= .rs index dba22d11a95f5..c694a96fcd9d5 100644 --- a/rust/kernel/time/hrtimer/pin.rs +++ b/rust/kernel/time/hrtimer/pin.rs @@ -3,6 +3,7 @@ use super::HasHrTimer; use super::HrTimer; use super::HrTimerCallback; +use super::HrTimerCallbackContext; use super::HrTimerHandle; use super::RawHrTimerCallback; use super::UnsafeHrTimerPointer; @@ -99,6 +100,12 @@ impl<'a, T> RawHrTimerCallback for Pin<&'a T> // here. let receiver_pin =3D unsafe { Pin::new_unchecked(receiver_ref) }; =20 - T::run(receiver_pin).into_c() + // SAFETY: + // - By C API contract `timer_ptr` is the pointer that we passed w= hen queuing the timer, so + // it is a valid pointer to a `HrTimer` embedded in a `T`. + // - We are within `RawHrTimerCallback::run` + let context =3D unsafe { HrTimerCallbackContext::from_raw(timer_pt= r) }; + + T::run(receiver_pin, context).into_c() } } diff --git a/rust/kernel/time/hrtimer/pin_mut.rs b/rust/kernel/time/hrtimer= /pin_mut.rs index aeff8e102e1dc..82242c79ee433 100644 --- a/rust/kernel/time/hrtimer/pin_mut.rs +++ b/rust/kernel/time/hrtimer/pin_mut.rs @@ -1,7 +1,8 @@ // SPDX-License-Identifier: GPL-2.0 =20 use super::{ - HasHrTimer, HrTimer, HrTimerCallback, HrTimerHandle, RawHrTimerCallbac= k, UnsafeHrTimerPointer, + HasHrTimer, HrTimer, HrTimerCallback, HrTimerCallbackContext, HrTimerH= andle, + RawHrTimerCallback, UnsafeHrTimerPointer, }; use crate::time::Instant; use core::{marker::PhantomData, pin::Pin, ptr::NonNull}; @@ -103,6 +104,12 @@ impl<'a, T> RawHrTimerCallback for Pin<&'a mut T> // here. let receiver_pin =3D unsafe { Pin::new_unchecked(receiver_ref) }; =20 - T::run(receiver_pin).into_c() + // SAFETY: + // - By C API contract `timer_ptr` is the pointer that we passed w= hen queuing the timer, so + // it is a valid pointer to a `HrTimer` embedded in a `T`. + // - We are within `RawHrTimerCallback::run` + let context =3D unsafe { HrTimerCallbackContext::from_raw(timer_pt= r) }; + + T::run(receiver_pin, context).into_c() } } diff --git a/rust/kernel/time/hrtimer/tbox.rs b/rust/kernel/time/hrtimer/tb= ox.rs index 3df4e359e9bb0..c5c952721cee2 100644 --- a/rust/kernel/time/hrtimer/tbox.rs +++ b/rust/kernel/time/hrtimer/tbox.rs @@ -3,6 +3,7 @@ use super::HasHrTimer; use super::HrTimer; use super::HrTimerCallback; +use super::HrTimerCallbackContext; use super::HrTimerHandle; use super::HrTimerPointer; use super::RawHrTimerCallback; @@ -115,6 +116,12 @@ impl RawHrTimerCallback for Pin> // `data_ptr` exist. let data_mut_ref =3D unsafe { Pin::new_unchecked(&mut *data_ptr) }; =20 - T::run(data_mut_ref).into_c() + // SAFETY: + // - By C API contract `timer_ptr` is the pointer that we passed w= hen queuing the timer, so + // it is a valid pointer to a `HrTimer` embedded in a `T`. + // - We are within `RawHrTimerCallback::run` + let context =3D unsafe { HrTimerCallbackContext::from_raw(timer_pt= r) }; + + T::run(data_mut_ref, context).into_c() } } --=20 2.48.1 From nobody Sat Feb 7 17:48:36 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7ABA7264609 for ; Tue, 29 Apr 2025 21:48:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745963312; cv=none; b=l+9sPYbTd1SB91iEsTH0rpUVBP2DylSo0pZfTiaX/dAwlcoav8Z5NWMFYmyOXb8llPS6pIZXC8l4scE5vfY1oxKXCI5uV4vj3CaXV1/4qeM265ydJUhspK1rPwpr8zBrUY0Hu1bg0F6n1if/bK5OY19sBdMP7j+g0XMUDczEgok= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745963312; c=relaxed/simple; bh=VVGfYvtNB+DqgQn8Dz0h8wpBYiXA/auxzT7EcUd/bLI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ItX7lHqjJEdu14vO8llMCaaipJHiK110+gIAOLmM6zhT8Eyq22THASUkFMunmYjvna4fNzqJ/CEBn/PZHkpeLGl0J6AWEZc1yo2uJPBib6RZVEhSbdFaw9Y/MNMv5Dsq9XQxlNZ/CCOa9UQVLrpwJUSqSYgyKrzcoNLPanH6VU4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=fPvdU812; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="fPvdU812" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1745963309; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mwV+qgsVrBGX1S4J98iXiHKoHWOuHRCS7QjqtlcZ2iU=; b=fPvdU812xIN852nmaMg+b9YsdBTufuvfQzFTLiAYdonyEx/5BsZF6ZUTrQ45Pce6YBgGxD 7NGgenA0bMQtptbzRP32gJoiMmVbt/sObTKmmpfd4+EMPeiBFGh8gcSnDQ4IdLhXWGt4Ug KtaqkqNKaFkA3uuKiA0wjSwTsNpYWqg= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-584-oag8ADNhP-OVvQTBsXqM5A-1; Tue, 29 Apr 2025 17:48:25 -0400 X-MC-Unique: oag8ADNhP-OVvQTBsXqM5A-1 X-Mimecast-MFC-AGG-ID: oag8ADNhP-OVvQTBsXqM5A_1745963303 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D19BA1800374; Tue, 29 Apr 2025 21:48:22 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.88.132]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 322C819560AF; Tue, 29 Apr 2025 21:48:20 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, Andreas Hindborg , linux-kernel@vger.kernel.org Cc: Boqun Feng , FUJITA Tomonori , Frederic Weisbecker , Thomas Gleixner , Anna-Maria Behnsen , John Stultz , Stephen Boyd , Miguel Ojeda , Alex Gaynor , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich Subject: [PATCH v4 4/7] rust: time: Add Instant::from_nanos() Date: Tue, 29 Apr 2025 17:44:40 -0400 Message-ID: <20250429214752.1637859-5-lyude@redhat.com> In-Reply-To: <20250429214752.1637859-1-lyude@redhat.com> References: <20250429214752.1637859-1-lyude@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 Content-Type: text/plain; charset="utf-8" For implementing Rust bindings which can return a point in time. Signed-off-by: Lyude Paul --- V4: * Turn from_nanos() into an unsafe function in order to ensure that we uphold the invariants of Instant Signed-off-by: Lyude Paul --- rust/kernel/time.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 8d6aa88724ad8..6dcb65ed954db 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -83,6 +83,21 @@ pub fn elapsed(&self) -> Delta { pub(crate) fn as_nanos(self) -> i64 { self.inner } + + /// Create an `Instant` from a time duration specified in nano seconds. + /// + /// # Safety + /// + /// The caller promises that `nanos` is in the range from 0 to `KTIME_= MAX`. + #[expect(unused)] + #[inline] + pub(crate) unsafe fn from_nanos(nanos: i64) -> Self { + // INVARIANT: Our safety contract ensures that `nanos` is in the r= ange from 0 to + // `KTIME_MAX`. + Self { + inner: nanos as bindings::ktime_t, + } + } } =20 impl core::ops::Sub for Instant { --=20 2.48.1 From nobody Sat Feb 7 17:48:36 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8584D270ED5 for ; Tue, 29 Apr 2025 21:48:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745963314; cv=none; b=rhaeyu4nVuZwCxNM4hG3iKuYiPNHbQbSlPMXLRDx8AFu9FSZBPfnMPu+Hz5CYYyK89G1sgynNnxUiYBPEgqTRptwjVs1l2EUsO1P43X2rNVGYY8M4MdwYJRlEyhloNkM8cIyyC0KDFKAvAF2xPzBSHXGaWuQcmunozMVRTrexaY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745963314; c=relaxed/simple; bh=boKpxPdq27k+oAkg/UOCG61wHmXN1RDEa6N4ZOXYkiI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=u932NUGdvlzjcRqAyrJd7LY79W0tCiRTcrhs+wqO+WU5IRVCnsDPnjHNdXQgoN0skGBjkaGWmdWA+pNvXw0j1IpgMWt81/geiPcl29R/KVKAYNzJacUXR8+iuLD41ysRYccd6ZNYFmH/QRO14sn/d/OPXxzgm98wae8HDUQWcT4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Rh9U8Hfn; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Rh9U8Hfn" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1745963311; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=MjTGTxy+2mRzzCLysTJFKthKDEummASAP6ilEo9uHW4=; b=Rh9U8HfnK6Mosq1PeV2S1gQrgAZpaxw+AzritIZhrMWNmGwResWcNdZk9lDHE4Gb1HqJ8I GeL2rZXRGYP0Vd5ICxeilWM+cx8OnfqlqcpBGn0lgRwyZZCTx867kQoVLJWSB0TK3ihBNx F+py4rC6kjLXbp0DBtMHq5YbD0RkC4A= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-425-o_h5bBUeMHKJ2lHmDLkK6w-1; Tue, 29 Apr 2025 17:48:28 -0400 X-MC-Unique: o_h5bBUeMHKJ2lHmDLkK6w-1 X-Mimecast-MFC-AGG-ID: o_h5bBUeMHKJ2lHmDLkK6w_1745963305 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BA1F81800370; Tue, 29 Apr 2025 21:48:25 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.88.132]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 3EE8119560A3; Tue, 29 Apr 2025 21:48:23 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, Andreas Hindborg , linux-kernel@vger.kernel.org Cc: Boqun Feng , FUJITA Tomonori , Frederic Weisbecker , Thomas Gleixner , Anna-Maria Behnsen , John Stultz , Stephen Boyd , Miguel Ojeda , Alex Gaynor , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich Subject: [PATCH v4 5/7] rust: hrtimer: Add HrTimer::raw_cb_time() Date: Tue, 29 Apr 2025 17:44:41 -0400 Message-ID: <20250429214752.1637859-6-lyude@redhat.com> In-Reply-To: <20250429214752.1637859-1-lyude@redhat.com> References: <20250429214752.1637859-1-lyude@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 Content-Type: text/plain; charset="utf-8" This is a simple private unsafe wrapper for retrieving the current time according to the hrtimer_clock_base struct for a given timer. This will be used for implementing functions such as forward_now(), which rely on retrieving the current time from the hrtimer's clock base. Signed-off-by: Lyude Paul --- V2: - Convert safety comment to invariant comment in from_raw() - Add raw_clock_base() and implement clock_base() on HrTimer as well V4: - Drop HrTimerClockBase entirely, reword commit as this is now about adding raw_cb_time() Signed-off-by: Lyude Paul --- rust/kernel/time.rs | 1 - rust/kernel/time/hrtimer.rs | 24 ++++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 6dcb65ed954db..ce6a991ce7583 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -89,7 +89,6 @@ pub(crate) fn as_nanos(self) -> i64 { /// # Safety /// /// The caller promises that `nanos` is in the range from 0 to `KTIME_= MAX`. - #[expect(unused)] #[inline] pub(crate) unsafe fn from_nanos(nanos: i64) -> Self { // INVARIANT: Our safety contract ensures that `nanos` is in the r= ange from 0 to diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index b0304b2cf2da9..80270e14daafd 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -186,6 +186,30 @@ unsafe fn raw_forward(self_ptr: *mut Self, now: Instan= t, interval: Delta) -> u64 } } =20 + /// Retrieve the current time according to the `struct hrtimer_clock_b= ase` for `self_ptr`. + /// + /// # Safety + /// + /// - `self_ptr` must point to a valid `Self`. + /// - The caller must ensure that the `hrtimer_clock_base` cannot poss= ibly change in the context + /// this function is being called in. This means either exclusive ac= cess to `self_ptr` is + /// required, or we must be from within the timer callback context o= f `self_ptr`. + #[expect(unused)] + unsafe fn raw_cb_time(self_ptr: *const Self) -> Instant { + // SAFETY: We're guaranteed `self_ptr` points to a valid `Self` by= our safety contract. + let clock_base =3D unsafe { (*Self::raw_get(self_ptr)).base }; + + // SAFETY: The C API guarantees that `get_time` is initialized to = a valid function pointer + // for as long as we expose hrtimers to users. + let get_time_fn =3D unsafe { (*clock_base).get_time.unwrap_uncheck= ed() }; + + // SAFETY: + // - get_time_fn() returns a ktime_t, so we're guaranteed its retu= rn value is between `0` + // and `KTIME_MAX`. + // - get_time_fn() itself has no special requirements. + unsafe { Instant::from_nanos(get_time_fn()) } + } + /// Conditionally forward the timer. /// /// If the timer expires after `now`, this function does nothing and r= eturns 0. If the timer --=20 2.48.1 From nobody Sat Feb 7 17:48:36 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0E65A275108 for ; Tue, 29 Apr 2025 21:48:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745963317; cv=none; b=Mi3sUu1K0yqO7ax63UGS9KNdzkJtbNDKHgXo/zi/0xr9Ts7DzaOdo0fBU8YL8pYQezx70DxIataUOZILFpvx/A80UPnVavD3rg44VnOv0ERlZOsuMKASxI+5cLrVZdA1XxzkIbx7rTxLFGG5/HUv29fodfmDPaBcdaGs1Boljn4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745963317; c=relaxed/simple; bh=EEi47JhKU4scdAOtuIqQPebfCQhZyRg+10HsI94IKe8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EId0Les9Z7rVO5bdhF6IIdIbxw48qtZTJrgPOz+9NFfYNiG+E10jTTyMUxr+GvYe21m1LPHduttu2sHOqJhd8wlKLt9GXZD/2CbCtvB00nqB5fUyRbejI0GjIhOZ5M/9CnHcIeNWSQjmbQqqH37zM6YfrrZ1I+8D5JgxjBUUrdQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=O3PfSCxc; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="O3PfSCxc" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1745963314; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=kNsJKZYf8Jen4eNtgKuwwIYr6+X3oK52kaO2gEfl+Gg=; b=O3PfSCxcDmQKtFBCdR1mmN9cGfQXe05y4mplLLUzLu+7v4zeu78AAAfg+z29wGDqKFNBNF XITGGKoyy7FyGlCeTh0/32NhixC5ggtkeVrIuqrYFjngbktMBheJZ4pnXRPyDg0oouz5sS l7HwCQGIMwxu+laN7emNpGLsZ9Mrvog= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-193-p6GeKYtnOtO5McjA0fF90A-1; Tue, 29 Apr 2025 17:48:31 -0400 X-MC-Unique: p6GeKYtnOtO5McjA0fF90A-1 X-Mimecast-MFC-AGG-ID: p6GeKYtnOtO5McjA0fF90A_1745963308 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C2573180087B; Tue, 29 Apr 2025 21:48:28 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.88.132]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 22CDF19560A3; Tue, 29 Apr 2025 21:48:26 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, Andreas Hindborg , linux-kernel@vger.kernel.org Cc: Boqun Feng , FUJITA Tomonori , Frederic Weisbecker , Thomas Gleixner , Anna-Maria Behnsen , John Stultz , Stephen Boyd , Miguel Ojeda , Alex Gaynor , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich Subject: [PATCH v4 6/7] rust: hrtimer: Add forward_now() to HrTimer and HrTimerCallbackContext Date: Tue, 29 Apr 2025 17:44:42 -0400 Message-ID: <20250429214752.1637859-7-lyude@redhat.com> In-Reply-To: <20250429214752.1637859-1-lyude@redhat.com> References: <20250429214752.1637859-1-lyude@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 Content-Type: text/plain; charset="utf-8" Using the HrTimer::raw_time_cb() function, we can now add an equivalent to hrtimer_forward_now() to both HrTimer and HrTimerCallbackContext. Signed-off-by: Lyude Paul --- V2: * Change from Ktime to Delta * Make sure that forward_now() takes a mutable reference to the timer struct * Reword this to point out that we're adding forward_now() to both callback context and mutable timer reference * Rename interval to duration V4: * Fix rust documentation for HrTimerCallbackContext (forgot to update both forward_now() declarations) * Use Pin<&mut Self> for context-less forward. Signed-off-by: Lyude Paul --- rust/kernel/time/hrtimer.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index 80270e14daafd..0d93866d7b20a 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -194,7 +194,6 @@ unsafe fn raw_forward(self_ptr: *mut Self, now: Instant= , interval: Delta) -> u64 /// - The caller must ensure that the `hrtimer_clock_base` cannot poss= ibly change in the context /// this function is being called in. This means either exclusive ac= cess to `self_ptr` is /// required, or we must be from within the timer callback context o= f `self_ptr`. - #[expect(unused)] unsafe fn raw_cb_time(self_ptr: *const Self) -> Instant { // SAFETY: We're guaranteed `self_ptr` points to a valid `Self` by= our safety contract. let clock_base =3D unsafe { (*Self::raw_get(self_ptr)).base }; @@ -230,6 +229,18 @@ pub fn forward(self: Pin<&mut Self>, now: Instant, int= erval: Delta) -> u64 { // exclusive access to it - fulfilling the requirements of the C= API. unsafe { Self::raw_forward(self.get_unchecked_mut(), now, interval= ) } } + + /// Conditionally forward the timer. + /// + /// This is a variant of [`forward()`](Self::forward) that uses an int= erval after the current + /// time of the base clock for the [`HrTimer`]. + pub fn forward_now(self: Pin<&mut Self>, interval: Delta) -> u64 { + // SAFETY: `self` is a mutable reference, guaranteeing it is both = a valid pointer to Self + // and that we also have exclusive access to `self`. + let now =3D unsafe { Self::raw_cb_time(&*self.as_ref()) }; + + self.forward(now, interval) + } } =20 /// Implemented by pointer types that point to structs that contain a [`Hr= Timer`]. @@ -592,6 +603,22 @@ pub fn forward(&mut self, now: Instant, interval: Delt= a) -> u64 { // - By our type invariants, `self.0` always points to a valid `Hr= Timer` unsafe { HrTimer::::raw_forward(self.0.as_ptr(), now, interval)= } } + + /// Conditionally forward the timer. + /// + /// This is a variant of [`HrTimerCallbackContext::forward()`] that us= es an interval after the + /// current time of the base clock for the [`HrTimer`]. + pub fn forward_now(&mut self, duration: Delta) -> u64 { + self.forward( + // SAFETY: + // - We're guaranteed `self.0` points to a valid `HrTimer` = instance by type + // invariants. + // - We're guaranteed to be within the context of the timer ca= llback by our type + // invariants. + unsafe { HrTimer::::raw_cb_time(self.0.as_ptr()) }, + duration, + ) + } } =20 /// Use to implement the [`HasHrTimer`] trait. --=20 2.48.1 From nobody Sat Feb 7 17:48:36 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1F64A2741CF for ; Tue, 29 Apr 2025 21:48:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745963319; cv=none; b=WVH/lYMT2nw4PVoZaMEcY4Gk/kT9byMuzXfRRphRzHN5ZptslbOT6YO+bgXCtipzDzYn5fA1jSWMV6WweTTyozcfUCNoWPseIIvVVFWC/o/+jdyXnnZJrfQskOvdUCfU2Dhbrck366pwti6S8q3xXeZHDA/QYb75UqwoRJncGgk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745963319; c=relaxed/simple; bh=LxDKiZxsWVn3MCgks6lAPdndhGyh67qFf+XcSfWRWnE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ajHr7OBWbNpxKrqa7JWe7jqysIghHVhsJbx3Us4rZdDzbTv+vXiO6WWiLkh0CTwesGzzjz2mhtcUBrSR3a8au/NAYxW5+vXEq3570NceLyfom4dOD9H6V+xFuWIphndPYJfN/CYZRfUXTj0ji0UhALabzrmK+lV41RNCReCA7uc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=iCoCvNYe; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="iCoCvNYe" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1745963317; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=YSxKqBS0jBP5OLr/ybCVe+hncFTUSwMxnhYHJYY0f+c=; b=iCoCvNYeFvpal7j29olxwF8EgpCJiMOFbRH301v+trGm/E77oCFvh/KOtn5kwL9TF3JCgj d1ROHtHsrbJ79kUS+1r2H09oBFeY22Bw3av5rd7zDM2ypWombLf7vo4hzuPEFdDQdYJ9uH xzoL0+T79wc6dcxHLTkm5tt1fU2owgw= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-323--EvGlmiQPZqq6ziMAosMNw-1; Tue, 29 Apr 2025 17:48:33 -0400 X-MC-Unique: -EvGlmiQPZqq6ziMAosMNw-1 X-Mimecast-MFC-AGG-ID: -EvGlmiQPZqq6ziMAosMNw_1745963311 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 8728A18004A7; Tue, 29 Apr 2025 21:48:31 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.88.132]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 0BAC319560A3; Tue, 29 Apr 2025 21:48:28 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, Andreas Hindborg , linux-kernel@vger.kernel.org Cc: Boqun Feng , FUJITA Tomonori , Frederic Weisbecker , Thomas Gleixner , Anna-Maria Behnsen , John Stultz , Stephen Boyd , Miguel Ojeda , Alex Gaynor , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich Subject: [PATCH v4 7/7] rust: hrtimer: Add HrTimer::expires() Date: Tue, 29 Apr 2025 17:44:43 -0400 Message-ID: <20250429214752.1637859-8-lyude@redhat.com> In-Reply-To: <20250429214752.1637859-1-lyude@redhat.com> References: <20250429214752.1637859-1-lyude@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 Content-Type: text/plain; charset="utf-8" This adds the ability to read the expiry time of the given timer. Signed-off-by: Lyude Paul --- V2: * Convert from Ktime to Instant * Use read_volatile instead of read and add a FIXME V4: * Correct read_volatile comment in HrTimer::expires() Signed-off-by: Lyude Paul --- rust/kernel/time/hrtimer.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index 0d93866d7b20a..5b508ff81aee2 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -73,7 +73,10 @@ time::{Delta, Instant}, types::Opaque, }; -use core::{marker::PhantomData, ptr::NonNull}; +use core::{ + marker::PhantomData, + ptr::{addr_of, NonNull}, +}; use pin_init::PinInit; =20 /// A timer backed by a C `struct hrtimer`. @@ -136,7 +139,7 @@ unsafe fn raw_get(this: *const Self) -> *mut bindings::= hrtimer { // SAFETY: The field projection to `timer` does not go out of boun= ds, // because the caller of this function promises that `this` points= to an // allocation of at least the size of `Self`. - unsafe { Opaque::raw_get(core::ptr::addr_of!((*this).timer)) } + unsafe { Opaque::raw_get(addr_of!((*this).timer)) } } =20 /// Cancel an initialized and potentially running timer. @@ -241,6 +244,26 @@ pub fn forward_now(self: Pin<&mut Self>, interval: Del= ta) -> u64 { =20 self.forward(now, interval) } + + /// Return the time expiry for this [`HrTimer`]. + /// + /// This value should only be used as a snapshot, as the actual expiry= time could change after + /// this function is called. + pub fn expires(&self) -> Instant { + // SAFETY: `self` is an immutable reference and thus always points= to a valid `HrTimer`. + let c_timer_ptr =3D unsafe { HrTimer::raw_get(self) }; + + // SAFETY: + // - `node.expires` is a ktime_t, so it must be within the range o= f `0` to `KTIME_MAX`. + // - There's no actual locking here, a racy read is fine and expec= ted + unsafe { + Instant::from_nanos( + // This `read_volatile` is intended to correspond to a REA= D_ONCE call. + // FIXME(read_once): Replace with `read_once` when availab= le on the Rust side. + core::ptr::read_volatile(addr_of!((*c_timer_ptr).node.expi= res)), + ) + } + } } =20 /// Implemented by pointer types that point to structs that contain a [`Hr= Timer`]. --=20 2.48.1