From nobody Sat Oct 4 06:27:55 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 8F49C33A01D for ; Tue, 19 Aug 2025 20:13:57 +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=1755634439; cv=none; b=pzhP+cxzjz3mudVv3ENUi9VW+shVwIZZYjbHeKG+usLXh3k0OvCFjt2Vbrmx1ZpjNSZsLIJskgDMzuEnc7jdaYVyDwURY0vitO+EnBNBzoOR0GwhPr3jD5XDS9TJwCaJsemhJpYtl1SnChkAcDTvHLnmpXqCygdLjcPOzaGllc8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755634439; c=relaxed/simple; bh=qlwaWio57GqLMA9Fb4mvAHFlhGAkvsC98uuNEQObk/w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=p21mgyvh2Qtoarf2Vns3fhIJTd/zf4FhQ2Gk9rtOAxKsvwpEZhlXYjjk+l7E7htKmM3NkkNHuImZamyPhrHrdZzsBklIVdJfLNTdLuMulMV1imQOe/gDyqKarslMQL1oqQrJVrTdM9Vg/agD3Fj9B+/yXrxLYsgFM8b0FleqHyE= 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=V0bUasNc; 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="V0bUasNc" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755634436; 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=BLWwbEpsyDLokTeeRPGfQvr4ehtZaUWiSPjkeJIZb9w=; b=V0bUasNcVBKNDC+XAM5lZ92CzIsAIDwNDSdV3ao7k7NKaJa1F/m89pb431ZpbUwvJ8b5/A WiY6+bR7lJbBq+fHD4ioW7KRZ01AKpFJUTC9UXjx+ygwm85gWFZqVm3XyF4P1Z+1qw2kT1 hu/hvI4fwypTIYgimKPAw9BcXjlQ1uk= 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-671-TC37_-DtM7OxnJDbkICEGg-1; Tue, 19 Aug 2025 16:13:51 -0400 X-MC-Unique: TC37_-DtM7OxnJDbkICEGg-1 X-Mimecast-MFC-AGG-ID: TC37_-DtM7OxnJDbkICEGg_1755634428 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 A94701800561; Tue, 19 Aug 2025 20:13:48 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.81.238]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 7464219560B0; Tue, 19 Aug 2025 20:13:44 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Thomas Gleixner , Andreas Hindborg , FUJITA Tomonori Cc: Daniel Almeida , Boqun Feng , 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 v8 1/7] rust: hrtimer: Document the return value for HrTimerHandle::cancel() Date: Tue, 19 Aug 2025 16:05:52 -0400 Message-ID: <20250819201334.545001-2-lyude@redhat.com> In-Reply-To: <20250819201334.545001-1-lyude@redhat.com> References: <20250819201334.545001-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.12 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." 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 Sat Oct 4 06:27:55 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 1120A341AB3 for ; Tue, 19 Aug 2025 20:13:59 +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=1755634441; cv=none; b=MkSLy+++BmO6IsJzAOY1Nz2nyr0zpOqs4jxvY+uKVHHkII+Kqihgo22/pY/6HqF3agKH+qiKbRTuacwPQAIkx0H/kxsJ3Z+J8VU1vx4gKHGLy2kxSAaudPHdRgiJdUHa3rJd2AU8tZvtfiXczp3tzZHrZj/y1eXft5ATs7gXJUo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755634441; c=relaxed/simple; bh=+wzIYFm7HBgKwCOSy9EFwHPyfPxep0WgbIxkXPongsE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KopulYq6Dc27sLHLjACoMgsVGWo3SJmJVUIkdc7VKGtxGK4V6Ggo8XvMSVf/RVH9OdK99nzOxMqnOMHKA7vuZhLi11L6zAcpiSJ8yg37hNdmgKWet5X4S1s8Z7xtWIzM9XdUEhRT7O/UQD2WFnXJIZQzCBkLRL2laP3CM9L9iic= 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=O3IHitd4; 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="O3IHitd4" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755634439; 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=4a7oEyPNBxhjMx3DnT1g8ru/QEOUUr1PYi92/P1Icd4=; b=O3IHitd4zVW11blkt4+eXXvv8HmMB4mEBrcJCgULs9wQqejEw+VAeCLMJILpf8XZBafDjg NrbS7t6PpD9eESTls3SnRKzFp/apbfX3RUihnsY2mTxtJ1NRNXQxl7vF0/SG+tkGN83sTa TZapoMP39bVS3wrY/aKUDkuVyZOwdKY= 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-663-uvItpLX5P5OW8SBA3YLgsQ-1; Tue, 19 Aug 2025 16:13:55 -0400 X-MC-Unique: uvItpLX5P5OW8SBA3YLgsQ-1 X-Mimecast-MFC-AGG-ID: uvItpLX5P5OW8SBA3YLgsQ_1755634433 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 18998180035B; Tue, 19 Aug 2025 20:13:53 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.81.238]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 1D14519560B2; Tue, 19 Aug 2025 20:13:48 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Thomas Gleixner , Andreas Hindborg , FUJITA Tomonori Cc: Daniel Almeida , Boqun Feng , 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 v8 2/7] rust: hrtimer: Add HrTimerInstant Date: Tue, 19 Aug 2025 16:05:53 -0400 Message-ID: <20250819201334.545001-3-lyude@redhat.com> In-Reply-To: <20250819201334.545001-1-lyude@redhat.com> References: <20250819201334.545001-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.12 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 Sat Oct 4 06:27:55 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 EF2FE341AB3 for ; Tue, 19 Aug 2025 20:14:04 +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=1755634446; cv=none; b=bfc2p+WZSPpTz8qyQPmXeGnNx8rGdXOt5ikmF43U6IHs5w0a6htg6X0TfSO1JLMhry7c+HrFH/1FXxR4vbTtnLeA+lbsR+RYIWW0ijROxQv1O/luwrk1HOOScLxYHRqZWs4EONN2MtUvsZZLUlIwhQErCaCwdaNRs8WhUAB+V8U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755634446; c=relaxed/simple; bh=1namyJhBXNTlgxWA4Sts203ok2F3Onjou2+vIHGaWMM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VWY0ZctWLhhz/BuQhOXn2ed8Wqq1JYUHZC1WGwMWNOBuvqcXoDVSkZVIZ6sINF8b1sbtsZfTP6ZeWDOfJRzGF8Dru94K9K0WkZTUrX8rsDzCYrBqupCROomvaV4MW4D8/FzYNMYIJNGnbLmKpGPP3jHblGIcTPI1ZvFeVkh/tOU= 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=H8lJT7Q7; 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="H8lJT7Q7" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755634444; 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=IdZatwK7YZ40vNNmIn9rSFrBBk7bYVx1796BmwT3dbI=; b=H8lJT7Q7l2Kz0iGBjyA6ymDZDahwtrX07GdUeDAsDhogI+fn0UHoP71IqbPbCu6CpaDdfM YuMYslTMcfMPevlL30eEWNfFXOxNMZwBJk0W95G/PYZo1CAyvMrjWibhjuDUp8c66AjcoM k7TEgRvFR7TsEe0VdRApzsiSzw9EuMs= 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-MlIwjDxdPEWfozbiMKVh_Q-1; Tue, 19 Aug 2025 16:14:00 -0400 X-MC-Unique: MlIwjDxdPEWfozbiMKVh_Q-1 X-Mimecast-MFC-AGG-ID: MlIwjDxdPEWfozbiMKVh_Q_1755634438 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 C42C11800294; Tue, 19 Aug 2025 20:13:57 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.81.238]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A830619560B2; Tue, 19 Aug 2025 20:13:53 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Thomas Gleixner , Andreas Hindborg , FUJITA Tomonori Cc: Daniel Almeida , Boqun Feng , 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 v8 3/7] rust: hrtimer: Add HrTimer::raw_forward() and forward() Date: Tue, 19 Aug 2025 16:05:54 -0400 Message-ID: <20250819201334.545001-4-lyude@redhat.com> In-Reply-To: <20250819201334.545001-1-lyude@redhat.com> References: <20250819201334.545001-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.12 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. V7 * Split up Timer::forward() a bit, apply Andreas's SAFETY comment recommendations 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..79fed14b2d98e 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` + let this =3D unsafe { self.get_unchecked_mut() }; + + // SAFETY: By existence of `Pin<&mut Self>`, the pointer passed to= `raw_forward` points to a + // valid `Self` that we have exclusive access to. + unsafe { Self::raw_forward(this, now, interval) } + } } =20 /// Implemented by pointer types that point to structs that contain a [`Hr= Timer`]. --=20 2.50.0 From nobody Sat Oct 4 06:27:55 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 9DA7030DECD for ; Tue, 19 Aug 2025 20:14:23 +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=1755634465; cv=none; b=rghQqCu021xYtrq7VHPeBdESUOL2/hOeVfMyx+4BROH9MqxSWQ7IoDb0GkLwF6pJdBycy8qGqmza8NJVqPzHsKr2pFaY8S9zjXa3BmGaTfyaxwIVC4eUH7j44y3KG3KCGlUTs5SZCbv3bvTT70xA5mPfBrd0lKOXKUatFVT0dFY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755634465; c=relaxed/simple; bh=U+RHJLEPoOu5aEa6o6ob+owYZ3FTasiYeFLqAF32YyU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JKZEyDgYM9jI3yAthq9/C59li6mv2+qpN8Ty7MjbrheUnSX1nGDKGxYW05q6hPFUnXY6kXoRJEjJzSRCcsbzu551gSU75WHU2VJJXrv0Codh47PLSf6sguUVnz/fcuM6tq0Y3d+aQEpqDfe+v2V4pn7bQaRVJODQNGymYZKEkjw= 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=DNaT+8sm; 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="DNaT+8sm" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755634462; 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=khkfrVfHumBuUwLUr11MvZty06PJhhGza2wz1vc7FYc=; b=DNaT+8smY6Ayp1M7tM4YfddXxvjl0RZVYm7+Oh8KndDWGURLPGeaCL+E797XMZlGHX33DQ E9F9AIQh4eDWVZeG1bA/PdYfH878TDnrO8iXMiuzI2AGSwOyQ63mj1Rx7U/STfn/uTdsR1 oV/AH6yj5UJcNCC86KtDrDz2ck9eREg= 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-549-Mz60PxOrMf--PaN3kpYiKA-1; Tue, 19 Aug 2025 16:14:15 -0400 X-MC-Unique: Mz60PxOrMf--PaN3kpYiKA-1 X-Mimecast-MFC-AGG-ID: Mz60PxOrMf--PaN3kpYiKA_1755634452 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 6AB8018002CB; Tue, 19 Aug 2025 20:14:12 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.81.238]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 53AC519560B0; Tue, 19 Aug 2025 20:14:08 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Thomas Gleixner , Andreas Hindborg , FUJITA Tomonori Cc: Daniel Almeida , Boqun Feng , 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 v8 4/7] rust: hrtimer: Add HrTimerCallbackContext and ::forward() Date: Tue, 19 Aug 2025 16:05:55 -0400 Message-ID: <20250819201334.545001-5-lyude@redhat.com> In-Reply-To: <20250819201334.545001-1-lyude@redhat.com> References: <20250819201334.545001-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.12 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: Daniel Almeida Reviewed-by: Andreas Hindborg --- 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) V5: * Fix unbounded T on HrTimerCallbackContext V6: * Move reference to HrTimerCallbackContext::forward() in HrTimer::forward() comments into this commit so rustdoc doesn't fail. * Deduplicate documentation for HrTimerCallbackContext::forward() * Add missing changelog note 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 79fed14b2d98e..1e8839d277292 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 Sat Oct 4 06:27:55 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 5F2E83101D2 for ; Tue, 19 Aug 2025 20:14:23 +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=1755634464; cv=none; b=UG/Ju/bvlwVYpx0R4JqZNXRXuGxaBF/4OZz5k3ArvUhBDECE9zs1ljJdFDNXNbf+lYjpQN63fwc+mfykmgEgXF4lfaeRhSrEzIc+7xBBnxhDCNCRMVQ+lOmeGPfvOmCVhu6ktNCjflBl6tlUDhpwiGByaNlA53CQV/67jlmBOhU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755634464; c=relaxed/simple; bh=9FWtx64Gf/D0FDEiQ1Oftbt3euOjkM+z0PSCApFccVE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PjFKIdJw9WoQ4dNLJFHPJ5xKzP5daoRTcPA0F3j9LJTrITJNl2Licz0zaIPxwgVfR2HP4jK456Uzn7bIoSAu36biKfzrGIsB8rvnykpyGg8YBZEgkdJjqkKXfEmccD0f2e0Hcl/JqxAgNHWDDdtGsf+SGbadA6YjpYSRtsNGUTo= 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=MSZNTckq; 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="MSZNTckq" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755634462; 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=oejlMLAAfPv/LBX4eO/QzEdA8WEyxlkO8EudoL3ZCiY=; b=MSZNTckq5vJAlqMQbZPziicQ0qwJHFay6PoGLpYJN6pE/CNKXPaiT+h/Z+/39OGO8A+PcM m9z8vbSuBa0ilG4qeNieDLkwb41NTZBa3F0+T49VtfI3MK5k0hZmSJKP2sJRUlvXWgEqX/ 95oQGRZ8dNG6nd72bbquDZFmnhm90rs= 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-416-0ZDiksbAMmm1rCkLVD7O4Q-1; Tue, 19 Aug 2025 16:14:19 -0400 X-MC-Unique: 0ZDiksbAMmm1rCkLVD7O4Q-1 X-Mimecast-MFC-AGG-ID: 0ZDiksbAMmm1rCkLVD7O4Q_1755634456 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 5A6AC18002CC; Tue, 19 Aug 2025 20:14:16 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.81.238]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id BF4EF19560B2; Tue, 19 Aug 2025 20:14:12 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Thomas Gleixner , Andreas Hindborg , FUJITA Tomonori Cc: Daniel Almeida , Boqun Feng , 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 v8 5/7] rust: hrtimer: Add forward_now() to HrTimer and HrTimerCallbackContext Date: Tue, 19 Aug 2025 16:05:56 -0400 Message-ID: <20250819201334.545001-6-lyude@redhat.com> In-Reply-To: <20250819201334.545001-1-lyude@redhat.com> References: <20250819201334.545001-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.12 Content-Type: text/plain; charset="utf-8" Signed-off-by: Lyude Paul Reviewed-by: Daniel Almeida Reviewed-by: Andreas Hindborg --- 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 V7: * Remove leftover comment about raw_cb_time from patch description 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 1e8839d277292..e0d78a8859903 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) -> // valid `Self` that we have exclusive access to. unsafe { Self::raw_forward(this, 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 Sat Oct 4 06:27:55 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 9DEE4326D48 for ; Tue, 19 Aug 2025 20:14:25 +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=1755634467; cv=none; b=GwVx7CdG80PmnNEj5tGZBZ0Of7rV/1QDWc0ukcfFnxJwyIZK7cgbfAc0YqZW8J+iKdTORKhI4dAeNeb8TKO/lVYls7Vm1+oY0qp+zNMjV27+PRGY1qyh07zvRHzXPO34njpRfnWDgL+d+vVLS3mJSwgNeKmkcE0r1deinix/d9w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755634467; c=relaxed/simple; bh=JcG8AO1bHRvEK0uCkPbFr4Yzn72QSE/1a/I9KFT6zpE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DIIGKbrHhs4NCSbwNfOk4JyQJiV/l2tXYvbe7lJyFkDQD1AbgWaP4QFO6a3iw5SwOOkDMlFq+CiePc3irUhtJyuQvhfSvKZSPoX8b5x2RflUAzq3qoncDK6tnkh9qZ3AJCXBiEDvC6dXYNjPq5UGFPzxRodzkUnFpDaZy4gelj0= 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=baDHRkTg; 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="baDHRkTg" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755634464; 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=VDxx71q3MVhyRyVcbCfH/wubs1F7yPIXcuCK3By5m/4=; b=baDHRkTgQRJce9w7GQoLOSNNVCeyxZjo0AgEEM3egI/CHzw3DAWW7EsAgp+LBtjqke9Rim jJLW4Z1Bo89UF77uAJe8p0mpbUJ3LZ/px+rbDXRtud42qet/sTKXg3u1XH4vp64JXRYH9f E/HQg8JNgVX4iAMTyf10tkOxqiqUxiY= 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-298-ssaWOvdYMI2VVfIdHjYDCg-1; Tue, 19 Aug 2025 16:14:23 -0400 X-MC-Unique: ssaWOvdYMI2VVfIdHjYDCg-1 X-Mimecast-MFC-AGG-ID: ssaWOvdYMI2VVfIdHjYDCg_1755634460 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 5A94F1954228; Tue, 19 Aug 2025 20:14:20 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.81.238]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id D414D19560B2; Tue, 19 Aug 2025 20:14:16 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Thomas Gleixner , Andreas Hindborg , FUJITA Tomonori Cc: Boqun Feng , 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 v8 6/7] rust: time: Add Instant::from_ktime() Date: Tue, 19 Aug 2025 16:05:57 -0400 Message-ID: <20250819201334.545001-7-lyude@redhat.com> In-Reply-To: <20250819201334.545001-1-lyude@redhat.com> References: <20250819201334.545001-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.12 Content-Type: text/plain; charset="utf-8" For implementing Rust bindings which can return a point in time. Signed-off-by: Lyude Paul --- V4: * Turn from_nanos() into an unsafe function in order to ensure that we uphold the invariants of Instant V5: * Add debug_assert!() to from_nanos V8: * Change name of function from Instant::from_nanos() to Instant::from_ktime() rust/kernel/time.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 64c8dcf548d63..5c40ebe096357 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 `ktime_t` without checking if it is n= on-negative. + /// + /// # Panics + /// + /// On debug builds, this function will panic if `nanos` is not in the= range from 0 to + /// `KTIME_MAX`. + /// + /// # Safety + /// + /// The caller promises that `nanos` is in the range from 0 to `KTIME_= MAX`. + #[expect(unused)] + #[inline] + pub(crate) unsafe fn from_ktime(ktime: bindings::ktime_t) -> Self { + debug_assert!(ktime >=3D 0); + + // INVARIANT: Our safety contract ensures that `nanos` is in the r= ange from 0 to + // `KTIME_MAX`. + Self { + inner: ktime, + _c: PhantomData, + } + } } =20 impl core::ops::Sub for Instant { --=20 2.50.0 From nobody Sat Oct 4 06:27:55 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 A28D3341AAB for ; Tue, 19 Aug 2025 20:14:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755634472; cv=none; b=oaAFWqmemJC2wPQqU1jnokYiPwZQptEazeettqFCybLNruYpSVyaKifd/vb4S9S3wtoNLktFysvLbrCCvaCTTNt5rkr91ebDT2Nn+xlirWnBpzoXdjMBRrq0v023dUb4tJhe/RVYbhPILfo4LQ3JQWv3BmQGaCvFmrjqZRFwwTw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755634472; c=relaxed/simple; bh=tG2lfCHLE8ygsJSIY3ueBGnk3/tzTKsqBPW8KH5mg8c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PjChS97ehulSPGWstkWVEfnzf3Bo+jr+HtuLhdpsAsYQSZY5pfR3Qz4SwaAHfwO77y25QJLBPPjWXuUxc6TmsEMpsVSSSfK67vPteJdqRvEhTurJoFU5HwG8I8DrLX/YKMEMHkWq80RDx4T+F+OFv/AhfnRIR5KBYKWVCoaDQvo= 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=Hf0cFcSA; 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="Hf0cFcSA" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755634469; 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=lJQRnLhxJvgIUuT5kaM/ex8erJ3gbHp4ky93mdCcTI8=; b=Hf0cFcSAk/keTvIIZqs4DtUYNsVrsOJcQAKzCtdbY29sar3CRH04jEomoPpTnuZLiuqcs5 FDvxzV8QIFn0/HlDy6g1dc+DUpEcq6qDYoURdxOpE5OICAp0PvBKS39vXxUCNEluRItZnx Qd3/n/gelWqwxNfLDsY82j7XRR4efEU= 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-80-D206bQRpNWWyQE_HTr6LpQ-1; Tue, 19 Aug 2025 16:14:26 -0400 X-MC-Unique: D206bQRpNWWyQE_HTr6LpQ-1 X-Mimecast-MFC-AGG-ID: D206bQRpNWWyQE_HTr6LpQ_1755634464 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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 50177180048E; Tue, 19 Aug 2025 20:14:24 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.81.238]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id E196A19560B0; Tue, 19 Aug 2025 20:14:20 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Thomas Gleixner , Andreas Hindborg , FUJITA Tomonori Cc: Boqun Feng , 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 v8 7/7] rust: hrtimer: Add HrTimer::expires() Date: Tue, 19 Aug 2025 16:05:58 -0400 Message-ID: <20250819201334.545001-8-lyude@redhat.com> In-Reply-To: <20250819201334.545001-1-lyude@redhat.com> References: <20250819201334.545001-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.12 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 --- V8: * Fix bogus safety comment I noticed after Fujita's comments. In expires() we're not guaranteed to get a non-negative ktime_t because of ktime_t itself, we're guaranteed to get one because a negative expiration time for a timer doesn't make sense. 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 5c40ebe096357..30f0086c2408f 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_ktime(ktime: bindings::ktime_t) -> Self { debug_assert!(ktime >=3D 0); diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index e0d78a8859903..856d2d929a008 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: + // - Timers cannot have negative ktime_t values as their expiratio= n time. + // - There's no actual locking here, a racy read is fine and expec= ted + unsafe { + Instant::from_ktime( + // 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