From nobody Mon Oct 6 05:00:37 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 A9ACF2135AD for ; Thu, 24 Jul 2025 18:52:52 +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=1753383174; cv=none; b=IPJCwtfBT0s8dpytA6DB3qp/+0bYpMUo22iNc08NI5oa+TZeED1N/XMOZYz33/mV8BdmYBEgItEdDcj3S1zD6RquTtZsQ6Ew8txysGMHOAqy0+cp1Wdyjmo1kQ7GoX07QLVyiVue4SlJByHlGMIeyOZQvBc7pXXYlcLsFGbDt58= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753383174; c=relaxed/simple; bh=Mc3rWMMaytq+BMv7z9nIj4IY5sYmoN610zORz4cBroo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UNAL7hc6VAl3vjkxx5l1ds8q7GngrbcAXPbbzOEGWy58KZ53Akp6Hc7SC5CRAsIglLQWmgqfyHi1BH1EuY5YVpD0tn/bKhvUzfL3V/Y531Reuo1/nTDnJLudXZNrBmlwiRtKi6F+TllJCn8wsVHBKvK0yzEU08tDFDPoSMp+dTo= 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=dOhTcOUk; 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="dOhTcOUk" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1753383171; 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=KwVDmCmYVZN6ImwcpxRuuiECBLrfVSkQ1shmopmWmW0=; b=dOhTcOUkHVDQOSnJZhPykD73w9Wm86uX1h/ggFsfBhj2li114Y9/ueXw4wcLTD2rLHy21C yFDu/M4yM4Aa6zi61Lrp0qZ045mexe41n3Hb1KleXdlpe7E3xPGhhKo+6m1aA6kjVt1xeF oD9+65xhyTmvdU1UU7dvIXONvybWRTI= 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-584-ytbLY9HDNjCZwQAz1VjnbA-1; Thu, 24 Jul 2025 14:52:50 -0400 X-MC-Unique: ytbLY9HDNjCZwQAz1VjnbA-1 X-Mimecast-MFC-AGG-ID: ytbLY9HDNjCZwQAz1VjnbA_1753383168 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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 74914180034A; Thu, 24 Jul 2025 18:52:47 +0000 (UTC) Received: from chopper.lyude.net (unknown [10.22.88.223]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 2F132195608D; Thu, 24 Jul 2025 18:52:44 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, Thomas Gleixner , Boqun Feng , linux-kernel@vger.kernel.org, Andreas Hindborg , FUJITA Tomonori Cc: Frederic Weisbecker , 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 v6 1/7] rust: hrtimer: Document the return value for HrTimerHandle::cancel() Date: Thu, 24 Jul 2025 14:49:27 -0400 Message-ID: <20250724185236.556482-2-lyude@redhat.com> In-Reply-To: <20250724185236.556482-1-lyude@redhat.com> References: <20250724185236.556482-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.17 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 Reviewed-by: Andreas Hindborg Reviewed-by: Daniel Almeida --- V4: * Reword to "Returns `true` if the timer was running." Signed-off-by: Lyude Paul --- rust/kernel/time/hrtimer.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index 144e3b57cc780..6bfc0223f4f57 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -324,6 +324,8 @@ pub unsafe trait HrTimerHandle { /// Note that the timer might be started by a concurrent start operati= on. If /// so, the timer might not be in the **stopped** state when this func= tion /// returns. + /// + /// Returns `true` if the timer was running. fn cancel(&mut self) -> bool; } =20 --=20 2.50.0 From nobody Mon Oct 6 05:00:37 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 26E3B19EEC2 for ; Thu, 24 Jul 2025 18:52:59 +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=1753383181; cv=none; b=Hnr4B12gaYhdOp264Qug8TxbVzqbLu2ggLQIL5sNylayJb6YkeFPYH/ZuEZJMZdy8Si5cjL9joKTxG4R33NLwC1EezMWh5MdLJ1OVnPJn50KYFfFtZAXwHbHLI23VttbasKOgBCM0i9fKmKJUY6wC2OIknPWvqisEyVIBck70i0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753383181; c=relaxed/simple; bh=buq/LJ7NlW/eSGAnORvj1c33aVLpLK+UF8CGoiyYQKQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=R29bavRCwL1RKjFhHwR/p8k/WGUrXvAa4uiRqieuaIf7o9eDsujVZJcNTUvkTluB2PyJ+T0S082nEkpLrY5RjkhG7CCSsYtXKYgKqimVzXZyKeQb7ZhwO8EjRqargEAChLQwx3bdsyhQkml6iXdOMQ2ECdHvGTIhpdhNrII2I+Y= 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=Wh2P2mBQ; 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="Wh2P2mBQ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1753383179; 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=XMg+SaGb5U8haz1IPUXEUUgFHsFXFQTxpkFsDHgvE74=; b=Wh2P2mBQxJ4Lg1eFe7z0rZs6MWSjyCP8h8uk+7tBRue+Lhm78sCWulK5op4tRIrdtf+p4K nW2AYv/yxjcmkuUKewKm0Dil7tgq6GBrfRTChjOI+bkoc1VhaJVkmHSBQ7BXeHxNGP9Aih 7mMWCjcwwlArEd07J2MFUtiP96VyFzk= 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-54-fRlSlkTtPuGT0v6azaEgVw-1; Thu, 24 Jul 2025 14:52:54 -0400 X-MC-Unique: fRlSlkTtPuGT0v6azaEgVw-1 X-Mimecast-MFC-AGG-ID: fRlSlkTtPuGT0v6azaEgVw_1753383171 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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 0ADB01955D86; Thu, 24 Jul 2025 18:52:51 +0000 (UTC) Received: from chopper.lyude.net (unknown [10.22.88.223]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id D83311956096; Thu, 24 Jul 2025 18:52:47 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, Thomas Gleixner , Boqun Feng , linux-kernel@vger.kernel.org, Andreas Hindborg , FUJITA Tomonori Cc: Frederic Weisbecker , 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 v6 2/7] rust: hrtimer: Add HrTimerInstant Date: Thu, 24 Jul 2025 14:49:28 -0400 Message-ID: <20250724185236.556482-3-lyude@redhat.com> In-Reply-To: <20250724185236.556482-1-lyude@redhat.com> References: <20250724185236.556482-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.17 Content-Type: text/plain; charset="utf-8" Since we want to add HrTimer methods that can accept Instants, we will want to make sure that for each method we are using the correct Clocksource for the given HrTimer. This would get a bit overly-verbose, so add a simple HrTimerInstant type-alias to handle this for us. Signed-off-by: Lyude Paul Reviewed-by: Andreas Hindborg Reviewed-by: Daniel Almeida --- rust/kernel/time/hrtimer.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index 6bfc0223f4f57..be1bad4aacaad 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -72,6 +72,11 @@ use core::marker::PhantomData; use pin_init::PinInit; =20 +/// A type-alias to refer to the [`Instant`] for a given `T` from [`HrT= imer`]. +/// +/// Where `C` is the [`ClockSource`] of the [`HrTimer`]. +pub type HrTimerInstant =3D Instant<<>::TimerMode as= HrTimerMode>::Clock>; + /// A timer backed by a C `struct hrtimer`. /// /// # Invariants --=20 2.50.0 From nobody Mon Oct 6 05:00:37 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 5916B2ED854 for ; Thu, 24 Jul 2025 18:53:04 +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=1753383185; cv=none; b=a2zneyer1qftOmlfQZoBYUeniQRAq6LMMrxrFxd8LBvslMwsxzTUwdbntSmKLEMAa3ZawshPksKeRx5K4mxnEE7+/a2PEK+SOhx7VIV+aB36d7HvGhtuNIz8W02LAUrx3FUbPn5OJiVJqT3Rh+D80ufAxYF+gc4S9wEtOskFdQw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753383185; c=relaxed/simple; bh=I6r/hVojm9kPsiME3lDLV24WqEKZE1S6dw/IIfQTPIg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XqjXxeigJMDS+dFla16JeL4Xf7nfRwf8QgEvm90jJgcSgq34gNNnRuLSwdcS6KCiAu8EBxzoVAn1ojChpOWXRkE9+EuAFqh7SlQ5lz6vlY3N1N6n5POOgrEAIqnzSIR4T4cQ7VD5fw1s0cC0/NlUqzKM9e4yQl4vVjENMlwhnjE= 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=jB6qC5jG; 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="jB6qC5jG" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1753383183; 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=SIvhLLPtzfNF63KacAB5y6ueD7cnnwqEnQG82pg+xts=; b=jB6qC5jGJOkRQrizoXt+lrdY+u+A+5nteIqfpHnl3z2yk/QeO+4CH6XaAxUHC4x7pJLNJr eWu23/dlIuoxX0miltq1OzrFefAvonZ3JMQE5jOgB6848YEY3FMMzqeLKNX4EUM4Wb6m1J Y0DAFJMw14zCgJaOxXGyf1xcT981amU= 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-408-WA3DVNVAPbi3ox1JdACujA-1; Thu, 24 Jul 2025 14:52:59 -0400 X-MC-Unique: WA3DVNVAPbi3ox1JdACujA-1 X-Mimecast-MFC-AGG-ID: WA3DVNVAPbi3ox1JdACujA_1753383175 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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 B6CDC180098E; Thu, 24 Jul 2025 18:52:55 +0000 (UTC) Received: from chopper.lyude.net (unknown [10.22.88.223]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 76AA21956096; Thu, 24 Jul 2025 18:52:51 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, Thomas Gleixner , Boqun Feng , linux-kernel@vger.kernel.org, Andreas Hindborg , FUJITA Tomonori Cc: Frederic Weisbecker , 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 v6 3/7] rust: hrtimer: Add HrTimer::raw_forward() and forward() Date: Thu, 24 Jul 2025 14:49:29 -0400 Message-ID: <20250724185236.556482-4-lyude@redhat.com> In-Reply-To: <20250724185236.556482-1-lyude@redhat.com> References: <20250724185236.556482-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.17 Content-Type: text/plain; charset="utf-8" Within the hrtimer API there are quite a number of functions that can only be safely called from one of two contexts: * When we have exclusive access to the hrtimer and the timer is not active. * When we're within the hrtimer's callback context as it is being executed. This commit adds bindings for hrtimer_forward() for the first such context, along with HrTimer::raw_forward() for later use in implementing the hrtimer_forward() in the latter context. Signed-off-by: Lyude Paul Reviewed-by: Daniel Almeida --- V4: * Fix the safety contract for raw_forward() * Require Pin<&mut Self>, not &mut self * Drop incorrect UniquePin example * Rewrite documentation a bit (re: Andreas) V6: * Remove the reference to HrTimerCallbackContext::forward() until this function gets added. Signed-off-by: Lyude Paul --- rust/kernel/time/hrtimer.rs | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index be1bad4aacaad..5ccdd1b0b8021 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -168,6 +168,46 @@ pub(crate) unsafe fn raw_cancel(this: *const Self) -> = bool { // handled on the C side. unsafe { bindings::hrtimer_cancel(c_timer_ptr) !=3D 0 } } + + /// Forward the timer expiry for a given timer pointer. + /// + /// # Safety + /// + /// - `self_ptr` must point to a valid `Self`. + /// - The caller must either have exclusive access to the data pointed= at by `self_ptr`, or be + /// within the context of the timer callback. + #[inline] + unsafe fn raw_forward(self_ptr: *mut Self, now: HrTimerInstant, int= erval: Delta) -> u64 + where + T: HasHrTimer, + { + // SAFETY: + // * The C API requirements for this function are fulfilled by our= safety contract. + // * `self_ptr` is guaranteed to point to a valid `Self` via our s= afety contract + unsafe { + bindings::hrtimer_forward(Self::raw_get(self_ptr), now.as_nano= s(), interval.as_nanos()) + } + } + + /// Conditionally forward the timer. + /// + /// If the timer expires after `now`, this function does nothing and r= eturns 0. If the timer + /// expired at or before `now`, this function forwards the timer by `i= nterval` until the timer + /// expires after `now` and then returns the number of times the timer= was forwarded by + /// `interval`. + /// + /// Returns the number of overruns that occurred as a result of the ti= mer expiry change. + pub fn forward(self: Pin<&mut Self>, now: HrTimerInstant, interval:= Delta) -> u64 + where + T: HasHrTimer, + { + // SAFETY: + // - `raw_forward` does not move `self`. + // - Self is a mutable reference and thus always points to a valid= `HrTimer` + // - The only way that we could hold a mutable reference to `HrTim= er` is if we have + // exclusive access to it - fulfilling the requirements of the C= API. + unsafe { Self::raw_forward(self.get_unchecked_mut(), now, interval= ) } + } } =20 /// Implemented by pointer types that point to structs that contain a [`Hr= Timer`]. --=20 2.50.0 From nobody Mon Oct 6 05:00:37 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 25A5F2ECEAE for ; Thu, 24 Jul 2025 18:53:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753383197; cv=none; b=RlmEgxIiW7qZ1t+jQYKRzPH5fX1y8jy/qSg722N7xrfILIeiSAGg1cWGeJwBLHMXQqiF3eAu81LyYR4ZFm5jOxkpNA/njDkF19e/8hFHkLDT02XkzdPc8BPW+rAhohshW2QuwgM0nyP3i5bNq4yZkAV6aaakFM+VPp9pi2BGHuQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753383197; c=relaxed/simple; bh=s57dYuZxKOyI0XuRGVuDHEJd1FZRMgT4QidogBLSSyM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bCk2hZHnJS/RmfhOdnwn2g0GskkT3uxz3LqDAC31VNphD5XPasHmQwHFHm+NnOp8B43wcGtwAUXhFp5OngCdAQLScKrA+Y4hK/WZAp/bSaA8EQbBfHuzkN/KZc4Docx8pe2VvJbh0OEypTV0IU0GFPPXkc5O4F0WGqDbNOnYwo0= 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=KSlMU4M3; 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="KSlMU4M3" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1753383194; 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=UWTYdJB0B+9A/bedIIpvwFNzDJ4Z7fYow/R6/ugnveU=; b=KSlMU4M3n2+2ase9p5Jxl+ybF9hslaQtIP0sBNRhv7zmfngZ/NWgO488d5neJnkb3wYK5A 5wb7CzPChx+IUk8reZ7g4SM1vUHfNkAkjwoakhM4YTB9z0QL23gVw8XUkby593f5Nc40eD 35kTE3kr5mLT+cg+E8bO5lq7AnhVcdU= 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-620-gKH1ctuZPjeM211xNSQbfA-1; Thu, 24 Jul 2025 14:53:10 -0400 X-MC-Unique: gKH1ctuZPjeM211xNSQbfA-1 X-Mimecast-MFC-AGG-ID: gKH1ctuZPjeM211xNSQbfA_1753383188 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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 4C58818002B2; Thu, 24 Jul 2025 18:53:08 +0000 (UTC) Received: from chopper.lyude.net (unknown [10.22.88.223]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 3028F195608D; Thu, 24 Jul 2025 18:53:04 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, Thomas Gleixner , Boqun Feng , linux-kernel@vger.kernel.org, Andreas Hindborg , FUJITA Tomonori Cc: Frederic Weisbecker , 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 , Tamir Duberstein Subject: [PATCH v6 4/7] rust: hrtimer: Add HrTimerCallbackContext and ::forward() Date: Thu, 24 Jul 2025 14:49:30 -0400 Message-ID: <20250724185236.556482-5-lyude@redhat.com> In-Reply-To: <20250724185236.556482-1-lyude@redhat.com> References: <20250724185236.556482-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.17 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 Reviewed-by: Andreas Hindborg Reviewed-by: Daniel Almeida --- V2: * Improve SAFETY comments for HrTimerCallbackContext uses (I forgot to mention that we're within RawHrTimerCallback::run() * Split forward into forward() and raw_forward() since we're going to have two contexts that we can call forward() from now. * Clarify contexts in which certain hrtimer methods can be called. * Make sure that we use a mutable reference for forward() here - just in case :). * Rename interval to duration V3: * Rename duration -back- to interval (now that I actually have read hrtimer_forward's source, interval does make more sense than duration considering the fact we return the number of overruns that occurred according to the given interval). * Rewrite documentation a bit (re: Andreas) V6: * Move reference to HrTimerCallbackContext::forward() in HrTimer::forward() comments into this commit so rustdoc doesn't fail. * Deduplicate documentation for HrTimerCallbackContext::forward() Signed-off-by: Lyude Paul --- rust/kernel/time/hrtimer.rs | 63 +++++++++++++++++++++++++++-- rust/kernel/time/hrtimer/arc.rs | 9 ++++- rust/kernel/time/hrtimer/pin.rs | 9 ++++- rust/kernel/time/hrtimer/pin_mut.rs | 12 ++++-- rust/kernel/time/hrtimer/tbox.rs | 9 ++++- 5 files changed, 93 insertions(+), 9 deletions(-) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index 5ccdd1b0b8021..a8fb176e599e8 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -69,7 +69,7 @@ =20 use super::{ClockSource, Delta, Instant}; use crate::{prelude::*, types::Opaque}; -use core::marker::PhantomData; +use core::{marker::PhantomData, ptr::NonNull}; use pin_init::PinInit; =20 /// A type-alias to refer to the [`Instant`] for a given `T` from [`HrT= imer`]. @@ -196,6 +196,10 @@ unsafe fn raw_forward(self_ptr: *mut Self, now: HrTime= rInstant, interval: Del /// expires after `now` and then returns the number of times the timer= was forwarded by /// `interval`. /// + /// This function is mainly useful for timer types which can provide e= xclusive access to the + /// timer when the timer is not running. For forwarding the timer from= within the timer callback + /// context, see [`HrTimerCallbackContext::forward()`]. + /// /// Returns the number of overruns that occurred as a result of the ti= mer expiry change. pub fn forward(self: Pin<&mut Self>, now: HrTimerInstant, interval:= Delta) -> u64 where @@ -345,9 +349,13 @@ 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<'_, Self>, + ) -> HrTimerRestart where - Self: Sized; + Self: Sized, + Self: HasHrTimer; } =20 /// A handle representing a potentially running timer. @@ -632,6 +640,55 @@ impl HrTimerMode for RelativePinnedHar= dMode { type Expires =3D Delta; } =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: HasHrTimer>(NonNull= >, PhantomData<&'a ()>); + +impl<'a, T: HasHrTimer> 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`]. + /// + /// The returned `Self` must not outlive the function context of [`Raw= HrTimerCallback::run`] + /// where this function is called. + pub(crate) unsafe fn from_raw(timer: *mut HrTimer) -> Self { + // SAFETY: The caller guarantees `timer` is a valid pointer to an = initialized + // `bindings::hrtimer` + // INVARIANT: Our safety contract ensures that we're within the co= ntext of a timer callback + // and that `timer` points to a live instance of `HrTimer`. + Self(unsafe { NonNull::new_unchecked(timer) }, PhantomData) + } + + /// Conditionally forward the timer. + /// + /// This function is identical to [`HrTimer::forward()`] except that i= t may only be used from + /// within the context of a [`HrTimer`] callback. + pub fn forward(&mut self, now: HrTimerInstant, interval: Delta) -> = u64 { + // SAFETY: + // - We are guaranteed to be within the context of a timer callbac= k by our type invariants + // - By our type invariants, `self.0` always points to a valid `Hr= Timer` + unsafe { HrTimer::::raw_forward(self.0.as_ptr(), now, interval)= } + } +} + /// Use to implement the [`HasHrTimer`] trait. /// /// See [`module`] documentation for an example. diff --git a/rust/kernel/time/hrtimer/arc.rs b/rust/kernel/time/hrtimer/arc= .rs index ed490a7a89503..7be82bcb352ac 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::HrTimerMode; use super::HrTimerPointer; @@ -99,6 +100,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 aef16d9ee2f0c..4d39ef7816971 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::HrTimerMode; use super::RawHrTimerCallback; @@ -103,6 +104,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 767d0a4e8a2c1..9d9447d4d57e8 100644 --- a/rust/kernel/time/hrtimer/pin_mut.rs +++ b/rust/kernel/time/hrtimer/pin_mut.rs @@ -1,8 +1,8 @@ // SPDX-License-Identifier: GPL-2.0 =20 use super::{ - HasHrTimer, HrTimer, HrTimerCallback, HrTimerHandle, HrTimerMode, RawH= rTimerCallback, - UnsafeHrTimerPointer, + HasHrTimer, HrTimer, HrTimerCallback, HrTimerCallbackContext, HrTimerH= andle, HrTimerMode, + RawHrTimerCallback, UnsafeHrTimerPointer, }; use core::{marker::PhantomData, pin::Pin, ptr::NonNull}; =20 @@ -107,6 +107,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 ec08303315f28..aa1ee31a71953 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::HrTimerMode; use super::HrTimerPointer; @@ -119,6 +120,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.50.0 From nobody Mon Oct 6 05:00:37 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 2E2872EE263 for ; Thu, 24 Jul 2025 18:53:19 +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=1753383201; cv=none; b=gorKV1PY8dSaZ2dcQOu5+GKOPevK3bpVC9cCdOjTbuJtXn4+UhK34mkC9itnGh5WzpkEc+9ZlEG7si7wk9SCZ8CkN4fz3ckxHVGlA77FYfUiH4AAkC4NDfCpTg/vpoWsGQavKz4o6RgzPigV04ZWofrNtwDeZ7zECn8nzs321tw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753383201; c=relaxed/simple; bh=YKRbPoAK3fgOTSCkr0VL0uVTvX7QyTPZdBAwEBeHOP8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rLa+abhLwH+oie6udnsosstocgPOdwDijHlpArNq7Yl4LtwW1oOVamejAtMbQrMlOfRSnQ1AM9Wur0Vtbuer6pJuPuU8Va01G45yLpv/lxR+2Xcyk7pqgHKGiD6NXDXIxzAI4r3nqtXXOn00UNs6r/dC6MV3sOr/tL7Q8ePQPgs= 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=Vmev+ZRS; 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="Vmev+ZRS" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1753383199; 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=K692SAsepPxZfbndfFazUHoePE2ffj8dBBSNIklQ0qY=; b=Vmev+ZRSRCmRGIEapD+xcBqKP+83ajrGBRf6QKPRlOIp2GbNoEmYuxmhgyYOSJpRT6PhCj bXyZt3cnDj02a4/nDbz7OB5PWP+wEav6H092oOX1eOQYXL90GyHIfPkL+JbIlRa2aCX+K2 wnEj0aO7J5tgwB2uIK1qzp+ozLRRKvg= 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-547-8szsOUW7Nr-eNbV5lGKWEA-1; Thu, 24 Jul 2025 14:53:15 -0400 X-MC-Unique: 8szsOUW7Nr-eNbV5lGKWEA-1 X-Mimecast-MFC-AGG-ID: 8szsOUW7Nr-eNbV5lGKWEA_1753383193 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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 F3FB2195FD06; Thu, 24 Jul 2025 18:53:12 +0000 (UTC) Received: from chopper.lyude.net (unknown [10.22.88.223]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C43C2195608D; Thu, 24 Jul 2025 18:53:08 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, Thomas Gleixner , Boqun Feng , linux-kernel@vger.kernel.org, Andreas Hindborg , FUJITA Tomonori Cc: Frederic Weisbecker , 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 v6 5/7] rust: hrtimer: Add forward_now() to HrTimer and HrTimerCallbackContext Date: Thu, 24 Jul 2025 14:49:31 -0400 Message-ID: <20250724185236.556482-6-lyude@redhat.com> In-Reply-To: <20250724185236.556482-1-lyude@redhat.com> References: <20250724185236.556482-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.17 Content-Type: text/plain; charset="utf-8" Using the HrTimer::raw_time_cb() function, we can now add an equivalent to hrtimer_forward_now() to both HrTimer and HrTimerCallbackContext. Signed-off-by: Lyude Paul Reviewed-by: Andreas Hindborg Reviewed-by: Daniel Almeida --- V2: * Change from Ktime to Delta * Make sure that forward_now() takes a mutable reference to the timer struct * Reword this to point out that we're adding forward_now() to both callback context and mutable timer reference * Rename interval to duration V4: * Fix rust documentation for HrTimerCallbackContext (forgot to update both forward_now() declarations) * Use Pin<&mut Self> for context-less forward. V6: * Drop raw_cb_time(), use Instant::now() instead * Split out expires() from this patch, at some point it seems I mistakenly combined it with this patch Signed-off-by: Lyude Paul --- rust/kernel/time/hrtimer.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index a8fb176e599e8..fa5a3ae81723b 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -212,6 +212,17 @@ pub fn forward(self: Pin<&mut Self>, now: HrTimerInsta= nt, interval: Delta) -> // exclusive access to it - fulfilling the requirements of the C= API. unsafe { Self::raw_forward(self.get_unchecked_mut(), now, interval= ) } } + + /// Conditionally forward the timer. + /// + /// This is a variant of [`forward()`](Self::forward) that uses an int= erval after the current + /// time of the base clock for the [`HrTimer`]. + pub fn forward_now(self: Pin<&mut Self>, interval: Delta) -> u64 + where + T: HasHrTimer, + { + self.forward(HrTimerInstant::::now(), interval) + } } =20 /// Implemented by pointer types that point to structs that contain a [`Hr= Timer`]. @@ -687,6 +698,14 @@ pub fn forward(&mut self, now: HrTimerInstant, inte= rval: Delta) -> u64 { // - By our type invariants, `self.0` always points to a valid `Hr= Timer` unsafe { HrTimer::::raw_forward(self.0.as_ptr(), now, interval)= } } + + /// Conditionally forward the timer. + /// + /// This is a variant of [`HrTimerCallbackContext::forward()`] that us= es an interval after the + /// current time of the base clock for the [`HrTimer`]. + pub fn forward_now(&mut self, duration: Delta) -> u64 { + self.forward(HrTimerInstant::::now(), duration) + } } =20 /// Use to implement the [`HasHrTimer`] trait. --=20 2.50.0 From nobody Mon Oct 6 05:00:37 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 6D5292EE5E4 for ; Thu, 24 Jul 2025 18:53:24 +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=1753383206; cv=none; b=SkfSz9FJ9zKxsvUeNWZ8RROtcr5Thik3RjskR2/oy3ZRKeayiGxPhzQRcCnLjHYhI2Zwd7nXo6nnY58+waN6DmpOSRwEbQmCbJWwZW/B79fQKYxgEnxMxXZkoZVzU9sMOGvn6RkL+ISua/Yf7LfsG69texQMVf3IBpo1cyBIu3s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753383206; c=relaxed/simple; bh=1cKp71OvP7YG8ixH5LKid29XhFAE7kjeQc19MfODs+A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UXbVyZ3TbtcOsti3gRfP9pWXd3Cu2N7fvw4TfzyelThIzeMqjULN2ZKNrpxgiPofXj92KwBH7W4VOJ+F+iIz10c2PaQMQ/97NYgp1nrNzRY0TtUK+398zBxChQbL0IUdqhhe2fuzZD2jReQ0p5KZcOVw5bdcCzEETvaY8yn9OTA= 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=BiUiprVT; 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="BiUiprVT" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1753383203; 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=bdevymO+VWaeP1oPd2AyCC29bi4KScNsc4kXrzWL+0c=; b=BiUiprVTyNaE0yVt/BY11Slft0o8u6pz4Kv+hzBvb/rqKFbPjZ980PmUOpXuTynvE8Nixs Te4zIeDpVOvWYnrWprrgieASQ7pNNNgo8u2qMNMWZxbTKBCNkL8hYxvzNJLGamhytlILL4 meN2yBmrI6sIrf0BfvxBFAA0G2LUy7Q= 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-124-GI4I4ewdO7SH1RAh3szMUQ-1; Thu, 24 Jul 2025 14:53:19 -0400 X-MC-Unique: GI4I4ewdO7SH1RAh3szMUQ-1 X-Mimecast-MFC-AGG-ID: GI4I4ewdO7SH1RAh3szMUQ_1753383197 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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 72C911955D95; Thu, 24 Jul 2025 18:53:17 +0000 (UTC) Received: from chopper.lyude.net (unknown [10.22.88.223]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 7D4BB195608D; Thu, 24 Jul 2025 18:53:13 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, Thomas Gleixner , Boqun Feng , linux-kernel@vger.kernel.org, Andreas Hindborg , FUJITA Tomonori Cc: Frederic Weisbecker , 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 v6 6/7] rust: time: Add Instant::from_nanos() Date: Thu, 24 Jul 2025 14:49:32 -0400 Message-ID: <20250724185236.556482-7-lyude@redhat.com> In-Reply-To: <20250724185236.556482-1-lyude@redhat.com> References: <20250724185236.556482-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.17 Content-Type: text/plain; charset="utf-8" For implementing Rust bindings which can return a point in time. Signed-off-by: Lyude Paul Reviewed-by: Andreas Hindborg Reviewed-by: Daniel Almeida --- V4: * Turn from_nanos() into an unsafe function in order to ensure that we uphold the invariants of Instant V5: * Add debug_assert!() to from_nanos Signed-off-by: Lyude Paul --- rust/kernel/time.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 64c8dcf548d63..75088d080b834 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -200,6 +200,29 @@ pub fn elapsed(&self) -> Delta { pub(crate) fn as_nanos(&self) -> i64 { self.inner } + + /// Create an [`Instant`] from a time duration specified in nanosecond= s without checking if it + /// is positive. + /// + /// # Panics + /// + /// On debug builds, this function will panic if `nanos` violates our = safety contract. + /// + /// # Safety + /// + /// The caller promises that `nanos` is in the range from 0 to `KTIME_= MAX`. + #[expect(unused)] + #[inline] + pub(crate) unsafe fn from_nanos(nanos: i64) -> Self { + debug_assert!(nanos >=3D 0); + + // INVARIANT: Our safety contract ensures that `nanos` is in the r= ange from 0 to + // `KTIME_MAX`. + Self { + inner: nanos as bindings::ktime_t, + _c: PhantomData, + } + } } =20 impl core::ops::Sub for Instant { --=20 2.50.0 From nobody Mon Oct 6 05:00:37 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 83E3F2ED842 for ; Thu, 24 Jul 2025 18:53:29 +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=1753383211; cv=none; b=dieaLjTc+y2dzwRYrerPtmHmv11tsrBzZBAkaJCt5fGfeZWi9SPEuGL4fS2yE4srE6KLbuzDnGIpUUnKOVE0RPmuoRxIxHUC+LttCjbRKD3gRL3IIWp1PZuuGKr0xzlPmzsOESkMJh2pqo33M/KEcewKPqUW9NlCTFOXf2W9x0s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753383211; c=relaxed/simple; bh=mUCzRYYIfWZf5GOkc293zl7pIfmGz+Gij1JSIuPXYco=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tQ+KFOOFQ1RHOgYLmDANrCODZqd/AJHRG/jk7Uk+vi28UUpEHEjNSNRZgqc6X03c5YSSInTtP23wuf5N7PjJfZKoXAP3cC5Qefle6lo2USjaaSsrhIOyDYkHGbc+4H0i7ULsJ6m0oV8q0KRqRShS9pOma+rEB14wJ1lw0NY2hqc= 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=ZP89sSfK; 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="ZP89sSfK" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1753383208; 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=rZ2yVlJPs2PJNhdTn0DP3jFnAjUYOrYzBe58F8qZCwE=; b=ZP89sSfKqPDRhwdboSYyZe/krUJwk9R/wr0uI04v0VKlNB4/ilFQaLODhA+lZBSouwbsAz p1JIcyGduGnCtd835CV8jpQrgM+V/fJEEk+Dv1zTLys0UUujIY3Ano9rTjiNeICS8HxVkn mRyefmfpb4sp/aSM2WJZQi6+CKtHMcU= 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-656-wIqSM_-TOYuW2bJkayz6_Q-1; Thu, 24 Jul 2025 14:53:24 -0400 X-MC-Unique: wIqSM_-TOYuW2bJkayz6_Q-1 X-Mimecast-MFC-AGG-ID: wIqSM_-TOYuW2bJkayz6_Q_1753383201 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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 C5F531800264; Thu, 24 Jul 2025 18:53:21 +0000 (UTC) Received: from chopper.lyude.net (unknown [10.22.88.223]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 0575B1956096; Thu, 24 Jul 2025 18:53:17 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, Thomas Gleixner , Boqun Feng , linux-kernel@vger.kernel.org, Andreas Hindborg , FUJITA Tomonori Cc: Frederic Weisbecker , 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 v6 7/7] rust: hrtimer: Add HrTimer::expires() Date: Thu, 24 Jul 2025 14:49:33 -0400 Message-ID: <20250724185236.556482-8-lyude@redhat.com> In-Reply-To: <20250724185236.556482-1-lyude@redhat.com> References: <20250724185236.556482-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.17 Content-Type: text/plain; charset="utf-8" Add a simple callback for retrieving the current expiry time for an HrTimer. In rvkms, we use the HrTimer expiry value in order to calculate the approximate vblank timestamp during each emulated vblank interrupt. Signed-off-by: Lyude Paul Reviewed-by: Andreas Hindborg Reviewed-by: Daniel Almeida --- rust/kernel/time.rs | 1 - rust/kernel/time/hrtimer.rs | 23 +++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 75088d080b834..6d6e71710ebdc 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -211,7 +211,6 @@ pub(crate) fn as_nanos(&self) -> i64 { /// # Safety /// /// The caller promises that `nanos` is in the range from 0 to `KTIME_= MAX`. - #[expect(unused)] #[inline] pub(crate) unsafe fn from_nanos(nanos: i64) -> Self { debug_assert!(nanos >=3D 0); diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index fa5a3ae81723b..2d52a1b3b39a0 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -223,6 +223,29 @@ pub fn forward_now(self: Pin<&mut Self>, interval: Del= ta) -> u64 { self.forward(HrTimerInstant::::now(), interval) } + + /// Return the time expiry for this [`HrTimer`]. + /// + /// This value should only be used as a snapshot, as the actual expiry= time could change after + /// this function is called. + pub fn expires(&self) -> HrTimerInstant + where + T: HasHrTimer, + { + // SAFETY: `self` is an immutable reference and thus always points= to a valid `HrTimer`. + let c_timer_ptr =3D unsafe { HrTimer::raw_get(self) }; + + // SAFETY: + // - `node.expires` is a ktime_t, so it must be within the range o= f `0` to `KTIME_MAX`. + // - There's no actual locking here, a racy read is fine and expec= ted + unsafe { + Instant::from_nanos( + // This `read_volatile` is intended to correspond to a REA= D_ONCE call. + // FIXME(read_once): Replace with `read_once` when availab= le on the Rust side. + core::ptr::read_volatile(&raw const ((*c_timer_ptr).node.e= xpires)), + ) + } + } } =20 /// Implemented by pointer types that point to structs that contain a [`Hr= Timer`]. --=20 2.50.0