From nobody Fri Dec 19 11:06:34 2025 Received: from mail-pg1-f180.google.com (mail-pg1-f180.google.com [209.85.215.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 240611A3161; Sun, 13 Apr 2025 10:43:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744541029; cv=none; b=NlOImYxhn3fzE/qJLOzQ6vBVLMEabZ0IjX63FO/8YhqkXFeguL37JiaEac/0m5vy9SGHxZta9AV5iLrLctPXp3TBFHr9f93FU1xc8WfWKzV3qoBP3nKQNkb9HDoHHgv+BzxxJKw/36FSK2Q3mCDav0LyJwhkv8AHohRG3bHJCUE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744541029; c=relaxed/simple; bh=kd3vW/wW1gabeQG7Fy0PVk4IKsLzJY/aNs7T6WdurXM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Xsf7F/ehyqhZBxK1o3eHriaUsqQKo2cbzilxGrRCJBRG9LhTJJYy0cbplm06Kn1yubuokT75viEZZNvg91AxGUH3jUXqkCB+69H8Eau+clE3UF8xI5tj9nCjG9ep07d6MoYXdvQjeeEJDWzCyporaqmQLI7UiyqhPLHGUh5EeAQ= 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=Hxkzz6TF; arc=none smtp.client-ip=209.85.215.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="Hxkzz6TF" Received: by mail-pg1-f180.google.com with SMTP id 41be03b00d2f7-af5cdf4a2f8so2523028a12.3; Sun, 13 Apr 2025 03:43:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744541027; x=1745145827; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=OA/MmqYliLwWhi3o92n3t+JslJEn4NlsVVnjQQ5ihp8=; b=Hxkzz6TFo6gvkivfe3NiQ4KquZsJQiYUXxzZu5//t+7AeF90KZ9lwyURQ72AZn9SWl zDEa5srn80bcME8BiAv34LVkzeBZP2ljITbieOXkPND1/r4BzAH2+UtLj1cMwp0ch/dv YD1TBx5EkV6tilXmIkkj98AFkGR3urD2xGpStA82tIm6f+SPjRoI8p5da69urqZIxGOQ bTNTEQ8k3R8V4YXzQiH31VWalQwpahzIGIzyIpiuGyBNKYYd6NYR/pnpYZZSw2HbhqYS rZMDjfHl9zJYNqq6bnf1UBr0f1Xfbo/zBRIDee94FV7JoiiPthH2qqVlZXsL2wc487pc H2Fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744541027; x=1745145827; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OA/MmqYliLwWhi3o92n3t+JslJEn4NlsVVnjQQ5ihp8=; b=imvBjrhRBk2cgsfQr44l+Z03aLb4+91TxqkTvufXEanBWizHR8jRf3VgXTJn4sdpVw qGXIl98Ez3GZuIqzuxUbtIQVk2DDBC/xI45zfcobMSBP4q93Y/P+8WXdhxVDehd5fQ5K zGEAOJj5Aw5NM1HRbD4R4rL8WjmCebUGBbY+Y67bQQMrYC378R2YhsMuJundPcNNDvZg Sq/1eFKUvVF0trqX6LVccI090TNrDxyRf2mskWd6Lt6Vi/C78f0vTEptZtSK35p0xPJ+ S0FqY36pKbjOTdneSGjbZy0APYZ85ULGe1sAxC7FeeJHMsNmPoi+i2TKTTnzgZTZQ+Yo d9lA== X-Forwarded-Encrypted: i=1; AJvYcCX12Ck6Km2weUy4Ve0CPNCS9m3Xw9ThQn59KU9PQ2H3Q+zCFD2xpzC8UIGhA6hCGNzhZ/qcf+U8@vger.kernel.org, AJvYcCX7bEcNvz7QEeMA7dq4jm5z0/hsv+zl/zemDY1i3NATjx7ZSVK5K1iD+JVAxC3wjPqYB7UiqSr7JBQAaHs=@vger.kernel.org X-Gm-Message-State: AOJu0Yy/P17JFWSwSV9vwoWJG/yAxQ4YXIXmbz9FWCYvX2QYo9/3ShqK Ns+0MDk1n+0wCFAacabsvfGLwQiEsfYgWfNMvAH/VkP/w99vCt/Kg7ofDMz6 X-Gm-Gg: ASbGncuV+RNdX9CYAemP/QgGOnauCa7PIFrqI7+8d9HTKyEvizpuhABPgoqiImJl6mL XVhajkC7ce6F/SHggXDK9wE+BHAn2ZF8mabUHH5loOgqIoUwQd2ACmHZ6MNxv1y1KSADGHtFV+6 KGAL8DihrMYNKyBzbMSPe+v2gbPZnnf8dO7Cez7iGMrD36JOcucDiU/HijoubQK0W9f/dtapsoV 526xA44QN3qNAmlZO9TnNeBcgd7wlPAKi2Z3tEtXE1q2DTgZrb42Q+6i8KuIHrd5vGLdtvRBLoV wJXZSipOqt7PHV01BAUHmwG3KlCxtn0bcKV5DO/KTXodfe74iJcgUdlmHAHejxwm7ubzI1OHwm2 ddYobDT635NvwWWmTnqVaro7NSFhK X-Google-Smtp-Source: AGHT+IGRkR4EaVgat4SBKAiC1GXIfoLQhYI6osVfl/nEhYCqGaHVV2NMBlqKKbIQY7b+ds5ZQ2BCzw== X-Received: by 2002:a17:902:cccd:b0:224:910:23f6 with SMTP id d9443c01a7336-22bea4fcc8dmr133509105ad.45.1744541026919; Sun, 13 Apr 2025 03:43:46 -0700 (PDT) Received: from mew.. (p4138183-ipxg22701hodogaya.kanagawa.ocn.ne.jp. [153.129.206.183]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22ac7b8af56sm80160885ad.66.2025.04.13.03.43.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Apr 2025 03:43:46 -0700 (PDT) From: FUJITA Tomonori To: rust-for-linux@vger.kernel.org Cc: Trevor Gross , Alice Ryhl , Gary Guo , Fiona Behrens , Daniel Almeida , Andreas Hindborg , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, andrew@lunn.ch, hkallweit1@gmail.com, ojeda@kernel.org, alex.gaynor@gmail.com, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, arnd@arndb.de, jstultz@google.com, sboyd@kernel.org, mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, vschneid@redhat.com, tgunders@redhat.com, david.laight.linux@gmail.com Subject: [PATCH v13 1/5] rust: time: Add PartialEq/Eq/PartialOrd/Ord trait to Ktime Date: Sun, 13 Apr 2025 19:43:06 +0900 Message-ID: <20250413104310.162045-2-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250413104310.162045-1-fujita.tomonori@gmail.com> References: <20250413104310.162045-1-fujita.tomonori@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" Add PartialEq/Eq/PartialOrd/Ord trait to Ktime so two Ktime instances can be compared to determine whether a timeout is met or not. Use the derive implements; we directly touch C's ktime_t rather than using the C's accessors because it is more efficient and we already do in the existing code (Ktime::sub). Reviewed-by: Trevor Gross Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo Reviewed-by: Fiona Behrens Tested-by: Daniel Almeida Reviewed-by: Andreas Hindborg Signed-off-by: FUJITA Tomonori --- rust/kernel/time.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index f509cb0eb71e..9d57e8a5552a 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -29,7 +29,7 @@ pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies { =20 /// A Rust wrapper around a `ktime_t`. #[repr(transparent)] -#[derive(Copy, Clone)] +#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord)] pub struct Ktime { inner: bindings::ktime_t, } --=20 2.43.0 From nobody Fri Dec 19 11:06:34 2025 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (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 A283820EB; Sun, 13 Apr 2025 10:43:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744541036; cv=none; b=lKU5lKJ9t6zqFXpXyzxnfIQRx3h7LdIVYaS0J2ZXXAVY68tr0H3lex86toS4ZzrNe2hS8EZq7xPzynn671O4KzLg2NhamXwThVQ3fWGm/Uwl1ZQZNV5m+h3EbMIPpHonu9ArrjO0ty0hv87pqWxyEAa4AfyatlnRf7rRw4HqUus= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744541036; c=relaxed/simple; bh=12o8EpFplw5vxKVaPZGoT3TplIy82zwNzBv2WcTOE8Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EhJIT9fHqqPttDiawS77IekRvfGuE1uviiPzXUimEnJmtiInS4odvgHXJorKKgdEHzMNNdkK5iXBk5TwmgFFVHL1ldSBHpw/6HymvRKPXqKoQNYvNGPcjshbA0GuGgf32LkdOg2NMUiUK4AwxjWdS34QTPPPqoOXqZAW/YlGSb0= 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=aS56iUWf; arc=none smtp.client-ip=209.85.214.169 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="aS56iUWf" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-2295d78b45cso47754485ad.0; Sun, 13 Apr 2025 03:43:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744541034; x=1745145834; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=sB3yUUfhGJKZTpW8owaV/xnUEivaCxVXrQXi/bn6tLQ=; b=aS56iUWfQ3jEqWJKmlnUNfTmmz+0VaBHfPkqzGrvkBlI/YQZUCW3ijdfgo8q/5qw9z zMrMZzu+e7f1WuBUtVQu3sGmhVXFCzbA+IwWxzHCMfLAfbucSSCYNynqgqqEMMZkNu61 ZDI7rA4Mhqg3ufLJbBLwGhNGDS7pxWsvTIYQWQpRRx2f1ySizLL7Eh3dAq9OWyIOme/6 R2YyJR/JqeLVsveQZH/J6s7hLTbOw/XCihsuzW8wO3xwxvzmWnZ3iKCnK0vxNjIZI8uo rEcZ9fHQY8KslZxSjn4lpIDhS6bQGSX5qkXV2OxOIInPFXsVQ+/opLlPxhbsSHFZ74I1 M8/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744541034; x=1745145834; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sB3yUUfhGJKZTpW8owaV/xnUEivaCxVXrQXi/bn6tLQ=; b=cyed78EEG7JwoD97F3dAeELCV84HmkwkFuxXXRhA7J8NhzGao1fHIdnExNODVeMliL Xzsn+FiZcp1u/25brrdhFtJVsO2VAIkfgmKDudWqaeNkrJBhMdBcl6HOOUylZ5nvU77J 0cWrNqLJ/u+7eYhvlRncKRYLfcabYkHiSkoowa1nfffNBPAG2IQyRCnOElcccKgyZNpI 9Jt7LTnS61ICGuTutmQDx6w++4V+lW8zp13QdQsw7HiXr/il05A0lf7AByUvB+txhcLy ZN0BKJwYkg+IbXzgQznN9tQnk+qBVT00nYwJv49JOD/WGwdQuKp333XyspWtmHlkc7Qt pflw== X-Forwarded-Encrypted: i=1; AJvYcCVEQEFR6a8xT/OTE+fxiliVlRdx0vXpwgaRmcmb6j3mhGTCzKsWGZBch0jvTgoHHaDsBh4wGXAt@vger.kernel.org, AJvYcCWBF2C/w+wfw9Ag7D+UGnpYISiktvfDjsNv1PK0DybnAxOZlYTAaCmm5MobIe8yH75sq7gbYAsO4HgKcNQ=@vger.kernel.org X-Gm-Message-State: AOJu0YzvDcHTk2c8lDDKO4QSbqsi2OUAcYQ0Y3sqDIKgJnNdHXjJxjjZ xIOuzFw7NycxYRv+wTH+LCxFC09xlNGqlI3Vt+e6y1IY7vXhTeWD20jkF0lX X-Gm-Gg: ASbGnctRGFjZbE9ruoKonWrUQt8Kn9xUHlYHUeHnFfmqX/92fgl4yIZk3MPdr3A5UUE W09TTMexod24SlZus4r/nzpSMFBQEOtd70R+qbYmEqLcRlzcJ2aPUtPfleug2Sy4gdrmgKGCa43 i0tyY3qXB8rLgwQRlBQPU2K0J40UCBCM+URtSdZWFlEzyXiIdn2aoa8oxFZwnDy95WVYeOpxZXA 8KjcdLmDZepOswzvyS6GMQ32kbFcp8BPPb8dXAnn7ef6AK/YEsJl7bIBvdjXMwKKjWxX99a1TsK vMHmuSlE7YNlmL27Ze0ZqB38ToHjS1LbQoEMtZ8+eI794E/Zsc9W8iU2odRrnTyGgLNaosxgRWz 8+6tXv1Y7MSfPkfqsLyt+aACe6B6K X-Google-Smtp-Source: AGHT+IGyJzXu/QZWZvnsXLrxgv5e1KiAX6WTGdLvQ/PfQMXGA3E+O7CbPNK9xknSW7PUkWb5rUTiLQ== X-Received: by 2002:a17:902:ce8d:b0:224:1579:5e91 with SMTP id d9443c01a7336-22bea50bcf5mr109395525ad.47.1744541033511; Sun, 13 Apr 2025 03:43:53 -0700 (PDT) Received: from mew.. (p4138183-ipxg22701hodogaya.kanagawa.ocn.ne.jp. [153.129.206.183]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22ac7b8af56sm80160885ad.66.2025.04.13.03.43.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Apr 2025 03:43:53 -0700 (PDT) From: FUJITA Tomonori To: rust-for-linux@vger.kernel.org Cc: Andrew Lunn , Alice Ryhl , Gary Guo , Fiona Behrens , Daniel Almeida , Andreas Hindborg , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, hkallweit1@gmail.com, tmgross@umich.edu, ojeda@kernel.org, alex.gaynor@gmail.com, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, arnd@arndb.de, jstultz@google.com, sboyd@kernel.org, mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, vschneid@redhat.com, tgunders@redhat.com, david.laight.linux@gmail.com Subject: [PATCH v13 2/5] rust: time: Introduce Delta type Date: Sun, 13 Apr 2025 19:43:07 +0900 Message-ID: <20250413104310.162045-3-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250413104310.162045-1-fujita.tomonori@gmail.com> References: <20250413104310.162045-1-fujita.tomonori@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" Introduce a type representing a span of time. Define our own type because `core::time::Duration` is large and could panic during creation. time::Ktime could be also used for time duration but timestamp and timedelta are different so better to use a new type. i64 is used instead of u64 to represent a span of time; some C drivers uses negative Deltas and i64 is more compatible with Ktime using i64 too (e.g., ktime_[us|ms]_delta() APIs return i64 so we create Delta object without type conversion. i64 is used instead of bindings::ktime_t because when the ktime_t type is used as timestamp, it represents values from 0 to KTIME_MAX, which is different from Delta. as_millis() method isn't used in this patchset. It's planned to be used in Binder driver. Reviewed-by: Andrew Lunn Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo Reviewed-by: Fiona Behrens Tested-by: Daniel Almeida Reviewed-by: Andreas Hindborg Signed-off-by: FUJITA Tomonori --- rust/kernel/time.rs | 88 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 9d57e8a5552a..e00b9a853e6a 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -10,9 +10,15 @@ =20 pub mod hrtimer; =20 +/// The number of nanoseconds per microsecond. +pub const NSEC_PER_USEC: i64 =3D bindings::NSEC_PER_USEC as i64; + /// The number of nanoseconds per millisecond. pub const NSEC_PER_MSEC: i64 =3D bindings::NSEC_PER_MSEC as i64; =20 +/// The number of nanoseconds per second. +pub const NSEC_PER_SEC: i64 =3D bindings::NSEC_PER_SEC as i64; + /// The time unit of Linux kernel. One jiffy equals (1/HZ) second. pub type Jiffies =3D crate::ffi::c_ulong; =20 @@ -149,3 +155,85 @@ fn into_c(self) -> bindings::clockid_t { self as bindings::clockid_t } } + +/// A span of time. +/// +/// This struct represents a span of time, with its value stored as nanose= conds. +/// The value can represent any valid i64 value, including negative, zero,= and +/// positive numbers. +#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Debug)] +pub struct Delta { + nanos: i64, +} + +impl Delta { + /// A span of time equal to zero. + pub const ZERO: Self =3D Self { nanos: 0 }; + + /// Create a new [`Delta`] from a number of microseconds. + /// + /// The `micros` can range from -9_223_372_036_854_775 to 9_223_372_03= 6_854_775. + /// If `micros` is outside this range, `i64::MIN` is used for negative= values, + /// and `i64::MAX` is used for positive values due to saturation. + #[inline] + pub const fn from_micros(micros: i64) -> Self { + Self { + nanos: micros.saturating_mul(NSEC_PER_USEC), + } + } + + /// Create a new [`Delta`] from a number of milliseconds. + /// + /// The `millis` can range from -9_223_372_036_854 to 9_223_372_036_85= 4. + /// If `millis` is outside this range, `i64::MIN` is used for negative= values, + /// and `i64::MAX` is used for positive values due to saturation. + #[inline] + pub const fn from_millis(millis: i64) -> Self { + Self { + nanos: millis.saturating_mul(NSEC_PER_MSEC), + } + } + + /// Create a new [`Delta`] from a number of seconds. + /// + /// The `secs` can range from -9_223_372_036 to 9_223_372_036. + /// If `secs` is outside this range, `i64::MIN` is used for negative v= alues, + /// and `i64::MAX` is used for positive values due to saturation. + #[inline] + pub const fn from_secs(secs: i64) -> Self { + Self { + nanos: secs.saturating_mul(NSEC_PER_SEC), + } + } + + /// Return `true` if the [`Delta`] spans no time. + #[inline] + pub fn is_zero(self) -> bool { + self.as_nanos() =3D=3D 0 + } + + /// Return `true` if the [`Delta`] spans a negative amount of time. + #[inline] + pub fn is_negative(self) -> bool { + self.as_nanos() < 0 + } + + /// Return the number of nanoseconds in the [`Delta`]. + #[inline] + pub const fn as_nanos(self) -> i64 { + self.nanos + } + + /// Return the smallest number of microseconds greater than or equal + /// to the value in the [`Delta`]. + #[inline] + pub const fn as_micros_ceil(self) -> i64 { + self.as_nanos().saturating_add(NSEC_PER_USEC - 1) / NSEC_PER_USEC + } + + /// Return the number of milliseconds in the [`Delta`]. + #[inline] + pub const fn as_millis(self) -> i64 { + self.as_nanos() / NSEC_PER_MSEC + } +} --=20 2.43.0 From nobody Fri Dec 19 11:06:34 2025 Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.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 958FE20EB; Sun, 13 Apr 2025 10:44:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744541043; cv=none; b=MOXJ7KjLKzdtoQWmlNRR5MqXbiNKyJT+H8tb/8rFF0cYMOzBOs+jAjgDmLyWcUn+9PV3SQJvuVdsbsWyr41kPkfb25R2dcZ/d9wXLmz1GLaW2kJWKxpj33o2314El7tv+WDhLtv7y75Gp4HKmE4gP356iiBlvPFa3uMHXbYuGT8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744541043; c=relaxed/simple; bh=7bbHq9b3raLhQY2lmc4D0BL+dx4bchO30xoDbujLTaQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=d8LgqYNsDISOcSMj9abFc/h8pTZinuwJQCmwVjvpFBY+tZ9k9t2qe+hYv0THeaLBBbJCz73nCHNMCBV6CDwWsFJ2ZquEC9VzpJj8zuZNfMqZc6TqXvC4munY6QVZpx5eU23KOMn2CNp9eDqmmaoEHAUc3WLxJRUxKcWzoP5CPd0= 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=FK5v9il8; arc=none smtp.client-ip=209.85.214.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="FK5v9il8" Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-22435603572so31503915ad.1; Sun, 13 Apr 2025 03:44:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744541041; x=1745145841; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=VHTGzVejhCWva0144p2G2kUfqECExts/ex9G9JHIPyU=; b=FK5v9il8zhN0TX1IOWCA7EFMyEQ4LEP+ngXuIN07ah9wMNYaGncsMnp5Puu5VuFFbj qT5J2CfN56oUWUNVakC+n4qPJy/x82OKgAqniAf4YepSFgNg0ryIZjwCUTqchzsdURUy /IcwJimVvvva1KZLiMH3WtU3MmitA7prOcGhfJ8rODMhGGkQtuut5jSjseOBBHlaZuVl +HZMOyFLyRgrozdQ7zeYOFuxusgNgsRdNc5nvuHbbywwU6L41uGkro0bIPZVMSPRU1Zy KfEvAJ2jNJOKgGeZdmZcQVboNHN0vg0Ri8yKGqc7Ovi1YF8xyRAEZN6HuArX9k80rmo7 UdXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744541041; x=1745145841; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VHTGzVejhCWva0144p2G2kUfqECExts/ex9G9JHIPyU=; b=NyH5nfSb7RA8vwjcBw46iwB19mLbEKIe/NVICv2R1PEoeoGJQZl/V354vtYbqr5o1w tcXP+ihM6+UVFPLobw9z0Qgx0VxThnjni93ogDSPN+z+oImj9pNsYvDj7WNJUBIDjWfG +pO42+ntaAknIK10QQukM8twnbb90SerTbJF+ijkQ70wVielNPTy0pj58Yh7fJE+Pxwl xeo1/VCVwahgWa3zuyUJRGER7g4WxNY1mceL0e4JeQmCzttLUd+r9glisfv0RTqHxo6J ePfwWg0qfZrgsS+iTnuQLPL4G+U4+iosVdixi98zSQo4fvRJ1b/tkfZparom2q+28SWy cQnQ== X-Forwarded-Encrypted: i=1; AJvYcCUIaeOW22DsfkyRf1RDqtVCW4RgOsxOYkVW6sGGHt1dzS+nZrZ/Kxg5oAHD0B+3fJnRjcU6BbNxY9TUpHs=@vger.kernel.org, AJvYcCVA3+MfQdG/oZobD/Qi19lrCH6ILw2FXYsZWVjvyBzX9PRI6wBMdOv4Lse+dKskMnZRGOTV6Ryp@vger.kernel.org X-Gm-Message-State: AOJu0YxpPFTGbFNsUYyen3HEMCjnA2QrzJjjrkoh9DFyls40oYkqcaJU P+cUDcmFvz3FSAw+SChKpx/DGNBAS5wRbu9S1AltmMl0CTtBIgW/Yzd2XoPM X-Gm-Gg: ASbGncuThLnNi3ovDwLNvAoLXfp73nWTLReRsESV/0jnMyErGXUyvnhtmAFBUqrXiIq 9EHXMbDB040z+eHsOoukyGeli3uRgqyWLqnsT25tQobFJ7XhYKYMuYagh0L2eZjryIfSK6HLb24 MDZctj2zR/xHDuHRp9n/E51YU3wXLN+A8hGUvRfMvgte1vQsWWUPIXiX/U+Lu+uXGJYuiOQ8IFq nOPesaZ/EYL0clTR55iXEwWIeCXl0Qrn5lExS9h10VsQVAfKZj41CeqOB5xVpsEVeFWwYT22cqP m4kDAGYauGB49+7Qb0ypRtaiY7ShpNFIiXLIip1uUJcdToutINv75tuJqsL/B18hiRGsAYjfqU0 Bh5ZjLujkyyRDO0Rv82ge17VnRnij X-Google-Smtp-Source: AGHT+IHDXKIT3AHcch7lbyPUnsfZnPfynlAnueHGOCE6rtjvFJ+aBdHKcgJRLH528MdScmjC6QgQrA== X-Received: by 2002:a17:903:8ce:b0:226:5dbf:373f with SMTP id d9443c01a7336-22bea4954b0mr154157335ad.10.1744541040328; Sun, 13 Apr 2025 03:44:00 -0700 (PDT) Received: from mew.. (p4138183-ipxg22701hodogaya.kanagawa.ocn.ne.jp. [153.129.206.183]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22ac7b8af56sm80160885ad.66.2025.04.13.03.43.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Apr 2025 03:43:59 -0700 (PDT) From: FUJITA Tomonori To: rust-for-linux@vger.kernel.org Cc: Boqun Feng , Gary Guo , Fiona Behrens , Daniel Almeida , Andreas Hindborg , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, andrew@lunn.ch, hkallweit1@gmail.com, tmgross@umich.edu, ojeda@kernel.org, alex.gaynor@gmail.com, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, arnd@arndb.de, jstultz@google.com, sboyd@kernel.org, mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, vschneid@redhat.com, tgunders@redhat.com, david.laight.linux@gmail.com Subject: [PATCH v13 3/5] rust: time: Introduce Instant type Date: Sun, 13 Apr 2025 19:43:08 +0900 Message-ID: <20250413104310.162045-4-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250413104310.162045-1-fujita.tomonori@gmail.com> References: <20250413104310.162045-1-fujita.tomonori@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" Introduce a type representing a specific point in time. We could use the Ktime type but C's ktime_t is used for both timestamp and timedelta. To avoid confusion, introduce a new Instant type for timestamp. Rename Ktime to Instant and modify their methods for timestamp. Implement the subtraction operator for Instant: Delta =3D Instant A - Instant B Reviewed-by: Boqun Feng Reviewed-by: Gary Guo Reviewed-by: Fiona Behrens Tested-by: Daniel Almeida Reviewed-by: Andreas Hindborg Signed-off-by: FUJITA Tomonori --- rust/kernel/time.rs | 74 ++++++++++++++++------------- rust/kernel/time/hrtimer.rs | 14 +++--- rust/kernel/time/hrtimer/arc.rs | 4 +- rust/kernel/time/hrtimer/pin.rs | 4 +- rust/kernel/time/hrtimer/pin_mut.rs | 4 +- rust/kernel/time/hrtimer/tbox.rs | 4 +- 6 files changed, 55 insertions(+), 49 deletions(-) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index e00b9a853e6a..bc5082c01152 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -5,6 +5,22 @@ //! 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. //! +//! There are two types in this module: +//! +//! - The [`Instant`] type represents a specific point in time. +//! - The [`Delta`] type represents a span of time. +//! +//! Note that the C side uses `ktime_t` type to represent both. However, t= imestamp +//! and timedelta are different. To avoid confusion, we use two different = types. +//! +//! A [`Instant`] object can be created by calling the [`Instant::now()`] = function. +//! It represents a point in time at which the object was created. +//! By calling the [`Instant::elapsed()`] method, a [`Delta`] object repre= senting +//! the elapsed time can be created. The [`Delta`] object can also be crea= ted +//! by subtracting two [`Instant`] objects. +//! +//! A [`Delta`] type supports methods to retrieve the duration in various = units. +//! //! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h). //! C header: [`include/linux/ktime.h`](srctree/include/linux/ktime.h). =20 @@ -33,59 +49,49 @@ pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies { unsafe { bindings::__msecs_to_jiffies(msecs) } } =20 -/// A Rust wrapper around a `ktime_t`. +/// A specific point in time. +/// +/// # Invariants +/// +/// The `inner` value is in the range from 0 to `KTIME_MAX`. #[repr(transparent)] #[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord)] -pub struct Ktime { +pub struct Instant { inner: bindings::ktime_t, } =20 -impl Ktime { - /// Create a `Ktime` from a raw `ktime_t`. - #[inline] - pub fn from_raw(inner: bindings::ktime_t) -> Self { - Self { inner } - } - +impl Instant { /// 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 co= ntext. - Self::from_raw(unsafe { bindings::ktime_get() }) + pub fn now() -> Self { + // INVARIANT: The `ktime_get()` function returns a value in the ra= nge + // from 0 to `KTIME_MAX`. + Self { + // SAFETY: It is always safe to call `ktime_get()` outside of = NMI context. + inner: unsafe { bindings::ktime_get() }, + } } =20 - /// Divide the number of nanoseconds by a compile-time constant. + /// Return the amount of time elapsed since the [`Instant`]. #[inline] - fn divns_constant(self) -> i64 { - self.to_ns() / DIV + pub fn elapsed(&self) -> Delta { + Self::now() - *self } =20 - /// Returns the number of nanoseconds. #[inline] - pub fn to_ns(self) -> i64 { + pub(crate) fn as_nanos(self) -> i64 { self.inner } - - /// Returns the number of milliseconds. - #[inline] - pub fn to_ms(self) -> i64 { - self.divns_constant::() - } } =20 -/// 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 =3D Ktime; +impl core::ops::Sub for Instant { + type Output =3D Delta; =20 + // By the type invariant, it never overflows. #[inline] - fn sub(self, other: Ktime) -> Ktime { - Self { - inner: self.inner - other.inner, + fn sub(self, other: Instant) -> Delta { + Delta { + nanos: self.inner - other.inner, } } } diff --git a/rust/kernel/time/hrtimer.rs b/rust/kernel/time/hrtimer.rs index ce53f8579d18..27243eaaf8ed 100644 --- a/rust/kernel/time/hrtimer.rs +++ b/rust/kernel/time/hrtimer.rs @@ -68,7 +68,7 @@ //! `start` operation. =20 use super::ClockId; -use crate::{prelude::*, time::Ktime, types::Opaque}; +use crate::{prelude::*, time::Instant, types::Opaque}; use core::marker::PhantomData; use pin_init::PinInit; =20 @@ -189,7 +189,7 @@ pub trait HrTimerPointer: Sync + Sized { =20 /// Start the timer with expiry after `expires` time units. If the tim= er was /// already running, it is restarted with the new expiry time. - fn start(self, expires: Ktime) -> Self::TimerHandle; + fn start(self, expires: Instant) -> Self::TimerHandle; } =20 /// Unsafe version of [`HrTimerPointer`] for situations where leaking the @@ -220,7 +220,7 @@ pub unsafe trait UnsafeHrTimerPointer: Sync + Sized { /// /// Caller promises keep the timer structure alive until the timer is = dead. /// Caller can ensure this by not leaking the returned [`Self::TimerHa= ndle`]. - unsafe fn start(self, expires: Ktime) -> Self::TimerHandle; + unsafe fn start(self, expires: Instant) -> Self::TimerHandle; } =20 /// A trait for stack allocated timers. @@ -232,7 +232,7 @@ pub unsafe trait UnsafeHrTimerPointer: Sync + Sized { pub unsafe trait ScopedHrTimerPointer { /// Start the timer to run after `expires` time units and immediately /// after call `f`. When `f` returns, the timer is cancelled. - fn start_scoped(self, expires: Ktime, f: F) -> T + fn start_scoped(self, expires: Instant, f: F) -> T where F: FnOnce() -> T; } @@ -244,7 +244,7 @@ unsafe impl ScopedHrTimerPointer for T where T: UnsafeHrTimerPointer, { - fn start_scoped(self, expires: Ktime, f: F) -> U + fn start_scoped(self, expires: Instant, f: F) -> U where F: FnOnce() -> U, { @@ -366,12 +366,12 @@ unsafe fn c_timer_ptr(this: *const Self) -> *const bi= ndings::hrtimer { /// - `this` must point to a valid `Self`. /// - Caller must ensure that the pointee of `this` lives until the ti= mer /// fires or is canceled. - unsafe fn start(this: *const Self, expires: Ktime) { + unsafe fn start(this: *const Self, expires: Instant) { // SAFETY: By function safety requirement, `this` is a valid `Self= `. unsafe { bindings::hrtimer_start_range_ns( Self::c_timer_ptr(this).cast_mut(), - expires.to_ns(), + expires.as_nanos(), 0, (*Self::raw_get_timer(this)).mode.into_c(), ); diff --git a/rust/kernel/time/hrtimer/arc.rs b/rust/kernel/time/hrtimer/arc= .rs index 4a984d85b4a1..acc70a0ea1be 100644 --- a/rust/kernel/time/hrtimer/arc.rs +++ b/rust/kernel/time/hrtimer/arc.rs @@ -8,7 +8,7 @@ use super::RawHrTimerCallback; use crate::sync::Arc; use crate::sync::ArcBorrow; -use crate::time::Ktime; +use crate::time::Instant; =20 /// A handle for an `Arc>` returned by a call to /// [`HrTimerPointer::start`]. @@ -56,7 +56,7 @@ impl HrTimerPointer for Arc { type TimerHandle =3D ArcHrTimerHandle; =20 - fn start(self, expires: Ktime) -> ArcHrTimerHandle { + fn start(self, expires: Instant) -> ArcHrTimerHandle { // SAFETY: // - We keep `self` alive by wrapping it in a handle below. // - Since we generate the pointer passed to `start` from a valid diff --git a/rust/kernel/time/hrtimer/pin.rs b/rust/kernel/time/hrtimer/pin= .rs index f760db265c7b..dba22d11a95f 100644 --- a/rust/kernel/time/hrtimer/pin.rs +++ b/rust/kernel/time/hrtimer/pin.rs @@ -6,7 +6,7 @@ use super::HrTimerHandle; use super::RawHrTimerCallback; use super::UnsafeHrTimerPointer; -use crate::time::Ktime; +use crate::time::Instant; use core::pin::Pin; =20 /// A handle for a `Pin<&HasHrTimer>`. When the handle exists, the timer m= ight be @@ -56,7 +56,7 @@ unsafe impl<'a, T> UnsafeHrTimerPointer for Pin<&'a T> { type TimerHandle =3D PinHrTimerHandle<'a, T>; =20 - unsafe fn start(self, expires: Ktime) -> Self::TimerHandle { + unsafe fn start(self, expires: Instant) -> Self::TimerHandle { // Cast to pointer let self_ptr: *const T =3D self.get_ref(); =20 diff --git a/rust/kernel/time/hrtimer/pin_mut.rs b/rust/kernel/time/hrtimer= /pin_mut.rs index 90c0351d62e4..aeff8e102e1d 100644 --- a/rust/kernel/time/hrtimer/pin_mut.rs +++ b/rust/kernel/time/hrtimer/pin_mut.rs @@ -3,7 +3,7 @@ use super::{ HasHrTimer, HrTimer, HrTimerCallback, HrTimerHandle, RawHrTimerCallbac= k, UnsafeHrTimerPointer, }; -use crate::time::Ktime; +use crate::time::Instant; use core::{marker::PhantomData, pin::Pin, ptr::NonNull}; =20 /// A handle for a `Pin<&mut HasHrTimer>`. When the handle exists, the tim= er might @@ -54,7 +54,7 @@ unsafe impl<'a, T> UnsafeHrTimerPointer for Pin<&'a mut T> { type TimerHandle =3D PinMutHrTimerHandle<'a, T>; =20 - unsafe fn start(mut self, expires: Ktime) -> Self::TimerHandle { + unsafe fn start(mut self, expires: Instant) -> Self::TimerHandle { // SAFETY: // - We promise not to move out of `self`. We only pass `self` // back to the caller as a `Pin<&mut self>`. diff --git a/rust/kernel/time/hrtimer/tbox.rs b/rust/kernel/time/hrtimer/tb= ox.rs index 2071cae07234..3df4e359e9bb 100644 --- a/rust/kernel/time/hrtimer/tbox.rs +++ b/rust/kernel/time/hrtimer/tbox.rs @@ -7,7 +7,7 @@ use super::HrTimerPointer; use super::RawHrTimerCallback; use crate::prelude::*; -use crate::time::Ktime; +use crate::time::Instant; use core::ptr::NonNull; =20 /// A handle for a [`Box>`] returned by a call to @@ -66,7 +66,7 @@ impl HrTimerPointer for Pin> { type TimerHandle =3D BoxHrTimerHandle; =20 - fn start(self, expires: Ktime) -> Self::TimerHandle { + fn start(self, expires: Instant) -> Self::TimerHandle { // SAFETY: // - We will not move out of this box during timer callback (we p= ass an // immutable reference to the callback). --=20 2.43.0 From nobody Fri Dec 19 11:06:34 2025 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (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 5CAC11AAE2E; Sun, 13 Apr 2025 10:44:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744541050; cv=none; b=UIdFQJ+QS2DmrPkS8ScSA2k+4GTKAGtusT6w+grVZ+/bRrUC4ugYS3rZ8ky+eSBrYzWwkVbqLK6lIJ58BPFJcPp4pvLqkQHnEYYfMXlkYMILHfGnT4DKpd+05/0sRJaGKQ1vxYYRSnThXej12GPRmccmF1Y1BCwLkhvMpY18eoM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744541050; c=relaxed/simple; bh=FRv0zURbSztvldjjHd6IPO8aj6GITfpWGVJmSmvg/MY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=o0PmP8Tz/76IboQ+btCDz8ZVY07sCJ9sJHPFFqbtVx6UNnYbX7Pq7wq8pbYqj0QpSaBP/3jdYVIJiOpS9mo+7B2YRmSTddeD3f2P+L9sZWGj6cpB9vyMxM+Hn2JxS2olVThV1gJ0lPK9+PH8bk/hJFjb46NQFR9oKNC2gfKjRbQ= 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=RvGhtNRI; arc=none smtp.client-ip=209.85.214.177 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="RvGhtNRI" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-22403cbb47fso35312455ad.0; Sun, 13 Apr 2025 03:44:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744541047; x=1745145847; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=dXYh9opa2X5pw5Rnzm4RLKwzbCVPmlk5ZFIxldQ6nv8=; b=RvGhtNRIwjXPUXxMAnw5DHiEx5LlyNbbTU52baIfgMOGcnluMc2o2ygBMXcwB3LvWF 8B+srV5piOKLuEAoE7Aq3Akw0aSvRLxMY5yPT8fUeRb2SCeMxW1IkbqjZFkoEJo+rpZY HKupA12/34qjhv7SyMlm3Kb2sd84P/bo7VNNdE7t6GZxSXHQCjNxjp567PPKzMWQkAMV ELyJgvaWc6uYBPknc62yhU6j5ldNUPWm+DCUbh330IX+TjYnGV8e8VFHK3FK70P9mHap +f4qsNEYDMB/MC/k8YGBT6C+Ejnbs+XRFG5XFeIqBHMQIKyJBHM6OVvZPL5YtF+Cu+yD HNNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744541047; x=1745145847; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dXYh9opa2X5pw5Rnzm4RLKwzbCVPmlk5ZFIxldQ6nv8=; b=iDDxdf32zVcfIr129SoIKpmTbtvlVKTYt7Py+YgxYZhoaqMTMmICE9tsgDGb8+kDBv zbe2WfDZZbhjoKnsRe7sr/dmCSyTsXWk3Ep0Dg99J4NHAciZRxA8Txpj+D4qkRfbykEm sPIhRXTcDFw9l+w9xZWsTTBZytKdhR1+A7ENbbkLxw2z0GrTSojYr5+xW5QnmaJ3pqlX By5zypZan+YKG3MoROPeU89XzAKG56YlK1bQv6ZIRGtMzei2YUFSNZiD4UL+uB7oA91j gxDuElN4rpbDwP9N5icBwULeUXEVK8xvqBYBVHpKQmK0FqxzcipxK7Qm2oB2ATwoQVNi Eftw== X-Forwarded-Encrypted: i=1; AJvYcCUO5rdMHLzx2DtCJlpA2uWgT10FD6FcpHO18aH59nVVBOTcP7FiyIHctE2N7lLJwmyVBYepZjdV@vger.kernel.org, AJvYcCUXpvUbdgmHmbugXDZCwBAk2smDjPeYlmXQJT+m6hyo2QRCIzriqJHHvmbZMwt1Cz0e8fXNPdICZGXzUwM=@vger.kernel.org X-Gm-Message-State: AOJu0YwddJaiWTT6KMikRZAPWKHsX4ROu1ccX/20EGgolxYe7huJmA/3 TZt6xwQF/MYjiTdDNX93+vrzaVeleJq4SwiUPDDPOf2KMnc1+25a7F/NU4U+ X-Gm-Gg: ASbGnctK7htoNiIzSZ7M+YHaERql8eSh6xP3/TgaFedCrQaqfcnzlHeoL+Li4kkdPMM CRm0hUeYzjrjGXClDYAamGQoCt3X6q8kDH9qGm5ZkerA2+uGmj/AK+7v1MeHetyY65mwT+SvAui Xa3RpeqeZ6xaOq8IDHxCg/7iqFEkxJEW618wEfQkptCB1ImMTlTry45PtjMKxgsOJdugrADski5 3ITEokRA4PNYEqljbD8S1/rV6Z5rV7K+3xJu4xlqThhU2kGAbAl6zQeSqzFNbj2TM5fyX8L+LoR wNwJNHkZxfTwkscpkPLRg1r02Q9kRnhCxiRUqLjRWFRkEc8yt9cQBJ3DnAkL8JlO/z3jSwZorTF TU4XeRYZO0aFO7WWQdA== X-Google-Smtp-Source: AGHT+IFypWOBT4wfsDQKHBuJUDjAJz72aQLPZ2NaN1OHXhRN1dQG4TjZcQjsuIdLpwZtdcbDumqOpQ== X-Received: by 2002:a17:902:e5c8:b0:224:78e:4eb4 with SMTP id d9443c01a7336-22bea50017emr120282245ad.39.1744541046974; Sun, 13 Apr 2025 03:44:06 -0700 (PDT) Received: from mew.. (p4138183-ipxg22701hodogaya.kanagawa.ocn.ne.jp. [153.129.206.183]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22ac7b8af56sm80160885ad.66.2025.04.13.03.44.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Apr 2025 03:44:06 -0700 (PDT) From: FUJITA Tomonori To: rust-for-linux@vger.kernel.org Cc: Gary Guo , Alice Ryhl , Fiona Behrens , Daniel Almeida , Andreas Hindborg , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, andrew@lunn.ch, hkallweit1@gmail.com, tmgross@umich.edu, ojeda@kernel.org, alex.gaynor@gmail.com, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, arnd@arndb.de, jstultz@google.com, sboyd@kernel.org, mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, vschneid@redhat.com, tgunders@redhat.com, david.laight.linux@gmail.com Subject: [PATCH v13 4/5] rust: time: Add wrapper for fsleep() function Date: Sun, 13 Apr 2025 19:43:09 +0900 Message-ID: <20250413104310.162045-5-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250413104310.162045-1-fujita.tomonori@gmail.com> References: <20250413104310.162045-1-fujita.tomonori@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" Add a wrapper for fsleep(), flexible sleep functions in include/linux/delay.h which typically deals with hardware delays. The kernel supports several sleep functions to handle various lengths of delay. This adds fsleep(), automatically chooses the best sleep method based on a duration. sleep functions including fsleep() belongs to TIMERS, not TIMEKEEPING. They are maintained separately. rust/kernel/time.rs is an abstraction for TIMEKEEPING. To make Rust abstractions match the C side, add rust/kernel/time/delay.rs for this wrapper. fsleep() can only be used in a nonatomic context. This requirement is not checked by these abstractions, but it is intended that klint [1] or a similar tool will be used to check it in the future. Link: https://rust-for-linux.com/klint [1] Reviewed-by: Gary Guo Reviewed-by: Alice Ryhl Reviewed-by: Fiona Behrens Tested-by: Daniel Almeida Reviewed-by: Andreas Hindborg Signed-off-by: FUJITA Tomonori --- rust/helpers/helpers.c | 1 + rust/helpers/time.c | 8 +++++++ rust/kernel/time.rs | 1 + rust/kernel/time/delay.rs | 49 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 rust/helpers/time.c create mode 100644 rust/kernel/time/delay.rs diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c index e1c21eba9b15..48143cdd26b3 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -33,6 +33,7 @@ #include "spinlock.c" #include "sync.c" #include "task.c" +#include "time.c" #include "uaccess.c" #include "vmalloc.c" #include "wait.c" diff --git a/rust/helpers/time.c b/rust/helpers/time.c new file mode 100644 index 000000000000..7ae64ad8141d --- /dev/null +++ b/rust/helpers/time.c @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +void rust_helper_fsleep(unsigned long usecs) +{ + fsleep(usecs); +} diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index bc5082c01152..8d6aa88724ad 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -24,6 +24,7 @@ //! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h). //! C header: [`include/linux/ktime.h`](srctree/include/linux/ktime.h). =20 +pub mod delay; pub mod hrtimer; =20 /// The number of nanoseconds per microsecond. diff --git a/rust/kernel/time/delay.rs b/rust/kernel/time/delay.rs new file mode 100644 index 000000000000..02b8731433c7 --- /dev/null +++ b/rust/kernel/time/delay.rs @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Delay and sleep primitives. +//! +//! This module contains the kernel APIs related to delay and sleep that +//! have been ported or wrapped for usage by Rust code in the kernel. +//! +//! C header: [`include/linux/delay.h`](srctree/include/linux/delay.h). + +use super::Delta; +use crate::ffi::c_ulong; + +/// Sleeps for a given duration at least. +/// +/// Equivalent to the C side [`fsleep()`], flexible sleep function, +/// which automatically chooses the best sleep method based on a duration. +/// +/// `delta` must be within `[0, i32::MAX]` microseconds; +/// otherwise, it is erroneous behavior. That is, it is considered a bug +/// to call this function with an out-of-range value, in which case the fu= nction +/// will sleep for at least the maximum value in the range and may warn +/// in the future. +/// +/// The behavior above differs from the C side [`fsleep()`] for which out-= of-range +/// values mean "infinite timeout" instead. +/// +/// This function can only be used in a nonatomic context. +/// +/// [`fsleep`]: https://docs.kernel.org/timers/delay_sleep_functions.html#= c.fsleep +pub fn fsleep(delta: Delta) { + // The maximum value is set to `i32::MAX` microseconds to prevent inte= ger + // overflow inside fsleep, which could lead to unintentional infinite = sleep. + const MAX_DELTA: Delta =3D Delta::from_micros(i32::MAX as i64); + + let delta =3D if (Delta::ZERO..=3DMAX_DELTA).contains(&delta) { + delta + } else { + // TODO: Add WARN_ONCE() when it's supported. + MAX_DELTA + }; + + // SAFETY: It is always safe to call `fsleep()` with any duration. + unsafe { + // Convert the duration to microseconds and round up to preserve + // the guarantee; `fsleep()` sleeps for at least the provided dura= tion, + // but that it may sleep for longer under some circumstances. + bindings::fsleep(delta.as_micros_ceil() as c_ulong) + } +} --=20 2.43.0 From nobody Fri Dec 19 11:06:34 2025 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (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 C5A5E1AF0B5; Sun, 13 Apr 2025 10:44:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744541056; cv=none; b=qO1A9PV8GVM4+14TEE82oJ6DWnJsURisiomJELL9fgr2IQukGmU5A8Q3pPKfWNVd08FbY61gRKHAUbIL/fp5gakYuyLPUpLe/Kxs9FtRjjcpIn5W2kPdsWVSbOg+d+MzQ8r4vXL5053MTak3IVzKfO0qMOyUQLqcPjfeuKOfmHU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744541056; c=relaxed/simple; bh=JD2Vcha6ElX+5r3tPICprQGEcLCZ3uM4HJkKZyfdIl0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HCRB9VeJUzv37zsZbPwudEWOcWBR/1KRTf3Vf9AtfDd21fJbmX8VB05D/tbHV/e8cduSJQzxf5GRLCyFOOIEtdsiw1EKYM/m5ybjPX11vZqXhXtkcWgkp2KYkI9YzMcnFLUo5CPRW0JXyF8frozhCXh8e2w8LBrFWyskvyaQxi8= 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=OCgAbWfW; arc=none smtp.client-ip=209.85.214.175 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="OCgAbWfW" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-2264aefc45dso49455195ad.0; Sun, 13 Apr 2025 03:44:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744541054; x=1745145854; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ZdoJvmpW6qddfbJB5WSgVAlVfvRBkfyp+bE+C2KQ9c0=; b=OCgAbWfW2nFoyC6v2xKjEKNGbQmAhdEx58/EpS3BNWVu+UfPVeOICaOESAWoA/gmEi cuj2hFa1EM7P7lHjrJC/yOaAPPVyk1YUHD4eX19z7AXzvfm/MJtjhFfENkDSD0Fu85Dn iRIQQGZWW8osze0Hm0hfcds78YJ8oiK2U8qQV6wcc6tbw93CkEMjpmeL08ypv3heQKq4 PNGleKlQuUro6KCeZuUHrcy7wZwc+kkmULomOSbqebsKtRev2N3P68/xmZwIXdgXdnOY 5Ihal1oQTd1HGkmITBFzbqS4P8nTh146oyozIpx9sDQqdf3QvF+gdRKhFd+M/Llxj2gt vlMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744541054; x=1745145854; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZdoJvmpW6qddfbJB5WSgVAlVfvRBkfyp+bE+C2KQ9c0=; b=UvecQDdjclnAsvHNjj3tWHyKau9WqIArqZcGiwpcuTyr2L4asN1Mt1qRCA7uiiSwDj Drf6h0iNgVNFlFDjG21PEPGfJhXdCi9ctM/QChPzkuvHbrg8sjWcF85gggh7NXy5lfAN eh4hrM8duAbSl+smkspokVr+dIkb0J87aHxUJY+1K9A+IoqpWCEjmCFdlwmnh88wk1Q1 Vpp12rvOuXx6dpnmoa3FUaXdBtLWqv41YqNXUcmOrdxlxUK8vtCc2Jeep8LTXxIIFC7K 9fjxCtHv0of6fKeOGl+4Ba4C2Rq534hcT0O0lMmQBLTeuSiiQ0BA0+jO1VGXc21toZ0a 42KQ== X-Forwarded-Encrypted: i=1; AJvYcCVWwQkE5VULQbPTifN7rD1kylkTtisJ6IgYgiD7dW3QeGVUMkKTyDz3HO52yvMgO+FScHqApmaCYNuD7N4=@vger.kernel.org, AJvYcCVleSyJOBIBgWIXP8YnMdRadl8u+mrCGPuV6n3lZ2yztZf3+AYBKwvhsrxq8JCrf0RUEF7L6G8J@vger.kernel.org X-Gm-Message-State: AOJu0YyOYhyn2QPrpxCG1yKj6uwBMdnomtpiq6g1/zHvskPMOcv40cxm QhUSlgM3Mq3TUyGLr/ZZ0LrkPZ64xoMiP+s2V7QqJqGTlU09lM0Fk5Awuzf4 X-Gm-Gg: ASbGncuYf5Mv471rtOR5u3zxItnOg/8iULlt7ZSHgdxXs397ok0HfyarIV+HcieJoCi 1GEEpLR4HvLRub7HTaFqVOJ48EV2MfcdBdYAdXhrvCmEcObWvlmQuZWdQ7sM0iatJBMQRME0Rlz m2dA2EKPwEMjIY5o0wESKQaZHb52lvOac81u23SRFivaIfxmvPf89Zv+Fyf6Ub4Q/BKsNd1lhM0 Fndb6T1wYFMnSsXN8bnG7absx5vsEx+c4AUJ2RWulmtWIOm0/ywH5WUSuRQXL8KpTyzwNFn+ipd NztkzdGGfzccE4riuE3vs2q8PbHJgrtjifYfLaFTYAYK+qcpN/HcJLIhcO9artZqtTMV3rEOtPd c68QyzVe50XEcaTwRlTLeROr3Eerb X-Google-Smtp-Source: AGHT+IFA/SwdxFVR9lqyRwioQ6mLAepuc6c36TjHQ1kU80v2qwDra/68I4JucotYa72SVT3yX3pXvA== X-Received: by 2002:a17:903:41c3:b0:223:5a6e:b16 with SMTP id d9443c01a7336-22bea49542dmr131704475ad.5.1744541053570; Sun, 13 Apr 2025 03:44:13 -0700 (PDT) Received: from mew.. (p4138183-ipxg22701hodogaya.kanagawa.ocn.ne.jp. [153.129.206.183]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22ac7b8af56sm80160885ad.66.2025.04.13.03.44.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 13 Apr 2025 03:44:13 -0700 (PDT) From: FUJITA Tomonori To: rust-for-linux@vger.kernel.org Cc: Boqun Feng , Andreas Hindborg , linux-kernel@vger.kernel.org, netdev@vger.kernel.org, andrew@lunn.ch, hkallweit1@gmail.com, tmgross@umich.edu, ojeda@kernel.org, alex.gaynor@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, arnd@arndb.de, jstultz@google.com, sboyd@kernel.org, mingo@redhat.com, peterz@infradead.org, juri.lelli@redhat.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, mgorman@suse.de, vschneid@redhat.com, tgunders@redhat.com, me@kloenk.dev, david.laight.linux@gmail.com Subject: [PATCH v13 5/5] MAINTAINERS: rust: Add a new section for all of the time stuff Date: Sun, 13 Apr 2025 19:43:10 +0900 Message-ID: <20250413104310.162045-6-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250413104310.162045-1-fujita.tomonori@gmail.com> References: <20250413104310.162045-1-fujita.tomonori@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" Add a new section for all of the time stuff to MAINTAINERS file, with the existing hrtimer entry fold. Reviewed-by: Boqun Feng Reviewed-by: Andreas Hindborg Signed-off-by: FUJITA Tomonori --- MAINTAINERS | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/MAINTAINERS b/MAINTAINERS index 96b827049501..104cec84146f 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -10583,20 +10583,23 @@ F: kernel/time/timer_list.c F: kernel/time/timer_migration.* F: tools/testing/selftests/timers/ =20 -HIGH-RESOLUTION TIMERS [RUST] +DELAY, SLEEP, TIMEKEEPING, TIMERS [RUST] M: Andreas Hindborg R: Boqun Feng +R: FUJITA Tomonori R: Frederic Weisbecker R: Lyude Paul R: Thomas Gleixner R: Anna-Maria Behnsen +R: John Stultz +R: Stephen Boyd L: rust-for-linux@vger.kernel.org S: Supported W: https://rust-for-linux.com B: https://github.com/Rust-for-Linux/linux/issues -T: git https://github.com/Rust-for-Linux/linux.git hrtimer-next -F: rust/kernel/time/hrtimer.rs -F: rust/kernel/time/hrtimer/ +T: git https://github.com/Rust-for-Linux/linux.git rust-timekeeping-next +F: rust/kernel/time.rs +F: rust/kernel/time/ =20 HIGH-SPEED SCC DRIVER FOR AX.25 L: linux-hams@vger.kernel.org --=20 2.43.0