Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp1012267lqp; Fri, 22 Mar 2024 02:57:07 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWWTluT1Fe2KdG/oX3CefgiMIy0F86mZE4HzdTOZMJyAdoJkdmLkE6BgQ3M32Cp3mSWgBPvOYDaRjgdep9pZDra5HEZwfoza0MOuzQhCw== X-Google-Smtp-Source: AGHT+IHPl3AdYFZ1GD5DkolvFRgjtvpbt2C9PlqiRSztvpsRRHY9LGX7IGOjWKP6hJPLvxRbFMUM X-Received: by 2002:a05:6808:1511:b0:3c3:88f3:970b with SMTP id u17-20020a056808151100b003c388f3970bmr1750374oiw.47.1711101427442; Fri, 22 Mar 2024 02:57:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711101427; cv=pass; d=google.com; s=arc-20160816; b=vbKROzjJaDGaZojk9HMA5A8b9abMMNBdTQjryKdvCE2SjoxTY7rhEmTPJ8kLaPUtKB fciBE7MaWShogbhdCYXEz8ALdQQVqd1QEJoyQegb2nTM3WC3OAeXiAiVHVyIYiKDoRjQ Qa2D8AEPYZCZKFctUqKGk99XROHxvi6z9H3RCHXMhjBENUgWCZm5ikhoxWiOFVLb5OKW kHGd9wgkQuQeZdY21iVR3KwaDZrDJeHeiIeKVvEwnQk6px312D9CNaJ+BzCBjbKteBr/ W5jQwsMXc+BMKxWcS8QkwilUbVps8aWxdLCzKUT2Nd3xEDqDND3F64YxlJACNe4ajawI vceg== 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=YAAnBuxNM7tVgIu9+wxVQ7JSvFKV0lJF3itk6SdkcfE=; fh=eMjp0M/EVN+/dtw6xEgIcYKBx+pP/Bs9gDctiFOpl8U=; b=x+60e80KfhUdgiSzNC+jvXHNQqXfMvJRjB/Bu2cBPe/aTBKoH0Hf1qk8tcbwPPbaLR C3dcj4Slmh6FiR/vGCn2cEQuAeOBfeQRPCzoisAs7qKhoEid+8rTqsQ0dw0teEeTzTqJ FP/7CEoeTxmExceowQL3jZQUxYNBqqG8K9Qfvn0s9uEsniL7zkM+yu5OPbHprHPKa1mD 0Pgx58J8oHH1WgT6G5iIDZMRl+wxbT1prCV1saiYGP/26jgHsM3l80umBl99m3MkqdZi zPux8z0uZwO0k90fwpzUj0C/VjX8QvdPmCJ8/4HTUTCoeGJQaG0jbFFFIMnsLLT0z54W fd3Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@proton.me header.s=modqwmhza5bkfgbjxu6nf6ij3m.protonmail header.b=b19OwU7R; 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-111257-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-111257-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id h25-20020a63f919000000b005ee3f1a07e7si1625511pgi.201.2024.03.22.02.57.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Mar 2024 02:57:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-111257-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=modqwmhza5bkfgbjxu6nf6ij3m.protonmail header.b=b19OwU7R; 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-111257-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-111257-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 09D86285BA7 for ; Fri, 22 Mar 2024 09:57:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 61FD62556F; Fri, 22 Mar 2024 09:56:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="b19OwU7R" Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) (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 309A3224CF; Fri, 22 Mar 2024 09:56:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.43.16 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711101417; cv=none; b=bQMEHZbC8KfH1IsNC1Hw8caBFOuA6zRpwtf+s2tG38hjg8NMrItjGF2e8btvtWLcdNfEe+wLsR8fmMJX+lrXuHR1hOZh4ZkFg6YJgAWXg9HOFwNHEFkLZA5BWWyEjoL1j8EUG3TSCLVaOVE1bfNhWsLHpwzDH2xdKZ/lxoKsNtY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711101417; c=relaxed/simple; bh=WKWZ/KICR+3/pNg0wrbFmwo4Yb28kZbDtGwYfzPTtko=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Z2YkkcNgeNao7ogszT2K2whY1wtj+xLtvm1SQxhh/KHVQDSo+wUVbHsAPv2z+Fnq6ZUZsBxHK8psVe5AAEu+1QVl/fvWW+LZqWsql7lQTY/Qm/pLnqvVNlz2cT4/EuPXH7Hi1uW2XaFfmFKlUd6n2aijegXHtjCex7u7oBssJwY= 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=b19OwU7R; arc=none smtp.client-ip=185.70.43.16 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=modqwmhza5bkfgbjxu6nf6ij3m.protonmail; t=1711101412; x=1711360612; bh=YAAnBuxNM7tVgIu9+wxVQ7JSvFKV0lJF3itk6SdkcfE=; 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=b19OwU7RLVBUrXyLzmCIuleRw6VLcau5MD21ndRCbpwNJGwisIvcSbnTFXHPfLSnj vKfM1cZj0wBkwQTbou81ZUm/4dEJ2oIihIZZHxC2DLQIeLlqM4zEPG5kqVw/1yO8P3 l4fJufGtV+KBxhch9/OPkCuERVXIC0XHz+AFrkIgasjXLVutgl8lAOsFr/qgJoSbyI 4fCA6yhSi5Uyi7XiaYVRnCWRHzKP+ukZo0jS3F7jftc4JreVGhWpYNh1kW4SPamNc4 q+klWutUh9skCoLnfT/5mZOyQUWi4NtqDVDKnxTUUuZL1lnnhtXED90FJCIx9rRlpT fu6YbZI3/UokA== Date: Fri, 22 Mar 2024 09:56:36 +0000 To: Alice Ryhl , Miguel Ojeda , John Stultz , Thomas Gleixner , Stephen Boyd From: Benno Lossin Cc: Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] rust: time: add Ktime Message-ID: <2ecf6529-5408-4621-becc-2f30e1039079@proton.me> In-Reply-To: <20240322-rust-ktime_ms_delta-v2-1-d98de1f7c282@google.com> References: <20240322-rust-ktime_ms_delta-v2-1-d98de1f7c282@google.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 3/22/24 09:59, Alice Ryhl wrote: > Introduce a wrapper around `ktime_t` with a few different useful > methods. >=20 > Rust Binder will use these bindings to compute how many milliseconds a > transaction has been active for when dumping the current state of the > Binder driver. This replicates the logic in C Binder [1]. >=20 > For a usage example in Rust Binder, see [2]. >=20 > The `ktime_get` method cannot be safely called in NMI context. This > requirement is not checked by these abstractions, but it is intended > that klint [3] or a similar tool will be used to check it in the future. >=20 > Link: https://lore.kernel.org/lkml/5ac8c0d09392290be789423f0dd78a520b830f= ab.1682333709.git.zhangchuang3@xiaomi.com/ [1] > Link: https://r.android.com/3004103 [2] > Link: https://rust-for-linux.com/klint [3] > Signed-off-by: Alice Ryhl I have one comment below, I don't mind leaving it as-is: Reviewed-by: Benno Lossin > --- > Changes in v2: > - Mention that ktime_get cannot be safely called in NMI context. > - Link to v1: https://lore.kernel.org/r/20240320-rust-ktime_ms_delta-v1-1= -ccb8672a0941@google.com > --- > rust/kernel/time.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++= +++++++ > 1 file changed, 60 insertions(+) >=20 > diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs > index 25a896eed468..6811d5cadbd4 100644 > --- a/rust/kernel/time.rs > +++ b/rust/kernel/time.rs > @@ -5,6 +5,9 @@ > //! This module contains the kernel APIs related to time and timers tha= t > //! have been ported or wrapped for usage by Rust code in the kernel. >=20 > +/// The number of nanoseconds per millisecond. > +pub const NSEC_PER_MSEC: i64 =3D bindings::NSEC_PER_MSEC as i64; > + > /// The time unit of Linux kernel. One jiffy equals (1/HZ) second. > pub type Jiffies =3D core::ffi::c_ulong; >=20 > @@ -18,3 +21,60 @@ pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies { > // matter what the argument is. > unsafe { bindings::__msecs_to_jiffies(msecs) } > } > + > +/// A Rust wrapper around a `ktime_t`. > +#[repr(transparent)] > +#[derive(Copy, Clone)] > +pub struct Ktime { > + inner: bindings::ktime_t, > +} > + > +impl Ktime { > + /// Create a `Ktime` from a raw `ktime_t`. > + #[inline] > + pub fn from_raw(inner: bindings::ktime_t) -> Self { > + Self { inner } > + } > + > + /// Get the current time using `CLOCK_MONOTONIC`. > + #[inline] > + pub fn ktime_get() -> Self { > + // SAFETY: It is always safe to call `ktime_get` outside of NMI = context. > + Self::from_raw(unsafe { bindings::ktime_get() }) > + } > + > + /// Divide the number of nanoseconds by a compile-time constant. > + #[inline] > + fn divns_constant(self) -> i64 { > + self.to_ns() / DIV > + } > + > + /// Returns the number of nanoseconds. > + #[inline] > + pub fn to_ns(self) -> i64 { > + self.inner > + } > + > + /// Returns the number of milliseconds. > + #[inline] > + pub fn to_ms(self) -> i64 { > + self.divns_constant::() > + } > +} > + > +/// Returns the number of milliseconds between two ktimes. > +#[inline] > +pub fn ktime_ms_delta(later: Ktime, earlier: Ktime) -> i64 { > + (later - earlier).to_ms() > +} Is there a reason for this function being standalone? --=20 Cheers, Benno > + > +impl core::ops::Sub for Ktime { > + type Output =3D Ktime; > + > + #[inline] > + fn sub(self, other: Ktime) -> Ktime { > + Self { > + inner: self.inner - other.inner, > + } > + } > +} >=20 > --- > base-commit: 768409cff6cc89fe1194da880537a09857b6e4db > change-id: 20240320-rust-ktime_ms_delta-74b00c9ab872 >=20 > Best regards, > -- > Alice Ryhl >=20