lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20241005122531.20298-2-fujita.tomonori@gmail.com>
Date: Sat,  5 Oct 2024 21:25:26 +0900
From: FUJITA Tomonori <fujita.tomonori@...il.com>
To: netdev@...r.kernel.org
Cc: rust-for-linux@...r.kernel.org,
	andrew@...n.ch,
	hkallweit1@...il.com,
	tmgross@...ch.edu,
	ojeda@...nel.org,
	alex.gaynor@...il.com,
	gary@...yguo.net,
	bjorn3_gh@...tonmail.com,
	benno.lossin@...ton.me,
	a.hindborg@...sung.com,
	aliceryhl@...gle.com,
	anna-maria@...utronix.de,
	frederic@...nel.org,
	tglx@...utronix.de,
	arnd@...db.de,
	linux-kernel@...r.kernel.org
Subject: [PATCH net-next v2 1/6] rust: time: Implement PartialEq and PartialOrd for Ktime

Implement PartialEq and PartialOrd trait for Ktime by using C's
ktime_compare function so two Ktime instances can be compared to
determine whether a timeout is met or not.

Signed-off-by: FUJITA Tomonori <fujita.tomonori@...il.com>
---
 rust/helpers/helpers.c |  1 +
 rust/helpers/time.c    |  8 ++++++++
 rust/kernel/time.rs    | 22 ++++++++++++++++++++++
 3 files changed, 31 insertions(+)
 create mode 100644 rust/helpers/time.c

diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c
index 30f40149f3a9..c274546bcf78 100644
--- a/rust/helpers/helpers.c
+++ b/rust/helpers/helpers.c
@@ -21,6 +21,7 @@
 #include "slab.c"
 #include "spinlock.c"
 #include "task.c"
+#include "time.c"
 #include "uaccess.c"
 #include "wait.c"
 #include "workqueue.c"
diff --git a/rust/helpers/time.c b/rust/helpers/time.c
new file mode 100644
index 000000000000..d6f61affb2c3
--- /dev/null
+++ b/rust/helpers/time.c
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/ktime.h>
+
+int rust_helper_ktime_compare(const ktime_t cmp1, const ktime_t cmp2)
+{
+	return ktime_compare(cmp1, cmp2);
+}
diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs
index e3bb5e89f88d..c40105941a2c 100644
--- a/rust/kernel/time.rs
+++ b/rust/kernel/time.rs
@@ -81,3 +81,25 @@ fn sub(self, other: Ktime) -> Ktime {
         }
     }
 }
+
+impl PartialEq for Ktime {
+    #[inline]
+    fn eq(&self, other: &Self) -> bool {
+        // SAFETY: FFI call.
+        let ret = unsafe { bindings::ktime_compare(self.inner, other.inner) };
+        ret == 0
+    }
+}
+
+impl PartialOrd for Ktime {
+    #[inline]
+    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
+        // SAFETY: FFI call.
+        let ret = unsafe { bindings::ktime_compare(self.inner, other.inner) };
+        match ret {
+            0 => Some(core::cmp::Ordering::Equal),
+            x if x < 0 => Some(core::cmp::Ordering::Less),
+            _ => Some(core::cmp::Ordering::Greater),
+        }
+    }
+}
-- 
2.34.1


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ