From nobody Fri Apr 4 11:43:54 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 9E8791F4628 for ; Wed, 2 Apr 2025 21:41:29 +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=1743630091; cv=none; b=W4aXmo3Qb7+XVRYZmzBaDEY/hSyILX8g0sebKpI5Rwj8Ak8rPDfd5fj5FZmxSgMw5lsvOR9mvbvmPtydycHyQwUZ2kk1f8OW+D0RMrcRaBKHe5Hnxq0RJEPmC340uRn9B0jiIXa49Beab2etBvu1v44QCcDEVnABZ8shjp1Aw2Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743630091; c=relaxed/simple; bh=9webYGdM454gxtHw9y7UrGofp4nqbdgFdzB3cD24yeo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=c9BzurXp8ViramNa4kVCCw5qrAkIlWlRIA9nEncH5ErL4yYnzmMtRdPIjd/5ww6KHO69EbVL2Pg9Ln4zAgqR/HpTc149+5y0DKdO3GtZVw2W8zCQRApXjK6TNaEOEzMgPy0oxWOcz/LD6zrGboHrkWVCNFIBHQCOGir4tV0njZw= 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=iJR/t8ZV; 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="iJR/t8ZV" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1743630088; 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=3S6L929NhUPmTnaNjv1n/DKXagAZ0HAL3Zw31NDUF20=; b=iJR/t8ZVYvT66XdSKjJqrRsqu59oWgNwfXQI216MhZw5ujVsnRrBdAVCb8F2bbP7U9l7EM Ggjstt+sIsS67AMBtSVliJ2nsIku1usSh5Xvy31OiTRbjraXYH9uD4JVdXW7Tk7vwCRyfT cuLQX9zlDQTXNd953JSYx7T+PdlYfgo= 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-413-mcQub3grOKWXvSpXfTPCdg-1; Wed, 02 Apr 2025 17:41:23 -0400 X-MC-Unique: mcQub3grOKWXvSpXfTPCdg-1 X-Mimecast-MFC-AGG-ID: mcQub3grOKWXvSpXfTPCdg_1743630081 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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 7EB2C195608B; Wed, 2 Apr 2025 21:41:21 +0000 (UTC) Received: from chopper.lyude.net (unknown [10.22.80.95]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id ABB1D3000704; Wed, 2 Apr 2025 21:41:18 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, Andreas Hindborg , linux-kernel@vger.kernel.org Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Thomas Gleixner Subject: [PATCH 1/6] rust: time: Add Ktime::from_ns() Date: Wed, 2 Apr 2025 17:40:29 -0400 Message-ID: <20250402214109.653341-2-lyude@redhat.com> In-Reply-To: <20250402214109.653341-1-lyude@redhat.com> References: <20250402214109.653341-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.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" A simple function to turn the provided value in nanoseconds into a Ktime value. We allow any type which implements Into, which resolves to Into. This is useful for some of the older DRM APIs that never got moved to Ktime. Signed-off-by: Lyude Paul Signed-off-by: Andreas Hindborg --- rust/kernel/time.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index f509cb0eb71e0..c05afda07a05f 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -1,5 +1,4 @@ -// SPDX-License-Identifier: GPL-2.0 - +// SPDX-License-Identifier: GPL-2.0=20 //! Time related primitives. //! //! This module contains the kernel APIs related to time and timers that @@ -9,6 +8,7 @@ //! C header: [`include/linux/ktime.h`](srctree/include/linux/ktime.h). =20 pub mod hrtimer; +use core::convert::Into; =20 /// The number of nanoseconds per millisecond. pub const NSEC_PER_MSEC: i64 =3D bindings::NSEC_PER_MSEC as i64; @@ -65,6 +65,12 @@ pub fn to_ns(self) -> i64 { pub fn to_ms(self) -> i64 { self.divns_constant::() } + + /// Creates a new Ktime from the given duration in nanoseconds. + #[inline] + pub fn from_nanos(ns: impl Into) -> Self { + Self { inner: ns.into() } + } } =20 /// Returns the number of milliseconds between two ktimes. --=20 2.48.1 From nobody Fri Apr 4 11:43:54 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 75C241F428C for ; Wed, 2 Apr 2025 21:41:31 +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=1743630093; cv=none; b=JnJvkKNL7/C3iHr7RUtAsvJ3bJHv3CQG4Qy2FaV0eMrRPhMk1s2n+mAImElaPRVX4iTrNAowhoVGiMb9nhC4YcoSOLEUHvzUQuRM/U6MrnaZi+wR6+mPq49O93KYbLf7BV2hW2uOeRlWqz7Xx4728PT/wM1bxPObNiAmV1tWSxg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743630093; c=relaxed/simple; bh=ZkZlqKh3TceDVJ4Clh8E+fEPxmXmVYODwVdNwB6xVMg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QSWSLXOtw2d9j/Ras0oEnWZ6s8cTrXK32zADzOUpKisNR8iOwtV+F1G5uDilx/HtL9NG/dJaV6KYu7Y0ukBSJ8h6YwIXxniRgvVAMm9av04bWHiX5C5kJ1InxzdQn/dP4YfE+Osgk81AD3FPc0hG26tyiOOu3MVYs7+sJTQBYXM= 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=chPi8+fk; 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="chPi8+fk" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1743630090; 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=FXmgHMVH1wAQ3fmowWavtpFj8y/HpxS5YV1/7Oqix2E=; b=chPi8+fkSA93REiPkrf/Lldgc0kxcbo1792VoDNPZ445bAjIPo3PLA8ydhrrhu9jt5gsvw CdO1t0bp1pjfwvkuK4gu1s1WmXKQE4yaSGcJ/scjcgebv1z4j1nAQUXisBfeE2Y4OcphrR IWzbOOhlz+tjj6J7nEdVO1W7MyTMCtU= 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-55-sjcS6BBnMGCnFnCANeXyNA-1; Wed, 02 Apr 2025 17:41:27 -0400 X-MC-Unique: sjcS6BBnMGCnFnCANeXyNA-1 X-Mimecast-MFC-AGG-ID: sjcS6BBnMGCnFnCANeXyNA_1743630085 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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 30C7019560AB; Wed, 2 Apr 2025 21:41:25 +0000 (UTC) Received: from chopper.lyude.net (unknown [10.22.80.95]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 0EE643000704; Wed, 2 Apr 2025 21:41:21 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, Andreas Hindborg , linux-kernel@vger.kernel.org Cc: Boqun Feng , Frederic Weisbecker , Thomas Gleixner , Anna-Maria Behnsen , Miguel Ojeda , Alex Gaynor , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross Subject: [PATCH 2/6] rust: hrtimer: Add HrTimerCallbackContext and ::forward() Date: Wed, 2 Apr 2025 17:40:30 -0400 Message-ID: <20250402214109.653341-3-lyude@redhat.com> In-Reply-To: <20250402214109.653341-1-lyude@redhat.com> References: <20250402214109.653341-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.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" With Linux's hrtimer API, certain functions require we either acquire proper locking to call specific methods - or that we call said methods from the context of the timer callback. hrtimer_forward() is one of these functions, so we start by adding a new HrTimerCallbackContext type which provides a way of calling these methods that is inaccessible outside of hrtimer callbacks. Signed-off-by: Lyude Paul --- rust/kernel/time/hrtimer.rs | 50 +++++++++++++++++++++++++++-- rust/kernel/time/hrtimer/arc.rs | 7 +++- rust/kernel/time/hrtimer/pin.rs | 7 +++- rust/kernel/time/hrtimer/pin_mut.rs | 9 ++++-- rust/kernel/time/hrtimer/tbox.rs | 7 +++- 5 files changed, 73 insertions(+), 7 deletions(-) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index 4fc49f1931259..c92b10524f892 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -69,7 +69,7 @@ =20 use super::ClockId; use crate::{init::PinInit, prelude::*, time::Ktime, types::Opaque}; -use core::marker::PhantomData; +use core::{marker::PhantomData, ptr::NonNull}; =20 /// A timer backed by a C `struct hrtimer`. /// @@ -279,7 +279,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; } @@ -470,6 +473,49 @@ fn into_c(self) -> bindings::hrtimer_mode { } } =20 +/// Privileged smart-pointer for a [`HrTimer`] callback context. +/// +/// This provides access to various methods for a [`HrTimer`] which can on= ly be safely called within +/// its callback. +/// +/// # Invariants +/// +/// * The existence of this type means the caller is currently within the = callback for a +/// [`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) + } + + /// Get the raw `bindings::hrtimer` pointer for this [`HrTimerCallback= Context`]. + pub(crate) fn raw_get_timer(&self) -> *mut bindings::hrtimer { + // SAFETY: By our type invariants, `self.0` always points to a val= id [`HrTimer`]. + unsafe { HrTimer::raw_get(self.0.as_ptr()) } + } + + /// Forward the timer expiry so it will expire in the future. + /// + /// 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(&self, now: Ktime, interval: Ktime) -> u64 { + // SAFETY: The C API requirements for this function are fulfilled = by our type invariants. + unsafe { bindings::hrtimer_forward(self.raw_get_timer(), now.to_ns= (), interval.to_ns()) } + } +} + /// 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 4a984d85b4a10..7dd9f46a0720d 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,10 @@ 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 `ptr` is the pointer we passed when q= ueuing the timer, so it is + // a `HrTimer` embedded in a `T`. + 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 f760db265c7b5..a8e1b76bf0736 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,10 @@ 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 `ptr` is the pointer we passed when q= ueuing the timer, so it is + // a `HrTimer` embedded in a `T`. + 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 90c0351d62e4b..2dd2ebfd7efaf 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::Ktime; use core::{marker::PhantomData, pin::Pin, ptr::NonNull}; @@ -103,6 +104,10 @@ 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 `ptr` is the pointer we passed when q= ueuing the timer, so it is + // a `HrTimer` embedded in a `T`. + 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 2071cae072342..e3214f7173beb 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,10 @@ 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 `ptr` is the pointer we passed when q= ueuing the timer, so it is + // a `HrTimer` embedded in a `T`. + 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 Apr 4 11:43:54 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 2FB301F540F for ; Wed, 2 Apr 2025 21:41:36 +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=1743630098; cv=none; b=sZgs22nhEBu0hNza8XiPLby+h7zqkHFsgKlEWmJXv6PxvrJThNnJFVWhj4I4UATSwUZDI2qPJWKtPxn7gYPBuIWMW+jHnaYB2XqocF4vLp/xcven8M3F4WUIKPRSYq8zu/8AqnaE5rp7mvxcJxvhUteNX80cnfQezEmbRXHqAJQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743630098; c=relaxed/simple; bh=Mt8jE/SZhTju8949LLhJnwf1Rcrul0/sLIfum+k63Lg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=D/GndbDGl0Q6iVjKGfUVxxYqbeJ4/zV1CviCdeyzjcG5yAREZ3Z4AHfdZQIcEvgCdknUjbfupxARtLUVUdyTe/NxvnGym8BtaE8EKSM/g3DQEnifIaO6Bfy8fTl+ltQBNMMpH86v1RIzbE1dePcLBfv+stVQ+IrnqaXjHmvaE7o= 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=Xyxxd5bN; 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="Xyxxd5bN" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1743630096; 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=MC4jjMAzL+F+yN9B7xbTp8xSqrSEsEpBfudmMEov+PI=; b=Xyxxd5bNxP+RJLtF4TsFdjzckRx8Gb40Y62908puJfOO9GABJoB+ZFHlhV3nX7tEo2D5FS UQY/yPSgXc9qlvoHsJlagOvYyaWOIKdVvnGIP7B8mnjL3k4NlABUZGOMGh6tt+/Q0NVhAL os6PbAFf60MIuiE6MZ4nfvW4UTCXRzU= 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-675-vY0zSiOWOe6kn0CqcftJkQ-1; Wed, 02 Apr 2025 17:41:31 -0400 X-MC-Unique: vY0zSiOWOe6kn0CqcftJkQ-1 X-Mimecast-MFC-AGG-ID: vY0zSiOWOe6kn0CqcftJkQ_1743630089 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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 DC2DA1955DC5; Wed, 2 Apr 2025 21:41:28 +0000 (UTC) Received: from chopper.lyude.net (unknown [10.22.80.95]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 9DC983000704; Wed, 2 Apr 2025 21:41:25 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, Andreas Hindborg , linux-kernel@vger.kernel.org Cc: Boqun Feng , Frederic Weisbecker , Thomas Gleixner , Anna-Maria Behnsen , Miguel Ojeda , Alex Gaynor , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross Subject: [PATCH 3/6] rust: hrtimer: Add HrTimerClockBase Date: Wed, 2 Apr 2025 17:40:31 -0400 Message-ID: <20250402214109.653341-4-lyude@redhat.com> In-Reply-To: <20250402214109.653341-1-lyude@redhat.com> References: <20250402214109.653341-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.4.1 on 10.30.177.4 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 --- rust/kernel/time/hrtimer.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index c92b10524f892..f633550882247 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -165,6 +165,29 @@ pub(crate) unsafe fn raw_cancel(this: *const Self) -> = bool { } } =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 { + // SAFETY: + // - `ptr` is guaranteed to point to a live `struct hrtimer_clock_= base` by our safety + // contract. + // - 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 @@ -506,6 +529,15 @@ pub(crate) fn raw_get_timer(&self) -> *mut bindings::h= rtimer { unsafe { HrTimer::raw_get(self.0.as_ptr()) } } =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 valid `Hr= Timer`. + // - `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_timer()).base) } + } + /// Forward the timer expiry so it will expire in the future. /// /// Note that this does not requeue the timer, it simply updates its e= xpiry value. It returns --=20 2.48.1 From nobody Fri Apr 4 11:43:54 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 83F981F7076 for ; Wed, 2 Apr 2025 21:41:41 +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=1743630104; cv=none; b=XQqlLiJ1dM+ZcRrvgvRmXc2Zu/OC5+y08shVpiuAHAV53M+qJ5ejNjzZC/5EimHAGOmDSiHwo3pHCEP0eiy2zacokQM4Kw9ZYDeh4SQVXA06/yuOy8N5HgT/0UoWJyw1d0xyv/aW3UVgk7wQslzqtFjarhxXmBQaiWhtfv0/vJM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743630104; c=relaxed/simple; bh=vShHOWeitIbxplb9p87vnjIjjfuH54NQw+mD4RqzBcA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GyedYghFnMjgKYnvDtCvBhmWP8n2swkBqketAcFFxMwteSrzohSuRZO+yHlyHYrZzmKz9gIvkbz75HKgtr29XM/zZ14y9rCcl1zPmDaX/x7mEqyn/kD5PCVYIbsqyT3yRl5jDBYLlYVnxRHyzGLyWW+QBtpPffLSTV181jD4BZE= 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=MSnbfa+1; 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="MSnbfa+1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1743630100; 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=n5nfhRkfaRcNmMXeHmWOJewhTgQsNNEyzLVy0F5Lha0=; b=MSnbfa+1JsbgICRmUQcAKVmLdkwNoolpwOhahbKrquIRgOXxHMtvOyJ6oqGk9LZnI2K67L UTBwP5pgsn2TFTNX35EB6NE5p2bnBSPahJHLF9j6aMBBB4l4hrjyxISZbp/g94So7NyGnT 5DnnRMwDV41dpRiCCQAZLkn+Ua/B7rg= 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-687--R6tZeW9NKOMopszrX3KMw-1; Wed, 02 Apr 2025 17:41:34 -0400 X-MC-Unique: -R6tZeW9NKOMopszrX3KMw-1 X-Mimecast-MFC-AGG-ID: -R6tZeW9NKOMopszrX3KMw_1743630092 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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 2B2AC1800263; Wed, 2 Apr 2025 21:41:32 +0000 (UTC) Received: from chopper.lyude.net (unknown [10.22.80.95]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 35CCA3000704; Wed, 2 Apr 2025 21:41:29 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, Andreas Hindborg , linux-kernel@vger.kernel.org Cc: Boqun Feng , Frederic Weisbecker , Thomas Gleixner , Anna-Maria Behnsen , Miguel Ojeda , Alex Gaynor , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross Subject: [PATCH 4/6] rust: hrtimer: Add HrTimerClockBase::time() Date: Wed, 2 Apr 2025 17:40:32 -0400 Message-ID: <20250402214109.653341-5-lyude@redhat.com> In-Reply-To: <20250402214109.653341-1-lyude@redhat.com> References: <20250402214109.653341-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.4.1 on 10.30.177.4 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 --- rust/kernel/time/hrtimer.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index f633550882247..521ff1a8a5aa8 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -186,6 +186,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`]. + fn time(&self) -> Ktime { + // 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 + Ktime::from_raw(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 Apr 4 11:43:54 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 683441F5822 for ; Wed, 2 Apr 2025 21:41:39 +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=1743630100; cv=none; b=hy8VFh7Vge9axxOzu00Nu3jtTp1Y0WlTqSwX6/oggLlqTtMLRQ11MgFAaYZsWf0jgmgQuSOecDCMEOA4rmE33R7VYQniAf2IwAthE1EOJ+0mkh5Zt4cotylXmLjNG1dQv/o3YA+OogpGTPbzSyfD9vh8JF/BED6gxg2JDtdv0eQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743630100; c=relaxed/simple; bh=kjwxKKeg1TEWoo7nt7NYEf0BLypObdO0+HBpB+tBaNg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=W2d/B7itnm4er0dmUoODkFA/lnneA0PNAT3+MtrZLs6j2mcmjcseX+fRHioqRrNRD67VYCIqz63B710QFNFatNzOq6eylim2we6AGrvktua1UNyltBbvO+iWITWGIW6Ft8/3sFy88z/Rt8mrSg/bz3fZZzp3Ub0DJzehYLN4v1U= 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=h6qicbaV; 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="h6qicbaV" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1743630098; 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=06MxKlOuB9aN0H2BLYnRnaIFm5vG7GdNp+2sASqjqO8=; b=h6qicbaVIj8cuBrEL1JtXmTaPHfbz02RbyqaeGJtPdOGSss9/ME4HNYF0QgFBGaYRPGI2P 8ZoW+lt3HFm6v+eNOSB+NJS2ACjniqv8MUnEsd6FGOCWG/nOLGxomrhzwnqzkSTEhnQDEm FUbyRN7MCQCht7+CX/P94T8sNhCW8D8= Received: from mx-prod-mc-03.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-633-Dmkyiqd-PNKpq4uS5lrgsw-1; Wed, 02 Apr 2025 17:41:37 -0400 X-MC-Unique: Dmkyiqd-PNKpq4uS5lrgsw-1 X-Mimecast-MFC-AGG-ID: Dmkyiqd-PNKpq4uS5lrgsw_1743630095 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 405581955BC5; Wed, 2 Apr 2025 21:41:35 +0000 (UTC) Received: from chopper.lyude.net (unknown [10.22.80.95]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 884BA3000704; Wed, 2 Apr 2025 21:41:32 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, Andreas Hindborg , linux-kernel@vger.kernel.org Cc: Boqun Feng , Frederic Weisbecker , Thomas Gleixner , Anna-Maria Behnsen , Miguel Ojeda , Alex Gaynor , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross Subject: [PATCH 5/6] rust: hrtimer: Add HrTimerCallbackContext::forward_now() Date: Wed, 2 Apr 2025 17:40:33 -0400 Message-ID: <20250402214109.653341-6-lyude@redhat.com> In-Reply-To: <20250402214109.653341-1-lyude@redhat.com> References: <20250402214109.653341-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.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" Using the HrTimerClockBase::time() function we just added, add a binding for hrtimer_forward_now(). Signed-off-by: Lyude Paul --- rust/kernel/time/hrtimer.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index 521ff1a8a5aa8..d52cbb6cfc57f 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -556,6 +556,14 @@ pub fn forward(&self, now: Ktime, interval: Ktime) -> = u64 { // SAFETY: The C API requirements for this function are fulfilled = by our type invariants. unsafe { bindings::hrtimer_forward(self.raw_get_timer(), now.to_ns= (), interval.to_ns()) } } + + /// 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(&self, interval: Ktime) -> u64 { + self.forward(self.clock_base().time(), interval) + } } =20 /// Use to implement the [`HasHrTimer`] trait. --=20 2.48.1 From nobody Fri Apr 4 11:43:54 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 26CA41F78F3 for ; Wed, 2 Apr 2025 21:41: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=1743630105; cv=none; b=hjibLCVtb15ANuEA2V+ijtC+Q1dBdPDm5t1e07fVnJ5PPevtRgJIbWWUaFIq+0E2clSXKXtzR8D96djtfcgSHITyYQCBHdTQ48x0dOIN5lQdwexAreNHfgUZbqRgcAYXewnnQKWcna59CwzcCNle1wb+T6DxLdzLzwq5kYcCCSY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743630105; c=relaxed/simple; bh=Je12764JTUiy+W/iiKjPImD2rYohU2GEdNzf9MOA5Jk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=otl3rGXsuwYlDFg62yFLlyJmKxh7/4UREhqoIh0WUlA0zFluV+wOvMBVoYTUegIBTDbLhG2v1hAYZv6VVtAp9KrVF7ypreJwxtkMsr5LZqFsDXZCF4UvScrvCV4YObprROdHEQFTnWLcRBkogyve7cPMAz/xREeRqoYM0oOgCuU= 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=gRHyicqt; 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="gRHyicqt" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1743630103; 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=5ysHQ7r1S5oI0I97dPALUJn4xw1ohknTDswpZN4NdyM=; b=gRHyicqttZEvuQxLQhc9vrQsYWFgN1rjnKJfSgdCJpczLdfWcWfjAgm03awlQuvmW3ZVK8 I3nT5sW8476a1W6d1ewDOen9JXiAp8v0BOG31O15sm8zqOYDmmFH5/Ou78CVGh/GFj33OQ rcVMUiMjVCKhJcoCx2wiuBJun9tJWSE= 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-648-1rOnHx3nMlmKTsPcwMi7QA-1; Wed, 02 Apr 2025 17:41:40 -0400 X-MC-Unique: 1rOnHx3nMlmKTsPcwMi7QA-1 X-Mimecast-MFC-AGG-ID: 1rOnHx3nMlmKTsPcwMi7QA_1743630098 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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 0EECC180034D; Wed, 2 Apr 2025 21:41:38 +0000 (UTC) Received: from chopper.lyude.net (unknown [10.22.80.95]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 820633000704; Wed, 2 Apr 2025 21:41:35 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, Andreas Hindborg , linux-kernel@vger.kernel.org Cc: Boqun Feng , Frederic Weisbecker , Thomas Gleixner , Anna-Maria Behnsen , Miguel Ojeda , Alex Gaynor , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross Subject: [PATCH 6/6] rust: hrtimer: Add HrTimerCallback::expires() Date: Wed, 2 Apr 2025 17:40:34 -0400 Message-ID: <20250402214109.653341-7-lyude@redhat.com> In-Reply-To: <20250402214109.653341-1-lyude@redhat.com> References: <20250402214109.653341-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.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" This adds the ability to read the expiry time of the current timer from the HrTimerCallbackContext. Signed-off-by: Lyude Paul --- rust/kernel/time/hrtimer.rs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index d52cbb6cfc57f..e28b7895d8f37 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -69,7 +69,7 @@ =20 use super::ClockId; use crate::{init::PinInit, prelude::*, time::Ktime, types::Opaque}; -use core::{marker::PhantomData, ptr::NonNull}; +use core::{marker::PhantomData, ptr::{NonNull, addr_of}}; =20 /// A timer backed by a C `struct hrtimer`. /// @@ -131,7 +131,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. @@ -163,6 +163,31 @@ pub(crate) unsafe fn raw_cancel(this: *const Self) -> = bool { // handled on the C side. unsafe { bindings::hrtimer_cancel(c_timer_ptr) !=3D 0 } } + + /// Return the time expiry for the given timer pointer. + /// + /// This value should only be used as a snapshot, as the actual expiry= time could change after + /// this function is called. + /// + /// # Safety + /// + /// `self_ptr` must point to a valid `Self`. + unsafe fn raw_expires(self_ptr: *const Self) -> Ktime { + // SAFETY: self_ptr points to an allocation of at least `HrTimer` = size. + let c_timer_ptr =3D unsafe { HrTimer::raw_get(self_ptr) }; + + // SAFETY: There's no actual locking here, a racy read is fine and= expected. + Ktime::from_raw(unsafe { core::ptr::read(addr_of!((*c_timer_ptr).n= ode.expires)) }) + } + + /// 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) -> Ktime { + // SAFETY: By our type invariants, `self.0` always points to a val= id `HrTimer`. + unsafe { HrTimer::raw_expires(self) } + } } =20 /// The timer base for a specific clock. --=20 2.48.1