linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: time: New module for timekeeping functions
@ 2023-02-21  7:06 Asahi Lina
  2023-02-21  7:25 ` Eric Curtin
                   ` (2 more replies)
  0 siblings, 3 replies; 28+ messages in thread
From: Asahi Lina @ 2023-02-21  7:06 UTC (permalink / raw)
  To: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng,
	Gary Guo, Björn Roy Baron, John Stultz, Thomas Gleixner,
	Stephen Boyd
  Cc: linux-kernel, rust-for-linux, asahi, Asahi Lina

This module is intended to contain functions related to kernel
timekeeping and time. Initially, this just wraps ktime_get() and
ktime_get_boottime() and returns them as core::time::Duration instances.
This is useful for drivers that need to implement simple retry loops and
timeouts.

Signed-off-by: Asahi Lina <lina@asahilina.net>
---
 rust/bindings/bindings_helper.h |  4 +++-
 rust/kernel/lib.rs              |  1 +
 rust/kernel/time.rs             | 25 +++++++++++++++++++++++++
 3 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
index 75d85bd6c592..587f3d1c0c9f 100644
--- a/rust/bindings/bindings_helper.h
+++ b/rust/bindings/bindings_helper.h
@@ -6,8 +6,10 @@
  * Sorted alphabetically.
  */
 
-#include <linux/slab.h>
+#include <linux/ktime.h>
 #include <linux/refcount.h>
+#include <linux/slab.h>
+#include <linux/timekeeping.h>
 
 /* `bindgen` gets confused at certain things. */
 const gfp_t BINDINGS_GFP_KERNEL = GFP_KERNEL;
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs
index 223564f9f0cc..371b1b17570e 100644
--- a/rust/kernel/lib.rs
+++ b/rust/kernel/lib.rs
@@ -37,6 +37,7 @@ mod static_assert;
 pub mod std_vendor;
 pub mod str;
 pub mod sync;
+pub mod time;
 pub mod types;
 
 #[doc(hidden)]
diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
new file mode 100644
index 000000000000..02844db47d34
--- /dev/null
+++ b/rust/kernel/time.rs
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0
+
+//! Timekeeping functions.
+//!
+//! C header: [`include/linux/ktime.h`](../../../../include/linux/ktime.h)
+//! C header: [`include/linux/timekeeping.h`](../../../../include/linux/timekeeping.h)
+
+use crate::bindings;
+use core::time::Duration;
+
+/// Returns the kernel time elapsed since boot, excluding time spent sleeping, as a [`Duration`].
+pub fn ktime_get() -> Duration {
+    // SAFETY: Function has no side effects and no inputs.
+    Duration::from_nanos(unsafe { bindings::ktime_get() }.try_into().unwrap())
+}
+
+/// Returns the kernel time elapsed since boot, including time spent sleeping, as a [`Duration`].
+pub fn ktime_get_boottime() -> Duration {
+    Duration::from_nanos(
+        // SAFETY: Function has no side effects and no variable inputs.
+        unsafe { bindings::ktime_get_with_offset(bindings::tk_offsets_TK_OFFS_BOOT) }
+            .try_into()
+            .unwrap(),
+    )
+}

---
base-commit: 89f5349e0673322857bd432fa23113af56673739
change-id: 20230221-gpu-up-time-ea9412204c3b

Thank you,
~~ Lina


^ permalink raw reply related	[flat|nested] 28+ messages in thread

end of thread, other threads:[~2023-02-22 19:55 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-21  7:06 [PATCH] rust: time: New module for timekeeping functions Asahi Lina
2023-02-21  7:25 ` Eric Curtin
2023-02-21 11:23 ` Björn Roy Baron
2023-02-21 12:32 ` Thomas Gleixner
2023-02-21 14:06   ` Boqun Feng
2023-02-21 16:02     ` Thomas Gleixner
2023-02-21 16:31       ` Asahi Lina
2023-02-21 18:45         ` Thomas Gleixner
2023-02-21 21:33           ` Heghedus Razvan
2023-02-22  0:01             ` Thomas Gleixner
2023-02-22 19:55             ` Gary Guo
2023-02-21 22:29           ` Miguel Ojeda
2023-02-22  0:24             ` Thomas Gleixner
2023-02-22  2:54               ` Boqun Feng
2023-02-22  4:45                 ` Asahi Lina
2023-02-22  5:20                   ` Boqun Feng
2023-02-22  6:52                   ` Heghedus Razvan
2023-02-22 12:29                   ` Miguel Ojeda
2023-02-22 12:28               ` Miguel Ojeda
2023-02-21 16:27     ` Asahi Lina
2023-02-21 16:37       ` Asahi Lina
2023-02-21 19:00       ` Thomas Gleixner
2023-02-21 19:49         ` Boqun Feng
2023-02-22  4:56         ` Asahi Lina
2023-02-22  8:33           ` Thomas Gleixner
2023-02-21 17:13   ` Josh Stone
2023-02-21 21:46     ` Thomas Gleixner
2023-02-22  9:43       ` Gaelan Steele

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).