From nobody Sun Feb 8 09:12:12 2026 Received: from mail-qk1-f172.google.com (mail-qk1-f172.google.com [209.85.222.172]) (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 C33741A38D3; Sun, 24 Mar 2024 22:34:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711319660; cv=none; b=IdTNCRD4R1MxtGjab6Ht/dJHskgypREta1yBT5Kq6t60z6awZQWgqUu8oN0IzQigLo7X1UuX0C08oMCB60acm0FUagv2BapyqVqUqCImhcQPzbRj2ftZ7IdlVmg+cn+gTQv0AhoykKcpLgi2mT3EjGKzGasmLtwbPZc+pVSK5E0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711319660; c=relaxed/simple; bh=T8jE1fNZfbPD7fRxzoZy3IXaoPYSqWA6sjSsFVqxxLU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pTP2F0KPHRz1ea5P2Pj6HF9H3TXH44yvpeTBVpnoKxbcevCluS5JiEnEU9y0Xp09bDhkwYP5NVyrodbz9rfBPvLQFz8JplwcKsBc9IFI4H7UdKLnGBq7ggXoJsth6k/JgkREmG9qVtK6uKZnkeMlWjagcC7v/Hio49DuCxTf08I= 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=OY25uz8f; arc=none smtp.client-ip=209.85.222.172 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="OY25uz8f" Received: by mail-qk1-f172.google.com with SMTP id af79cd13be357-78a5580333bso29496985a.2; Sun, 24 Mar 2024 15:34:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711319657; x=1711924457; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=8vg6DKo7pmohO9e53wVPKI3PJtqwPw3kiGTVfZV5+Bk=; b=OY25uz8fllWafBPnMi/tSTt+NGPNSLNT5/19vxVFszFm8arBv9txzwzG8TRvpSarj5 vD3yCkC5M3zKz8+wszvviT9fQbH4DoI16zaSXunCcMv86esKMnzuGI6spk0Q41E+xyzj FBlq7MsgyiZVsgXOb4soiKi8guO3aD/Sem1BA2B7qDCQNDe4bUWTPuiUf3A1BqD5WnnU X+PlaKhmowRhRL0vgxlzyAoBIOsME8GnvwL0LoMq4hCc1t0pqPSI+gysD+nfO0KPz6Lu /2rBZm7IXp+Yo244E23Nwa3weDsOjj1ccZj87iYady47n8IaaFPZoynEKkv09zltH3Q3 /e7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711319657; x=1711924457; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=8vg6DKo7pmohO9e53wVPKI3PJtqwPw3kiGTVfZV5+Bk=; b=dwAhYvajDK1qmQbBZKbGAu6bm6fu6LNi3Qwkuetr/CE+ba4YxPx6FoOdzBqt5lrPs8 gnrG1L/uFJqPuaiCxIyzb2XQ+3KK0ym9FXdFiFcjXcMwQm0Sxuog0IYgp77LO5LEaP8b M1D3yIr646g82AqYGN03M+iybtCnuZ4dtfAxvaujnjqMhXh704bTR/lUIafDkFr9jL7R 9ZRvNr/HyuVdPbfHg6u+WYaUL8gHGKlan29bWOP24EPRiN+QmZOnkyocRBB/shBv15Oy xdEAvAK7+COhwMq3bEfsV8FBJlVviCYjHo40NS7z3UeMRSzBq4LU+2qAbBpA9Tqa+Zep V+zA== X-Forwarded-Encrypted: i=1; AJvYcCU+ZPGGngwBVbDKqKa83BgRPWtyWneKt1h8hfDsStW9VtQuxnvZsJtWEbfBA5VbB8WV6lFqfNYPCmwgGWHGbizI+hCB7JgGk6th7lqP X-Gm-Message-State: AOJu0YxGwtM383GXYQH0zc3GcGPOfx0NcZS8D0WAI9Feh2ov5q/puc+w AdG5HHT8jCkhkYG6PBRc7xLCgh39E8eWGZVRP9huqSkQOedtFx0s X-Google-Smtp-Source: AGHT+IGspEUpnu/ZPsX/qIFQsL6kaiAPPv2KtchS+uvNToGluKWw7BWFObL4Oc4kqlya+VW8yWG6OA== X-Received: by 2002:a05:6214:f07:b0:696:7ff4:7f09 with SMTP id gw7-20020a0562140f0700b006967ff47f09mr5468198qvb.54.1711319657700; Sun, 24 Mar 2024 15:34:17 -0700 (PDT) Received: from fauth2-smtp.messagingengine.com (fauth2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id q1-20020a05621419e100b006968077890csm1588628qvc.118.2024.03.24.15.34.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Mar 2024 15:34:17 -0700 (PDT) Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailfauth.nyi.internal (Postfix) with ESMTP id E32DD1200032; Sun, 24 Mar 2024 18:34:16 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute7.internal (MEProxy); Sun, 24 Mar 2024 18:34:16 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledruddtjedgudeihecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefhvfevufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeeuohhq uhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrf grthhtvghrnhepgeeljeeitdehvdehgefgjeevfeejjeekgfevffeiueejhfeuiefggeeu heeggefgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomh epsghoqhhunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedt ieegqddujeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfh higihmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 24 Mar 2024 18:34:16 -0400 (EDT) From: Boqun Feng To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Thomas Gleixner Cc: Alice Ryhl , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , benno.lossin@proton.me, Andreas Hindborg , John Stultz , Stephen Boyd , Valentin Obst Subject: [PATCH 1/5] rust: time: doc: Add missing C header link to jiffies Date: Sun, 24 Mar 2024 15:33:35 -0700 Message-ID: <20240324223339.971934-2-boqun.feng@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240324223339.971934-1-boqun.feng@gmail.com> References: <20240324223339.971934-1-boqun.feng@gmail.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 Content-Type: text/plain; charset="utf-8" The definitions related to jiffies are at linux/jiffies.h, and since `kernel::time` provides the functionality dealing with jiffies, it makes sense to add a link to it from Rust's time module. Signed-off-by: Boqun Feng Reviewed-by: Benno Lossin --- rust/kernel/time.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 25a896eed468..bbb666e64dd7 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -4,6 +4,8 @@ //! //! 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. +//! +//! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h). =20 /// The time unit of Linux kernel. One jiffy equals (1/HZ) second. pub type Jiffies =3D core::ffi::c_ulong; --=20 2.44.0 From nobody Sun Feb 8 09:12:12 2026 Received: from mail-qt1-f178.google.com (mail-qt1-f178.google.com [209.85.160.178]) (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 3E0C33D575; Sun, 24 Mar 2024 22:34:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711319661; cv=none; b=EJttXu+BmhTpazyVrf6CZxEM7Og9r/FRG27ZsD5CtFDXgiPPoKb9JGpr9ph4L7E8TTaMDJiz+clpxwnL85FjapuG9iJvNLrf4vzznX488AsVnrk54dBDW8CTxvxg8cjjlPWvy0+3IEDmhj0un2nxdPWhAv78BiBNqHm3io9QZTs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711319661; c=relaxed/simple; bh=vvpQmM/Tf5QRvh84FsdH0Tqa7QhnDpFIQ+omIra6w3E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GnN7QAzp4Ud+NmR+D0LMwwDGaAcOX+CqpzzcO2uzbVq64vdYMU/lrLAbb4HQPj98D2eWXeKerDLRIdFKmaqWLunQZPxJlG/EfpTwW8WgYa+6KmezbWkW5+euScqYhXLwA1ACCXdn0nQFaewKT/qRGXD73Z2KYkBEVbqb/6pUJmc= 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=S4GwqlM8; arc=none smtp.client-ip=209.85.160.178 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="S4GwqlM8" Received: by mail-qt1-f178.google.com with SMTP id d75a77b69052e-430acd766beso21109751cf.1; Sun, 24 Mar 2024 15:34:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711319659; x=1711924459; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=vdcgG0wCfsjDnuqEkDhhA7hvo7PO+8wNdJ07rpMikmg=; b=S4GwqlM81vlSasN7SFjspaCZHKKE3QBTURLPuF59GqWTYrmRvpDPcXfRc6qtOJfiFR 6QgwGZmTLhd+7Zl6wBRjUHSXZ+wrA121sIWSocCjHdciBOilQ08nmhs6GYgmARyrieu+ LVVQYLWe5a4MymM7dUCEbwrx+nIEOMVzUfbKuRP6009RIR6Rjax7Bpee6Xfg9I3INgTA rSJl389RNzvvkmSYwnKJTNU2U+bvurZN6WtEs8y/weM/CWUc9vJtuZuvKpqHXbJwzmbw srNL0Sj8FK6pecVUEkeu5+5ppJS+JLz2cZNkSXitGdFHyp/dfThgE/6PVXtwnHR2WhEY ZlHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711319659; x=1711924459; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=vdcgG0wCfsjDnuqEkDhhA7hvo7PO+8wNdJ07rpMikmg=; b=t4WYa7duoGGfFuAYdzEKgSIluuyCVklreO4CZeZ+WPNzx0Qju6AFBTiMEaL861W0JB 5p0I0CgGQQIllOeTtfmpaWdPvkCo1MI4wZ83nLvcQkHnbkHTccEfiW/sYofnOkyxnDCJ ExZukGdRgGO/w94e2vTMUh8AKL/Jpbw97AdbIz5SK8Cwu0EQ7xbo6lWUrXCD/dSJZXDa iex+tM14n9hztKstkeN3vA8yOpXNBkJDOKWTet7EY54yJpWQB0aoVTSdd6admpot1VUP Dra8uWQPF4bxIQ851+qUHnPjS4bFNgrAI0xQh/HUHEdEp/D69N4EWMcxFtYUVN/NH+GD teZw== X-Forwarded-Encrypted: i=1; AJvYcCXAfOM+9jbW7LJzDsFiIRbEHWT0PRNuVwYv/ZASGqpj4/ZasXjl3TzLykJvhsxE9FdSTkOX2iXjlu3Mwm5XLWcOrVSYB8i92AFar6bu X-Gm-Message-State: AOJu0YzHnKdRrZrH3UqqmGM93p4pQOPRyT6mi3+qvWj/ml1ugQPZwKhS OA9lo6YaeLFRqrx5LCIGv07ltI7kk5VVB9sZzyGH1AZrj1NeX2Xg X-Google-Smtp-Source: AGHT+IGyoyk66ziQtdmRMnAVtGYqPKFVrzwqgoF2f1PR+0p7X/wr6BEC7PCrJegX1V23TpgNtn15fw== X-Received: by 2002:ac8:7e90:0:b0:431:4f15:69f9 with SMTP id w16-20020ac87e90000000b004314f1569f9mr3427042qtj.23.1711319659224; Sun, 24 Mar 2024 15:34:19 -0700 (PDT) Received: from fauth2-smtp.messagingengine.com (fauth2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id ge11-20020a05622a5c8b00b004309754810fsm1976124qtb.89.2024.03.24.15.34.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Mar 2024 15:34:18 -0700 (PDT) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailfauth.nyi.internal (Postfix) with ESMTP id 58F1A1200032; Sun, 24 Mar 2024 18:34:18 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Sun, 24 Mar 2024 18:34:18 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledruddtjedgudeihecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefhvfevufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeeuohhq uhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrf grthhtvghrnhepgeeljeeitdehvdehgefgjeevfeejjeekgfevffeiueejhfeuiefggeeu heeggefgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomh epsghoqhhunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedt ieegqddujeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfh higihmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 24 Mar 2024 18:34:17 -0400 (EDT) From: Boqun Feng To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Thomas Gleixner Cc: Alice Ryhl , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , benno.lossin@proton.me, Andreas Hindborg , John Stultz , Stephen Boyd , Valentin Obst Subject: [PATCH 2/5] rust: time: Introduce Duration type Date: Sun, 24 Mar 2024 15:33:36 -0700 Message-ID: <20240324223339.971934-3-boqun.feng@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240324223339.971934-1-boqun.feng@gmail.com> References: <20240324223339.971934-1-boqun.feng@gmail.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 Content-Type: text/plain; charset="utf-8" From: Alice Ryhl Introduce a type representing time duration. Define our own type instead of using `core::time::Duration` because in kernel C code, an i64 (ktime_t) is used for representing time durations, an i64 backed duration type is more efficient when interacting with time APIs in C. Signed-off-by: Alice Ryhl [boqun: Rename `Ktime` to `Duration`, and make it a type of durations] Signed-off-by: Boqun Feng --- rust/kernel/time.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index bbb666e64dd7..b238b3a4e899 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -7,6 +7,9 @@ //! //! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h). =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 @@ -20,3 +23,44 @@ pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies { // matter what the argument is. unsafe { bindings::__msecs_to_jiffies(msecs) } } + +/// A time duration. +/// +/// # Examples +/// +/// ``` +/// let one_second =3D kernel::time::Duration::new(1000_000_000); +/// +/// // 1 second is 1000 milliseconds. +/// assert_eq!(one_second.to_ms(), 1000); +/// ``` +#[repr(transparent)] +#[derive(Copy, Clone, Debug)] +pub struct Duration { + inner: i64, +} + +impl Duration { + /// Creates a new duration of `ns` nanoseconds. + pub const fn new(ns: i64) -> Self { + Self { inner: ns } + } + + /// Divides the number of nanoseconds by a compile-time constant. + #[inline] + fn divns_constant(self) -> i64 { + self.to_ns() / DIV + } + + /// Returns the number of milliseconds. + #[inline] + pub fn to_ms(self) -> i64 { + self.divns_constant::() + } + + /// Returns the number of nanoseconds. + #[inline] + pub fn to_ns(self) -> i64 { + self.inner + } +} --=20 2.44.0 From nobody Sun Feb 8 09:12:12 2026 Received: from mail-qk1-f180.google.com (mail-qk1-f180.google.com [209.85.222.180]) (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 F01E04085A; Sun, 24 Mar 2024 22:34:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711319663; cv=none; b=nFfhwQ6Dr+YAkBBekHrvhXMDt5N4K/TAh47FdTqpvjYnY4wDT+9w6YHbhks7NR7Wa15Ixrnei8N329aOLKv0KD9aByhyAUmFxyDpjlS/wAlY7ZrWCHK5/n9vxlXGdxqCHAtSMfUrTtQTXsNQ5Z5YaR+IncGUJBLezaG8/MH/sp8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711319663; c=relaxed/simple; bh=rblbZbeUNDFkN6Ya4j5DhEAFl/f3CnZ68qMOk7KGkcM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=do07k7R1K4fd7YNZISi2Ml+KTx6asdgQlYOiWqbM1Kp52lEwL0cz30CEdGJDIdt57wI9eQIQwOqczhDBJDTUjXIgC9uKCtaAyqk2f97N3kzG57h7veNfKoK+edsefIK5sxOjmBX5dLFafPRsiR8pIcpDWx0+JsFgSDuR5FwY+HQ= 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=K1R+xqfN; arc=none smtp.client-ip=209.85.222.180 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="K1R+xqfN" Received: by mail-qk1-f180.google.com with SMTP id af79cd13be357-78a5580324eso26510285a.3; Sun, 24 Mar 2024 15:34:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711319661; x=1711924461; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=r4dkugQkcRSjLN47IMqLis/1vAo+zF2RZMH9C3fgvJ4=; b=K1R+xqfNaBFKjCbU9RZeZ0xAtQSzd42QwsQfJugTXogkO90NBOpeaS4f7Mmxd9onrd 1DegnjrAyUHhJJewfdunuxei7Icv6gp/uIg8NLYLuH1S+AriQLqyvgCzNgMS9fK9D2HK Oxm0hISab4cZQ9GMhZ7dbAXfnwRdIcat0Fzu5OwzZCFT7tP3j4JNytvWIPqCyd6ivruh NgZ96mDTw7ik+RcAETTWxJ4CmcDyW3TkNCXO/Ki+2v61dUUa6nx0JnIU2kafnbKwPH53 jyjj+K6Rvdz7AnopH3Oz/rEvw4GPLl6h10uREmG3DXPZ19H8F1RmzZWkf/HFxQZ682aJ pvrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711319661; x=1711924461; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=r4dkugQkcRSjLN47IMqLis/1vAo+zF2RZMH9C3fgvJ4=; b=B3s9q7UjGnU8wR2D/T5a1nj4Nk5Z/rbMHIMc0TXAJCIdte9PflzDS055fPhvB+Cdte nG6LPL4Tys5O2A/VGKFwdec4HrC9U12RcgS0irT4FE58zGysS7E8dXHC4vcF0jZdyJZH hUTu4H7C2oDYNEFegUhcr74APwARTpfAXd/S9yFN2bqp/1Ha+AJrmLEiLDjuNKBNefBX SF02lRmeDDPSNuLD7xk1V2V3dO/pM8Ln27H/Lor9ssMi9MDNZ7X7nfBmQV0mzOcbBCg9 /9pEKbXSOvZSm6U/F3K1RwHyw0eavy12PZ9lmqvTC1sUHYw0SpGgWRBGd7/k/FP/UxHs VDWQ== X-Forwarded-Encrypted: i=1; AJvYcCXN24RQHCtg63cJfLGciRJ/6ZjHY2bJcKMIFaewa43oztLZH/XeIJA0TCG906OowQu+vN8tjZw81Q7L27UfeoulQWvvoDUcsypacldr X-Gm-Message-State: AOJu0YwuzN8SN74P0l7iyqH4CmQANBfHctXW7NcreUctEkf1utxdyRk6 7RcA1ba9rduAspjl/sZMsCTu4cwZ7BNXJD9OpfDVnyyKfKMXgF65McvrILz8 X-Google-Smtp-Source: AGHT+IEGXwFi4OApdpQX0xAGF4IQqEMsHc2HieH20JpsoDEe2F+hszVBR1E3kHFAFNt1xADcoOjI+g== X-Received: by 2002:a05:620a:5d82:b0:78a:2b1b:e56b with SMTP id xx2-20020a05620a5d8200b0078a2b1be56bmr5899720qkn.68.1711319660903; Sun, 24 Mar 2024 15:34:20 -0700 (PDT) Received: from auth2-smtp.messagingengine.com (auth2-smtp.messagingengine.com. [66.111.4.228]) by smtp.gmail.com with ESMTPSA id q3-20020ae9e403000000b0078a48172335sm1356193qkc.24.2024.03.24.15.34.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Mar 2024 15:34:20 -0700 (PDT) Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailauth.nyi.internal (Postfix) with ESMTP id EAE1D27C005B; Sun, 24 Mar 2024 18:34:19 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute7.internal (MEProxy); Sun, 24 Mar 2024 18:34:19 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledruddtjedgudeihecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefhvfevufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeeuohhq uhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrf grthhtvghrnhepgeeljeeitdehvdehgefgjeevfeejjeekgfevffeiueejhfeuiefggeeu heeggefgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomh epsghoqhhunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedt ieegqddujeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfh higihmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 24 Mar 2024 18:34:19 -0400 (EDT) From: Boqun Feng To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Thomas Gleixner Cc: Alice Ryhl , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , benno.lossin@proton.me, Andreas Hindborg , John Stultz , Stephen Boyd , Valentin Obst , Heghedus Razvan , Asahi Lina Subject: [PATCH 3/5] rust: time: Introduce clock reading framework Date: Sun, 24 Mar 2024 15:33:37 -0700 Message-ID: <20240324223339.971934-4-boqun.feng@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240324223339.971934-1-boqun.feng@gmail.com> References: <20240324223339.971934-1-boqun.feng@gmail.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 Content-Type: text/plain; charset="utf-8" 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; =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 querie= d 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 {} + +/// A timestamp of a given [`Clock`] +#[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 { + 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 defines = signed overflow to behave + // like 2s-complement, hence `wrapping_sub()` is used here to mirr= or `ktime_sub()`. + Duration::new(self.inner.wrapping_sub(other.inner)) + } +} --=20 2.44.0 From nobody Sun Feb 8 09:12:12 2026 Received: from mail-qk1-f178.google.com (mail-qk1-f178.google.com [209.85.222.178]) (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 8EFEA43ACC; Sun, 24 Mar 2024 22:34:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711319665; cv=none; b=NT63A3eHlqNTrZg3A2JSXQsAqHRLRRxpGXWjUbayWKfXgQ0ZQLv+c6hDBnvLDl7cY5Mo9lIrdvisR/p3dmPnIXg7wUFvT3akOG4ESK+ielkMiDKjmjv5sMF0oYaDAQtSLpKA22efFHhUbFo8aSppghaQSAADVg4BR2LZRiM4P9Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711319665; c=relaxed/simple; bh=bJ86mbIQjLwbn//NFmnurEsU7QTgkvcp3FVZZRVJCis=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WqE8ABaSuDsxVk2yMoCmdfkL+Mroijz9h97zoEQ16MqCSsSH1AIV1WApN4aSYU66IRpq6FuMGe/mbigXT3mE+IEK9cTHP/a6ht0HwlxZe0XeMlENyJ7Bp7+dSbyeBKkwTVhDIPw07UpujLBahrSCahn0lPyIBdmCpkuhf/6feV8= 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=XcT/QOgL; arc=none smtp.client-ip=209.85.222.178 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XcT/QOgL" Received: by mail-qk1-f178.google.com with SMTP id af79cd13be357-78a3bccc420so171560485a.1; Sun, 24 Mar 2024 15:34:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711319662; x=1711924462; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=sdmtxUeccR9mblyOKVx+vdMDj9Zw7MyhLqQWnpnpKTE=; b=XcT/QOgLouX+XbDSUamrw2L1UgLIY3emyDz0iMOsWLpSBaWjC6iMuTq8wvkN/yqUFC TPwbFO4ksYueykT9CHGjELt4NeVzpQfZSu+tgVGTNFNXK9sxqGCRCwh3JmQtHIvqQnpM HVPyIy+BpNZ/hIZ77I67yYZzjwS2X/idCRf9hA5HEajahrv/k5prgdyAwFjjr2IGPo4o pw6QOZw23+cJh4nY9DNYat5wp5+d8BQAVtDhn2+NC6SZwFZ6K7rebDnTyPLIetwcjv/8 uMIKoYzndb9ptybt8iGwlG4hueRAT556nsdyrRA0EqQYrHeyuNZMsSVmoeaFn6V36cvi Xz1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711319662; x=1711924462; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=sdmtxUeccR9mblyOKVx+vdMDj9Zw7MyhLqQWnpnpKTE=; b=VTf/fzQnedF45TgW/jLjz9RAwJnKi1fDwB8mHCiR+wH5ocx0vc/QS1hSia9sTuR81g NOUypzSLaTJAVEKvvXya+7+gfpFI9oOtp2Eclz2MtGBo5u6k3xE7sIqbseWNl50k4yNC 7ZqBlFz751/xP85O4zCd0QnNiyI/07DarmU8+pwkPzQjN2l2rzW1cqw2VFv6JmhYuCyW TJsWjvQmmxzViGcwG00IyTmoIHZ0zdmzHamoWNbQCj3wLnFMFy4DnhOM3oty6VdC4NFH uBvr1sApTJ0/iKVGczGXSIN3OjVfFyyHExecH0GeA4zUPrJbRVoRUyMcuLOor42SFh7X +i5w== X-Forwarded-Encrypted: i=1; AJvYcCVadp1Edc2E/5TmGhTGLUkeaEz5w8fySiql5T0gkBIpmIZOHXOeOCSCISRpLGgTz7L/o2wi2U8dqT7SjjoHjR6UaOu4gxIZb7BFC+8P X-Gm-Message-State: AOJu0YyFToaDUSvo3Nb/hguVuiQS/L8pmwvGl+pX9gy2/+Iu3bNYglqz cWCB50u6z5qFJPHQ8Y9dJuWKWlSRTntL/gTr29OJkNis+yQ1o14q X-Google-Smtp-Source: AGHT+IFLQPh7gqo+zXF+9PbKRGc+rHtCUybFWEY/2W6Tn5dZpR95LD4k7sPOmVENHxAxa3tgQSbCdA== X-Received: by 2002:a05:620a:4c:b0:78a:3326:13f with SMTP id t12-20020a05620a004c00b0078a3326013fmr6094290qkt.58.1711319662549; Sun, 24 Mar 2024 15:34:22 -0700 (PDT) Received: from fauth2-smtp.messagingengine.com (fauth2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id w14-20020a05620a094e00b00787930320b6sm1652237qkw.70.2024.03.24.15.34.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Mar 2024 15:34:22 -0700 (PDT) Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfauth.nyi.internal (Postfix) with ESMTP id A14901200032; Sun, 24 Mar 2024 18:34:21 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Sun, 24 Mar 2024 18:34:21 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledruddtjedgudeihecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefhvfevufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeeuohhq uhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrf grthhtvghrnhepjeeftdetjeekjefguefggffgiedvieffleejieejueeugfehiefhhfef geekkedvnecuffhomhgrihhnpehkvghrnhgvlhdrohhrghdprghnughrohhiugdrtghomh dprhhushhtqdhfohhrqdhlihhnuhigrdgtohhmnecuvehluhhsthgvrhfuihiivgeptden ucfrrghrrghmpehmrghilhhfrhhomhepsghoqhhunhdomhgvshhmthhprghuthhhphgvrh hsohhnrghlihhthidqieelvdeghedtieegqddujeejkeehheehvddqsghoqhhunhdrfhgv nhhgpeepghhmrghilhdrtghomhesfhhigihmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 24 Mar 2024 18:34:20 -0400 (EDT) From: Boqun Feng To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Thomas Gleixner Cc: Alice Ryhl , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , benno.lossin@proton.me, Andreas Hindborg , John Stultz , Stephen Boyd , Valentin Obst , Heghedus Razvan , Asahi Lina Subject: [PATCH 4/5] rust: time: Support reading CLOCK_MONOTONIC Date: Sun, 24 Mar 2024 15:33:38 -0700 Message-ID: <20240324223339.971934-5-boqun.feng@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240324223339.971934-1-boqun.feng@gmail.com> References: <20240324223339.971934-1-boqun.feng@gmail.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 Content-Type: text/plain; charset="utf-8" Rust Binder will need to read CLOCK_MONOTONIC 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]. Hence add the support for CLOCK_MONOTONIC read. 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] Co-developed-by: Heghedus Razvan Signed-off-by: Heghedus Razvan Co-developed-by: Asahi Lina Signed-off-by: Asahi Lina Co-developed-by: Alice Ryhl Signed-off-by: Alice Ryhl Signed-off-by: Boqun Feng Reviewed-by: Benno Lossin --- @Alice, I still put the link to the usage of Android binder here, if you want to remove that, please let me know. rust/kernel/time.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 0f9f5605ed48..5cd669cbea01 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -113,3 +113,22 @@ fn sub(self, other: Self) -> Self::Output { Duration::new(self.inner.wrapping_sub(other.inner)) } } + +/// Contains the various clock source types available to the kernel. +pub mod clock { + use super::*; + + /// A clock representing the default kernel time source (`CLOCK_MONOTO= NIC`). + pub struct KernelTime; + + /// `CLOCK_MONOTONIC` is monotonic. + impl Monotonic for KernelTime {} + + impl Clock for KernelTime { + #[inline] + fn now() -> Instant { + // SAFETY: It is always safe to call `ktime_get` outside of NM= I context. + Instant::::new(unsafe { bindings::ktime_get() }) + } + } +} --=20 2.44.0 From nobody Sun Feb 8 09:12:12 2026 Received: from mail-qt1-f170.google.com (mail-qt1-f170.google.com [209.85.160.170]) (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 D666145027; Sun, 24 Mar 2024 22:34:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711319666; cv=none; b=okneP21+0SEIkBYrJlKFIvuSyTVJ7y6rIfZOJCkbGO6DWv6fWgzUQjhS30suEDyXwEaDXhPhaw4qt/9NLYMsW7GHwjUye5CBbvZH6x+SOTMHcOQSGlNISE6nEI83pcmXpV1whIo8cwzLwXMvTltitpq81jFw/r1oofqCFA37kQ8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711319666; c=relaxed/simple; bh=lTwVdmQCrScr7m5mI/A+ln2I0q5l4JBsQMRgmXUoJ3I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pyLRWzQmWC/Bk97TlZTnbmC9uJ2mn+eEHRs8g+rIMAWFVsKYX45Ap2LfGdeoDfTgyHdpKULXZHcK4xVhnZ+qEhThDMrkqqYMDuohMMoI4qVi/PUEiesmsiI55vz0+8AYsNMuaUu8/PTFld7oG8QOZHJzbW8i1C+uVPra23hH9U0= 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=KnMexYax; arc=none smtp.client-ip=209.85.160.170 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KnMexYax" Received: by mail-qt1-f170.google.com with SMTP id d75a77b69052e-42a9c21f9ecso16186301cf.0; Sun, 24 Mar 2024 15:34:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711319664; x=1711924464; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=ZEa8g/haIngtd9EpZwnZ7Ys0qyF84K/G4/YzTdjByEo=; b=KnMexYaxRUU0FeoWJcoYitFZWyJ1dMQ88xehbhuXXND3KT6/krUGhm7R+I5ZrKKz5C c50Sa7EBBe7S8HalDnDE6bLEtc9mWJ24LzsDQl1vaYIqDyFAMQAP5hj8ZFijQoEqi0iB zzZXCxH/eFsPaoVYkuVkvCEjE98PIBiDc/Rg5oipcsRFa1FQDYzSBAQsQ3Gc0bSnBN7b xuj8yM9Gh8R/vrUwhB0Zh10FBuHHMJTPF0F4lKGNzKcP84nfPMzXlBT0LxXv8jwkfHAe PQmWS3XucQNjrmGNoFutJaTXCLeKE8kvINsinu5BTppGcWsVZakwrjDETQuQlAXltgPf /+VQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711319664; x=1711924464; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=ZEa8g/haIngtd9EpZwnZ7Ys0qyF84K/G4/YzTdjByEo=; b=Ah1uAFrddukajt6zuAN7ECjRsmhOUE1KjwtK9x00Rxh6fhupnpiAI+U2kzIlRIcE1Z clbBx522wmRFi1NS8f1b2ukxMfp69dXT/mEMYkA0Ad7pEvN3hPXaRkSF0a29WTdXsggp UD2IZVu66RmZfOuvq0IxaDrUwjWu9WJYStgpavw7bFInGXDN5L+Zy2j741G181wACgIs 7qhCaEk8bE8k9ApX1xc8eZb7prfWk8GOuMsR9/4y3aX2GP9fcsiNEA6s9Sq/0yfVMHl7 8r70mRvE0z6YE2ORzCNG9X1TomWpRo6ZA31SUNw0Q0ex9ieI1plJGzLAtj6LsJ2aStxT K7Pg== X-Forwarded-Encrypted: i=1; AJvYcCVeUH+RVmNSOyMkSsj6Qv8tPpZHDVsdKJMPgaW3wCgMPOVYhVW/dX54VkzKofj/T/gcUdwshA5xAchW4XH3tyajdrpPe+aoLtuCkDEM X-Gm-Message-State: AOJu0YwLoJpKleGOazJu2PTAaobedEjv/EwVxnWmN8waS5GULl8ConEU 1ymrDQREbtemKePP+A3Jlz1TMnudcOCLGSC860SSbtQHpGhO1rwH X-Google-Smtp-Source: AGHT+IHTDI1JEoCUmChP9SyqwdtkgaX+JR845mvTO/Rmz18qw2TzjG+FQ3+khfqZhaaZOZ9wobjYgA== X-Received: by 2002:a05:622a:4e85:b0:431:3df7:6fd4 with SMTP id dj5-20020a05622a4e8500b004313df76fd4mr6891381qtb.2.1711319663869; Sun, 24 Mar 2024 15:34:23 -0700 (PDT) Received: from fauth2-smtp.messagingengine.com (fauth2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id hg1-20020a05622a610100b00430b5dcac34sm1980354qtb.8.2024.03.24.15.34.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Mar 2024 15:34:23 -0700 (PDT) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailfauth.nyi.internal (Postfix) with ESMTP id 052F21200043; Sun, 24 Mar 2024 18:34:23 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Sun, 24 Mar 2024 18:34:23 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledruddtjedgudeihecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefhvfevufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeeuohhq uhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrf grthhtvghrnheptdehvdeivdfhieffgeegleetfefgjeefgfevvdfhteeljeehhfdtvdfg tdehfeetnecuffhomhgrihhnpegvlhgrphhsvggurdhtohenucevlhhushhtvghrufhiii gvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsohhquhhnodhmvghsmhhtphgruhht hhhpvghrshhonhgrlhhithihqdeiledvgeehtdeigedqudejjeekheehhedvqdgsohhquh hnrdhfvghngheppehgmhgrihhlrdgtohhmsehfihigmhgvrdhnrghmvg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 24 Mar 2024 18:34:22 -0400 (EDT) From: Boqun Feng To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Thomas Gleixner Cc: Alice Ryhl , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , benno.lossin@proton.me, Andreas Hindborg , John Stultz , Stephen Boyd , Valentin Obst , Heghedus Razvan , Asahi Lina Subject: [PATCH 5/5] rust: time: Add Instant::elapsed() for monotonic clocks Date: Sun, 24 Mar 2024 15:33:39 -0700 Message-ID: <20240324223339.971934-6-boqun.feng@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240324223339.971934-1-boqun.feng@gmail.com> References: <20240324223339.971934-1-boqun.feng@gmail.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 Content-Type: text/plain; charset="utf-8" This is a convenient way to do: t1 =3D Clock::now(); ... delta =3D Clock::now() - t1; Hence add it. 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 | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 5cd669cbea01..cd1e45169517 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -114,6 +114,31 @@ fn sub(self, other: Self) -> Self::Output { } } =20 +impl Instant { + /// Returns the time elapsed since this [`Instant`]. + /// + /// This provides a convenient way to calculate time elapsed since a p= revious [`Clock::now`]. + /// Note even though the function only exists for monotonic clocks, it= could still return + /// negative [`Duration`] if the current time is earlier than the time= of `&self`, and this + /// could happen if `&self` is a timestamp generated by a [`Instant`] = + [`Duration`]. + /// + /// But for typical usages, it should always return non-negative [`Dur= ation`]: + /// + /// # Examples + /// + /// ``` + /// use kernel::time::{Clock, clock::KernelTime}; + /// + /// let ts =3D KernelTime::now(); + /// + /// // `KernelTime` is monotonic. + /// assert!(ts.elapsed().to_ns() >=3D 0); + /// ``` + pub fn elapsed(&self) -> Duration { + T::now() - *self + } +} + /// Contains the various clock source types available to the kernel. pub mod clock { use super::*; --=20 2.44.0