From nobody Fri Dec 19 20:33:56 2025 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 668CF25229C for ; Tue, 15 Apr 2025 19:50:39 +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=1744746640; cv=none; b=GTgRqwasWoW5DqM3G+VZC6PzVaIXvAcMKlAgRkSyETw71fOITzLXQ0xtj+N401bcDDVPgdB3ZuxIb+hhCsYUyAGTKsjdPdGeq3sALLXLTsiqcVPonIBsgr4sLdl9x6txxW91fBOhy+6KngtJqAEKGd7D5HnjatqCS/4nUazK0H8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744746640; c=relaxed/simple; bh=1pFZgZUVzroWedN9r23GFxrL9lq5T+7jK1Nzx+c3C7A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Qzr+lSImKgvAKbPnitE7pgNh0gKfWkKcPET2i0UfE4IG2c8brDNQH8pm6qanBa9FCsmdY4KjNeD6H3C1N9sbcNoVJLEUBeh1VZXTRYRUonFEoBpqciFhC0yAN4chaV8bx844xneATI217pSbrfp16pabxxpBt/fJXC7qk2c5HRY= 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=MbKRphT9; 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="MbKRphT9" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1744746638; 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=dI4Ws7EHhkTkz0/bTYs4IgNA+1e1Pw41RQF94m79F10=; b=MbKRphT9iwGbE62gSFKLhg1uBbuLmVmUt+bypv32JErpmF6vysE2kieLbHp7vmhWFnfSy6 cIFflVvOAe2zSonBJz60ic6guEuvNIl1HXGK5dE/5UoeECq9B/63HCk5c4lCqZzqmicW9J I+0smKj7YZEnd+BYt0IDanuD1Ff/j9I= 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-142-YsO8sebeO9qsFhDBRjLr8w-1; Tue, 15 Apr 2025 15:50:34 -0400 X-MC-Unique: YsO8sebeO9qsFhDBRjLr8w-1 X-Mimecast-MFC-AGG-ID: YsO8sebeO9qsFhDBRjLr8w_1744746632 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 3AAB01956087; Tue, 15 Apr 2025 19:50:32 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.88.101]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 5C97E19560AD; Tue, 15 Apr 2025 19:50:27 +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 v2 1/8] rust: hrtimer: Document the return value for HrTimerHandle::cancel() Date: Tue, 15 Apr 2025 15:48:22 -0400 Message-ID: <20250415195020.413478-2-lyude@redhat.com> In-Reply-To: <20250415195020.413478-1-lyude@redhat.com> References: <20250415195020.413478-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 --- 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..bfe0e25f5abd0 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 whether or not the timer was running. fn cancel(&mut self) -> bool; } =20 --=20 2.48.1 From nobody Fri Dec 19 20:33:56 2025 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 7111D25229C for ; Tue, 15 Apr 2025 19:50:44 +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=1744746646; cv=none; b=qaTw4dEd5jnXkfUQhqFn8tijD/DXq2wwZs51fP0NL4RirG8piG3hw6oKzyNjcKAp08OneuUuaJfM8LmhH8NrO3BrTf93UOMAHiM7UHS/1RS77H+eUnQl8KF1FW2yh9VYe78FhYnORZwmCXxAiegu45kRNvEaqNC92uc5vWAyJLo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744746646; c=relaxed/simple; bh=BbdWQ0YbkE2htZOld0PCHaFrACpyTKErMgewUedBg3U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Iz2eT/YNFsGmm1F0ccb+/RtENyTVOIL4E2+UdQgJWayYiSPqDN89kubDfP4d5IPUAGz7nCh2SZ6sWThD4UKhZqgIfet1IZI3Zvvu/bTirun0vv23WkBxgLn5ZjA9M/aSYkCt5USGLK5TM2urC95EqbqA0w3Q+iOYmC0MhVJ05aM= 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=gNG4ImVw; 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="gNG4ImVw" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1744746643; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=9caeaEvOrQMjWI6dFVaM+47fZcpqeIYiQQAHDs+mWKA=; b=gNG4ImVwQbOzxpXLWNS+82DcrsR5c/PP9g2Obd6Fk7/A++9mPDHLisyLoRjToUDGrxCsWD SX9Zu/DDwe8FJ/uDzLCUq55j80CmSuKag6DkJ647o6gP3VaUxPqwQ8llG4SK5wjUVrfbd+ B+hNU7UemMevgOH00R0fcBZmW+2/29Y= 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-446-K2_kQgv8NMaDj3e4fNMfLw-1; Tue, 15 Apr 2025 15:50:39 -0400 X-MC-Unique: K2_kQgv8NMaDj3e4fNMfLw-1 X-Mimecast-MFC-AGG-ID: K2_kQgv8NMaDj3e4fNMfLw_1744746637 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 0C3D518007E1; Tue, 15 Apr 2025 19:50:37 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.88.101]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 924A519560AD; Tue, 15 Apr 2025 19:50:32 +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 v2 2/8] rust: hrtimer: Add HrTimer::raw_forward() and forward() Date: Tue, 15 Apr 2025 15:48:23 -0400 Message-ID: <20250415195020.413478-3-lyude@redhat.com> In-Reply-To: <20250415195020.413478-1-lyude@redhat.com> References: <20250415195020.413478-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-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 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. Since we can only retrieve a &mut reference to an HrTimer in contexts where it is not possible for the timer to be accessed by others or currently executing (e.g. a UniqueArc), a &mut is actually enough of a guarantee to safely fulfill the C API requirements here. Signed-off-by: Lyude Paul --- rust/kernel/time/hrtimer.rs | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index bfe0e25f5abd0..aadae8666f7ea 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,36 @@ 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`. + 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()) + } + } + + /// Forward the timer expiry so it expires at `duration` after `now`. + /// + /// This is mainly useful for timer types that can start off providing= a mutable reference (e.g. + /// `Pin>`) before the timer is started. + /// + /// Note that this does not requeue the timer, it simply updates its e= xpiry value. It returns + /// the number of overruns that have occurred as a result of the expir= y change. + pub fn forward(&mut self, now: Instant, duration: Delta) -> u64 { + // SAFETY: + // - Self is a mutable reference and thus always points to a valid= `HrTimer` + // - The only way we could hold a mutable reference to a `HrTimer<= T>` is if we have + // exclusive access to it, which means the timer is either idle = or we're within the + // timer callback context - fulfilling the requirements of the C= API. + unsafe { Self::raw_forward(self, now, duration) } + } } =20 /// Implemented by pointer types that point to structs that contain a [`Hr= Timer`]. --=20 2.48.1 From nobody Fri Dec 19 20:33:56 2025 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 AE0722512DC for ; Tue, 15 Apr 2025 19:51:01 +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=1744746663; cv=none; b=cw0UPmvZl8eeji/4sEpcIXl7eYw7BMpnV+o4Oc1ovikWEOoF3dhwk/v37iOKLLKCEk6iMnA7nIXpy6iSjB9QSRPnbbYgpqYQWuDk9pB8E9KnQFOQSK3FNMKUse/58acrEHw831GSKM1ECG9BDMBtOsRZA3vt46VKAwe/vRG/yzo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744746663; c=relaxed/simple; bh=7qcwkn32Fa0sfbLRLUeoYAai3MiiIaWCqHs+F9VCGZ8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MprIqeIdif50wYPTPcoaY66UTrmDvcTd0K44ekbBTqm0Q32eIQWPf1ZVqx0L/UoeuQf7on/Fjk8KniOFqTfpQc2kFu8p4t8KyeUTn2vmGXxuC5fI+BnOe3R6iICjXXiX/J8GTDAojbkNPDtnkdVZFBn59S759qtiKIJRw+yksZs= 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=htnxiwXR; 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="htnxiwXR" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1744746660; 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=2ryYGmJ3J41KdHRoTFjagDnmKVKLfVIOtjqIQ3uqQjs=; b=htnxiwXRHzmL18ESvinLfTeO6MJnDzgFwf0mrkMqmD+rQYLRhaSxL0+WOUUQSetgw7MFS2 6GOrmpLyDYN34pV2aoIXjQ57JjRa96fZCXNX68Oma9VhwEgr5CqVsT8eT6rVqk06lrgSAP jaAy+XtAex2KxoSQi6g2+Be5xpa7F+U= 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-250-zmeoNb1RNfaU2f96RZ3cZg-1; Tue, 15 Apr 2025 15:50:53 -0400 X-MC-Unique: zmeoNb1RNfaU2f96RZ3cZg-1 X-Mimecast-MFC-AGG-ID: zmeoNb1RNfaU2f96RZ3cZg_1744746651 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 37CDD1800A38; Tue, 15 Apr 2025 19:50:49 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.88.101]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 2EC3919560AD; Tue, 15 Apr 2025 19:50:45 +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 v2 3/8] rust: hrtimer: Add HrTimerCallbackContext and ::forward() Date: Tue, 15 Apr 2025 15:48:24 -0400 Message-ID: <20250415195020.413478-4-lyude@redhat.com> In-Reply-To: <20250415195020.413478-1-lyude@redhat.com> References: <20250415195020.413478-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 Signed-off-by: Lyude Paul --- rust/kernel/time/hrtimer.rs | 51 +++++++++++++++++++++++++++-- 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, 82 insertions(+), 7 deletions(-) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index aadae8666f7ea..507fff67f8ab2 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`. @@ -314,7 +314,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; } @@ -507,6 +510,50 @@ 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) + } + + /// Forward the timer expiry so it expires at `duration` after `now`. + /// + /// Note that this does not requeue the timer, it simply updates its e= xpiry value. It returns + /// the number of overruns that have occurred as a result of the expir= y change. + pub fn forward(&mut self, now: Instant, duration: 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, duration)= } + } +} + /// 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 Fri Dec 19 20:33:56 2025 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 22F99252912 for ; Tue, 15 Apr 2025 19:51:01 +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=1744746665; cv=none; b=tY4Z+252YyyA6qw7v/FjwcwjeswJcj1UYOizc2voXsBz3pXFpnBzzu20MZ8VuOzAFrDjzI65sCP+6vxCPFW/fJfJjrFiDKFKM3IeoQz8ITPEaJPlXHPoDt+PYpYK8XVVmpMfG+SzjdXtY3qR5Td8FqHQTUrLHpPb77UNNu7pSGc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744746665; c=relaxed/simple; bh=zchRka33d94ED3NLGmKD0KwCjnIWbYH40bk5JXAkI50=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VY4ld9yoKnwnGeFfE8CMEOxR2vNFpf035ShUJye3ZHM4UnfcWnRc/3m0nKNVGpzyVgd/Zi73QbdeW8KJPUrdz1UGg+Th/Vq/XoV5KVd1MJEM+Qw/PnQZG4dsMX+EXxhJS/1xCsUFMzx2xuBqTM4+Ui5bgNoM6sPMcme1ptRupeE= 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=VpLA4yOU; 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="VpLA4yOU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1744746660; 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=/ji2FnGc7GWmpbxp6bUnSlbcZu0xWECkZ+19vDopO8c=; b=VpLA4yOUcXAHOd+bJoTOEpd4OiBfYn8z9kuWWhsOxXI2mLxeMfmRQojq9xl6uUHACjgrtk 4LkAvYRNKupKKqUn/IhGtWrVaWVtoTMyuCdpKR9wkxTjhEKKft4sGM/03gpBteRIqnLyTw VtIy8xX7Qyrvy7kGO+O9FVklM3Lt2EM= Received: from mx-prod-mc-05.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-664-IHBt5OCyP2iiotkbdl0cNA-1; Tue, 15 Apr 2025 15:50:57 -0400 X-MC-Unique: IHBt5OCyP2iiotkbdl0cNA-1 X-Mimecast-MFC-AGG-ID: IHBt5OCyP2iiotkbdl0cNA_1744746653 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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 9FEFA195609E; Tue, 15 Apr 2025 19:50:53 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.88.101]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id AB33419560AD; Tue, 15 Apr 2025 19:50:49 +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 v2 4/8] rust: hrtimer: Add HrTimerClockBase Date: Tue, 15 Apr 2025 15:48:25 -0400 Message-ID: <20250415195020.413478-5-lyude@redhat.com> In-Reply-To: <20250415195020.413478-1-lyude@redhat.com> References: <20250415195020.413478-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 a simple wrapper for the hrtimer_clock_base struct, which can be obtained from a HrTimerCallbackContext. We'll use this in the next commit to add a function to acquire the current time for the base clock driving a hrtimer. 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 Signed-off-by: Lyude Paul --- rust/kernel/time/hrtimer.rs | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index 507fff67f8ab2..a56c66104f692 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -183,6 +183,25 @@ unsafe fn raw_forward(self_ptr: *mut Self, now: Instan= t, interval: Delta) -> u64 } } =20 + /// Retrieve the [`HrTimerClockBase`] for a given timer pointer. + /// + /// # Safety + /// + /// `self_ptr` must point to a valid `Self`. + unsafe fn raw_clock_base<'a>(self_ptr: *const Self) -> &'a HrTimerCloc= kBase { + // SAFETY: + // - By our safety contract, `self_ptr` always points to a valid `= HrTimer`. + // - `base` is initialized and points to a valid `hrtimer_clock_ba= se` for as long as + // `HrTimer` is exposed to users. + unsafe { HrTimerClockBase::from_raw((*Self::raw_get(self_ptr)).bas= e) } + } + + /// Retrieve the [`HrTimerClockBase`] for this [`HrTimer`]. + pub fn clock_base(&self) -> &HrTimerClockBase { + // SAFETY: `self` is an immutable reference and thus always points= to a valid `HrTimer` + unsafe { Self::raw_clock_base(self) } + } + /// Forward the timer expiry so it expires at `duration` after `now`. /// /// This is mainly useful for timer types that can start off providing= a mutable reference (e.g. @@ -200,6 +219,30 @@ pub fn forward(&mut self, now: Instant, duration: Delt= a) -> u64 { } } =20 +/// The timer base for a specific clock. +/// +/// # Invariants +/// +/// The layout of this type is equivalent to that of `struct hrtimer_clock= _base`. +#[repr(transparent)] +pub struct HrTimerClockBase(Opaque); + +impl HrTimerClockBase { + /// Retrieve a reference to a [`HrTimerClockBase`] from `ptr`. + /// + /// # Safety + /// + /// `ptr` must point to a live `struct hrtimer_clock_base`. + unsafe fn from_raw<'a>(ptr: *mut bindings::hrtimer_clock_base) -> &'a = Self { + // INVARIANT: + // - `ptr` is guaranteed to point to a live `struct hrtimer_clock_= base` by our safety + // contract. + // SAFETY: + // - Our data layout is equivalent to said struct via our type inv= ariants. + unsafe { &*ptr.cast() } + } +} + /// Implemented by pointer types that point to structs that contain a [`Hr= Timer`]. /// /// `Self` must be [`Sync`] because it is passed to timer callbacks in ano= ther @@ -542,6 +585,13 @@ pub(crate) unsafe fn from_raw(timer: *mut HrTimer) = -> Self { Self(unsafe { NonNull::new_unchecked(timer) }, PhantomData) } =20 + /// Get the [`HrTimerClockBase`] for the [`HrTimer`] associated with t= his + /// [`HrTimerCallbackContext`]. + pub fn clock_base(&self) -> &HrTimerClockBase { + // SAFETY: By our type invariants, `self.0` always points to a val= id `HrTimer`. + unsafe { HrTimer::::raw_clock_base(self.0.as_ptr()) } + } + /// Forward the timer expiry so it expires at `duration` after `now`. /// /// Note that this does not requeue the timer, it simply updates its e= xpiry value. It returns --=20 2.48.1 From nobody Fri Dec 19 20:33:56 2025 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 4D9FE2528E5 for ; Tue, 15 Apr 2025 19:51:06 +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=1744746669; cv=none; b=GFMTWwwAkHx0QQD1kdjPGOCgmbDUhN8fbf+gzGoNeIKTigLYFdnIa4/MmF5XFoMZepSCxawk9U62dnNchH2wqrTerK2ol2DzYEA1cyyqTjm9b5DGMBTdLxeKjnF/ZYAlwDQZespi+kSXBtOMLqUMbV/ewkYFGh9fQzcmOnwoeWQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744746669; c=relaxed/simple; bh=q+rFCQn/nCK6GK8pRaSsYmHQsuaeaGNABm4AEBpwNhM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=febHnZkWs/1V+fHRk/PW2Tqee4/BDKgikVDBz0XCF12JOe3YjrIS/dBuyipE6c3YR3305Yu9mC6kZDvdVcLs9uO1Isc03HEu7ZJyWKFMsmLIzPAVFm7tnawxh5/+IKDh5C903oTXNVW8EptSRFJdOtljVzFeHb2QI4leF+0FvRg= 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=Wos2R0of; 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="Wos2R0of" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1744746664; 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=rP0tcaBZdVdRNJOSEcl9UgtJffLuvgd7dyVPPNDPWv8=; b=Wos2R0ofZE8NqzPIPVdxpbd8O4QWZt3lBe4Fm3vL/l9ttB5jXC9kV9ONM9b8OPU6Jtv0ne ZAPCzPiSPf6Tax7HSyW2L/SytgwkUbH2Mm5kDgj5xZww+Bzi17R2sOuJuH18Lk2N7e4OOZ /u9zh0n1Pab/xA1IyeXzo6U5vToH0Sk= Received: from mx-prod-mc-04.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-625-SmHKAeNsOGmnD6GcoUFbAg-1; Tue, 15 Apr 2025 15:51:00 -0400 X-MC-Unique: SmHKAeNsOGmnD6GcoUFbAg-1 X-Mimecast-MFC-AGG-ID: SmHKAeNsOGmnD6GcoUFbAg_1744746658 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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D8ACB1955DDD; Tue, 15 Apr 2025 19:50:57 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.88.101]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 18CD41955BEF; Tue, 15 Apr 2025 19:50:53 +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 v2 5/8] rust: time: Add Instant::from_nanos() Date: Tue, 15 Apr 2025 15:48:26 -0400 Message-ID: <20250415195020.413478-6-lyude@redhat.com> In-Reply-To: <20250415195020.413478-1-lyude@redhat.com> References: <20250415195020.413478-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 --- rust/kernel/time.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 8d6aa88724ad8..545963140f180 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -83,6 +83,14 @@ pub fn elapsed(&self) -> Delta { pub(crate) fn as_nanos(self) -> i64 { self.inner } + + #[expect(unused)] + #[inline] + pub(crate) fn from_nanos(nanos: i64) -> Self { + Self { + inner: nanos as bindings::ktime_t, + } + } } =20 impl core::ops::Sub for Instant { --=20 2.48.1 From nobody Fri Dec 19 20:33:56 2025 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 E397C254855 for ; Tue, 15 Apr 2025 19:51: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=1744746675; cv=none; b=DcLcmPKcAjqeTcTPecNDybRj+rS3Ppj8FXN8NXXGlO3Exd1OgipudZO7bZkAtFYfOBdV04a0FWCKFvofCgLf/9CSJM/no2nJGkrghUpIPvGYNzEEQdp9/+YTBuSL6r6TOHjq5uE/2cAzt0Hqr+ImeQkSr5ek+m1wlS/nxM9Mcow= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744746675; c=relaxed/simple; bh=9+ZKX7BMpPCcKPmHdR+Ydn+RWm+AObN4sCEuLcun7UU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AF3NhJxBvRVQgHLVhMYR7WhvETTJJLPLZ7uZ/UzJbGVQ8QfdWTybL34mh6+L2BGwW8osIbRibgotwm230AGzoGoYLu6Y8NpqX0p2Bei6TBBOPPznB+0EX1o+dIn0pNXptyVj+nSj50GVpaAisg4tZe+/1XpgmyRGQAkbQ5iVCoQ= 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=YbeLv9t6; 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="YbeLv9t6" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1744746669; 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=/GUTMQLXiEZ1s45es323+Zi8DveURJQBpjAPFJ/wdxY=; b=YbeLv9t6TS4YhMffu+qXqjxJZh+CYRPnAkS0b+NEMAZIOqlq34Tp3SX4jI0vuCbuvnTyZ4 hXfu44JR+W89YbCQQCNnG2uooW4uqk/ow8CNqSiccUK3VP0aKsWm/LTwsNJOIuuB3ggRZx PcOTyy9KZXqlN+srANEK8ZYlLmXr228= 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-31-AcTJ1EYZN4632VSEKBAGPQ-1; Tue, 15 Apr 2025 15:51:03 -0400 X-MC-Unique: AcTJ1EYZN4632VSEKBAGPQ-1 X-Mimecast-MFC-AGG-ID: AcTJ1EYZN4632VSEKBAGPQ_1744746661 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 6700D180AF65; Tue, 15 Apr 2025 19:51:01 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.88.101]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 4FBFB19560AD; Tue, 15 Apr 2025 19:50: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 v2 6/8] rust: hrtimer: Add HrTimerClockBase::time() Date: Tue, 15 Apr 2025 15:48:27 -0400 Message-ID: <20250415195020.413478-7-lyude@redhat.com> In-Reply-To: <20250415195020.413478-1-lyude@redhat.com> References: <20250415195020.413478-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 a wrapper for the get_time() callback contained within a hrtimer_clock_base struct. We'll use this in the next commit in order to implement HrTimerCallbackContext::forward_now(). Signed-off-by: Lyude Paul --- V2: * Convert from Ktime to Instant * Don't forget to make HrTimerClockBase::time() pub Signed-off-by: Lyude Paul --- rust/kernel/time.rs | 1 - rust/kernel/time/hrtimer.rs | 10 ++++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 545963140f180..a9a29dd80fc28 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -84,7 +84,6 @@ pub(crate) fn as_nanos(self) -> i64 { self.inner } =20 - #[expect(unused)] #[inline] pub(crate) fn from_nanos(nanos: i64) -> Self { Self { diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index a56c66104f692..4da1e72e016d1 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -241,6 +241,16 @@ unsafe fn from_raw<'a>(ptr: *mut bindings::hrtimer_clo= ck_base) -> &'a Self { // - Our data layout is equivalent to said struct via our type inv= ariants. unsafe { &*ptr.cast() } } + + /// Retrieve the current time from this [`HrTimerClockBase`]. + pub fn time(&self) -> Instant { + // SAFETY: This callback is initialized to a valid NonNull functio= n for as long as this type + // is exposed to users. + let get_time_fn =3D unsafe { (*self.0.get()).get_time.unwrap_unche= cked() }; + + // SAFETY: This FFI function has no special requirements + Instant::from_nanos(unsafe { get_time_fn() }) + } } =20 /// Implemented by pointer types that point to structs that contain a [`Hr= Timer`]. --=20 2.48.1 From nobody Fri Dec 19 20:33:56 2025 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 738F3253B7B for ; Tue, 15 Apr 2025 19:51:21 +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=1744746686; cv=none; b=f7ayNvxwxrEanqtt/pvMf0SrMJ3RzdK518mh9MXK0Q/1OOXUU6pbfRSXWUu8Gqu4BlZNNZ0sdB8Xc5lQxSs6LQWDKRtmglupy9Q73u137uqLk9Eu+eC4Trb3aRM4be/IQtahTiQas7bK1PDrTBkxjbLRa9ZvRVvd0UUQ0zHgZx0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744746686; c=relaxed/simple; bh=0G1KuodMEyIcRnZKJ6Q+rj4XT5an2oMOznfG5sLkrtw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QtJeHU8sWqemyc6+/APyYZT76EcoZyWmGj6Q9c655D8mx55VcZSRo3giMjwgnc9YaLZ4+/02eD40FdBKdjEo0AZvPuqsUdw/8BLQqA2sJjW6qXL7Gy1tYetKkedSrtoNdJaj8/pEEVbainZm7CJTh+Yx6+WRxd/EB/etixoY2ok= 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=jE3ytDZD; 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="jE3ytDZD" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1744746679; 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=Tjk0+slSv3zs0lqjn04tupz+JEYT+G9QQhKT2WLYntg=; b=jE3ytDZDDJ29JJOLW3eacqQ9vrCin311gQ3y+nc+g6O0hMclOT5GvlhObPp8/WUtIGQcT8 44WseUlIA40x1SREdyfWCeKwBOrCyI4tBg/0uPj29KnuAVX445U7pAah6o6fU7X5a6XnkY SQu+7jiv7JgA4jJrn+d7DGDnuZwExpE= Received: from mx-prod-mc-05.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-274-CJhbr3v-Os-jgdMOWyh6JA-1; Tue, 15 Apr 2025 15:51:11 -0400 X-MC-Unique: CJhbr3v-Os-jgdMOWyh6JA-1 X-Mimecast-MFC-AGG-ID: CJhbr3v-Os-jgdMOWyh6JA_1744746665 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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E1A2419560B0; Tue, 15 Apr 2025 19:51:04 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.88.101]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C60AD1955BF6; Tue, 15 Apr 2025 19:51:01 +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 v2 7/8] rust: hrtimer: Add forward_now() to HrTimer and HrTimerCallbackContext Date: Tue, 15 Apr 2025 15:48:28 -0400 Message-ID: <20250415195020.413478-8-lyude@redhat.com> In-Reply-To: <20250415195020.413478-1-lyude@redhat.com> References: <20250415195020.413478-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 HrTimerClockBase::time() 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 Signed-off-by: Lyude Paul --- rust/kernel/time/hrtimer.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index 4da1e72e016d1..c84dcdacb4882 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -217,6 +217,14 @@ pub fn forward(&mut self, now: Instant, duration: Delt= a) -> u64 { // timer callback context - fulfilling the requirements of the C= API. unsafe { Self::raw_forward(self, now, duration) } } + + /// Forward the time expiry so it expires at `duration` after the curr= ent time. + /// + /// This is a variant of [`forward`](Self::forward) that uses a durati= on after the current time + /// of the [`HrTimerClockBase`] for this [`HrTimerCallbackContext`]. + pub fn forward_now(&mut self, duration: Delta) -> u64 { + self.forward(self.clock_base().time(), duration) + } } =20 /// The timer base for a specific clock. @@ -612,6 +620,14 @@ pub fn forward(&mut self, now: Instant, duration: 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, duration)= } } + + /// Forward the time expiry so it expires after now. + /// + /// This is a variant of [`HrTimerCallbackContext::forward()`] that us= es an interval after the + /// current time of the [`HrTimerClockBase`] for this [`HrTimerCallbac= kContext`]. + pub fn forward_now(&mut self, duration: Delta) -> u64 { + self.forward(self.clock_base().time(), duration) + } } =20 /// Use to implement the [`HasHrTimer`] trait. --=20 2.48.1 From nobody Fri Dec 19 20:33:56 2025 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 9DD6F25178B for ; Tue, 15 Apr 2025 19:51:21 +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=1744746684; cv=none; b=SDcw7TBC25I/W4F/cbG+GkL/PYi23SjNKAJ5NXzWJuAUNDxySW9lXDEAbOeFGtlUKn2EnEXB9fB8+foFuAJoree4wB18URJuEQXyEnhunLhmtH/+yiTOwUdHllArbgUhnjFMpzYG8N7lzl5aJbWhIE+zUDhlXHRyUKoX/qqScGw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744746684; c=relaxed/simple; bh=eQ+r648TrWpuEgvULfWc7sb7QOPoBlGuqKdN3eqAVsQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZhfUJWgXe2l2rxrVwnneMDMq2qEaTr3XYkATo72eft3T6Si9pKLknkgn8iw4TS0zGOHGolhhrsgrHTN752HVPz3g9fvhEmug7VjDui5wabQaE0kvNKQeoG9PcXEmqIhUR48+LOUf7KscnRzCiqG6IDcZYJBKNMxZr7ngskDeUik= 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=Im1u0Yib; 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="Im1u0Yib" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1744746679; 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=6lTDNYrXkRzbCWl+8hVKAKf9CSfOeUD8xTgHV59ocoE=; b=Im1u0YibVqjQmD9zlw5gDjI+s5Q2BNp2CvRCSqB50xuNfhbjfBZkgRqrYrJXaezQe6FsZ7 FYvohw8hDcf27ZlG1dQXU7zUXILgAsGt01GI4XG/PCfyC/qz22O86R7DBSmEtoakupMcEN bcJzFV7XyNB+6uI7OYr8LnnrnppO1oc= 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-226-2N7zl2tZMw-0O1P2LZCdTw-1; Tue, 15 Apr 2025 15:51:14 -0400 X-MC-Unique: 2N7zl2tZMw-0O1P2LZCdTw-1 X-Mimecast-MFC-AGG-ID: 2N7zl2tZMw-0O1P2LZCdTw_1744746668 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 69BB31956059; Tue, 15 Apr 2025 19:51:08 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.88.101]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 4AFDF1955BEF; Tue, 15 Apr 2025 19:51:05 +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 v2 8/8] rust: hrtimer: Add HrTimer::expires() Date: Tue, 15 Apr 2025 15:48:29 -0400 Message-ID: <20250415195020.413478-9-lyude@redhat.com> In-Reply-To: <20250415195020.413478-1-lyude@redhat.com> References: <20250415195020.413478-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 Signed-off-by: Lyude Paul --- rust/kernel/time/hrtimer.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index c84dcdacb4882..b8a74c15e6609 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. @@ -225,6 +228,21 @@ pub fn forward(&mut self, now: Instant, duration: Delt= a) -> u64 { pub fn forward_now(&mut self, duration: Delta) -> u64 { self.forward(self.clock_base().time(), duration) } + + /// 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: There's no actual locking here, a racy read is fine and= expected. + Instant::from_nanos(unsafe { + // FIXME: read_volatile + core::ptr::read_volatile(addr_of!((*c_timer_ptr).node.expires)) + }) + } } =20 /// The timer base for a specific clock. --=20 2.48.1