Received: by 2002:ab2:7988:0:b0:1f4:b336:87c4 with SMTP id g8csp115899lqj; Thu, 11 Apr 2024 11:22:19 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWzLaTKMY56uxLLQYWPOlHco1+SpcE/kP7Cgm6OzSaRDDu8b5vC8WhAWT3D6cT0wBcY80omGXzyAl4Z4+J6GkTpTgYGKap5/7RZai2kHA== X-Google-Smtp-Source: AGHT+IHVc465VZXr+Zb0lW+9BAMIpa+BiwBt/LmplwJAL/w6iA9x3iRTAThiEu0NCDmKKoRRPhYd X-Received: by 2002:a17:903:11c9:b0:1e2:bfd4:b83 with SMTP id q9-20020a17090311c900b001e2bfd40b83mr312917plh.44.1712859739045; Thu, 11 Apr 2024 11:22:19 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712859739; cv=pass; d=google.com; s=arc-20160816; b=xzBk9q6Uo2375EyWJ/O7Ba/5m4vWaPotye0Fw+RLhWzk1yDTVT6xi5SHHpcoDcBOFC nJ9Zc0a9nZCLlDQ8dN5vJdN46lZdwoSJY+WAbiuEhaSpXY1FQBgh/leiMY2ZPw/OAI1v KS4iTbRpo9ddZWVYlGXfJ0hQNdbYb7yFkzBURuQlW61UhYtZBkywSTK9msuw/YJUT1ki cUpQCNjfY12a9PWkRe3cupA6a2lhp9x9TC3psPpjJGZs1+Ay6MXEeOD8QZhvp/txArL2 c1Mw/4LpfVzb4lLGxb4xl7RPh82Q/qqRiOzuG8Jn786wfH9qZffOqxZLHabUD9s24j3/ z0DA== 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=xIcOsLyrVQHki9Dz95LMR/vycR1ddNkayx5GTY0yswM=; fh=rwae0lx0ELU1T+CM9ndqU6vbIwJmtZq/KvDwn3KAEuY=; b=Lgbad5R8D5TMOGLAW5Nf9FFYEey2Zp4vEQaYfAp6S9NF0+BTGNy22EZqD3Nm7z384e ozYcowTZRB7IJ9MRULZn7d3tc04TfZPCkeim0rA+pMmFAtrcyJEQHGwduwufDwXVD2+T XWTplpe49ZXTa8zgTCmj3I6duUYuFkd3IoCUnzcDMGOEqPlrWDTE+pHRtkLVmP5WIJuq kCPAP+Dr5f63XcBgllNW2/2pKdjTIgWIQATJ5Ms9GD606Mnzyz1/U+oIDClXHO88bMlw 5FS55gBrcqxwpYceZTpZstn6fTUdK2d1CAF4sRBCi+KrWqCkBS8eb2sgg0dEn6+Ga1Kc WAZQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=ZscvUQgx; 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-141246-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-141246-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id r15-20020a6560cf000000b005dcd296e2bcsi1600101pgv.665.2024.04.11.11.22.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 11:22:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-141246-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=ZscvUQgx; 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-141246-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-141246-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id E8E63B30F4F for ; Thu, 11 Apr 2024 17:26:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6E2995D479; Thu, 11 Apr 2024 15:57:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ZscvUQgx" Received: from mail-qk1-f171.google.com (mail-qk1-f171.google.com [209.85.222.171]) (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 CD82A2BD06; Thu, 11 Apr 2024 15:57:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712851026; cv=none; b=WEv9qM0UzhlifW/lypuLAuGS24RiHrKppBL0JnwGVCR+u8aP5ZeXRuCsgsvyONuDVk2m7QHlOOOISIYVHE9rcCeniP7U4Wk9kGT7ASzUBolwox50mD36vdpn4K4LpS+3jQvNGNUi3oCD3oEgRXQS4nCoXm/lnH8KV8/N+d8++JA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712851026; c=relaxed/simple; bh=j0C88cQjcRARX8NAqzbWswdUlX/bmV95PPqBm9svuP8=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=bmG+b7CUq7KhvFV6G6oGz3Q42AzfObbAHty4YcCkROF0QmezpVFy8c3Pl4zNS7vEd1bC4MHOblhOC8BvbSIgjuVuK8S4AJOydh/VehMuaP5bgvr5SJQ0VuY60/AixpAwpCQsoqCwMVQG2yV/rAU2B+akAPLeV+oukvJkqji/KyA= 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=ZscvUQgx; arc=none smtp.client-ip=209.85.222.171 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-qk1-f171.google.com with SMTP id af79cd13be357-78d7558bf10so184882885a.1; Thu, 11 Apr 2024 08:57:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712851024; x=1713455824; 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=xIcOsLyrVQHki9Dz95LMR/vycR1ddNkayx5GTY0yswM=; b=ZscvUQgxIU91VYb68QmTzdyLUFFzC3NUc1r0j/2HbgTfzSA7kt1SPles5xbVctjf0Y kNghhPNO4rIAM5oeYMjPRKzKtXoM+6szzGRd1aTl7NhhZK9EJC3xnfTFP828RcW+y66a yZqjZkcqEhI5/2j7xa3eVe37oQbuu8Xj1prcTV7KKMx+w0xJNkk05J0YBOd2SNNtQJos X4OpmnrcYrNA6lspYM6//Z6b7K0ImGc8ectzi3DxMVzr8S2kzY73o+rkjy/5JSRHkUwE sqX/cyOpoTKEoF9yCp4XUsaQVxKjjn/5b+mMtVt6L+2Ev3GlijI/J9VyLgy/OulCQJ7w VlmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712851024; x=1713455824; 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=xIcOsLyrVQHki9Dz95LMR/vycR1ddNkayx5GTY0yswM=; b=aPnefqZGDSHU1Hd/gsPK3ZfEZCy7Mqx/VylBYPAKUS38faniD9tuOQhazQDNZMIqks EMFCqWaditG28892WGAzRHy/DWYhL+4Z2S1U/ZnmU0yL/lNm10+sWJuf+9BawcY8PJaC WnyRpyZwV3u1pR7K4kMFBiY4lp3uXBKyyODcsjXSTJIlxttFMQZ+npJk/Q2DkKM+nVFn 2nkvcKj3AuO01v/QPaj0vL3+J7kAZxcBHLHXkziE23jJI1D6IMbqug0CBU4AvevKiehr knj3K1b1dBlfJqcNivCdaJVm2ImRprGCYUMqE+UaINnpqay028TAiM26AH2ci0D8xpE+ eE4w== X-Forwarded-Encrypted: i=1; AJvYcCU/EtOcPg5yPgvqLEs3jA3eUUSHrIDcINQXnOcOOOBhSwbefMPGyhHexqwZTMieinScg5s6Nf8NmFueOoSHDDMWaF9qjadungtpLmA455K96AaJBnNzRcsCbbokFws+RXjCs1PZqv7s8nq0Zq4= X-Gm-Message-State: AOJu0YypZG/JVPp2SWpbpzis4yib8mYHtjaCDYfUSbbinBcGJffOcRP6 QjNNROyzUIX2/g41vngeqGTEfJUKMLeV4uN7+qU23keO3zoYf1lP X-Received: by 2002:a05:620a:890:b0:78d:5a03:2595 with SMTP id b16-20020a05620a089000b0078d5a032595mr53988qka.24.1712851023748; Thu, 11 Apr 2024 08:57:03 -0700 (PDT) Received: from fauth2-smtp.messagingengine.com (fauth2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id b18-20020a05620a119200b0078eba18c234sm1182666qkk.47.2024.04.11.08.57.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 Apr 2024 08:57:02 -0700 (PDT) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailfauth.nyi.internal (Postfix) with ESMTP id 38072120006A; Thu, 11 Apr 2024 11:57:02 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Thu, 11 Apr 2024 11:57:02 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrudehkedgleehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesthdtredttddtvdenucfhrhhomhepuehoqhhu nhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrg htthgvrhhnpeffhfekuedvjedtudekgfetffeiffeludejuedufeeftddvjeefkedvveeh ueegvdenucffohhmrghinhepkhgvrhhnvghlrdhorhhgpdgrnhgurhhoihgurdgtohhmpd hruhhsthdqfhhorhdqlhhinhhugidrtghomhenucevlhhushhtvghrufhiiigvpedtnecu rfgrrhgrmhepmhgrihhlfhhrohhmpegsohhquhhnodhmvghsmhhtphgruhhthhhpvghrsh honhgrlhhithihqdeiledvgeehtdeigedqudejjeekheehhedvqdgsohhquhhnrdhfvghn gheppehgmhgrihhlrdgtohhmsehfihigmhgvrdhnrghmvg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 11 Apr 2024 11:57:01 -0400 (EDT) Date: Thu, 11 Apr 2024 08:56:58 -0700 From: Boqun Feng To: Alice Ryhl Cc: Miguel Ojeda , John Stultz , Thomas Gleixner , Stephen Boyd , Alex Gaynor , Wedson Almeida Filho , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , Andreas Hindborg , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] rust: time: add Ktime Message-ID: References: <20240322-rust-ktime_ms_delta-v2-1-d98de1f7c282@google.com> 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: <20240322-rust-ktime_ms_delta-v2-1-d98de1f7c282@google.com> On Fri, Mar 22, 2024 at 08:59:38AM +0000, Alice Ryhl wrote: > Introduce a wrapper around `ktime_t` with a few different useful > methods. > > 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]. > > For a usage example in Rust Binder, see [2]. > > 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. > > Link: https://lore.kernel.org/lkml/5ac8c0d09392290be789423f0dd78a520b830fab.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 > --- > 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(+) > > 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 that > //! have been ported or wrapped for usage by Rust code in the kernel. > > +/// The number of nanoseconds per millisecond. > +pub const NSEC_PER_MSEC: i64 = bindings::NSEC_PER_MSEC as i64; > + > /// The time unit of Linux kernel. One jiffy equals (1/HZ) second. > pub type Jiffies = core::ffi::c_ulong; > > @@ -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() > +} > + > +impl core::ops::Sub for Ktime { > + type Output = Ktime; > + > + #[inline] > + fn sub(self, other: Ktime) -> Ktime { > + Self { > + inner: self.inner - other.inner, Nit: although we use "Release mode" to compile Rust code in kernel, so i64 substraction behaves as 2's complement wrap, I think it's better we use wrapping_sub here: self.inner.wrapping_sub(other.inner) however it's not a correctness issue for now, so with or without it, Reviewed-by: Boqun Feng Regards, Boqun > + } > + } > +} > > --- > base-commit: 768409cff6cc89fe1194da880537a09857b6e4db > change-id: 20240320-rust-ktime_ms_delta-74b00c9ab872 > > Best regards, > -- > Alice Ryhl >