From nobody Sun Nov 24 23:56:08 2024 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) (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 2967F38FA6; Fri, 1 Nov 2024 01:02:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730422943; cv=none; b=X7eggeSyrpcfEXcHnGAV7sN3YTJSIMVwq3dgYnYJDcxXUUkh4f6oAikxVlb5xjjgfUmf2QDKdrcMM3SYjFydyW0ilBeY+r3X4hDTFfDgf2uakVXBTxAMbPIGAjxob8zifB143cPXjdwRxhs4ELqhW4ReLHxHB90CbApkUIbAP2M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730422943; c=relaxed/simple; bh=ty5/y7Xtb2hwXPXaqKl+csNiJiUlRX6POVGSNuNzKgI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=A/062jMpiu/NG2StGWvNXW9YKi2eLewygOmhtXJbU7jOx0e4ZDZFditn5p6qwnvWv29QDMzuSWg0DEZDGf6FODFzhMRhWr8x8VQiB90H21bHeE45VLV+2J8Xajtzix/6exRvQEn8rb+7JgC3oFCXcooExrNI07jZFRItu0S2XhI= 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=EHI8nVAb; arc=none smtp.client-ip=209.85.210.173 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="EHI8nVAb" Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-720c2db824eso954217b3a.0; Thu, 31 Oct 2024 18:02:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730422937; x=1731027737; 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=7UR/Ms0FQM0UCt080++CvJa6JNyMuRguxnIevuegujk=; b=EHI8nVAbzMetpB8XqFtwYRvkn+u+OSC1V+vMUSeUgAhqcSCV+ILXK2Q624fD/hig3H BUtL68VE5BtYIPexDd0249iSaW87ykWYB0Ud9rjhA4gCNMGVc6a/6G+xi250jEa8PBrG pz2ATQdylFQPTDkfS0Y74KlMs3A2UbFnRoR/SYyA8kKuDaSFQEyViW/GReeiWWJYI+9p kWhtK2jm6HUpGBx8ADfOAxi9Aj66ZNjytM/5w8+zLvoWQ+FuzJT7wQGyvRCxQ4+MlL79 ER/Vq9zN3SMlPMurk3tkvjlUE26mtd8RX9Tjs75wchfXjthx5UX7vv+9TemxDq+5CL5J Tn1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730422937; x=1731027737; 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=7UR/Ms0FQM0UCt080++CvJa6JNyMuRguxnIevuegujk=; b=KYLlJJQZhO+TJTxmGc+NxaQtpEcjrPbTaZtL0hMRBN1AlmvEztM5D85atLEFoRQVef l3vt4tHG+uVTwBibRJWQD4dGFjehkyuGInn9iB+CW/HqGTZnSO5x1zSdG9C0tA8LM4PU phPQSN6Oe3Z7gQiLom4LmgHmyMAiHxvrMlJ1iZwndVZ9Fpk2Y4/gJSIWowISLG+a5rGs yb3ShdrewQhocVFZ0nTfR2+5iRuvPXD1GnXCefZ19xIgE0CcJQPdxuJTKO4UpxlTkxRC sPT46jTod15mZCXfQ+biwQDPj9VBTp50E8Sreusn/NDs/8VvwTHqdzgbd21b8EmAc01D KU9A== X-Forwarded-Encrypted: i=1; AJvYcCVRo41FwBvGjtLfGRjwoEhtpwrtx1TOXnVb1wMJahHvFojhRu2+D/DaYa4c61H86vsVi1HrroBatmEGfMMwc5k=@vger.kernel.org, AJvYcCXhCxvF54+Tpd3y+MD3G30ShFxDrYwqgluKKZegx1fNlQGvpl49CaVo7LPOD73n3efsfEicUVvFXnuJnQs=@vger.kernel.org X-Gm-Message-State: AOJu0Yzt5QToX1YIbekr2lpP64o+M5of2BW0UD9wS9kcyTB9A9s1Tycg uWl81t0qQGBwgtcaT2f2mUh9QzK51cGTHUHVHacKR/V2bdyiDgiZ X-Google-Smtp-Source: AGHT+IHq9PCnfOGKPlXnGN+8ljS5aTE7+Ms55aNXzF9qxiuWry8bC8I/0a9Qg7t83bcX8lUCnIA0xA== X-Received: by 2002:a05:6a21:1349:b0:1d8:a96c:e9bf with SMTP id adf61e73a8af0-1d9a850a5a7mr28826274637.44.1730422937462; Thu, 31 Oct 2024 18:02:17 -0700 (PDT) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-720bc1ea6a1sm1743403b3a.74.2024.10.31.18.02.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Oct 2024 18:02:17 -0700 (PDT) From: FUJITA Tomonori To: anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, jstultz@google.com, sboyd@kernel.org, linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org, rust-for-linux@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, arnd@arndb.de Subject: [PATCH v5 1/7] rust: time: Add PartialEq/Eq/PartialOrd/Ord trait to Ktime Date: Fri, 1 Nov 2024 10:01:15 +0900 Message-ID: <20241101010121.69221-2-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241101010121.69221-1-fujita.tomonori@gmail.com> References: <20241101010121.69221-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 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 e3bb5e89f88d..4a7c6037c256 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -27,7 +27,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 Sun Nov 24 23:56:08 2024 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) (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 7D0E04D599; Fri, 1 Nov 2024 01:02:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730422948; cv=none; b=cSgalrtkDGXiN4mLd/rmu15MUHML4nIAFY2NYW4NYZ1SBC8Q2dVDUKVwvF2YtBQnNcpdLQiLV9G64ypbR3hf/nplAO5phBn9HYNICiJYAnucgs9EFhNwvKivfB9MHZEy8bm8XLgbUAPKcbw1AELhXutqaIa1XuCY1LdSg6dwT/s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730422948; c=relaxed/simple; bh=3l3DdrvXhQbQdgKLu1mca5O/A7DDoZLjSiqXMJiZBOo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=POSEqFWkNTSuJP7CxXWdTHGaRHXRunzdB4w/8gz87FTCvdpd0vQkDszi8W1sh6XlKzIn9T8GwAO5LmrR0MSWjvRnlgamILQrJ9cM0ZlSblaD/oEP/uwjyqZ/W5vlaDE1qG2FMevERZ8BHnX8Nq2HbUamD+wG6NhSnKE/umH09n8= 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=bSksDpoJ; arc=none smtp.client-ip=209.85.210.173 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="bSksDpoJ" Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-720d01caa66so136255b3a.2; Thu, 31 Oct 2024 18:02:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730422942; x=1731027742; 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=x6DjLAHViVa3N3XgZMB4Dtmm1+ysQiv0CFhsw4EncfE=; b=bSksDpoJctZtCwpRWZjMVPKWeKExmKmuukk0PueILm3mJaKiJ7MF0IUmBysJ1dh1Ft qWVYToTUu4kjUiFJIf/bOpIKk91qR64Vuc7VQRXMjC6M6skd4LlOzTe5dUEmLvuozO2T ZRUurBW4S6a8u8KZQ1DDTIJGS/IXN3T5eYhbIS5FrozAV84/CxICOjX5w7zJfIIv637q 72SYJn8KP95PZWReJvc8ZtF+zRCn4vvvz3TSPfRILwbMHAGVlgREub8N5Q3uBkqjcTM6 Ef5ZUwOcBW3+TYCDDZ1kRjbPlTgUS6wmbSX20MqiqUCux81eXLaCfMdOXwx9m5wgxzcr aNUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730422942; x=1731027742; 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=x6DjLAHViVa3N3XgZMB4Dtmm1+ysQiv0CFhsw4EncfE=; b=tjCMl1t2BHdmPO+SycYBFKCxL/ggN9j2OgHHwd5PRYXIpOoDkKELjRRS4Vb4M7SLLb 25f0v2KyhFCBLKPl6ClY+m+YGKi282ww7X121FoeuRxGUHC6t/JABTh/NjGSx8rFVoUx Jqs9Hc8ZAxmdjbLwMcYQvUVV3T3tfBWdF/UQh9pB9m11rQUlTzzYwXoAoULDC86W16Yf fHAQychi4/aczscgzTco8bVg4ptcBUUeMXX7Qmopj0GZcnb6ApXIqntbqYaUCza2UEfB LUc2j99BczWksOX5BRDyrrkW0mhXxOPjLmugXIiZUZKegWh+PyTmiTUHPW7qn7w1fqOf RN2w== X-Forwarded-Encrypted: i=1; AJvYcCUHvhv0/hHQMCCBkIc0loA3MnWEpp93ybmioRus83zt9wBfnEECXvwSv3iBJqAoGHrJKvyFE90I1Upf5cU=@vger.kernel.org, AJvYcCXVELG4yPLenAoSd4WJHwLfj21/U8jMCVMfngO4Wo/jU8YeSey9clrSqp9a+tupF/ZDkfvdhvb55LwFKNCBKyg=@vger.kernel.org X-Gm-Message-State: AOJu0Yz9pavU0g6h7bwH6jSQma7aL0D4MSDmhqhmQGmiIJDPuNZnksXL kG0vWHPFzb/xespfFGfZe/ZyCuttxgyqzUK5mwpYPh9gec1wVeCt X-Google-Smtp-Source: AGHT+IEJsh82diESCENfnMqEG7eGqPX1euFeN1oc0n8jzzl3OeosbLM9F5ISpsHYs+KNLqO+dRMnVw== X-Received: by 2002:a05:6a21:2d0b:b0:1db:8d70:25e7 with SMTP id adf61e73a8af0-1db91e68234mr6729919637.34.1730422941597; Thu, 31 Oct 2024 18:02:21 -0700 (PDT) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-720bc1ea6a1sm1743403b3a.74.2024.10.31.18.02.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Oct 2024 18:02:21 -0700 (PDT) From: FUJITA Tomonori To: anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, jstultz@google.com, sboyd@kernel.org, linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org, rust-for-linux@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, arnd@arndb.de Subject: [PATCH v5 2/7] rust: time: Introduce Delta type Date: Fri, 1 Nov 2024 10:01:16 +0900 Message-ID: <20241101010121.69221-3-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241101010121.69221-1-fujita.tomonori@gmail.com> References: <20241101010121.69221-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 different from Delta. Delta::from_[millis|secs] APIs take i64. When a span of time overflows, i64::MAX is used. Reviewed-by: Andrew Lunn Signed-off-by: FUJITA Tomonori --- rust/kernel/time.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 4a7c6037c256..dc8289386b41 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -8,9 +8,15 @@ //! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h). //! C header: [`include/linux/ktime.h`](srctree/include/linux/ktime.h). =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 core::ffi::c_ulong; =20 @@ -81,3 +87,54 @@ fn sub(self, other: Ktime) -> Ktime { } } } + +/// A span of time. +#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Debug)] +pub struct Delta { + nanos: i64, +} + +impl Delta { + /// Create a new `Delta` from a number of microseconds. + #[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. + #[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. + #[inline] + pub const fn from_secs(secs: i64) -> Self { + Self { + nanos: secs.saturating_mul(NSEC_PER_SEC), + } + } + + /// Return `true` if the `Detla` spans no time. + #[inline] + pub fn is_zero(self) -> bool { + self.as_nanos() =3D=3D 0 + } + + /// Return the number of nanoseconds in the `Delta`. + #[inline] + pub 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 fn as_micros_ceil(self) -> i64 { + self.as_nanos().saturating_add(NSEC_PER_USEC - 1) / NSEC_PER_USEC + } +} --=20 2.43.0 From nobody Sun Nov 24 23:56:08 2024 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.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 D7FC982D91; Fri, 1 Nov 2024 01:02:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730422955; cv=none; b=hXwVNRNdZYjc/d6X2tsL63hGocIqw7/+joSJNj+pFBUhXEFdrZ8BKIOAAsuqPSkdetJqsts3ovjJQX2cuEilUky55k9IfuCN4tHS4vA0sevePBlZehnkuOfYurYy02Pv8sooZBIEVrzmIZ9ymbMFZ6anf19/d8ySso/X7XS4EYw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730422955; c=relaxed/simple; bh=8NjLDyCC+D7+b9Udwktjf3Mpe3Crs3poHbrK2jYZmEM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=blHYZKWaSBrnPISZZcuBMWWogJsSLccGUgkdRHNZ5774YZZJjnHUCuUYe4faBEzoXxVp1znspTJmPeoED+wkE++NOGTIYrlqoI/OL1qxE2KZMrc0VdrbhuCy5f1Ihl9/PWTyfnjeqHdxsJ1cUYaOfmSpGDZwsyxI4b0/5NYig/Q= 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=CgebQ3iF; arc=none smtp.client-ip=209.85.210.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="CgebQ3iF" Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-720be27db74so879624b3a.1; Thu, 31 Oct 2024 18:02:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730422946; x=1731027746; 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=Ik2J1cGAabc4dT5du2CznrsW5mp7db+YsB0nTu9yjmc=; b=CgebQ3iF69hGh7FWBSN1+C/c9KTma+ijAmLDS+buopdXW8eGEhZVFumtr0Ac5alMxf vpnZd2v7yyqcWFpSSEkm1eUJRdyYLd5xdExQHmYFH9PTBVOJQGuhPHjJzdlpXiZNAQ80 Rm4er66OJ2rOgv6X6wHQ2N1hyIYK3mibLNs83NhmFiy0Tauf1LovQfaaKeh/R1I8jwDk iSQ5m43F8G+rKlzFALsDjnlQdvGT1Pke50Tn5EgXtIW7z2U5IEIGWv2QdDXvhA/FLEPs i1h/UCRLjP097wCgykHVL6m+eE2xQ1u1b4OC0zH8jeBzr1aF6eM9ZoFn6ULkH2OHGX59 5b3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730422946; x=1731027746; 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=Ik2J1cGAabc4dT5du2CznrsW5mp7db+YsB0nTu9yjmc=; b=ifWVfUxSbaAzuaqOtVzpvSoJPTLW+Z2GLRC3CvCJVwNgzttW4YvtbOQD0xBKMEUhTn 8Chy3omVuudFRqR4ZQs+CG7vMZiKHvnDT5RFb7qmaJg/z91dhc6rhwekmNjCRSudsMtf E+xyc0EdVaMW4HwJPC8ZsK7dPi/b5Le5FMZGpT9QG1m2ZwuM9+B1k4o7EufuwFTVWDt7 gtY6AsXQ8/kWt4R3+3XoE/FCTxIb792fQPMm63SFDCXd5VLify1xgED9b8CIhkuNId71 fu3YpvMyNKyn5l/V+Y6DXSMKwB+WwUIA5OFNA9C9aET/495MIQqA0U21B3DP4VqnZdUr ljdg== X-Forwarded-Encrypted: i=1; AJvYcCVEepiq8hRG5vZQozBl6GRwZ6aZukHdQM51ZO/x5RWJkCWjUUYyZlGZ6Uw9DXHdeUuqb7NBkpa06n8AZVM=@vger.kernel.org, AJvYcCX8DEd5Q6dCg0wWdTNnLpYk47er6n13WhXm28f+dm2mDr62MKCEllVCEeuvk48QoMIYGfW0BNWF9/O1LsdKy38=@vger.kernel.org X-Gm-Message-State: AOJu0YwmM2HEomvCx7qSsuXAY0nZ3Pr6kF1pK0SEH+M2x+SpOD+tBEB5 Q5C45R02T0uKODsb/EiyMOsk3GVKM8JrtUEstlTLB4bTnabBnjGE X-Google-Smtp-Source: AGHT+IF04EKGKp5c3JjUwNqnvXlSR6AJv+5qa9eq/VlZ+Ae2bswJdsHXXNHY5K2mBkCEsXNcBe21YA== X-Received: by 2002:a05:6a00:a05:b0:71e:5252:2412 with SMTP id d2e1a72fcca58-720c9628be0mr3079329b3a.2.1730422945905; Thu, 31 Oct 2024 18:02:25 -0700 (PDT) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-720bc1ea6a1sm1743403b3a.74.2024.10.31.18.02.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Oct 2024 18:02:25 -0700 (PDT) From: FUJITA Tomonori To: anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, jstultz@google.com, sboyd@kernel.org, linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org, rust-for-linux@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, arnd@arndb.de, Boqun Feng Subject: [PATCH v5 3/7] rust: time: Introduce Instant type Date: Fri, 1 Nov 2024 10:01:17 +0900 Message-ID: <20241101010121.69221-4-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241101010121.69221-1-fujita.tomonori@gmail.com> References: <20241101010121.69221-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 The operation never overflows (Instant ranges from 0 to `KTIME_MAX`). Reviewed-by: Boqun Feng Signed-off-by: FUJITA Tomonori --- rust/kernel/time.rs | 48 +++++++++++++++------------------------------ 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index dc8289386b41..e4f0a0f34d6d 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -31,59 +31,43 @@ 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. #[repr(transparent)] #[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord)] -pub struct Ktime { +pub struct Instant { + // Range from 0 to `KTIME_MAX`. inner: bindings::ktime_t, } =20 -impl Ktime { - /// Create a `Ktime` from a raw `ktime_t`. +impl Instant { + /// Create a `Instant` from a raw `ktime_t`. #[inline] - pub fn from_raw(inner: bindings::ktime_t) -> Self { + fn from_raw(inner: bindings::ktime_t) -> Self { Self { inner } } =20 /// Get the current time using `CLOCK_MONOTONIC`. #[inline] - pub fn ktime_get() -> Self { + pub fn now() -> Self { // SAFETY: It is always safe to call `ktime_get` outside of NMI co= ntext. Self::from_raw(unsafe { bindings::ktime_get() }) } =20 - /// Divide the number of nanoseconds by a compile-time constant. #[inline] - fn divns_constant(self) -> i64 { - self.to_ns() / DIV - } - - /// Returns the number of nanoseconds. - #[inline] - pub fn to_ns(self) -> i64 { - self.inner - } - - /// Returns the number of milliseconds. - #[inline] - pub fn to_ms(self) -> i64 { - self.divns_constant::() + /// Return the amount of time elapsed since the `Instant`. + pub fn elapsed(&self) -> Delta { + Self::now() - *self } } =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 + // 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, } } } --=20 2.43.0 From nobody Sun Nov 24 23:56:08 2024 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) (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 D587C29D19; Fri, 1 Nov 2024 01:02:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730422956; cv=none; b=LTM6ht27ffXEIIZB0nLBoXCxr4LxJZEMlgJHS9A61xRD82t7FnMddYRgGbc4aocDmHPabzvbptjqHxN9jOymJRv6IE8/5Obe0h2O5BgIW78u8uF0gtgUu+UKjB/kJbeYEwfScCnhY3GPnh1omelnsXkT/XM44Xk3mHmVURR860w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730422956; c=relaxed/simple; bh=FvpqWnzB0jVSnw59sQwssA4N+d90d6I3eymP7JQbvn8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=U9vNc6PQmrdyJuFIB0V5xgP+KkzWfKxRSm5qz6b3zLXyc8y8YGMUT3k0Qhurz7h+QX9lkXHSZQX095SdYJQVo8U31N08liFKqDr9qj/k86XtVuVNHNgeQk68A12kxta85jbR2rAO51L15O334QWUSnlUlXutsST3V+IL/7PKSVg= 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=L0dnR0Zs; arc=none smtp.client-ip=209.85.210.178 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="L0dnR0Zs" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-7205b6f51f3so1304721b3a.1; Thu, 31 Oct 2024 18:02:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730422950; x=1731027750; 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=1YjnWtPONxIMZE5drgHndbLVFH/a0haqtE8goaJuGsg=; b=L0dnR0ZswCXvmuhLoxBurEL9SzwdG7BRhL5kwJaApsE7AYKUJ1dPcqoQOMAKE7eHeV ga0b2kmucaL00QF8GoQTHtGY4fRcKZPOV+9eljRfBqKmJsc2t+UpioWAsYujI6loaXhD ScRNAWyKFxYwkP3vxEuQrCny5YErSpd56HdIlZmom/R220Ban9Sf1ZTl5mTqGrPWknie PldF22EynhBJVeQl+CVlAOcc7KB4lRiUuNnkc7Yh/j9+W9ulc2sANLYKU1rhgU4CaXox 0Pn8zI18/GGHWg/eeZDYlitTUiq5TmDflhkbe+HQN47Jyd1Wj8EdDi8LzNpNBxS2yIxY /iuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730422950; x=1731027750; 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=1YjnWtPONxIMZE5drgHndbLVFH/a0haqtE8goaJuGsg=; b=U0/M6H3Pv2J6RW1Cqd4dQXJh9iMvuuvmTsz5gnpwcijnFejaA09djw38p3yEb53SYh AayqOoTUwjiayS31wqt5uIFw7tUSW/xN7u+iv1ZzzemEQzcJVy1brdunzymoce1u7W9Y g6pLcJQ1titQ8tF2W35wHjXB219/+FK1VWrTtccW/ovQzSQKPJFZ+E+3HAPu6T/qoHRE mE3yPAs/1eE4q6t7SdWSComDpc2LlPsm+g2RmKU8XQ1falc2RNuFDqAEpElNbXK/JRv7 UuTw90AlbzePMMVU+zWQkU2Iio9IxOi08FB9IfuYpAZHBt4iLxI8qHZpZjK6d2BRdxn2 jnsQ== X-Forwarded-Encrypted: i=1; AJvYcCWJ/POqJEEzgjrCLt5sRB5gU0Ek3l3Jko8OK84RdItY03sHu51fCRiDOqb55ZzAvvIE+psuNJq2AwDf1HS6ZyY=@vger.kernel.org, AJvYcCXeuirqZIeiUneYm6Mlo6vc24ax/3Eb7f6zGPsSgCtCzwUIVZXhAvQzvRUj6EeeWGYrXGeGJuWTYQhehsA=@vger.kernel.org X-Gm-Message-State: AOJu0YxKHGbM34FKqMP6Qn7b1R+LnpU05nGNQnoWf4s7s/glNhW2FuJ1 b3NVDTofsQIECsaQsilRO4TQmqP6wonsYPJFOU3npJEa7Xq0vYPU X-Google-Smtp-Source: AGHT+IH/mMWTOXxIyKN1UpAnVdpI4tYSCS0MdqWDYFilbE3n9k4VQVdfEhU/HA2cLajGDyUZycdNkQ== X-Received: by 2002:a05:6a00:2385:b0:71e:3b51:e856 with SMTP id d2e1a72fcca58-720c98833a6mr2643314b3a.1.1730422950021; Thu, 31 Oct 2024 18:02:30 -0700 (PDT) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-720bc1ea6a1sm1743403b3a.74.2024.10.31.18.02.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Oct 2024 18:02:29 -0700 (PDT) From: FUJITA Tomonori To: anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, jstultz@google.com, sboyd@kernel.org, linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org, rust-for-linux@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, arnd@arndb.de Subject: [PATCH v5 4/7] rust: time: Add wrapper for fsleep function Date: Fri, 1 Nov 2024 10:01:18 +0900 Message-ID: <20241101010121.69221-5-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241101010121.69221-1-fujita.tomonori@gmail.com> References: <20241101010121.69221-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] Signed-off-by: FUJITA Tomonori --- rust/helpers/helpers.c | 1 + rust/helpers/time.c | 8 ++++++++ rust/kernel/time.rs | 4 +++- rust/kernel/time/delay.rs | 43 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) 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 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..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 e4f0a0f34d6d..9395739b51e0 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -2,12 +2,14 @@ =20 //! Time related primitives. //! -//! This module contains the kernel APIs related to time and timers that +//! This module contains the kernel APIs related to time that //! have been ported or wrapped for usage by Rust code in the kernel. //! //! 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; + /// The number of nanoseconds per microsecond. pub const NSEC_PER_USEC: i64 =3D bindings::NSEC_PER_USEC as i64; =20 diff --git a/rust/kernel/time/delay.rs b/rust/kernel/time/delay.rs new file mode 100644 index 000000000000..c3c908b72a56 --- /dev/null +++ b/rust/kernel/time/delay.rs @@ -0,0 +1,43 @@ +// 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 crate::time::Delta; +use core::ffi::c_ulong; + +/// Sleeps for a given duration at least. +/// +/// Equivalent to the kernel's [`fsleep`], flexible sleep function, +/// which automatically chooses the best sleep method based on a duration. +/// +/// `delta` must be 0 or greater and no more than u32::MAX / 2 microsecond= s. +/// If a value outside the range is given, the function will sleep +/// for u32::MAX / 2 microseconds at least. +/// +/// This function can only be used in a nonatomic context. +pub fn fsleep(delta: Delta) { + // The argument of fsleep is an unsigned long, 32-bit on 32-bit archit= ectures. + // Considering that fsleep rounds up the duration to the nearest milli= second, + // set the maximum value to u32::MAX / 2 microseconds. + const MAX_DURATION: Delta =3D Delta::from_micros(u32::MAX as i64 >> 1); + + let duration =3D if delta > MAX_DURATION || delta.as_nanos() < 0 { + // TODO: add WARN_ONCE() when it's supported. + MAX_DURATION + } else { + delta + }; + + // SAFETY: FFI call. + 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(duration.as_micros_ceil() as c_ulong) + } +} --=20 2.43.0 From nobody Sun Nov 24 23:56:08 2024 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 EB8C212CDAE; Fri, 1 Nov 2024 01:02:34 +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=1730422961; cv=none; b=j2jqiMESXeuqcpmWLFDMChRcYwgitU/ZtiDCmZCL7C8cQJbygWsKty7VjTvC0D5Sp0ZqJ+BqoXM/960FWY4kTPOsNIukU47oMqyJGKzL0l2SCsEc5WKK2aZsPnqG9SFGM9uaTbpl8VzshY75TkON2gOjoMFhxaKRKl4MI7jgjcM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730422961; c=relaxed/simple; bh=YZIDna+Nw7Nb23XtlYPw5rT7pMv1qHX5PotS8Nu/1Lg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QP6n755/7fmr/Vr1uOz1CvMmhgQsjcCTMyJXcbfQLjeflfMBoPryez8XOAGO8k2b3mFJdIoeq6pJifwGYcK02+8nm4t3OY/0fjtM6t2bxx78Z7hOaS8ya47VnktRG9Joy7i/AZNyx/P+aLqEhvmuPh7FjZBeCUNBtUs8by/Wqy8= 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=AX6WsMZH; 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="AX6WsMZH" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-71e61b47c6cso1278319b3a.2; Thu, 31 Oct 2024 18:02:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730422954; x=1731027754; 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=RjDi4l6vEO1+WFS2aicMTlZT3n6TL7cER+Vr/4E6ZEo=; b=AX6WsMZHCRljslgezs3esaFjVievgtetdGVb2vQ9JqdU0ADwcf638JitmbMojqczxU GJC6+pQ4g3xIqAXmspdyWOYIR9njhNHePmRU/HgYJ2RVMYVBun4PTRsMcId9MUYLbLF6 P3VYy2BYka/UIwgDaeg5U+D+NZdzzrHM3xEDekKNCB5uaZ+KykrL8sqelnN5IG0iH8NJ 6UR3SI6toHzcqwkNOIFBXSNnaZCT0kGQPDX6TCzxiEkKXkIZMBbZ27b7MLgRNtuAbdf3 hy7lI7CLk85Ut1oCrugm7GBD9Vf73w9kFw/7b11QiBfTtWeSb+6Wix0Ho9CGw+1lng80 mrxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730422954; x=1731027754; 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=RjDi4l6vEO1+WFS2aicMTlZT3n6TL7cER+Vr/4E6ZEo=; b=ZGkrqr9WRjruz27VmhEPWLPaZkEiuHNOUGdwCTBTDswzwSbtyRtz9WC1wyG4u3nsvu trC7vPDN5kyxz4mzpaAvy9/bcQN5sJv4G6D25cwo+Hr1vI3blU5bj6NvnfI8ms70fleu 4Dtz1aom4DPkxD/a1AfsBylcjn57kFJlLxSXtLk1xzdSIibu+yNaEGKDdkiuIsYtYpBU ZzEmmNBTaSjlcrk0XPRGCVmjYsr8Dw+JhvqsthV+8XhEBL1fQdoy26FDdSSq0p5MurlW qA1c/uuoA/tFAxKo77bgD/8rnkfGKM2Gc83LNCCAKkPCsxJEeW2kVgGsrZ88ti5HVf6f fSNw== X-Forwarded-Encrypted: i=1; AJvYcCW1JRUBhsyxGdBP+w855l3erTNmQCbHr+By3qgudy5dAegJX9qGWc6G2RpKcY2gNV4DMvTJpjS0drX93Su7t7k=@vger.kernel.org, AJvYcCXVDMYWaFrsBTICiO8Z/jI/iDKRqDqOVHBPZ1vqsBOLE/aaOitl62CmIHNxxM0eryoG7NGDTSmcqQ5yLzg=@vger.kernel.org X-Gm-Message-State: AOJu0YwEUXhcnQ+9ztVmuL4AF5W4MhTev705Ud6J2elI6zB+Y+r9GAtK mXfemynaMw7p8zMghA85Jo/E4wzcWyGTvb7UNioMK7i8pZKE1mA8 X-Google-Smtp-Source: AGHT+IHhtMYwMi2k7TtJQkwncbROihJEQz/Z+Rv6bBIiZf8Etqi+7+J26vfBxCV4iCv70+DHdTj12A== X-Received: by 2002:a05:6a00:2384:b0:71e:68ae:aaed with SMTP id d2e1a72fcca58-72062f81e6dmr27887100b3a.1.1730422954152; Thu, 31 Oct 2024 18:02:34 -0700 (PDT) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-720bc1ea6a1sm1743403b3a.74.2024.10.31.18.02.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Oct 2024 18:02:33 -0700 (PDT) From: FUJITA Tomonori To: anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, jstultz@google.com, sboyd@kernel.org, linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org, rust-for-linux@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, arnd@arndb.de Subject: [PATCH v5 5/7] MAINTAINERS: rust: Add TIMEKEEPING and TIMER abstractions Date: Fri, 1 Nov 2024 10:01:19 +0900 Message-ID: <20241101010121.69221-6-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241101010121.69221-1-fujita.tomonori@gmail.com> References: <20241101010121.69221-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 Rust TIMEKEEPING and TIMER abstractions to the maintainers entry respectively. Signed-off-by: FUJITA Tomonori --- MAINTAINERS | 2 ++ 1 file changed, 2 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 2250eb10ece1..6fb56965b4c2 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -10171,6 +10171,7 @@ F: kernel/time/sleep_timeout.c F: kernel/time/timer.c F: kernel/time/timer_list.c F: kernel/time/timer_migration.* +F: rust/kernel/time/delay.rs F: tools/testing/selftests/timers/ =20 HIGH-SPEED SCC DRIVER FOR AX.25 @@ -23366,6 +23367,7 @@ F: kernel/time/timeconv.c F: kernel/time/timecounter.c F: kernel/time/timekeeping* F: kernel/time/time_test.c +F: rust/kernel/time.rs F: tools/testing/selftests/timers/ =20 TIPC NETWORK LAYER --=20 2.43.0 From nobody Sun Nov 24 23:56:08 2024 Received: from mail-pf1-f169.google.com (mail-pf1-f169.google.com [209.85.210.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 3A5A613B2BB; Fri, 1 Nov 2024 01:02:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730422965; cv=none; b=cnO85QNU2GEsM5s/MJm1V+554eP4vcJOBocsBt0EfY08weVBDuX6A7DKmXohnXogNy2Lv7XYpBb5bf/hyyd1ZwYp0C0a5gwjMlwcjvNwMVsHBdjEjX4DX4phLdWUiJPsGCyuwAiRk9DnWOfCZLpyODPMM5jv/vXo8A1p7jqQEmM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730422965; c=relaxed/simple; bh=uay9p/gCr6OW7dCyCbNlhab+NCYTxQsSdsnFjQ4S6yU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Zg9sMJHqSbciKrsxHTM0HMZN+hoAg46NVAKAgJUMBVJcxIWOt+LDU6fzdj9g7oy0bmsSaIfqGflOLwk/jJyl/EJkeCyb92P14B2+rNqtu0or32Q5Pfdw/b/KtT2OMYsT4O7s+uzpf/5qs1rwAATzVTPLATkQ70K3xPNKJEwfH5M= 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=TlYeZ7DK; arc=none smtp.client-ip=209.85.210.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="TlYeZ7DK" Received: by mail-pf1-f169.google.com with SMTP id d2e1a72fcca58-7206304f93aso1321021b3a.0; Thu, 31 Oct 2024 18:02:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730422958; x=1731027758; 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=56yzkoCbHMeW0kr62bZqoBqGxQ1mmiPy9d//hWnafTQ=; b=TlYeZ7DK7szPr9xpPV1NQ4KXAgxVBE0yr7Z/X0hPUKyTKEwv7otADS8hGwWdvX53zp sZ8iC5edFI7Y0No/8iEs/FDGAo/UY7m9Cu9fgYeUgHPv133hYv0FAWnZ9cFau97TqMp4 akNgIRP9+jijBvJsrCbWdpyO2rpqLrGmq98+pd+K0IztYFsv672gLmx5WjDDehoY9J0i O9EOqCtM/mkiZUnbTbxPYGxBzDOci1uuEoHZ9JEsQ31FWlSIVwpgjQpo/IeVazaaODv0 bAMXFrVBy7V+nRXJj9V2aCXb4lL/5oFOrbtx69urq4MOJDLbWqMb+QEqzpuQDHSnvqBt rlMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730422958; x=1731027758; 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=56yzkoCbHMeW0kr62bZqoBqGxQ1mmiPy9d//hWnafTQ=; b=c/N1H2cIt6mJy0TR8+Y7bs9Vdg1SGkaJKo7Bz7PEY6rFgYG0t5hGci67tR3Y+Peixm apjUCQv7sw04nKfJlU/dt0Eox7Yr/WJIvFOwMT2fVxWH5qZqlV3Zm7fDZ+XoPtoC1dQ3 KNp5NVjwjKbBRgkoUGd0BIuBdnWR+T8zYIywwyR2WVkgwjBTA0Gmzk2RZIDjWsVfQUAl jc8LX0wghFFjqPHAKJHQcM21a7CfRnZhCE6fMMR+2xhLrox3KdsPFa9D8W4uDogLe3qt ErU/LcRjBtjOHiplwOSrVYw5VfsCYg61Wg/zfquNhIP8FQjqZXB9XB1uELy4d6Fs8sSJ c4ug== X-Forwarded-Encrypted: i=1; AJvYcCVN15pCFOyG8aGPrSKfQGFovJutfUh25Oac/NGuaEujUlUMBRuIYlzaK+5WG8yOjuYITzLxZtFvjcO1yAlRe1c=@vger.kernel.org, AJvYcCWLQtf4jowDPNAAmcG04Izf6kGK3PNQ+a1pFHChJezjcv76pW4H07gePpd/8rya2KSdpKc1iPaoNsMGBPQ=@vger.kernel.org X-Gm-Message-State: AOJu0Ywf7g46JKAJu2q6hQVA/xek8QOM34fxqBx79dOnL3kETFUZeAIr CRBv/ZBooRC/CMos3akmpGDdst4LXkF2+AZg11nSazyhGTq971jj X-Google-Smtp-Source: AGHT+IEtwvME9HaiQrHAqd7RUCTTa2wd36EYhnoVoJiWkZoP6O6JD64TlBq66x+GV/bED9sKGO6yYA== X-Received: by 2002:a05:6a00:a2a:b0:71d:d2a9:6ebf with SMTP id d2e1a72fcca58-720c98a1c80mr2713938b3a.6.1730422958338; Thu, 31 Oct 2024 18:02:38 -0700 (PDT) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-720bc1ea6a1sm1743403b3a.74.2024.10.31.18.02.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Oct 2024 18:02:37 -0700 (PDT) From: FUJITA Tomonori To: anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, jstultz@google.com, sboyd@kernel.org, linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org, rust-for-linux@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, arnd@arndb.de Subject: [PATCH v5 6/7] rust: Add read_poll_timeout functions Date: Fri, 1 Nov 2024 10:01:20 +0900 Message-ID: <20241101010121.69221-7-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241101010121.69221-1-fujita.tomonori@gmail.com> References: <20241101010121.69221-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 read_poll_timeout functions which poll periodically until a condition is met or a timeout is reached. C's read_poll_timeout (include/linux/iopoll.h) is a complicated macro and a simple wrapper for Rust doesn't work. So this implements the same functionality in Rust. The C version uses usleep_range() while the Rust version uses fsleep(), which uses the best sleep method so it works with spans that usleep_range() doesn't work nicely with. Unlike the C version, __might_sleep() is used instead of might_sleep() to show proper debug info; the file name and line number. might_resched() could be added to match what the C version does but this function works without it. The sleep_before_read argument isn't supported since there is no user for now. It's rarely used in the C version. For the proper debug info, readx_poll_timeout() and __might_sleep() are implemented as a macro. We could implement them as a normal function if there is a clean way to get a null-terminated string without allocation from core::panic::Location::file(). readx_poll_timeout() 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] Signed-off-by: FUJITA Tomonori --- rust/helpers/helpers.c | 1 + rust/helpers/kernel.c | 13 ++++++ rust/kernel/error.rs | 1 + rust/kernel/io.rs | 5 +++ rust/kernel/io/poll.rs | 95 ++++++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + rust/kernel/processor.rs | 13 ++++++ 7 files changed, 130 insertions(+) create mode 100644 rust/helpers/kernel.c create mode 100644 rust/kernel/io.rs create mode 100644 rust/kernel/io/poll.rs create mode 100644 rust/kernel/processor.rs diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c index c274546bcf78..f9569ff1717e 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -12,6 +12,7 @@ #include "build_assert.c" #include "build_bug.c" #include "err.c" +#include "kernel.c" #include "kunit.c" #include "mutex.c" #include "page.c" diff --git a/rust/helpers/kernel.c b/rust/helpers/kernel.c new file mode 100644 index 000000000000..da847059260b --- /dev/null +++ b/rust/helpers/kernel.c @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +void rust_helper_cpu_relax(void) +{ + cpu_relax(); +} + +void rust_helper___might_sleep(const char *file, int line) +{ + __might_sleep(file, line); +} diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 6f1587a2524e..d571b9587ed6 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -58,6 +58,7 @@ macro_rules! declare_err { declare_err!(EPIPE, "Broken pipe."); declare_err!(EDOM, "Math argument out of domain of func."); declare_err!(ERANGE, "Math result not representable."); + declare_err!(ETIMEDOUT, "Connection timed out."); declare_err!(ERESTARTSYS, "Restart the system call."); declare_err!(ERESTARTNOINTR, "System call was interrupted by a signal = and will be restarted."); declare_err!(ERESTARTNOHAND, "Restart if no handler."); diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs new file mode 100644 index 000000000000..033f3c4e4adf --- /dev/null +++ b/rust/kernel/io.rs @@ -0,0 +1,5 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Input and Output. + +pub mod poll; diff --git a/rust/kernel/io/poll.rs b/rust/kernel/io/poll.rs new file mode 100644 index 000000000000..a8caa08f86f2 --- /dev/null +++ b/rust/kernel/io/poll.rs @@ -0,0 +1,95 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! IO polling. +//! +//! C header: [`include/linux/iopoll.h`](srctree/include/linux/iopoll.h). + +use crate::{ + error::{code::*, Result}, + processor::cpu_relax, + time::{delay::fsleep, Delta, Instant}, +}; + +/// Polls periodically until a condition is met or a timeout is reached. +/// +/// Public but hidden since it should only be used from public macros. +#[doc(hidden)] +pub fn read_poll_timeout( + mut op: Op, + cond: Cond, + sleep_delta: Delta, + timeout_delta: Delta, +) -> Result +where + Op: FnMut() -> Result, + Cond: Fn(T) -> bool, +{ + let start =3D Instant::now(); + let sleep =3D !sleep_delta.is_zero(); + let timeout =3D !timeout_delta.is_zero(); + + let val =3D loop { + let val =3D op()?; + if cond(val) { + // Unlike the C version, we immediately return. + // We know a condition is met so we don't need to check again. + return Ok(val); + } + if timeout && start.elapsed() > timeout_delta { + // Should we return Err(ETIMEDOUT) here instead of call op() a= gain + // without a sleep between? But we follow the C version. op() = could + // take some time so might be worth checking again. + break op()?; + } + if sleep { + fsleep(sleep_delta); + } + // fsleep() could be busy-wait loop so we always call cpu_relax(). + cpu_relax(); + }; + + if cond(val) { + Ok(val) + } else { + Err(ETIMEDOUT) + } +} + +/// Print debug information if it's called inside atomic sections. +/// +/// Equivalent to the kernel's [`__might_sleep`]. +#[macro_export] +macro_rules! __might_sleep { + () =3D> { + #[cfg(CONFIG_DEBUG_ATOMIC_SLEEP)] + // SAFETY: FFI call. + unsafe { + $crate::bindings::__might_sleep( + c_str!(::core::file!()).as_char_ptr(), + ::core::line!() as i32, + ) + } + }; +} + +/// Polls periodically until a condition is met or a timeout is reached. +/// +/// `op` is called repeatedly until `cond` returns `true` or the timeout is +/// reached. The return value of `op` is passed to `cond`. +/// +/// `sleep_delta` is the duration to sleep between calls to `op`. +/// If `sleep_delta` is less than one microsecond, the function will busy-= wait. +/// +/// `timeout_delta` is the maximum time to wait for `cond` to return `true= `. +/// +/// This macro can only be used in a nonatomic context. +#[macro_export] +macro_rules! readx_poll_timeout { + ($op:expr, $cond:expr, $sleep_delta:expr, $timeout_delta:expr) =3D> {{ + if !$sleep_delta.is_zero() { + $crate::__might_sleep!(); + } + + $crate::io::poll::read_poll_timeout($op, $cond, $sleep_delta, $tim= eout_delta) + }}; +} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 22a3bfa5a9e9..b775fd1c9be0 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -35,6 +35,7 @@ #[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)] pub mod firmware; pub mod init; +pub mod io; pub mod ioctl; #[cfg(CONFIG_KUNIT)] pub mod kunit; @@ -44,6 +45,7 @@ pub mod page; pub mod prelude; pub mod print; +pub mod processor; pub mod sizes; pub mod rbtree; mod static_assert; diff --git a/rust/kernel/processor.rs b/rust/kernel/processor.rs new file mode 100644 index 000000000000..eeeff4be84fa --- /dev/null +++ b/rust/kernel/processor.rs @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Processor related primitives. +//! +//! C header: [`include/linux/processor.h`](srctree/include/linux/processo= r.h). + +/// Lower CPU power consumption or yield to a hyperthreaded twin processor. +/// +/// It also happens to serve as a compiler barrier. +pub fn cpu_relax() { + // SAFETY: FFI call. + unsafe { bindings::cpu_relax() } +} --=20 2.43.0 From nobody Sun Nov 24 23:56:08 2024 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (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 3D00413CFA1; Fri, 1 Nov 2024 01:02:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730422968; cv=none; b=jQXfFCvoI9Ym4LOtrFe7k9ENzh9a76CCUJV0MCixUy7HgNUn27u87qO2O/5EKQD1swgCLrjzSUL8GQ3cj6t5K2CAB4lDKCrquZkox5Z4Rg/Kf8hToKf+cITMEuDATzEOHsTijaisBIAi+GjU+ovQu0UEHsFYxX9St0MWssK5Qfs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730422968; c=relaxed/simple; bh=jB92JpeDW2yhP2VRKGudKbbfDREsNE6Gpy5FBMVK5aM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JbaSPEK61hHqqRkZIfrvYBpEWbdYbkdZdIOWYXHb58WjLi4O289s/PiTezCltprmEah+ENvCcrZDk6PEEtIZH6a9E67Cs0+OyhFas+c9CeFHVYfaoBP/+lVvzj54BwYW5RuK2nq3zxWvri/Y6X4ZELViAdjlJuGbvIH6TT8H7+I= 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=K6RcZ5PQ; arc=none smtp.client-ip=209.85.210.172 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="K6RcZ5PQ" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-720b2d8bb8dso1167682b3a.1; Thu, 31 Oct 2024 18:02:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730422962; x=1731027762; 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=mK2RJ1Xug5oMAzwhSCqVHPpqDu7p/3YqJqaxy25r6IM=; b=K6RcZ5PQ/jHuVET2LPDWRT2gGOMPoZJ3qgUoBgvFr5oV7CIMS/r7SDhWAGALecqJXE uHIg/rdboZmHcPGHbZdnY+Sv4wFRG90tUv+HHpxJd83gr82jEaHcpkS4RKkFm+tnuNCo 9NcUL37CXyiAiqF1h7uQunn9XcAiZyVyMFIqQ09Lr+I9DvO7/yQ8fnNoOXxgUqRQDaA/ u1bw65umUQ0CoPf0csCpvTO2Wxw8u1OyytzRaY4PZTx62GHVBfgiNJmBTRhJl0VzL8wC 4ggImIqFGbMRwqbeCI62tNhRZ+qeJVED9eEISmprzUrAKwOPJWymAOZLdX/AJ08eeX3n 5+PA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730422962; x=1731027762; 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=mK2RJ1Xug5oMAzwhSCqVHPpqDu7p/3YqJqaxy25r6IM=; b=ncht1IYR0gmTY+/Q2S1H48UqXVOTUXlwnHkXp1cZSmrwqaK0RcFJGr/j3UwbgwlYpP 4uw6UyAjhe9POED8FUB/A5GMZEcDcXxf9znaeYHdt6jILuv9dRSo3u5MuAdBo4nkH2gy /0b1g6BonFyLCq5c4ov/jdFno6KiS6GeofuQ18mQjnS6gpGLy3d4RQpHb5WBVwoQR6Zv Ol/L7M9pVi6sZMiX7ycwj0l+pBTxSWbRqf5SkGfsiEqXXlhJoaWQU5ebZc615y7hByIc 0qjpr+bbmQxavUQt4cWk+n+EId2/9cB+DNOvzJyloW29Wuhd3XRe59O6PraD5kIEli1O +NXA== X-Forwarded-Encrypted: i=1; AJvYcCVbYywT/5Drc6KJs4nr+b3TdQrs3bX2tuHhYpFzXHJ//PXIOL/9feYfPDh+kmVv1kY2JhSXs/KvkTf2Lvs=@vger.kernel.org, AJvYcCWwy0HpAgae6Ff52/tBqJbv9o592t8fBR0VN2G3sVfEHXfY+LdjDwcO0kHuHlmdGxkjuCF3lQKXue2wSorxdCM=@vger.kernel.org X-Gm-Message-State: AOJu0Yw+JezzUUcCeRSrU8yDK1PFhuQgN0oe7yjdJv9tUlc05L7unOW3 24llqtl4BHzUbW8xQtHiwd15b8rPKcq7G1hBdM5Ad5J1dMYf4J9S X-Google-Smtp-Source: AGHT+IHkyE9UpBFyYv5S5Va5DuUIZq6OF/wcFyP2zyu3sOmg17gD9KqyIrhK+dK32peNyOFGyFxLSQ== X-Received: by 2002:a05:6a21:58d:b0:1d6:e6b1:120f with SMTP id adf61e73a8af0-1d9a83d0b04mr30096430637.11.1730422962531; Thu, 31 Oct 2024 18:02:42 -0700 (PDT) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-720bc1ea6a1sm1743403b3a.74.2024.10.31.18.02.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Oct 2024 18:02:42 -0700 (PDT) From: FUJITA Tomonori To: anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, jstultz@google.com, sboyd@kernel.org, linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org, rust-for-linux@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, arnd@arndb.de Subject: [PATCH v5 7/7] net: phy: qt2025: Wait until PHY becomes ready Date: Fri, 1 Nov 2024 10:01:21 +0900 Message-ID: <20241101010121.69221-8-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241101010121.69221-1-fujita.tomonori@gmail.com> References: <20241101010121.69221-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" Wait until a PHY becomes ready in the probe callback by using readx_poll_timeout function. Signed-off-by: FUJITA Tomonori --- drivers/net/phy/qt2025.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/drivers/net/phy/qt2025.rs b/drivers/net/phy/qt2025.rs index 28d8981f410b..f7480c19d4cc 100644 --- a/drivers/net/phy/qt2025.rs +++ b/drivers/net/phy/qt2025.rs @@ -18,7 +18,9 @@ DeviceId, Driver, }; use kernel::prelude::*; +use kernel::readx_poll_timeout; use kernel::sizes::{SZ_16K, SZ_8K}; +use kernel::time::Delta; =20 kernel::module_phy_driver! { drivers: [PhyQT2025], @@ -93,7 +95,13 @@ fn probe(dev: &mut phy::Device) -> Result<()> { // The micro-controller will start running from SRAM. dev.write(C45::new(Mmd::PCS, 0xe854), 0x0040)?; =20 - // TODO: sleep here until the hw becomes ready. + readx_poll_timeout!( + || dev.read(C45::new(Mmd::PCS, 0xd7fd)), + |val| val !=3D 0x00 && val !=3D 0x10, + Delta::from_millis(50), + Delta::from_secs(3) + )?; + Ok(()) } =20 --=20 2.43.0