From nobody Sat Oct 4 19:15:50 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