From patchwork Sun Apr 6 01:34:41 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 14039275 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 2A5FD6ADD; Sun, 6 Apr 2025 01:35:23 +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=1743903328; cv=none; b=Hh3qGZyP3pMQvaudE+RYv+ZXn9sV6I8FNZgn70FvB/R8MUAoqNvYFl1spvDiczM+ywfgXB11FVv1xYixPyaSXgCfjYnqRJCvkZNt5IOBSN+/jVYEKl1lnLwhvRu79rfLmE6nwLb1pj3nP0jP4EHKADx8qfBSQKM4XCPkPPDOdaA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743903328; c=relaxed/simple; bh=kd3vW/wW1gabeQG7Fy0PVk4IKsLzJY/aNs7T6WdurXM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Gl6xyjgh5MseFmLxM9E+dq/OBXEMjvr7mtSAR+DmORRv36H0xedTYjWjxDQl8aayNsBL45ZyXovK+4Lq+KIfoxc+2lzI7/C6STzmjf+TuwR+sCyzRh2o/T5N+aWxdDrNCJ1wfPkfD9/8ktYAGJk9zoD/ahC0RHciBS79Dg23Trk= 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=Q1iRBB7t; 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="Q1iRBB7t" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-22438c356c8so31542325ad.1; Sat, 05 Apr 2025 18:35:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1743903323; x=1744508123; 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=Q1iRBB7tOkeSDHqL6rUgY+4EfHAeQlu2X/ABvhI1lvs9XL9C8A5Y6aNSmkB7xrGx5b n+4XZCbiDXZj4jiSZ75ZJ6e+SazXB6sKneAkdWVxaY/j3fYctc7OSl/geahamfLMIqcK ES/i76Po+FWU5XyQ+B6lec0NyQRcAjTxv/tnKIqyKe3tAZE+JliHHpQBqrrrxKS2SoqV 91UzuAyaq8/fIiP5s5bBFDvxuhcaw/YABiNK5IpdUM39DTDU6/dMlu+W0jBOC7yLWXJq EJ/LX6vqEEsQjUCAFsYrJY6INjlCok2EdFaZUkjkhxFyNqqIKyHFQCRaHgrv35T3AtQb EWeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743903323; x=1744508123; 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=rFh+teBjlWSMwansO7jJ+niwuKuFVKGGuoGVsJEmOyV4ctV7i+2FquUZ18aQGwAgRn UYBFxb4EPs6WS0sEbZdbppthV5ggGpY2BL0j5cmIzMhdrYQKjWXLQZqDOdZSEKrUd0/+ zA12A4wQbKYPyJXn0QGc1oUxkCf7kL7Y+3H5xbWPl2cCZaP6ZlOGSzwZvw71chYOmeYP Cu81zthK8/lR/loJ5S2fvkRnl+60t8vfj7LPWlVZhKVAVlV6whB9qIvktB86VLvFl8NQ D+4DrlMV+Gve0EebOuXaOcHH2x8yX2rvVSIy/F8VjtPOtgsmgPoKmWX9AuJNVAUNpbns fksg== X-Forwarded-Encrypted: i=1; AJvYcCVrNBtL1fzXPVTkrYcf5AwCpMyVeZoO/nwc+fhxN0DYYUxQ9T51Kc0VO6k8vIwzpESQ3/JH3uQSx9gVGiI=@vger.kernel.org, AJvYcCVzTgtlc6Um9ybobvwiA+pW+9Vl8f1ybZ/KkxU3UkNr7W5LaT+39ioXuD6WzqGi6ylptYm6qQlt@vger.kernel.org X-Gm-Message-State: AOJu0Yy7+NbbaoAIa6Lg8X0SLLoT6c/N1M55ZeO1zSQ3qlQqkmNv20pH t9wtVZfGziAIdbLhu9RJ0la28sCFeGMsYmH+uat17F6TI61Q4zdqw9jRbfEg X-Gm-Gg: ASbGnctOCtBMy9rbWvzlqoGbXseWK/p/Mk+ElJ5hcKXEeLYGakBjpsccBS0XpPR2xhZ i+4sW87e9qpmRy1MbTvO7yeE1qPEm3YvM+y4+R7yLRFNGDt6KHtykeXZePXpFhmsJadp/+QV5GU oVTWlLWKfN6dgSOihbZlBL7ohNW1TZEfnzWTeqOr3HulzkxrLbXjYJfFd5CzDp3X2SBFFbq42B8 dSkcBS/42CNY23r7COY3GcrFgjCOwo2O5Va0du8yPYFtjZUJDaCEcREhdB0vnjaK9d/Ty5s2zOI kUwNBVU0AvGk2RBAQAGuog38o+Ht155v5wBruzWRhXd3DPN+a2WEa/rTuMPzj3VuFDc5ZUBOj7u HFNwQ9mVJ7CkdhbwlzbXc0w== X-Google-Smtp-Source: AGHT+IE57Dl+F0N1TQEaOsi/sbH5n6hJ/G3XQQLk90KL0m8ajkTtv+vVbK/m8UqmqYQVoEPK4WSnsw== X-Received: by 2002:a17:902:ef11:b0:21f:507b:9ad7 with SMTP id d9443c01a7336-22a8a8794a7mr123090205ad.25.1743903322983; Sat, 05 Apr 2025 18:35:22 -0700 (PDT) Received: from mew.. (p4204131-ipxg22701hodogaya.kanagawa.ocn.ne.jp. [153.160.176.131]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-739da0bc052sm5846849b3a.156.2025.04.05.18.35.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Apr 2025 18:35:22 -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 v12 1/5] rust: time: Add PartialEq/Eq/PartialOrd/Ord trait to Ktime Date: Sun, 6 Apr 2025 10:34:41 +0900 Message-ID: <20250406013445.124688-2-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250406013445.124688-1-fujita.tomonori@gmail.com> References: <20250406013445.124688-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 { /// 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, } From patchwork Sun Apr 6 01:34:42 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 14039276 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.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 F29E5145B27; Sun, 6 Apr 2025 01:35:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743903333; cv=none; b=SUTGVuX6OYp61kS2QYHHLb7lfFFCV46FN/4r/O+SHwk81uGNKqJNUGffFzYkog5ALF2r31ZE0Zm3LxaFzQHUlURxXaDa/NffZu8FthS6QjFiS6g5FTgtNesQzYEGhGSeP0sXjthPnaM42d5MRsv5utWjBRU+LRrtD2m85SaI1ds= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743903333; c=relaxed/simple; bh=12o8EpFplw5vxKVaPZGoT3TplIy82zwNzBv2WcTOE8Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NYcqmBCDxXLi4VWID7diBfoPMI6crGlxs1Oeo7Lfie9/ys3YfWDxE/GY9QDynUahVm9Jzi+nzwhSTXyBTHtik1ptksoOqJaPgvl/DBb1SBXEbUS0KpBPGCBk7DDjZXNWa143lPLvAhb3rH1mNnkM4yg3dqL1S1B7QjpMW3FEv9E= 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=btwZxAxO; arc=none smtp.client-ip=209.85.210.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="btwZxAxO" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-7398d65476eso2435515b3a.1; Sat, 05 Apr 2025 18:35:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1743903330; x=1744508130; 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=btwZxAxOt1+n8xv3VDlLyyYpAArQlAaN4yyRwN+/PLYQwCc845ZTypkQN0TajPd00o /ESashceJQ/ZpG0ZNr7SUbEV9buWVYp2QxvtrHAaWQbcFdOOr402uOCI9dXmfyU8FROh vltMULZLN6cV39Gdm9w3apNTXFGc8gY6aE0/u0jJpcSngAk6WjjV/yHAttSyi8uX55tG IRvf0/s/LY5UGZYk5hjRAOEJjGP2s6I90Er1EFaYJTO7JDMa7lvf+wG4ItGNdmMt2kEI oC92ljbecIG/fGClU/kqQBVpI5ubyPJiyKwjJec1QqsTdB/jsbfZml84hppkCaYwBkgD ZSQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743903330; x=1744508130; 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=ckXNRFxtofSkMLwaGbugGVxwMWmNS7kRNkewqouGN/Z/p58tSNZQJK68fXi0B5RTjH pprT/GFd2vMuwtJlfOEw0yGfm8gDAxA2W+HAYOaRRFTgzr4gRIlJHa5ajBKcGDxNWjzk waVexHokDY1igxXJjmMzutlUfCyJOhTVHRQFsvGsWc/ynQrIPxPijeMA2R5QpCCRnbZT m613Q6/Ft+zLbjzq8bOKW+OVDB0iL3ly9H6sYKWCz7c+3b9k9G0YtVh+Oy8vvQ2EkvTH 1RjhlAVyMfkeAwEbm3IZEMYyu4V1uk8wv7LniSNYmJERVEJp67Eyds3YswbGuzhb3sJ7 841A== X-Forwarded-Encrypted: i=1; AJvYcCUPP9leJSEybTk5mmkUTnuzBflR/YsNmC1Jmz0QvbQ2aeJicLzhWYCY0xQfhVSQfkdTX9wFmNTxDG9CWXw=@vger.kernel.org, AJvYcCWJpAH5KbGqo9UmBAXGa8iRn65t/6nK4B720AKB6r7dYRxfLD5mnjGKq3+gtVELD0smu0TKtKMd@vger.kernel.org X-Gm-Message-State: AOJu0YweQVSZeb2Wi+ApbcC1C1bfiF7OAS55ti/szDK9ia2xFxgbowy4 W6MRnTTZ2pcmSZfJIEAZIkGDbhNxUTrIR4DVKEYFTnJcMyfZrSXVC3bwIkj3 X-Gm-Gg: ASbGnctR8rjZAKUHuUuPH8uUSL4NbV28Bz6pDtXIETa7ejNkbYy6aCEPKbfUIwGBUJq e/CuD96Wk7h2/86s8O79pH6LrRhUYOOh7LE7axw3tORo3/Ur9esnkzatJELUQSLEbRBjXqQfVFi msQwpWDDs2N9IEnnjtZN+EJ6xzGlSti9aExxaLEw9FXdU/fzRFoPLcrO0TfWKz8XIRH4OecxzZ0 FddjFMBfC8zYVyOqC4uGTaYM+0TAX0Uuzc169Px/6mpZbg1AA1acHzD7aUzi4qHZHZJsmO4OpC6 Cn7HVTfdYNDnF27+oLqs5iAzs+Qw+Teh+90CxPKCWE3TyK6Ke6gUYnD7qL5qdf7DsDQNkj3Us28 QyTCVcyUsb/0rbzvnvLFRJw== X-Google-Smtp-Source: AGHT+IEbwgDdA/kX+ABcQShgFyEHV5XHvuy/sTlvOc5sIg+Mq92JK95U6Bk7PTZeuiIQ7Iv6uPQtnw== X-Received: by 2002:a05:6a00:8617:b0:727:39a4:30cc with SMTP id d2e1a72fcca58-739d6351a52mr13604341b3a.1.1743903329587; Sat, 05 Apr 2025 18:35:29 -0700 (PDT) Received: from mew.. (p4204131-ipxg22701hodogaya.kanagawa.ocn.ne.jp. [153.160.176.131]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-739da0bc052sm5846849b3a.156.2025.04.05.18.35.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Apr 2025 18:35:29 -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 v12 2/5] rust: time: Introduce Delta type Date: Sun, 6 Apr 2025 10:34:42 +0900 Message-ID: <20250406013445.124688-3-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250406013445.124688-1-fujita.tomonori@gmail.com> References: <20250406013445.124688-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 @@ pub mod hrtimer; +/// The number of nanoseconds per microsecond. +pub const NSEC_PER_USEC: i64 = bindings::NSEC_PER_USEC as i64; + /// The number of nanoseconds per millisecond. pub const NSEC_PER_MSEC: i64 = bindings::NSEC_PER_MSEC as i64; +/// The number of nanoseconds per second. +pub const NSEC_PER_SEC: i64 = bindings::NSEC_PER_SEC as i64; + /// The time unit of Linux kernel. One jiffy equals (1/HZ) second. pub type Jiffies = crate::ffi::c_ulong; @@ -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 nanoseconds. +/// 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 = 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_036_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_854. + /// 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 values, + /// 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() == 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 + } +} From patchwork Sun Apr 6 01:34:43 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 14039277 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 DD85F70814; Sun, 6 Apr 2025 01:35:37 +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=1743903341; cv=none; b=ZB/vbCbqT00VgNF9iRW5l9hMWUlDuJ13a4n03GQWdHozW7xMlrT36iwPGsprwZCNz384L4rmsUYL/0kOmRhoZFeqKSGJF2va9Px31YgbVgbKxR7+jAHzJ9Ac+ETfDANgzpU34+kYxJb2VyM0lm4yF/+rLMhwqM2REHI75Q+WJ9s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743903341; c=relaxed/simple; bh=7bbHq9b3raLhQY2lmc4D0BL+dx4bchO30xoDbujLTaQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KqzU7SsXCfq2KX/nBEOuP/NUDfdo541tGsHErgsXfTqkpa9y5IckjO65u/G7XlMsXz3dedWydZ2kKj7AxnBMr/jG9xffaJ9/BGvKgXeLX5HeVnwAMrCWvcjZJcNcjMmq+Hu6MAxEKu35lUXzDnCznhHwK9AKDX/6XjI4xL54TiQ= 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=MeUAyEHm; 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="MeUAyEHm" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-2260c91576aso26098425ad.3; Sat, 05 Apr 2025 18:35:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1743903337; x=1744508137; 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=MeUAyEHm1mXi5O42d2HapphrQeuaJfndBAnY+hxS7waeigYjwbfULfAC/f/oNlWltH BPqeIKJ0SHl/9A4qA1wx68DPOlmwlfVk6DkaRYvKNGJ8Yejkt7KbiYCvOfT6lIbAeRSW EjNmoXFL397Jb5UYL58Z/EqgirbJmR4tdMsicmwzU7SJ0SmSPUnXxMj9F1TBFHUuaCPd aUe0b/gLMKw6h+UT8oXf6S8de5U2P1xHIq8RtwkVC/LneEmdT8jRF08dTdxKNvRrI6S5 fUJGQeEKYmwDxEuC8oJg6CI7mIqDx8eNl2LS2rD1AR1/Kru7z/DL8HeQGQxuPsU+seGD N7+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743903337; x=1744508137; 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=isvQCwkoWU4AA0tMm2+TlStzIcseMzyBHttpyPBJFaKmEBqhZlAjaYn+/2CUqv7I6g 5TEahMFSeglDnRLouJGTUHd0wsdZVqG3zN3UCfK2xCodvuiyXqV77ZPOrFIvRYDeDbr+ uLfkNs49nc1EcvoaIYCAr8KgvbZbRQypf0+VcSegQ7x/+hFIQkBMtWDuh393kX/Fxi3U XkA3+A4CS5naqNhNFFDvWy55x5H6AGV5oDiZvuYr7vHIkGWAlzvJJUn/VfGSMkCDXxq0 kih6cpgPlaakIKExF8FO9hGNKKoTktidKD4ZHziUvUQ6w3i429RZSIwOvesJmmV9t6Qz tiqg== X-Forwarded-Encrypted: i=1; AJvYcCXgG4jEf04iQn2/EbQ7eUfMb8+AvHqdo2Sk3tF11IJmS5/1OLxD7PWTRiFdzIS9szySBr8tjUf545u4xzQ=@vger.kernel.org, AJvYcCXgRvm3/pZ0iih826nTQ73iiq90QyjBGur4uP8C4Pre08f930IqO1cKat0LJL8OxJ/g7io/uDn/@vger.kernel.org X-Gm-Message-State: AOJu0Yy6OuT5cctfwH+qZvfMoMfhkWeD7nP0NML5n4bUVmzGMUjNoa+2 a6nk7ibG2vjNNTk2+o1VKgbYZF6Ii3wf5RCGuCBI5gvXnNz/N6Zw6rtuMPNl X-Gm-Gg: ASbGnct5tdO0Et9eghGyXcXuQp8hQ/uh+3xSz083Kmo9lBflHeA4+j3Hdraitiuf7j3 pIUgywf7o7lVeM1rRQIlYVOUM884kQw7XXWrxaS/gISERjQn49Kiw144YmKBA0LLxHkQjPBNmRq Cl68cbR+G/s8QfNneewpKPr3Li24IIllxfbUpTTZCIEpPD8WdqKcf/4XZ73FyPyxIU53+qTcXQs xRJB97WDJcaMxD3j55ozegdLxwb++dOGnTOUgldt0WlLK+r/rlST4tlBUMspzkbV7UJSs4FrHaE gAR6XDaxInnbSraJgv9jN3TY3McrDlaLY9DT9E11PkxyYfs0Syt3bY4ru18o2qBMvk9tNLXy1mo ioKcAtuFtrf/DKcXP3gW/6Q== X-Google-Smtp-Source: AGHT+IGUd7xiJH6zoiTKkAidl0ZaDrSGipBvNZnAw55xHaEbYZq5J6Sco0D+dmO0D21XIhKATyTNGA== X-Received: by 2002:a17:902:da8f:b0:21f:85d0:828 with SMTP id d9443c01a7336-22a8a1bb664mr117156115ad.41.1743903336503; Sat, 05 Apr 2025 18:35:36 -0700 (PDT) Received: from mew.. (p4204131-ipxg22701hodogaya.kanagawa.ocn.ne.jp. [153.160.176.131]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-739da0bc052sm5846849b3a.156.2025.04.05.18.35.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Apr 2025 18:35:36 -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 v12 3/5] rust: time: Introduce Instant type Date: Sun, 6 Apr 2025 10:34:43 +0900 Message-ID: <20250406013445.124688-4-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250406013445.124688-1-fujita.tomonori@gmail.com> References: <20250406013445.124688-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 = 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, timestamp +//! 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 representing +//! the elapsed time can be created. The [`Delta`] object can also be created +//! 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). @@ -33,59 +49,49 @@ pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies { unsafe { bindings::__msecs_to_jiffies(msecs) } } -/// 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, } -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 context. - Self::from_raw(unsafe { bindings::ktime_get() }) + pub fn now() -> Self { + // INVARIANT: The `ktime_get()` function returns a value in the range + // from 0 to `KTIME_MAX`. + Self { + // SAFETY: It is always safe to call `ktime_get()` outside of NMI context. + inner: unsafe { bindings::ktime_get() }, + } } - /// 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 } - /// 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::() - } } -/// 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; +impl core::ops::Sub for Instant { + type Output = Delta; + // 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. 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; @@ -189,7 +189,7 @@ pub trait HrTimerPointer: Sync + Sized { /// Start the timer with expiry after `expires` time units. If the timer 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; } /// 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::TimerHandle`]. - unsafe fn start(self, expires: Ktime) -> Self::TimerHandle; + unsafe fn start(self, expires: Instant) -> Self::TimerHandle; } /// 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 bindings::hrtimer { /// - `this` must point to a valid `Self`. /// - Caller must ensure that the pointee of `this` lives until the timer /// 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; /// A handle for an `Arc>` returned by a call to /// [`HrTimerPointer::start`]. @@ -56,7 +56,7 @@ impl HrTimerPointer for Arc { type TimerHandle = ArcHrTimerHandle; - 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; /// A handle for a `Pin<&HasHrTimer>`. When the handle exists, the timer might be @@ -56,7 +56,7 @@ unsafe impl<'a, T> UnsafeHrTimerPointer for Pin<&'a T> { type TimerHandle = PinHrTimerHandle<'a, T>; - unsafe fn start(self, expires: Ktime) -> Self::TimerHandle { + unsafe fn start(self, expires: Instant) -> Self::TimerHandle { // Cast to pointer let self_ptr: *const T = self.get_ref(); 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, RawHrTimerCallback, UnsafeHrTimerPointer, }; -use crate::time::Ktime; +use crate::time::Instant; use core::{marker::PhantomData, pin::Pin, ptr::NonNull}; /// A handle for a `Pin<&mut HasHrTimer>`. When the handle exists, the timer might @@ -54,7 +54,7 @@ unsafe impl<'a, T> UnsafeHrTimerPointer for Pin<&'a mut T> { type TimerHandle = PinMutHrTimerHandle<'a, T>; - 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/tbox.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; /// A handle for a [`Box>`] returned by a call to @@ -66,7 +66,7 @@ impl HrTimerPointer for Pin> { type TimerHandle = BoxHrTimerHandle; - 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 pass an // immutable reference to the callback). From patchwork Sun Apr 6 01:34:44 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 14039278 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (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 2F1821581E1; Sun, 6 Apr 2025 01:35:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743903345; cv=none; b=nQwrvBdrXZlSioXxXmnbc03DeQij4Npco8llxBPzcgclPIOOnvpk/c1BRN/sVtceR3F8iryu6aLDdvSc9yqlkNyhF3mBDM3348qmFsQADztMeulKl714nSxjvIXDh9st1hPKL654rKmN6YVZbkbU0Hb5fWqKufp1mKms1Y2xVDw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743903345; c=relaxed/simple; bh=FRv0zURbSztvldjjHd6IPO8aj6GITfpWGVJmSmvg/MY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cTQLMlPEYuNDbroL5PydMYtzrRQOFpxc5eXdXeb3XliXclwBani49fmGW4gTKLOQZBxjtXh/emPy0n27MyN6algHTu40PadR9ZRmEpj/czURqDAd0ZXbNdJTQrsuiG8/IOfcM9Rb0wO8j6sNUtAfB4O1I4P6cxoV8118RKRt8s0= 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=HKoeXlP8; arc=none smtp.client-ip=209.85.210.182 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="HKoeXlP8" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-736b350a22cso2704340b3a.1; Sat, 05 Apr 2025 18:35:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1743903343; x=1744508143; 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=HKoeXlP8SLDchTCERi8lpGK3R/P1wkGHaKwhlOh6J47VvfYk+Kr2lHWFE+rQX3zMjd Xn2erNnn9XOqL4A/6x12CAKSvE24fAFDOc+jy3KaI+7o8xfvR53ljj8vXYmsYIZIFy60 Qsl11mH4+6GKTEOFOjYmTbCnRTirDEC3bTzRY1yu66nux8R20tgGay/Rrgxp86FZoYne FMU/eFBJ/atxdn1SA5VEQuQ52V5ehJ5gGF/NQ+fUyoymm+MChT+hEvOnL+ngQNLn6sjX yFgv/utX6rI7DeUNyACKUQx2LOKa5HhPHdLDOLiSUK3RfHkJp6EcbkRs0w1QhzHLcLBQ Mqeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743903343; x=1744508143; 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=iOOvnGpiztkJa69loJXWzrTVg6VX6k3D+MnGff7H/rsRbsM25pvCkHXGQ9BxNL7Gwz 1sfAemDR4VZGxIDCQXCc6aNqwnFA5IdK8Be8xhdbul0uejTFXtpqoO09EO0KwNwR4fuM 0q07NBp8Ty+g7sZYXvDmdi4hgiA3Hw5frrUFrKUtjkdOe2QUe9C92n2L1ShyalJ0L72Z 9tOb+g1+5iSMaMV3VXpfdPBq/DgCLKXg13dzCEU4slfnuFcNZtvhIs5ibShGPrdN2z5X zqHE5U+KInHVal1bMPnvRLKmC94HkdPadUIA8RGVcOmfq07NuiPWuxP/0MC0dNZRRtFt vqYA== X-Forwarded-Encrypted: i=1; AJvYcCX69TLEWg2KA/TZAKwCIc3S3ijmYWDh3/hN8FLseKA4wXBkVHiJTTVi/CukCdDdUvgdzS08tsIG@vger.kernel.org, AJvYcCX9Hq/qfdsumwnvekQrD+xApj5XjHXEVvb1L48akBEBIcQWRCYjWyVLly2hBZhNhQBnZo/LbGsgMo7RQYg=@vger.kernel.org X-Gm-Message-State: AOJu0Yx8wQ6P99phcaZ7gixHrSFGUxB3rfNn00Nl+x3un074YV+QDHJZ rJC+Vp+VbMMN2WPVlLbqNqmWjpgLOcRGc7xX68jxbJrSk3Ii9wqYWHbR7lcX X-Gm-Gg: ASbGncs2wac+4YLwVMQKzBcR+WnPe4gpVjDWo8zif7dCfhZhjM+pvemt65km7OczXy8 8JcG6GKsJiDjp0UIPTN6bNe4wCp9YnFXFEiJbiFypFWIZxsf2jYBq6m3iSkEeEV1LWlu6AuMkyL 46Itjcq3Pjnxml70Mpyi4CHQV+nMOYp9vHA1TT7fkxzSbXhlBcP9DoDk7LJaoD8l6Hdl6fRpwJi c0uliJNZ6sDbghq0nC4PwnHaIGiPzyKojmOp65ZUN2jYoa48dy0VonaCxYPC5K9O7jR1EX4xLXX LYqyqf1EsLWR9Rp6KD6q138a1vN/feKuMVgQipJ70umZAoMbWP/HSF9foK+76J/mJ68hi0CT4SN 1P16lnfS21hsSZSuMoqPwsQ== X-Google-Smtp-Source: AGHT+IG6hE0mQ449T9GZriMtq73qyEdn8TF9CAcRp5qLKDDBJzPLbaW8MFSTKxQTDCfVOH/tGo1Ttg== X-Received: by 2002:a05:6a21:2d06:b0:1f5:8a1d:38fd with SMTP id adf61e73a8af0-2010447b3f7mr14210091637.2.1743903343181; Sat, 05 Apr 2025 18:35:43 -0700 (PDT) Received: from mew.. (p4204131-ipxg22701hodogaya.kanagawa.ocn.ne.jp. [153.160.176.131]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-739da0bc052sm5846849b3a.156.2025.04.05.18.35.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Apr 2025 18:35:42 -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 v12 4/5] rust: time: Add wrapper for fsleep() function Date: Sun, 6 Apr 2025 10:34:44 +0900 Message-ID: <20250406013445.124688-5-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250406013445.124688-1-fujita.tomonori@gmail.com> References: <20250406013445.124688-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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). +pub mod delay; pub mod hrtimer; /// 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 function +/// 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 integer + // overflow inside fsleep, which could lead to unintentional infinite sleep. + const MAX_DELTA: Delta = Delta::from_micros(i32::MAX as i64); + + let delta = if (Delta::ZERO..=MAX_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 duration, + // but that it may sleep for longer under some circumstances. + bindings::fsleep(delta.as_micros_ceil() as c_ulong) + } +} From patchwork Sun Apr 6 01:34:45 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 14039279 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.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 67848136351; Sun, 6 Apr 2025 01:35:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743903351; cv=none; b=Rk7NNoH4cqh1Ig42drrbNP5RvPbumG4Uo3CP7sdCcguFkVzGPbrXKe2PWklLuukY4y3J0ojcQP5J1TLbATQqT+DhF/YKnrm7W5ZiF22bO+9JVSHp2QFFq1ZJyzXcoxqA/YddQwGAtdXg0SXKrXDK6Nmf/bvW0R8mHRN2UUCyoQI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743903351; c=relaxed/simple; bh=SRzA+4cwuLWbi3l+rrGRpJ0i0ekQpxdDzlpPMeSGvI8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=X/WlOtoT8GJDNSZrkc0GZ3HZXkrgOmKkL+3KSC2nqnE0FV5eAuD2KC79xZ5ziHiOhTszkyv2ZlSnqQcKgN/ijGODTPdYJ2DGdmNMQjxqDpKxbGO2ezfIINU56x7DNGPg/7aKFM8e897Hs2a/puQFUE5LHhPSq9Rt9HBQ75pnNww= 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=YdrAySVH; arc=none smtp.client-ip=209.85.210.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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="YdrAySVH" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-736b98acaadso3082245b3a.1; Sat, 05 Apr 2025 18:35:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1743903349; x=1744508149; 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=AfV6c322LhNIzDcptdQq7JkAT5PgJ5P9MLOfezDMm4c=; b=YdrAySVHJ2opsTq4DSUOh3RzauNGVleIdvb2AX20zGIxTtJJnz1n8Qxhj6/Fu+1itf 9rn7fwYI4ZWA9cld+QirLHThoWq1zWWvf/a4J8IGEyUsez6lDSfMSBXAKdrZHvmMquk7 ykiuK/xWhzvGSOkclhuQ2syQED52hUwP0vkhUp6BHXK2kNgTjEcWPdAMHokXtTKS7F9L HXrgW00nqRFHPFDBcQCLxghXVwZrCKsdDQbCjHfdfIIEZtwRk6BUVHaeSWOTYed9JA03 Xk7PY6f0nJ8SNnY/BZhkKtW1Nl2fDdSN3vse1Is2Neq/rhrlwQQNKer/b5rlPv+Lwl/Y gNTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743903349; x=1744508149; 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=AfV6c322LhNIzDcptdQq7JkAT5PgJ5P9MLOfezDMm4c=; b=U9+OlQBhi2S+Wh2dsHPg2l56OD2B9gZd4+nNp66TIA+UATBWbAhAaN5ozXyO5OwHug yDDG2pufHzjxavO9wt8OpGAR3MNhTebSQD6rYisE/htbaoOsDsxUvQgw2BBBykYr9rKv Mil1qCNHe68Ar0xWQM5dPRo4vYvu/2HnvyNVS3mEkT1d9tHb7Nh2j8QwuT+VQ3k3w4ew r0UELruP52rnjEye4yvwYVvjDophiuya2HHU7bnDcsku3B73cA6YXPOyjV2V0O0GD2+v 84INkA666lRCo+gCMDNl86wtHcB3VD1ieuBRpWWMrjiRfjvPKjK1QUdcHo6Ai0k3EWe5 aHAA== X-Forwarded-Encrypted: i=1; AJvYcCUcK9b1F3pyS2VSG504LGNN4FFVa9s1wnLVz093st/xNgzXK3WBqEzaa6k3OA7qICgY6QV/p5U=@vger.kernel.org X-Gm-Message-State: AOJu0YyOgV69NKJvkMjswB9SsM1G1QBlHZLf684EM+i+qQNrRJ7MD+6/ d3P1LIV/OEKu3kH5VlvbDHUKBQpc3Dy/GC7yxUQWxH7BKvjfZkM+dhw3UJFp X-Gm-Gg: ASbGncvlXxhRb4XKiazVd0lMUTaPW+4NM4RPqcydlAw/YR++F5kJJ7dapK4tdhqSowa i2xGaP5jZNhr8OdglvA//xKbTtDOhs1TeeiQ+8EbELPLutwvSJAUj33kS9KM81FEjswOCf/emmF rTKOYQxCejmPZ/zqwhzMAHo99VRKIN9fJRhGSD+DlPGViN/DUKWoYF91FwPz/BP9YtH7Sj0yGUd pDlVIJ2THuSlcdKlt1W3rDoSJBI1xwCJOdEekbOF8pT54eVMF/oZvwAshoKLtObfdCty1v2D77E onM0zCsvKeiJZr2UKK0+3iBK6oTJBRcm5I+6/0x1bFOwU5odLuKLkbRaeDKdO2hkLC+iBwLe6gt CulPuL73li7GQQ2YkgYQaQA== X-Google-Smtp-Source: AGHT+IEpUDhA1POUXQPRGA+uLs2g/QiNP48C8u7+VJ4QKBtX6WUHQsTe3TvseG7lQb9C2WXizf1tAw== X-Received: by 2002:a05:6a00:14d4:b0:736:476b:fcd3 with SMTP id d2e1a72fcca58-73b6b8fd296mr6864136b3a.24.1743903349373; Sat, 05 Apr 2025 18:35:49 -0700 (PDT) Received: from mew.. (p4204131-ipxg22701hodogaya.kanagawa.ocn.ne.jp. [153.160.176.131]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-739da0bc052sm5846849b3a.156.2025.04.05.18.35.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Apr 2025 18:35:49 -0700 (PDT) From: FUJITA Tomonori To: rust-for-linux@vger.kernel.org Cc: 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 v12 5/5] MAINTAINERS: rust: Add a new section for all of the time stuff Date: Sun, 6 Apr 2025 10:34:45 +0900 Message-ID: <20250406013445.124688-6-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250406013445.124688-1-fujita.tomonori@gmail.com> References: <20250406013445.124688-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add a new section for all of the time stuff to MAINTAINERS file, with the existing hrtimer entry fold. Signed-off-by: FUJITA Tomonori Reviewed-by: Boqun Feng Reviewed-by: Andreas Hindborg --- MAINTAINERS | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/MAINTAINERS b/MAINTAINERS index d32ce85c5c66..fafb79c42ac3 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -10581,20 +10581,23 @@ F: kernel/time/timer_list.c F: kernel/time/timer_migration.* F: tools/testing/selftests/timers/ -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/ +F: rust/kernel/time/time.rs HIGH-SPEED SCC DRIVER FOR AX.25 L: linux-hams@vger.kernel.org