From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-lj1-f201.google.com (mail-lj1-f201.google.com [209.85.208.201]) (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 09DB722EF4 for ; Thu, 4 Jan 2024 14:03:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="tC9ORHNy" Received: by mail-lj1-f201.google.com with SMTP id 38308e7fff4ca-2cd11ec6159so5487131fa.3 for ; Thu, 04 Jan 2024 06:03:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704377004; x=1704981804; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=pY0HtCQNVEDI/Vsc7JQzdf3b/zPkklUdbelm2DZN3NU=; b=tC9ORHNy6QoR6B4TYQt9gNqVlmQs1ACK4sGo09SZWQai8RvV7jScTv/UiPplMbzZNW 7sYyu5sN3wjUTPNK9Czml5/Jh7qFo6TPTjU/CbWKhmL7DeveUlZVRn7xPEdYzctnbcg6 IeBpuvpB3O3R49g4szniHihILXy6BuT2cGmwsDS02A3J8S/4IkCq+a0XMbenkcDoytML 5xZMVOBl5WdiUC8Q6PF7QWvTD0UsutIXBDYpyfb4NTTT5E4punH6TfvA/DxKNz8h/s7X aGVgo1fD4Kmz/ZT3cnNhzuGJLzp5RaITCuAfwhORKdRrAWTGIhYc7UOjQh9w7FfcOK77 F8yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704377004; x=1704981804; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=pY0HtCQNVEDI/Vsc7JQzdf3b/zPkklUdbelm2DZN3NU=; b=a+oBJUGWlut5ryqK7KF20yh6n7DK15LgjZFydVIJL6lXRFbIs7YW2ls2hA32VzN9e0 BpMIlJuxLQWNjFMW78K4JTNbn/gSVbjKcgQLOjs9ZyJtEnnDLyodEKf5jF4Px9cCPA5D t85XrA92yeALwGLj0vy0DJi98SzyigibrAChzF3YcgZ8swBLEDQLGjnrmecpLeBtyQbp kLLLlqUxhzwkKvYYHGRaHQBkVIZS0QeLSoD6HiYF/NpGTCOykd7vi+C0CDJ94P5clGYR 3UQPEkVzUvokkRGWUetCxWVAhPPKoHKLdfFXA3THM6BISmSNXIk4a+4fUA1NXfcXdQPP EswQ== X-Gm-Message-State: AOJu0YwW8DOs1WUibUefBPFIEd4pgg7D0zaaSzNHjQqnU5WvtMeBT1iO DoTMLLZZCtcPkEkt1ez2kfZxbGC9p0F1pOzOF0mE+Q== X-Google-Smtp-Source: AGHT+IELjFaDM7ROjJaxp18Ms8IfQA7iRjeQ9qHX1i3T0MNVzQ4ejL88pbznMOE5OdBX0g7vTGq6MfKXN7GuOsI= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a2e:97ce:0:b0:2cc:e8cd:c15e with SMTP id m14-20020a2e97ce000000b002cce8cdc15emr1111ljj.4.1704377003781; Thu, 04 Jan 2024 06:03:23 -0800 (PST) Date: Thu, 04 Jan 2024 14:02:42 +0000 In-Reply-To: <20240104-rb-new-condvar-methods-v3-0-70b514fcbe52@google.com> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240104-rb-new-condvar-methods-v3-0-70b514fcbe52@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=2768; i=aliceryhl@google.com; h=from:subject:message-id; bh=p9Hjc0DIm01aLLvHmXgxckf25tmtqAjiAqjiP2XhYzQ=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBllrqhTED+RqzR3DGEktEueQ+JDDTTkuSAC7YXM 8GqPGpOWRWJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZZa6oQAKCRAEWL7uWMY5 RvdLD/95YfRPjzU4C6ejj3MZZi1uOUQ5JEy8zAgsnwEwRg7KjRm41K7YtnKhlSKDOYCYd2ujRQ4 MUQGC3KDpZeKiqZBSDvWBFGZ9f+5Drjm4BQm7zcvFKqgP/JGSQByEzvv8Tq1aqad8YBsHT1WNk1 TmUtuWFY3bWmxQF8rcKksMmSo4sqipN+3nGMeKPHN4li4Ubq39P/mpYJ3BWPf7wpR4Lozvvtlkj UC4fvQIjxi7YketJEDrtJzOAPB7Og65RaakngKqBabtEzmH+gOl/mzowmbhmPV02CJ/WJ8/Q6m+ xaPg10C0xKvwUjVvQDs/M95CX1RLjD92e6fGhaGOVVCbisqRJdSm3VPK6bjZKdctrfcfMNxHOxP wjS8H34nu+6R/c55v1aznjQXFXcFVZhLhZQjFXSOspWwzWxkChAT98aBCs7V8dTHcZKKm+bR4L4 A8kEpD97vAx/c/bG25K3sNRP+wzenJfYLFjrSenTmXI9UkFHHF+07GdCR1Tb9SrVtHpTDHaVfvt Nh4/4q/lBojDMVaXNKHBMhcwhWnQYbddNNqX4QDk0fiYN6k0wHQhAhFN0y5F7W806x8VbXEV8lS /HJIell9u/uNHy8GHm8owOr3AksMeQRm1oXaLv2sMU1u/Rwoh7Egz3lKlB8Quupqpz8UHHi9/94 348GYEmm7Am6CDA== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240104-rb-new-condvar-methods-v3-2-70b514fcbe52@google.com> Subject: [PATCH v3 2/4] rust: time: add msecs to jiffies conversion From: Alice Ryhl To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Tiago Lam , Thomas Gleixner Cc: Martin Rodriguez Reboredo , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Defines type aliases and conversions for msecs and jiffies. This is used by Rust Binder for process freezing. There, we want to sleep until the freeze operation completes, but we want to be able to abort the process freezing if it doesn't complete within some timeout. The freeze timeout is supplied in msecs. Note that we need to convert to jiffies in Binder. It is not enough to introduce a variant of `CondVar::wait_timeout` that takes the timeout in msecs because we need to be able to restart the sleep with the remaining sleep duration if it is interrupted, and if the API takes msecs rather than jiffies, then that would require a conversion roundtrip jiffies-> msecs->jiffies that is best avoided. Suggested-by: Boqun Feng Reviewed-by: Boqun Feng Reviewed-by: Benno Lossin Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Tiago Lam Signed-off-by: Alice Ryhl --- rust/bindings/bindings_helper.h | 1 + rust/kernel/lib.rs | 1 + rust/kernel/time.rs | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index b5714fb69fe3..227dc9240f3f 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -8,6 +8,7 @@ #include #include +#include #include #include #include diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index e6aff80b521f..d4f90acdd517 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -46,6 +46,7 @@ pub mod str; pub mod sync; pub mod task; +pub mod time; pub mod types; pub mod workqueue; diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs new file mode 100644 index 000000000000..25a896eed468 --- /dev/null +++ b/rust/kernel/time.rs @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Time related primitives. +//! +//! 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. + +/// The time unit of Linux kernel. One jiffy equals (1/HZ) second. +pub type Jiffies = core::ffi::c_ulong; + +/// The millisecond time unit. +pub type Msecs = core::ffi::c_uint; + +/// Converts milliseconds to jiffies. +#[inline] +pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies { + // SAFETY: The `__msecs_to_jiffies` function is always safe to call no + // matter what the argument is. + unsafe { bindings::__msecs_to_jiffies(msecs) } +} -- 2.43.0.472.g3155946c3a-goog