Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp3473128lqp; Tue, 26 Mar 2024 10:01:22 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXL7bhqDvkQRqMEyG/6Ec/lGut7fn+8PhnXqUCoy5+WHRQFmRajFJgAKr9UwbsWyAwI1avjp5DsMmg0uxv8t/0AfucBuuWoZYFC/0xY4g== X-Google-Smtp-Source: AGHT+IGJo234N12TkXvMaWeF4nGbb4XMhPAijZAxLpHwsBLpzb+BqZJER7dQ6d9qZTsMO39VJMN2 X-Received: by 2002:a50:cc9e:0:b0:56b:c1c7:63cd with SMTP id q30-20020a50cc9e000000b0056bc1c763cdmr315088edi.31.1711472481974; Tue, 26 Mar 2024 10:01:21 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711472481; cv=pass; d=google.com; s=arc-20160816; b=o0uRB1na5uQmlwoOYhfe7EKAdhclyYSQRF9IQWVG5nL+XWi5f1dAo3SnzlSWakifnN wGnpPA3xK3prxzJLMYIlNEbYzv4Mm22JXOrWR7stoq5mS0eAgvhDvBvConreRXOy4Hl5 3YZf1iMv+QAHnpxDO5D0T5me1ygU/9ieagcC85PKxhq2+of+/Zqp/VCmZjFQaXYJCxzG 1ysl7zTmuX4FZm3HShGataQbN9jFwxPTLbraV5aSYaPFUPfdgiUNXVYOSWc1jGfgZNUd t6K1af65g6G8F+dWvr/MdUM3HworGBEZRaK2BRs8zMiK/LiiYvvVzegEOPdCjCri9TK2 mYGw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:feedback-id:references :in-reply-to:message-id:subject:cc:from:to:date:dkim-signature; bh=JbKvJkvXu2iWSuwGdU1AIzET2bU96rDLVAc7bBO6aLw=; fh=6w4G9LgDrc0wGmF4gz6L02UDi66UhJm+aeDXAs5EOUU=; b=JWNS3wdJ87FrHlrVFWMKAbxJzIh4eAXERqFRqUqMix+/HOWJxAPiPmToP8J/KtZpKd 0KIMo2vvhN1dCLvBeaJRKJ48uN3JCd+9LyREpypS5uo1pSgnX3+1RW/T8m5WG6KYofY1 +UM1mCmKTrQ2o6fn9vrH+vfxu2FAt0x0k6mSKZ41sDA+J/SLKJOCIO3jGfM1sUDxDpRJ KeHkUhWXALo7sz0vDssH8nDaFIeH6hXiPkr2UrgbUzPPyKfgyditQhuvMb5XKZeIZ9Og uzIo1bSdAOXKWztJlZ/dPT56plE/Eze3Q9HQEVkJRiS9GMcScEcOgNX95PJVpKfYfb1A TkwQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=Bq8BxIi3; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-119533-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-119533-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id em4-20020a056402364400b0056c1bc953afsi1516133edb.448.2024.03.26.10.01.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Mar 2024 10:01:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-119533-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=Bq8BxIi3; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-119533-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-119533-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id C6A611F612C0 for ; Tue, 26 Mar 2024 17:01:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5373C13D53E; Tue, 26 Mar 2024 17:00:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="Bq8BxIi3" Received: from mail-40133.protonmail.ch (mail-40133.protonmail.ch [185.70.40.133]) (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 3834F13C91F for ; Tue, 26 Mar 2024 17:00:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.40.133 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711472453; cv=none; b=RpqIDl7NVPM39En5IKJ1zcaSYr9madK8/aOpPJwJ7lreFvgFUD5Eb6ZdFdtKTKMox9OUezwM7yvmfV+4Wgu4sTCCHvRmIp10m2kcTeuD/yzYuCBfA75llAltPu+8Hh5R83A/zNgi3wF0dz1gCMxOlBlQE//zqTEtMlX4SN+W64Q= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711472453; c=relaxed/simple; bh=CtLOcxudgrp9+pmo6QzQf8rk8HsbaSoWbUzcGjkYPpg=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=cp7leSTq6+XGnB9eV8vFpz/lOaQHPU52QGyV7Ph4m59JNPMVmQWZb017lepR1WK1nfaz+qyhCePlLWE2YDBRLrC77PGcBuV9uDAR3Nn6pdIAHFAdGNtQH7d7mdX69V1pgiW2vb2HaFkYAIpar7+81Jb0S8k0A/YvK2obKibavpM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b=Bq8BxIi3; arc=none smtp.client-ip=185.70.40.133 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1711472449; x=1711731649; bh=JbKvJkvXu2iWSuwGdU1AIzET2bU96rDLVAc7bBO6aLw=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=Bq8BxIi3qd+9EkR1NWQWDt5q8Prr2Fm1Ac2IhTEUUxaOW6KBU6GWhluOBpMaSl6y7 01zCTsOYMhDrhWApuTstxbepjc5uWNulk81Ew5AXYb0OR21s87HSZ+i5Fd30Qmc1DM rORVqAYnEwmGmZ0y7aU9/KmybTyYTwb4lHJMCVb3zo3Ol4yMcB+pL1jQzAEEVpOejO zkxvrhp5SWXKIIZ/sk1tKtBmuWVHbJmtpsMppUAK32D1Y9Xu/HDJTZ+CiPWZICWYHM nzmr0IzmCKKcUn4/LLV36U7MZn5hqbGGxNdnpCufxVte8Gv5kJommIzKvKy5+NHeJy GebvbgHJdWLcw== Date: Tue, 26 Mar 2024 17:00:39 +0000 To: Boqun Feng From: Benno Lossin Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Thomas Gleixner , Alice Ryhl , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , John Stultz , Stephen Boyd , Valentin Obst , Heghedus Razvan , Asahi Lina Subject: Re: [PATCH 3/5] rust: time: Introduce clock reading framework Message-ID: In-Reply-To: <20240324223339.971934-4-boqun.feng@gmail.com> References: <20240324223339.971934-1-boqun.feng@gmail.com> <20240324223339.971934-4-boqun.feng@gmail.com> Feedback-ID: 71780778:user:proton Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 24.03.24 23:33, Boqun Feng wrote: > To make sure Rust code doesn't mix timestamps from different clocks, a > type safe clock reading framework is introduced. It includes: >=20 > * A `Clock` trait that represents different clocks, to read a particular > clock, one needs implement the `Clock::now()` function. >=20 > * A `Instant` type that represents timestamps of different > clocks, whose implementation is just a `ktime_t`, so all the > calculation on `ktime_t` should apply to it as well. >=20 > Co-developed-by: Heghedus Razvan > Signed-off-by: Heghedus Razvan > Co-developed-by: Asahi Lina > Signed-off-by: Asahi Lina > Signed-off-by: Boqun Feng > --- > rust/kernel/time.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 49 insertions(+) >=20 > diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs > index b238b3a4e899..0f9f5605ed48 100644 > --- a/rust/kernel/time.rs > +++ b/rust/kernel/time.rs > @@ -6,6 +6,9 @@ > //! have been ported or wrapped for usage by Rust code in the kernel. > //! > //! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.= h). > +//! C header: [`include/linux/ktime.h`](srctree/include/linux/ktime.h). > + > +use core::marker::PhantomData; >=20 > /// The number of nanoseconds per millisecond. > pub const NSEC_PER_MSEC: i64 =3D bindings::NSEC_PER_MSEC as i64; > @@ -64,3 +67,49 @@ pub fn to_ns(self) -> i64 { > self.inner > } > } > + > +/// Represents a clock, that is, a unique time source and it can be quer= ied for the current time. > +pub trait Clock: Sized { > + /// Returns the current time for this clock. > + fn now() -> Instant; > +} > + > +/// Marker trait for clock sources that are guaranteed to be monotonic. > +pub trait Monotonic {} Why is this trait not an extension of `Clock`? > + > +/// A timestamp of a given [`Clock`] Missing '.'. > +#[repr(transparent)] > +#[derive(Debug)] > +pub struct Instant { > + inner: bindings::ktime_t, > + clock: PhantomData, > +} > + > +impl Clone for Instant { > + fn clone(&self) -> Self { > + *self > + } > +} > + > +impl Copy for Instant {} > + > +impl Instant { > + fn new(ktime: bindings::ktime_t) -> Self { When compiling, this function is marked as dead-code in this patch. --=20 Cheers, Benno > + Self { > + inner: ktime, > + clock: PhantomData, > + } > + } > +} > + > +impl core::ops::Sub for Instant { > + type Output =3D Duration; > + > + /// Returns the difference of two timestamps. > + #[inline] > + fn sub(self, other: Self) -> Self::Output { > + // `ktime_t` is an `i64` value of nanoseconds, and kernel define= s signed overflow to behave > + // like 2s-complement, hence `wrapping_sub()` is used here to mi= rror `ktime_sub()`. > + Duration::new(self.inner.wrapping_sub(other.inner)) > + } > +} > -- > 2.44.0 >