Received: by 2002:ab2:6991:0:b0:1f2:fff1:ace7 with SMTP id v17csp216511lqo; Wed, 27 Mar 2024 10:58:41 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXARhwGzTt+iBXTdl8iJX1Y8xUR/V22Wl3fWDkwL84Vat8s4Z3E/jraq4ObTMWbq2y0zzJ5G2zj0wIRUDWCQ6V4oEu00UjZ0nKmga7gtA== X-Google-Smtp-Source: AGHT+IGyxkkYThJuLf5fHU2cDm/cBfYPmkGOk/HBX03fO3RDQIUZ8uTu1WK0usCx2UwEZkfKbuKf X-Received: by 2002:a2e:92c9:0:b0:2d6:e295:e81f with SMTP id k9-20020a2e92c9000000b002d6e295e81fmr553170ljh.35.1711562321266; Wed, 27 Mar 2024 10:58:41 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711562321; cv=pass; d=google.com; s=arc-20160816; b=zF7gMwsjytYizlJnKNMSUXHhtVZIV+bDys4Q2219975aGFC8eoUV2PNzuMn91Pv2b5 jWWcu4v7x9wdw8BBCsgKrckKI1FWQZr130UXn2rTVyn7ovN2t9VOy/jEhV8wCKDXzAA9 yBFEoMoRjVESahz2X3r/c2qF+1LHWmwtywpz0HIPE0Pmq4EAlrvtuXV+vTsIlqE69H+Z gqp+OnHMBwgoSqUiKGypdxrar6M5SaWRSUmHw4JLvBz8PjN56w+Iuj2ztUK3hyExKgUC t+DRC5Q9c5luQBctD9GfRua0KV5GCl9L9QVlTmDuvFh6VtIV6dGBKhXuEsZomE6FCBIv aW/w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:feedback-id:dkim-signature; bh=68jA08xSlThbpLPscVbPB/44K2PEdU1UR7hQ+UQDb/Y=; fh=E87UbyGQCLM6o2MuyghdS45oqMc3GIGAqBz6x+TUveY=; b=O3syrSZhQdLp/0ccSwvBGV+f8eiMMaRKVTF9U+oLLj0bu5Ij/r+0MRHJb1YnLrT1FV a2ZhleuwQOD6s0XvZWUoCPRq32XCmxs2Ni8wJkSzwhdOEItUv1Uq0ub9nZ2gKhncsYsV 3sreOjOuCmlbEVxiIkdOqvxXLN4QqDckDdf8ZzLSEb58Y9DaWHoJxtbDHhlSFA/HIHVY tn6+vIz8PfqbfVBQS0OnAUvEyb6kDd8G2CiWw92naUyqgNuB59JzHmiQa+E3s/+pYtpN OVPELQg/Tli7PbRBH/PoPawMe+vpQV1C7Vteuh3OV+yBQqXtFY6QFlUYdJs0QIuNu6o/ Q0ig==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Sjealq06; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-121764-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-121764-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id 8-20020a508e48000000b0056baae1d8d2si749368edx.468.2024.03.27.10.58.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 10:58:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-121764-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Sjealq06; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-121764-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-121764-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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 CC1ED1F2DD9F for ; Wed, 27 Mar 2024 17:58:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0DFE1153507; Wed, 27 Mar 2024 17:50:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Sjealq06" Received: from mail-oi1-f176.google.com (mail-oi1-f176.google.com [209.85.167.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 453F614F13F; Wed, 27 Mar 2024 17:50:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711561830; cv=none; b=X4uwr7aTq0cU6he8x6yfnHhlDfsMAwYBWdxaEo+BqQ346iyDlY2lI7xoJcuzNTqRmnDOPZ4N2o0EZSUfWwtuF5Ewb/Ht5pWceLB5345Uv2owHLW/IMq79l5NyG0Ea2pSOOLGFRnsCWQ8yt1NmgUJeSDR3PtoJDk/iWwMqottr68= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711561830; c=relaxed/simple; bh=jSrI8f+0WHime70CtD6JWOJKTtwPj8gpVVqn8XzW2rc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=CnQe7KCxuVZ0zeM8D5+O0anbUq6RfdUYrN94N692IdlrnSG5WotFyraRyo8qqfzcqZlD22KrfHER7g9DGXEqbNX5zZ06daYP0b4gA6D5D0DDr65VYKpfVNH+9Z3V1oTMR3NjtY3TEehr/siXeCnxAUgB1qwnQSFH/h97nQsZ0TE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Sjealq06; arc=none smtp.client-ip=209.85.167.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-oi1-f176.google.com with SMTP id 5614622812f47-3c3ae8fa863so82611b6e.2; Wed, 27 Mar 2024 10:50:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711561828; x=1712166628; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:from:to:cc:subject:date :message-id:reply-to; bh=68jA08xSlThbpLPscVbPB/44K2PEdU1UR7hQ+UQDb/Y=; b=Sjealq06oRsqc//ZM3n1C85TI9dIgMH74466PITgpaiz+q8i/67syPja3WWCci0G49 sflwhxaDZ/UGuRNqBXzaag7trw42xtQ9PbmLDaKxiD2EYUla6CMeBF0zZUqY8/v63PZe RxMPayeHYUaCRA8sXHRcKe3I6rUTcVc1ZpQPriHlOpTy273nI6MRaSAfUrwIzVS3uEjV T7z1Xbh+VNcGUS9SoleoAmVfUKnDuRk1X+kbm65Mar748DetVM9vptNPbuhwT47mYUXr XGyBefjy4DiqnBZZBkKFIDMME6nRhD2Hdv2CFu5wgS5Md+Q7/I+XPSRuoAHZGXbhUKo5 2tQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711561828; x=1712166628; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=68jA08xSlThbpLPscVbPB/44K2PEdU1UR7hQ+UQDb/Y=; b=aClgR4WNXvo0P9Tq9NvbXASDRqNnqIp/7awqQ78FVTDXxEQaCfDm2MK+L4/1XQaGkL 9xiERcipwtKZdRv2bV6UNaIYcK317Yp1U4yuPkqJGXbcB0h7H/N8qEYLxMEzQ9G92e+v tvyPTHED52oD+TlpmFG79tGg/VOS8/tmptmr1Iyo5zQ/e4A8Hr1BvTw1bkydigwTiqxI tkUw3kSnPaTAWl0nPOUVamVrKlLTyy5SBmg9cJc1X/bHV5r6faq8rBfrfvYdrVXLEF2q Myws1rZvYmwtJts30iSePBqBRERExr8LlaHf/O6HfrIpv1TNc0YZkBzs2u5WdmW1qkWz tE8g== X-Forwarded-Encrypted: i=1; AJvYcCXlEfx4ZItxS/FCI09mkD22FnDHLPVDIkpWuB/Tc0hCBuEqIzoBVACWO4G4rJlBKLO8eLQYesjGn2tmL8MOANR4Je4vwAaGGswnDV1P X-Gm-Message-State: AOJu0YxC4wDbMDGyCyG/zsyrQ39RUA80ASRaQlTSJHHhC7ko/mGqaNmq 60+UzJ4Lx5hR019hQcARQHYj800pH9a19uLs8bX+6SvDumiAdskR X-Received: by 2002:a05:6808:15a6:b0:3c3:7a97:3de4 with SMTP id t38-20020a05680815a600b003c37a973de4mr653528oiw.15.1711561828290; Wed, 27 Mar 2024 10:50:28 -0700 (PDT) Received: from fauth1-smtp.messagingengine.com (fauth1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id i5-20020ac813c5000000b004312a7b4715sm4954354qtj.88.2024.03.27.10.50.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 10:50:27 -0700 (PDT) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailfauth.nyi.internal (Postfix) with ESMTP id 2B60C1200066; Wed, 27 Mar 2024 13:50:27 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Wed, 27 Mar 2024 13:50:27 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudduiedgkeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvdenucfhrhhomhepuehoqhhu nhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrg htthgvrhhnpeehudfgudffffetuedtvdehueevledvhfelleeivedtgeeuhfegueeviedu ffeivdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpe gsohhquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhithihqdeiledvgeehtdei gedqudejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmhgrihhlrdgtohhmsehfih igmhgvrdhnrghmvg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 27 Mar 2024 13:50:25 -0400 (EDT) Date: Wed, 27 Mar 2024 10:49:49 -0700 From: Boqun Feng To: 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 , =?iso-8859-1?Q?Bj=F6rn?= 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: References: <20240324223339.971934-1-boqun.feng@gmail.com> <20240324223339.971934-4-boqun.feng@gmail.com> <15DXPOXjBar-JYHVrrgVXI_f3BF5JenfeXEmmxgra6my42Ot9O9Wrc-nc-0O13C5EFTnm0SDac1Muau15mHLmb4So2292uALNSwXKoK75FQ=@proton.me> 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=us-ascii Content-Disposition: inline In-Reply-To: <15DXPOXjBar-JYHVrrgVXI_f3BF5JenfeXEmmxgra6my42Ot9O9Wrc-nc-0O13C5EFTnm0SDac1Muau15mHLmb4So2292uALNSwXKoK75FQ=@proton.me> On Wed, Mar 27, 2024 at 12:50:50PM +0000, Benno Lossin wrote: > On 26.03.24 20:19, Boqun Feng wrote: > > On Tue, Mar 26, 2024 at 05:00:39PM +0000, Benno Lossin wrote: > >> 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: > >>> > >>> * A `Clock` trait that represents different clocks, to read a particular > >>> clock, one needs implement the `Clock::now()` function. > >>> > >>> * 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. > >>> > >>> 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(+) > >>> > >>> 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; > >>> > >>> /// The number of nanoseconds per millisecond. > >>> pub const NSEC_PER_MSEC: i64 = 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 queried 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`? > >> > > > > This was carried over from the old version, for myself, it doesn't make > > much difference between: > > > > trait A { .. } > > trait B { .. } > > > > impl ... > > > > vs > > > > trait A { .. } > > trait B: A { .. } > > > > impl ... > > > > hence I kept it as it is, but yes, it a `Monotonic` *`Clock`*, so I will > > change it in the next version. > > I think it would also make sense to rename it to `MonotonicClock`. > > > > >>> + > >>> +/// A timestamp of a given [`Clock`] > >> > >> Missing '.'. > >> > > > > Fixed locally. > > > >>> +#[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. > >> > > > > Hmm... I cannot trigger any compile errors with this patch. If you see > > an error, could you share the build command? It's not a `pub` function > > though. > > I created a fresh config using `menuconfig` and enabled CONFIG_RUST. > Then I get this error: > > error: associated function `new` is never used > --> rust/kernel/time.rs:97:8 > | > 96 | impl Instant { > | ------------------------- associated function in this implementation > 97 | fn new(ktime: bindings::ktime_t) -> Self { > | ^^^ > | > = note: `-D dead-code` implied by `-D warnings` > = help: to override `-D warnings` add `#[allow(dead_code)]` > > error: aborting due to 1 previous error > > Note that the error vanishes in the next patch, since there you do have > a user for `new`. > Seems I didn't select CONFIG_WERROR=y, so I missed it as a warning. and I can hit it now with that config. Will fix it in the next version. Thanks! Regards > -- > Cheers, > Benno