From nobody Sat Oct 4 17:30:17 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 870252FD7D2 for ; Wed, 13 Aug 2025 22:43: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=1755124985; cv=none; b=ZmdChDV+CmWW9TLIn+tA/YwLMA8SEUTehO266nupGsDrEOg6+1d1D4giNtYLieHYxQ1aScZVFL/FDR/UTwA1K4A3bjfgalwW2foF/CLthGNAYtfM4CiyW9TDtPwN4eBjSzytY0sOil5Ac0VNzVcFz8hiDKw4Y6ajChVirlbMnis= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755124985; c=relaxed/simple; bh=qlwaWio57GqLMA9Fb4mvAHFlhGAkvsC98uuNEQObk/w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NuGO/wVgXmn+CV4SR5e7ovcFNy8Vkm2d71bXY6h0ygxGKy0utxEgjVkwTSNLNjxqWzqSAhRUVqHL7JD/jBPPZ4hKOSffjl1/wwwcp0betEdhHOvwGuibrTXyQwP8P6uw35ro+WasiNgFYiQ1wwbjsTv9mooGKlgSgT3fwlTpSpA= 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=JBCjQYjl; 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="JBCjQYjl" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755124983; 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=JBCjQYjlDLFsv5SINuRhDq4eCgkhwBFa35KeEATOqTumg6LxlFD63ZG6i12f+VkiekgCMl 1rhqzXjiEgy5vO1kRCB6pOmlw8g9Q/XVSyHCgoVumNsicUg9YiyZJ2A0Ujxfli13hcxqLJ c0y/iInMtFbsv1WnfNFx1nyrzuQOdzM= 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-620-32bRiNvjOumdIDxS9T09RQ-1; Wed, 13 Aug 2025 18:42:59 -0400 X-MC-Unique: 32bRiNvjOumdIDxS9T09RQ-1 X-Mimecast-MFC-AGG-ID: 32bRiNvjOumdIDxS9T09RQ_1755124976 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 89185180034B; Wed, 13 Aug 2025 22:42:56 +0000 (UTC) Received: from chopper.lan (unknown [10.22.64.13]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 70FD030001A1; Wed, 13 Aug 2025 22:42:52 +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 v7 1/7] rust: hrtimer: Document the return value for HrTimerHandle::cancel() Date: Wed, 13 Aug 2025 18:42:16 -0400 Message-ID: <20250813224240.3799325-2-lyude@redhat.com> In-Reply-To: <20250813224240.3799325-1-lyude@redhat.com> References: <20250813224240.3799325-1-lyude@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" 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 17:30:17 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 9F0AA2FE07A for ; Wed, 13 Aug 2025 22:43:08 +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=1755124990; cv=none; b=TM3AVkMgRga+aNwiMxsrNsRpCi76fKv5wwRJiRT01rrOgp6T6VTHLlAKTGulHTrME8JD39mlWXPzTgL0PWMCw43hmx1DH113btR4+F/Ve1KeymLaPVNkE/SJANjqcpshtjFh5k3a3rz/ps6MHJ5Elm8VBmgXwBni92fEsfJLswk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755124990; c=relaxed/simple; bh=+wzIYFm7HBgKwCOSy9EFwHPyfPxep0WgbIxkXPongsE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Crni2oNA5agYj2XwFogHNLmBNEinpuduF8X3r+yk8oJHsLO5VF5/cLkpFrUnOqYHLn0yNxc+WXqvHkSDmnaROEOa5rgZ+1m/TIuh331NXLhh/rthvg61e4rOOiiKyngUkyF7/miwoubOjOd6Npcwavspned06H4sRKTUnNadzGg= 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=SYWXDP9S; 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="SYWXDP9S" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755124987; 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=SYWXDP9SAZwLQ6xfgDWgiJDNIvC7JOjA1QlJiDs3JlKxNRibd45zWbO4iY/jHWvUTO9jpW oTNtsWkOR3bbgHREtY0fmapLyNA90j1oVHf7FvkN60VluzHDj3IS4AfJ/cokEJ0tygk/4I qRart8Ov4iUwTmwyOXa57nEqrRe9t0o= 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-594-ln4xpe6XOmqlbmG8PSvtpA-1; Wed, 13 Aug 2025 18:43:03 -0400 X-MC-Unique: ln4xpe6XOmqlbmG8PSvtpA-1 X-Mimecast-MFC-AGG-ID: ln4xpe6XOmqlbmG8PSvtpA_1755124981 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 128FD1956094; Wed, 13 Aug 2025 22:43:01 +0000 (UTC) Received: from chopper.lan (unknown [10.22.64.13]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 0B96630001A1; Wed, 13 Aug 2025 22:42:56 +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 v7 2/7] rust: hrtimer: Add HrTimerInstant Date: Wed, 13 Aug 2025 18:42:17 -0400 Message-ID: <20250813224240.3799325-3-lyude@redhat.com> In-Reply-To: <20250813224240.3799325-1-lyude@redhat.com> References: <20250813224240.3799325-1-lyude@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" 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 17:30:17 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 00C432FCC19 for ; Wed, 13 Aug 2025 22:43:15 +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=1755124997; cv=none; b=sYvodKz3iyKbYYOrLYI6ZP9CsSTH6jwTDRxYlcDcaDwfEe46DYEWxTcw7WGjN9cZosdDrAkgWFgA/WkLiuJXxMwWfvBpqyNe93N6yBIBadCsFQCi/IIE/k7oTHg+o3dVZA2rtfMWN/R254/0KkGh+ZLmyr+Imv6nJPzTN/O4Q1Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755124997; c=relaxed/simple; bh=1namyJhBXNTlgxWA4Sts203ok2F3Onjou2+vIHGaWMM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jKlzNgXNeu1ebQj0KG00NwcPEh55T55r3KWDTwDtDNpiZrfEMg9F2N/SFGQPKOUGojAnop7QfecSn3zcvtwWLOvGxR7Ff/SjmFr7etf2S/6RzalldRB2gCRs3ZF3dm1lBkO29HT8ljp+kwYi+nEG4aktppmmPU2aoxgAKKRZQvA= 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=KdT+AZGm; 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="KdT+AZGm" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755124994; 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=KdT+AZGmxfN2guT5rOVgesWfQpusclMbvHtBNnNqttFDw1zL331p/wr6aVeLn0Tu+wePhm C6qeAEsVUimJSXFfmYpIeSvZ02Vzvcg29s/KJ8n7PFZyvYdGhvha1rKw7goWAjBGlCbtG7 IbTNtBTT+QJCYiBa/0MyuSSt3SWvTk4= 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-608-QDqf1h8JPYGL8gjUc5I6dw-1; Wed, 13 Aug 2025 18:43:08 -0400 X-MC-Unique: QDqf1h8JPYGL8gjUc5I6dw-1 X-Mimecast-MFC-AGG-ID: QDqf1h8JPYGL8gjUc5I6dw_1755124986 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C8591180036F; Wed, 13 Aug 2025 22:43:05 +0000 (UTC) Received: from chopper.lan (unknown [10.22.64.13]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 88EB730001A1; Wed, 13 Aug 2025 22:43:01 +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 v7 3/7] rust: hrtimer: Add HrTimer::raw_forward() and forward() Date: Wed, 13 Aug 2025 18:42:18 -0400 Message-ID: <20250813224240.3799325-4-lyude@redhat.com> In-Reply-To: <20250813224240.3799325-1-lyude@redhat.com> References: <20250813224240.3799325-1-lyude@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Andreas Hindborg --- 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 17:30:17 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 9EDC72FE065 for ; Wed, 13 Aug 2025 22:43:26 +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=1755125008; cv=none; b=b6IMosqZ3zIrTdg6PSRbDKLkwMQD0SAktRVLXd+mxfQ+UU4Rbb0nJ4vxW1RpCDLW5ba6cxWNLOJkAkoymCyReo72pNzAhUzejMZcsg/6Wb6Mf/jzHD+VoUPBOlx/Xs5V3umWMUxyaN9WDpCwRBHyZYqyMV1pdWXyG89NHbJUeYE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755125008; c=relaxed/simple; bh=U+RHJLEPoOu5aEa6o6ob+owYZ3FTasiYeFLqAF32YyU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nZn4kJctFG7jeG3FhaYWQIHkNAvoBLywJMtOz4eapLuLKz9DJvHGEv+Balig2wPkPWmPp15lvZLUV0Zl5VCbJFxr8GWwRaGzDOqFk71YRjhz8ApAkr7U7Yk2B+GCk35gPQq1Ni/B1nfK8bHEWmVWPHkLRVUoPXEKnUcHGE8QyM4= 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=hjfXqBKh; 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="hjfXqBKh" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755125005; 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=hjfXqBKhDApSxBCslhuyEBozgBWWWWhUrcTwQkNsAW1/xKtl5wBGFWGQwERzBJlT6bDub5 +U5QD1pIufU1rnMk0JPaTzcDBrG/AMV/iUcWrKr7BYR8KLVGQWEJTQIzHP2TGqrzbgp2S3 u+qBVtgJUUPPzQZrQlLykP2VUhxsfQc= 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-159-HccB4t4XP-6famJ8QV_2Yg-1; Wed, 13 Aug 2025 18:43:20 -0400 X-MC-Unique: HccB4t4XP-6famJ8QV_2Yg-1 X-Mimecast-MFC-AGG-ID: HccB4t4XP-6famJ8QV_2Yg_1755124998 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E38A0180047F; Wed, 13 Aug 2025 22:43:17 +0000 (UTC) Received: from chopper.lan (unknown [10.22.64.13]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 885DB30001A1; Wed, 13 Aug 2025 22:43:14 +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 v7 4/7] rust: hrtimer: Add HrTimerCallbackContext and ::forward() Date: Wed, 13 Aug 2025 18:42:19 -0400 Message-ID: <20250813224240.3799325-5-lyude@redhat.com> In-Reply-To: <20250813224240.3799325-1-lyude@redhat.com> References: <20250813224240.3799325-1-lyude@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" With Linux's hrtimer API, 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 17:30:17 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 CCDA02FE06F for ; Wed, 13 Aug 2025 22:43:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755125011; cv=none; b=TYQjpF6df0FUXymGHs9VSO1+ExNxztbDF9OBEk1xoFmx+liirjy8kwLDugAZL1qombKHfhwcqLP3MX/GbLwBXO1VOHvPhJ2vtXowK5xo/m1EaqIP+U2X1s/r2rEPyALwJYK9DGvBKNGDb+XSu36TWNSlfOLqw91OXq31rtrbll8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755125011; c=relaxed/simple; bh=9FWtx64Gf/D0FDEiQ1Oftbt3euOjkM+z0PSCApFccVE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XxQrEPIIJadi+YKOgo7nhBainP966f/qN6ncEA+8DeUBhP9lm321DXoMDevloo5DnGI5I0UnXWmdNOFOxbdzsccrrFRZQTgasOewD1AK8M9wW9Aop+ARP64H4+xMdXkzYP0xRc4PpsZfMXzFg+CwA/3DKQEk6Sd1tGsAcgAED6Y= 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=AkjWjh/h; 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="AkjWjh/h" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755125007; 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=AkjWjh/hJZxxe0zV+nGY1spIPQ+T2j5FeBxd73Vl1in0CBvOATdJC5dCnyQLb+JrgLCZq2 t64IJ+KY9YkfdsCEem+3Ui1EvJKO4MSCaU0hJn5AHAjXESgSYHdSSGuUMl9C6/JA8SRct2 gSyC548+9+5JRn8IrurGsLP3I+yVeaM= 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-651-nFI4v9ifP-6qbwAMz9hmsQ-1; Wed, 13 Aug 2025 18:43:24 -0400 X-MC-Unique: nFI4v9ifP-6qbwAMz9hmsQ-1 X-Mimecast-MFC-AGG-ID: nFI4v9ifP-6qbwAMz9hmsQ_1755125002 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id DAA31195608F; Wed, 13 Aug 2025 22:43:21 +0000 (UTC) Received: from chopper.lan (unknown [10.22.64.13]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 5B72430001A1; Wed, 13 Aug 2025 22:43:18 +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 v7 5/7] rust: hrtimer: Add forward_now() to HrTimer and HrTimerCallbackContext Date: Wed, 13 Aug 2025 18:42:20 -0400 Message-ID: <20250813224240.3799325-6-lyude@redhat.com> In-Reply-To: <20250813224240.3799325-1-lyude@redhat.com> References: <20250813224240.3799325-1-lyude@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" 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 17:30:17 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 012C53009CE for ; Wed, 13 Aug 2025 22:43:32 +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=1755125014; cv=none; b=GcvgrDTu5aJ8ChlRFW6idyBaa3COXHzt7i3zgbbrUK5sDk21krbNgnPs8fjcggVGv+gJEcmUYRJ8BLfKafHYQIdeSRVXZD3/SbHALtZYbGr/fnl6xPaoajg7KBsib83yzUykOPa75mzj8Kky4GkFXnpcR8i6WG8d4AZ6R/T8EVc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755125014; c=relaxed/simple; bh=YK0ho87O0mYnaGZ8BiewUHNbR5/VNJClhk7oxU+qJeE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aZDzrXIpCthNK/ldZa+jt7JKJQJwrhIcAIbCpRPwlumnIVwOR6BRBCsER7xRbu+g8zQ1qLdtuXDhsrtjF2PpStl4XleAtkVvDa0ExI3GrHQ/PvRkuH7jR21uvtYh/lDl4vRfJnZ6rzrZwEUV9kLNajQmikr0w2itG3QwNoKDuEw= 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=ORwCp1WH; 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="ORwCp1WH" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755125012; 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=Ux39uZmkbHeNw02CB9I+BW9qm3yblDKIUXvHRPVmBm0=; b=ORwCp1WH8PnVJRqeXiYF/teg6Zy960SMwDI3wJX4u+0oei03A9PgaaNGw8+DTicse7tPF7 7gEJu60ngyS4Mmsf/2oCtnIfhsCaPbV8szwDUz1ulgksXRcS5zEfGUu+sLmlJSyGPNBjO+ t2j9BnlqyhhoXh2nptEIANPAt14N08E= 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-306-EhLTdoRCN-Knff4nU5OXoA-1; Wed, 13 Aug 2025 18:43:28 -0400 X-MC-Unique: EhLTdoRCN-Knff4nU5OXoA-1 X-Mimecast-MFC-AGG-ID: EhLTdoRCN-Knff4nU5OXoA_1755125006 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 2E0CD18003FC; Wed, 13 Aug 2025 22:43:26 +0000 (UTC) Received: from chopper.lan (unknown [10.22.64.13]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 531E930001A1; Wed, 13 Aug 2025 22:43:22 +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 v7 6/7] rust: time: Add Instant::from_nanos() Date: Wed, 13 Aug 2025 18:42:21 -0400 Message-ID: <20250813224240.3799325-7-lyude@redhat.com> In-Reply-To: <20250813224240.3799325-1-lyude@redhat.com> References: <20250813224240.3799325-1-lyude@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" For implementing Rust bindings which can return a point in time. Signed-off-by: Lyude Paul 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 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 Sat Oct 4 17:30:17 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 056232E9EAF for ; Wed, 13 Aug 2025 22:43:41 +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=1755125023; cv=none; b=CRq4eyXSb7ftIOxDPpb1jGXPPJYCnwqLwGfN06DhKWOc+S4Z9vhXO7eepH69TbSia4ho4K4XAxIwsp6n2LKLkMi92yK5CMcUu/DhPrJQAzbny+qwzniZ7AXbUwUMx+M6bCeYgaEUVFh4Mq88oKgpn5C6M1TMhgpkQ66E7Mx7Igs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755125023; c=relaxed/simple; bh=kw9aKeOPm+kK2NtplyKqg/lcu6lHjVJFBGPczsehNto=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CUK55Agti5BtLCsvedkIp+weBBItjWHO+Iba4NvsN1/FGqIte/0JKk3Vn5VkAyTFpTQQfuD32QkOHAD660i+EIYl/0QbBNGSFWzL+U+rgSPaH53kjkjacOJ98lQidIZAl7DIHjwuoGM1VldybFYytoImkbNYkstd6Hf8Kd/lEyM= 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=WDNCxygj; 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="WDNCxygj" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755125020; 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=bFKyJpyWxav4iF539g8SRnmzrL3AOgHfE3g3GrGyBb0=; b=WDNCxygjs/OM30fkiOoO5cjT7mRbvufaJuyCkan1jgww4Af+B26OH084dBx/BZdrkbmZ6z Koj4euVfp8x47YYrUd0dfTAbJ4FT8Tq1F3B16f4uwKk/VdcAGJbmUU/RcX47a1jVrSHq+s FlDCBK7sVA4x8W0S8cF5WbLsllgZVgw= 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-286-DRXOyBYnPq2d5oz_HQzM4w-1; Wed, 13 Aug 2025 18:43:36 -0400 X-MC-Unique: DRXOyBYnPq2d5oz_HQzM4w-1 X-Mimecast-MFC-AGG-ID: DRXOyBYnPq2d5oz_HQzM4w_1755125010 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 893651956089; Wed, 13 Aug 2025 22:43:30 +0000 (UTC) Received: from chopper.lan (unknown [10.22.64.13]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id A487E30001A1; Wed, 13 Aug 2025 22:43:26 +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 v7 7/7] rust: hrtimer: Add HrTimer::expires() Date: Wed, 13 Aug 2025 18:42:22 -0400 Message-ID: <20250813224240.3799325-8-lyude@redhat.com> In-Reply-To: <20250813224240.3799325-1-lyude@redhat.com> References: <20250813224240.3799325-1-lyude@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" 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: Daniel Almeida Reviewed-by: Andreas Hindborg --- 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 e0d78a8859903..cdf583f870376 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