From patchwork Thu Nov 14 07:02:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13874632 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 F01CD1E8857; Thu, 14 Nov 2024 07:06:47 +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=1731568009; cv=none; b=SRYjsp1YpW0eIOpKRv20AzEL7O9uFVq2oiDM2oN1FdBVRH8nnQHO5VwGp6qzyaaTPN0v9V4whctuyxjY1NBMV8JFXjesSVN7JLryhKh9+irg152yilK4Iig92YnwKrTcMyYfJbliY/1SsOlb+oJkzwPr715RV1PTp19kyOFNVkY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731568009; c=relaxed/simple; bh=ty5/y7Xtb2hwXPXaqKl+csNiJiUlRX6POVGSNuNzKgI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=n7Bc/0uRalEvVaE3tv/fv3qUKkfz4MZzfsIkFysliPZzsKow1qbjTtAkfMKdu019oYklJHyogEHaLO0maKliA5zdNcOQqxo+jdCUmPXldua5SkESNCBnAMRpD/S5ChgNy3CqY4pNtn9/oLcdAfmNioKJXa0JW2WjISSVL8BNVJM= 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=bw9PKj4k; 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="bw9PKj4k" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-20cd76c513cso2083775ad.3; Wed, 13 Nov 2024 23:06:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731568007; x=1732172807; 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=bw9PKj4kE6MpNCOKFXy668V63xDMrXlDYx9ocx9/MM9wlkWTS/rp4dEBRpv81Qt6Js F6/+cRshbasnch09vodxWb5B4b4JP9OsI/zj4vkYj/FmkXkyQbCWqa2olciQnvDPfkfj xl3g9o08FNyv0LSkPVQzAB3kfq5S4cS0I0dfWKBXFkWEtCpD+09wYVeboKdweQ1K4RSs TcgkMKo9pO28RgOIYdiSOyRHvD4klr+Ac3VF7rWSUQp0ENcqRWByhEl1oz1/jmo5tp0R GmnxLKwC4ZDy4CZjsvWrPDaP59sBfCpJjTuDfuvx41cgZ5WjU/JrrE1PQ+hGmZjfIh/6 XiZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731568007; x=1732172807; 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=DbyJeI68opn/yQZMrBKICV314ZvQRUNFrYtBWCxzBicEcEZzROztbaw+POetx2FOnS FLuvm/uqFo8KPx3u6uTZFKCB7fN11YBiW5EQGWPSqgcfGKegCoAh4KDyMRJOZvlmL39r rLtFTTNjG895kTDvYG3+F0VarQU7SzFmFhVQx/dhvQ0rCfr9wDbydPSUiwVqX9DMaZcs /KH670iLdvpicttCZAgMPm2HdA+sDmalS027vLr5HetdDU6zOe67DasjAOAH3TmWzK8s 5rMQqeHIOYmwiyLnE9JG4iF6JXTFjj/gp252dPH7S9HEgpt0lk80Iyh0cmXtEAi0m7Jz OvCA== X-Forwarded-Encrypted: i=1; AJvYcCWglPEj3jPBOSk44S1Pr+McRVYbVKW0Iz8zcXeBOfMgc4opgqJ4wotL22T7w97+Dpj2RyBXzBuDxSJUs9VSPg==@vger.kernel.org X-Gm-Message-State: AOJu0YytvUrvYE0/YUIT/FQ2pzyvnCPpmuUfBBG0PrXwZLlWn6cbMG8F cqz8JUtAL3dofUz1rZg1L7t1FPWZ2YZmH7ETXPQdV0mY7sIt6EGxuqH6nQh/ X-Google-Smtp-Source: AGHT+IGq/HvLxWaE0m8L7MssJOLCP6/da8B3DdYgcAMAuK1g2Gb9zfKbxCDwloOXe60p6VYw6d+E5g== X-Received: by 2002:a17:902:fc46:b0:20b:96b6:9fc2 with SMTP id d9443c01a7336-211ab90a5b4mr130250605ad.10.1731568006900; Wed, 13 Nov 2024 23:06:46 -0800 (PST) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-211c7d24a00sm4260315ad.244.2024.11.13.23.06.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Nov 2024 23:06:46 -0800 (PST) From: FUJITA Tomonori To: 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, 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 Subject: [PATCH v6 1/7] rust: time: Add PartialEq/Eq/PartialOrd/Ord trait to Ktime Date: Thu, 14 Nov 2024 16:02:28 +0900 Message-ID: <20241114070234.116329-2-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241114070234.116329-1-fujita.tomonori@gmail.com> References: <20241114070234.116329-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 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 { /// 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 Thu Nov 14 07:02:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13874633 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.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 5FDA81EBA19; Thu, 14 Nov 2024 07:06:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731568014; cv=none; b=Q2IPZITqoALSRWt+eMwCa7DPKr9cRR8gv2xDqQ3JS3dikUzNsXKKVbuCKyYivipCslwk9JmOq16r5iYpRp4eP8OSa4FIXuJzgDraG8XEKrf14kKHR7itZR/+hUt3KnY9gfpkwvIgwvwlp4yq7VmzuptAk84DCPeeIE6Gq5sMTy8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731568014; c=relaxed/simple; bh=9FuytnUSrj6P+R6JWfz0Tu5tze1hGWVWVAysMwNaFVY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sO/P7kNzlxQNgqh/TRTLn8dC+C6wCfo+CLPqW1iPLMP8/9JMHtMzB1FbHFafusH9+GTtjJ99Jaq+865J0HxRFv1bdgSw20g3Ie9HQstNX2GYZoOU0XlUUzrxOL097vjE2n9QDUpfYdxv4DWoVdpBK00bOlRG9e7uhWfMPWDOkHk= 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=agGCwBwq; arc=none smtp.client-ip=209.85.214.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="agGCwBwq" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-20ca388d242so2273515ad.2; Wed, 13 Nov 2024 23:06:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731568012; x=1732172812; 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=rtyoKIARQ8seXM8o5IIvlTX2rizP8oksd7bJLCrQQXo=; b=agGCwBwqFi4RPJ33veY2P1HJjxTbItp0UeqE0Nstv72PzlRAkg0ehkOQzkyQiHy3Cn SlnBx6gr+ZueuTJdwtxSR/1e8T6/tCIvCGXRswhFVaqYsx047H6kaulkOEk05rdn7iMd HvqCgAHqMgG/vfKjpYZtUD0BDwyKPwdPEnqCzupbKiSk6hOSb2tSr2MHUOWXEJSAohSx Zs4orrgcNdRsDxiUNP5hWmjCECR2OClVqCjkMjA1LK1E/cmqEH0A168FkO1GF9KF9zwR maVXH6gWwtpmOE3X5We9cbY1Rizb/qz72RZP/3Ad3Sv6APx+WmrUCmPxZ2YO8rtpGcf3 Ncog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731568012; x=1732172812; 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=rtyoKIARQ8seXM8o5IIvlTX2rizP8oksd7bJLCrQQXo=; b=Hi2COiGsfIP5rMUdqZXPtjGczOmLAifK3BS7Knk2urvz6YBAYLOQkMj4HjIqpHP2Xs VUH2o2TIuV1nWxCbwqBQLIsjTLEY/frczV8JhDkJTzKQJ76tE64RTonuv7YrQ/uDdgoq bIM1Dxp6WWW1QXHZsh2rYSFqxjajwC7X67KIBHA2YbiI8RmLAklkr8bXKHd0iwwoyu+q /Tl/50ARM3adXrXB9g9LJLE3t0ZnzaiEtQiJp5KadoQMj3iObWjlvQ9ITTEr/l1jV//M co8OW+GgKCWZFd+aBUqUI7k74M8lqeHNYqy4+0fAx+oGLWh60ZNxv/cQKOHJcy6FMErF cgog== X-Forwarded-Encrypted: i=1; AJvYcCXzWB/08UtToYvA+0Mf1VsIX7mhKaGW1umZdxk4EC92WM2MzqUxlSh7i04UirpFKKv5yuRhhJORz88gVa9tAQ==@vger.kernel.org X-Gm-Message-State: AOJu0YyhiXCuraq2y+f+X9DGxt6zh2STQxULC/AfoOrmWpskhnhufZ7v uXgRl2MdfsiF6LTvnKZGnbZxRLIh5Mo7oNmxTPx5J7UIVK5eFjCdc2xc86Gu X-Google-Smtp-Source: AGHT+IHLEu9jgn7WM091IKVmw7rzozNDxfAJ44mO9V+ZimaQMFmr8Az19ay0Ko5lPTIYn1NJUv0LQg== X-Received: by 2002:a17:902:e5c9:b0:20c:ac9a:d751 with SMTP id d9443c01a7336-211c501cc43mr17208895ad.32.1731568012440; Wed, 13 Nov 2024 23:06:52 -0800 (PST) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-211c7d24a00sm4260315ad.244.2024.11.13.23.06.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Nov 2024 23:06:52 -0800 (PST) From: FUJITA Tomonori To: 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, 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 Subject: [PATCH v6 2/7] rust: time: Introduce Delta type Date: Thu, 14 Nov 2024 16:02:29 +0900 Message-ID: <20241114070234.116329-3-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241114070234.116329-1-fujita.tomonori@gmail.com> References: <20241114070234.116329-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 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 | 63 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 4a7c6037c256..1df6ab32636d 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). +/// 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 = core::ffi::c_ulong; @@ -81,3 +87,60 @@ 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() == 0 + } + + /// Return `true` if the `Detla` 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 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 + } +} From patchwork Thu Nov 14 07:02:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13874634 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 246C21EE018; Thu, 14 Nov 2024 07:06:58 +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=1731568020; cv=none; b=cvh0Xnpxyn4fktJcCd2py2W6iiDcnpN5NkQIdMSWpn1rpwcrz6ntsvg5nMowbAra1LX1etbihBYlAIK3qPTeVi4KNkS5JJ4VRoWODD2hDv7DXQdcIrx+sCgYm4bp5mKXXDWNPv7HiDPUtrXhKiwWTHHlNcst/ICl+zTByRd5lXk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731568020; c=relaxed/simple; bh=tZkOshAWSGactm0EQv1YdTABOHlR5Eq+JZM9H1xmWhM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=D6TdXvLkeKYVv7orJkgZlP212ko2dECK4JbDVZ4t5onj95081ETan5OAIO7QGIf2aanbfApezps2+sjeSZUyhNFexXb8F7I/G49BHLl8yEHvKARqxAFITPpt5IGoiBuGPkvDXYg4WHzMobI2RjbW18k5J1HclRqmXaN7jkV3xuE= 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=GHgNlbkb; 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="GHgNlbkb" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-21145812538so2010085ad.0; Wed, 13 Nov 2024 23:06:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731568018; x=1732172818; 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=NKV96Ua35gYpKAf6j7w0Qp3ANNf3E5xmw9JmIBsPCj0=; b=GHgNlbkbC0M/CTgv87KTeXaYoNfMpYxuW6Omz/p2BW75BXrLcToNkxmYjnX/MApqMP 4hH/kK9jDIpoc5961s2cFINmG5QyVyHRnQ3HrSUIknMlgu8RcQ5nKW4hNGGf5g66ekRp QCCYXHr+N8nV6Ronks8b5PEm5NubXQI3QtFjE3lvy4+0BTAWADaBjPxy1ve3UeZi4iz4 7NCPhFkB+azo6eNPmAA4SJMBK+bP+3V3FOTwZknFiek1zYIP5lAYBFqOGapi4elSmZh5 Y0LG7ZinHqMEyw3qTbg9VbM/y9Q+A8XpyEgoESdXMwcMqI9TGrb+weS6X1Fnr2r2lZKq xILg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731568018; x=1732172818; 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=NKV96Ua35gYpKAf6j7w0Qp3ANNf3E5xmw9JmIBsPCj0=; b=AKS5RyvEkpxfkveUSZ0W+FxGxM48inZcm+vHPkZ+b6mIB1F9fsc6/+6xCx3UWf8/9M h4ZwWbC95/0jyYnPNJp6cNyAzPNjSUzvEV6oBCO2uKWIf9VjnGNpD1VZN/HhFz1Z/sSN nNZBSEQP11x/hbSQJf0Ou241Fjv43KmN14rs1SD/QLtugGlIvVs7C3QyxJadnAsA6+F+ MxdXbmXjr4N1hJaNKiZTfnXGOLpXfqXLzcPiS1umMfP5PvUz7UUbstQ/xlduFIOzNfHi 8aZGrVM24JAXpaThOjigqRnW3E0kjAet1vW2ZXSOLBAs/Xgi/e+PEHJ/JCFs9MtSZCY7 kYsA== X-Forwarded-Encrypted: i=1; AJvYcCVulVIYjnexNaa4rF7DJzdcvvzNtzJC3Spm1X9KvXpARCNdKVUOf+ItlIW+P+WTthi92ap/AcUokmoe5F7U5w==@vger.kernel.org X-Gm-Message-State: AOJu0YxJoGpWZO9VBD8ptspeNERjVrx9XnRg9S7zvTYYiSkk2/ZBRWrc xjZj/+CkRNi5/NGuDPaOxyY7aVBKYU0+LIDH3UI0kTfCYQspOHbO+PbDy1w6 X-Google-Smtp-Source: AGHT+IGKcSsT5X+tYlpniPsTidkG7q/kgD54IMaWvC1NkgcDlj4G6Tjs6npWNur7rSbaHiezyTdREA== X-Received: by 2002:a17:902:d508:b0:210:f6ba:a8c9 with SMTP id d9443c01a7336-21183c8cc59mr323740565ad.17.1731568018175; Wed, 13 Nov 2024 23:06:58 -0800 (PST) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-211c7d24a00sm4260315ad.244.2024.11.13.23.06.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Nov 2024 23:06:57 -0800 (PST) From: FUJITA Tomonori To: 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, 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, Boqun Feng Subject: [PATCH v6 3/7] rust: time: Introduce Instant type Date: Thu, 14 Nov 2024 16:02:30 +0900 Message-ID: <20241114070234.116329-4-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241114070234.116329-1-fujita.tomonori@gmail.com> References: <20241114070234.116329-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 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 1df6ab32636d..ae0d3e3ff475 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) } } -/// 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, } -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 } } /// 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 context. Self::from_raw(unsafe { bindings::ktime_get() }) } - /// 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 } } -/// 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; + // 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, } } } From patchwork Thu Nov 14 07:02:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13874635 Received: from mail-pl1-f176.google.com (mail-pl1-f176.google.com [209.85.214.176]) (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 D8B651F708D; Thu, 14 Nov 2024 07:07:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731568026; cv=none; b=Ef0ke0l3EM7qvs+WuhS5oOk58IGpUlY5W/AJx5JDopCG8Ay54U3GKaQrk0oXxdkgOb2M2T0ETimkMWOmKq7IJLc+qXLcR0P20bpz8G3uOnG67dpQW4iayTfiVURm9jyOnNkSPIUalt/EuIYjJnTGImnW7mGAmpXnC8h+/zkqrCE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731568026; c=relaxed/simple; bh=VtU5QbtZ8Kc/FmsxjH+iKgugsyaUqp7xsciNvoFPadc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=avvtmnyGfzZ18ioaTNjVyFgFxNM83xVmPaELqLRxGjIxG5iAX5XxSTL6Fez8BVg/T5Uqdu2HgCJADn7W0MG3MrQp9U6KKloApPBBo1kmlzKcIBbK14Xthu4wTG+YZ8XLtJ56KbWIEXC3C+/SPBT1bt6EIxR0eKE9hVWL232xO1M= 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=QGI7n/3X; arc=none smtp.client-ip=209.85.214.176 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="QGI7n/3X" Received: by mail-pl1-f176.google.com with SMTP id d9443c01a7336-20e576dbc42so2586425ad.0; Wed, 13 Nov 2024 23:07:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731568024; x=1732172824; 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=5qXPUrrsm2D4XazfdnD4Km80VNViJ3aj9orHJknL1YU=; b=QGI7n/3XR0PhbzTUu3fmydMQt/zhFcHF12nZzPQHE/QzJrVNYUEyZsS9WZInDv3ekf 7w+y7mKLhGjJhQzrRyZKmafm3uJuQwFKLCSMT0KWbTU6Pr+K5R/F7lRms7mJTjTMrQKf QB89lz0gEE+kz4v6DsLpq/kn50aRNNqQRI5M3LIgD6WQscvOP59yWo8NwYtZMQdsfFGp C0YBN1BtbkSP9M0EwAFNyiTw4Z9HH5DmoPTJ2t52Ge4J0S9MCOroMdb1HjPtbPRXeIox YcANQJKdoEOKq++kYF28y2GnsrM6k6S4pZVYR7Nv0AnopiKzwCMQi0nzAU65FQggyocg eBnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731568024; x=1732172824; 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=5qXPUrrsm2D4XazfdnD4Km80VNViJ3aj9orHJknL1YU=; b=FWBEQtjEUY6wP8RnZrtZEcsa9CBHxfPyo8fthJOP8pSYM+DEUHKRHAirNo5BMq1cRm puzrXGiBlgYcon9X9rAD9Pyg+W/ZgbBYpQG5JocE/syVssAH9U5hxMH1EPxaB2OEqxaT Vkf9pm5N/36kQUSkmTIhfOrmLkiqK4e4sP5PMDUJTWRci0Bwy8lST+fnydFY0cv6d1/b B2WTuANguwrfr1Qw8TsnK99LDFiclziZVglvPXmz8f8dB+jUMOHwouobsSINB/a/MGSZ ChuH+RQktrYq7MefowFnzggj/j7kvUdAoWO0rhgoFIDyhl8mbZb4x4ZTeg/298atgr76 cUCA== X-Forwarded-Encrypted: i=1; AJvYcCUtE/VlYUa8oJW0LlJTf/ku5DV3l2gSSflmrLs0oCq5NbKS/Pu+oOsViUQyBwiiLvxzhG4rvkRehadcTbsw+Q==@vger.kernel.org X-Gm-Message-State: AOJu0YxiRLrr4+XnMz4egXSnP5aU/oW5YCGRYLRs7CF4D5ERvfgFwQrB 57oqjfN3dB5GxAXdA3fP4y+Zzw0MeUI29A/6TDa6BSmX3D2HHyYsiI4TLYzF X-Google-Smtp-Source: AGHT+IG3F/bclIC4fPNbIRxhxpkbWb1GslUeD7u/O1retYuA4k9nXmBoBd422RcNq8dURjFBzkELEw== X-Received: by 2002:a17:902:d508:b0:20c:e6e4:9d9f with SMTP id d9443c01a7336-211b6698a53mr72386475ad.40.1731568023709; Wed, 13 Nov 2024 23:07:03 -0800 (PST) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-211c7d24a00sm4260315ad.244.2024.11.13.23.06.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Nov 2024 23:07:03 -0800 (PST) From: FUJITA Tomonori To: 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, 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 Subject: [PATCH v6 4/7] rust: time: Add wrapper for fsleep function Date: Thu, 14 Nov 2024 16:02:31 +0900 Message-ID: <20241114070234.116329-5-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241114070234.116329-1-fujita.tomonori@gmail.com> References: <20241114070234.116329-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] 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 ae0d3e3ff475..cfaac41fa6e2 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -2,12 +2,14 @@ //! 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). +pub mod delay; + /// The number of nanoseconds per microsecond. pub const NSEC_PER_USEC: i64 = bindings::NSEC_PER_USEC as i64; diff --git a/rust/kernel/time/delay.rs b/rust/kernel/time/delay.rs new file mode 100644 index 000000000000..e384a7f7eec4 --- /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 super::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` microseconds. +/// If a value outside the range is given, the function will sleep +/// for `u32::MAX / 2` microseconds (= ~2147 seconds or ~36 minutes) 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 architectures. + // Considering that fsleep rounds up the duration to the nearest millisecond, + // set the maximum value to u32::MAX / 2 microseconds. + const MAX_DURATION: Delta = Delta::from_micros(u32::MAX as i64 >> 1); + + let duration = if delta > MAX_DURATION || delta.is_negative() { + // 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) + } +} From patchwork Thu Nov 14 07:02:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13874636 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.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 6DE7D1F76B6; Thu, 14 Nov 2024 07:07:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731568031; cv=none; b=ruA+vEgJZ/ZYwsHnokkjJpxWD9Y+aQv9XDjC/bJJq7sm6u+Vy1oKxD/31ttcstz/tLijYVgrSB+MsbfRgEYg9ZXDUF7LncN0kiAzs3STuFM6uMMnqiZryBIh4IiWJxkRIU3JcH16Sx+A/zUQpPolupx3o/SfJAVPcCRe4Ck7NS4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731568031; c=relaxed/simple; bh=coO/62PFielIrZRTO+usYpn2tan0xME9EFYmpjouIPU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=J///Hye/g3riqd0hk+TbRezLE+bb0d6Jih07Um5StAKj+yGJQlESzp2tGGEB4cRSTMCfMzjJzOEb7b8UFa4kpFLC3E56zQ2nh92795sn/8EftOv9cfFDqvrXsLLQJDQxjaJvGaHVnaAgL9YfdyCL3pS2wbeD2fMRP2T/40Qk8Jw= 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=Y4Mkcfra; arc=none smtp.client-ip=209.85.214.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="Y4Mkcfra" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-20c714cd9c8so2613195ad.0; Wed, 13 Nov 2024 23:07:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731568029; x=1732172829; 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=fDf3qaNZuFjpFW/AdnAs8K9jS8fTv/EUiczvsLPk+9k=; b=Y4MkcfrafM2JzrLrMNJNnao5a0Ebmz904NLT8cFOy9TtcfAx0T7fruuEKrOjayULVu 5z3+4m0dvHcaye5+TnxuXTX1MeBhiHTTv0hrePZNgBRGFCsJiGVFG033Ux/NL+7uOULX Y9e17E2jCIHIcqfCHUCUUizE20WHsYEBNWakLk/Brg3pc1iQU0Id5yEROJTLkfwV1UkE j+7MU6MvVgYHMdOE1uu/yeSxE5dXyTGzb8GoEA9r0iYgCnbDB4sG4oIwfKrPTYm429Fs H7jk0tZ6LjaTTWSsT05WzSzN/XewnCbEyxbOD2ulAevHyrykMQkORre5HwB68QR3X6te oJNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731568029; x=1732172829; 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=fDf3qaNZuFjpFW/AdnAs8K9jS8fTv/EUiczvsLPk+9k=; b=PgvLtmKThBsY0l9Bn90UG1ACyVR6kv7XDT9jPzslwI3Sjrh4c4d8XRycrbzfN5SZnH d+VtHJhldJSdRSfj/Lf8Z1KX/g5mGwiYOI0zhNr6I5S/N4Qw8w3p+CUJLxm50ieJ99OB 4mM9/0RTNJy1dgIIAhOb/Ox/VjOZdZU5HWYm5ByeJog4GEIKxFoEKTk8haokHISQ27Dj 6IgovlNuqaWZgwXlIVo70ILniEp4AtW+KQezZK/gJ3LZEHq+V+BDHwJPkdCuMeYYd6hb /lkg5g2ynX7fby7DCxgVpRLb4EfcCzQXNE8voC6cpSk6TNAFCBTn8lvyUjvDYyrBAVYk RKEQ== X-Forwarded-Encrypted: i=1; AJvYcCX/l/+gE0YURVhZjHBTJfw63eqg2LWdAoipqs+7bp8gMFOZVGO9ujcZ6Dwx6kmftf/zXzPgZO/1clOjGhbUaw==@vger.kernel.org X-Gm-Message-State: AOJu0Yy65pgIBr1dlAnNaj0Pq4Jve7xVNHbKTQI/V3hnfPTscJH3dPUT NNd2LeFf1zVbv8TECHOA9oUrNbnnodVgrkuOZ//TyE0E5CHm7uEOHNeWAOg9 X-Google-Smtp-Source: AGHT+IHXrERLGZfkPf94/cBZFjn1mjI79ApKerufewROWOtHV8p5cqTsreZmlE6wcxcLAwgsWI4CcQ== X-Received: by 2002:a17:902:e892:b0:20c:637e:b28 with SMTP id d9443c01a7336-211c5092094mr11987835ad.39.1731568029285; Wed, 13 Nov 2024 23:07:09 -0800 (PST) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-211c7d24a00sm4260315ad.244.2024.11.13.23.07.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Nov 2024 23:07:08 -0800 (PST) From: FUJITA Tomonori To: 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, 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 Subject: [PATCH v6 5/7] MAINTAINERS: rust: Add TIMEKEEPING and TIMER abstractions Date: Thu, 14 Nov 2024 16:02:32 +0900 Message-ID: <20241114070234.116329-6-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241114070234.116329-1-fujita.tomonori@gmail.com> References: <20241114070234.116329-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 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 3a24287712f1..9a7899f3685d 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/ 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/ TIPC NETWORK LAYER From patchwork Thu Nov 14 07:02:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13874637 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (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 484B21F470B; Thu, 14 Nov 2024 07:07:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731568038; cv=none; b=L/Gt4g905tBtP9M+UyfWP+4ByD7bdlBRhoqF5oUPGBSC+fTumTG2OWXMaxOHUYRV0EhiCc+QWu9RSjFTzTykmdv3HFeU4MmHvwVN2RTHBkzhRvrQSooNNXa4tv/BPtTE9F2L8LKadc8zcEIcN7anZsqHQ8YpEFCVjSXcCQlKR+o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731568038; c=relaxed/simple; bh=o13rF7xXb2nrxZLvzkhhe0RZrCFicG3sT7cryBqCQeU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=i1B9iYNuYuo/RL287HAKKGxvLqzWhRtkDZq+0/MBjnlfUpAOF9p8sl7Hl0fWBHHQiakjejJYEm7ZNQSXQU2sPJeZdvHAiPX9zGvpC25SIzW211I1Ob3YhJ+QLjHOpQfS4q0XnzAMM+79pANvXshJhMd02FQvUYHZxgPa3a68lZ0= 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=FjhbneQQ; arc=none smtp.client-ip=209.85.214.175 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="FjhbneQQ" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-2113da91b53so1698815ad.3; Wed, 13 Nov 2024 23:07:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731568035; x=1732172835; 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=izx/1TmaWdZTGGVem4OBbdFm7p18B6BW1T/W5pkcSJM=; b=FjhbneQQf5Ib+j+T6y96pUWVPjAMUVFMQ2EaSNO6KReuR5BK4TjLkftvFC6Wgkxikl GZ6V7UZTtSts4DUWFZd3N62lhFr/raeIXsZ8qnoa2c/r6Mfcgaa5C5s9ZKTJth7iE3lc 8hv26tpTifycoyYa7HO4O8Xt5Yai/oqOK8xuIG5IOfcgD8/xvVf3wXgvfIZX7TyO2qn9 Mhpqla+xEP8P0OleIrNe8jUA9wU4WK/XF/oBnjG0krwfw0QaHBHRKlfZEdQRYEP+x46Q 07j18CCiHsR5sAIzqfKPuquAqqv3mXNCcbVa2n1MC+g4KLP9o0ymvm9U51yp5RmXGVSp dsEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731568035; x=1732172835; 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=izx/1TmaWdZTGGVem4OBbdFm7p18B6BW1T/W5pkcSJM=; b=ZTR0AuUHihph5eP6BAi7c8b+USJs32eUqXF9L4n2x8bMi3MvCX7hHPrf4FsLdFxVKG VmZ4GsBFWm1wmzJz2ljkjskX5xMGHvw+PoV/6muUndsoGlcD9QaFfrQk0hJj0Whgu+Ve +owuaOSTHiai8zKquq4LR8lUrB3wY+8Z2/AF3CbJl0+7gyr9v9D/ZRrNVddLQ6y9A9FQ ho3V1L+N3N8caNSRtYocRp1ybgQiNTSvfbgJcHR3Cx5bp3FTcvGsKElAYZo9z+zD7ITE IgQJFjWvP5sCZh6H1ey6QAA4cWJnronAscIXLAw3+bEe75vAXDaBUWsBp1SJ9KUKHBT7 hpTw== X-Forwarded-Encrypted: i=1; AJvYcCVTrI8Qo8SzKm+iYvELZuHJ9HbI57wewP4f/3dAoHJNCm5EB29/sjqgAsj5e5JPHSOHXbUuWN8ARkcB3YRwsA==@vger.kernel.org X-Gm-Message-State: AOJu0YwoO1CxUqwfuL+7hp+Ucyp70spTIHt2fHOpSCGQ+UoemwgVp56C EFcizGSWjPM7HInnPbBO2gechQqaMFYmNvprkQL/cAwf9T1ymxCSLCQwAyoB X-Google-Smtp-Source: AGHT+IEkIVBz2fIYq8pIW7ZUyR0JdkO0+sHq7ppSxlZwzZcAXzN07PIqAsvlcNq/b6aViKcM2uHpmQ== X-Received: by 2002:a17:902:ecd2:b0:20c:98f8:e0fa with SMTP id d9443c01a7336-21183ccf7camr301661955ad.11.1731568035039; Wed, 13 Nov 2024 23:07:15 -0800 (PST) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-211c7d24a00sm4260315ad.244.2024.11.13.23.07.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Nov 2024 23:07:14 -0800 (PST) From: FUJITA Tomonori To: 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, 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, Boqun Feng Subject: [PATCH v6 6/7] rust: Add read_poll_timeout functions Date: Thu, 14 Nov 2024 16:02:33 +0900 Message-ID: <20241114070234.116329-7-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241114070234.116329-1-fujita.tomonori@gmail.com> References: <20241114070234.116329-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 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. core::panic::Location::file() doesn't provide a null-terminated string so add __might_sleep_precision() helper function, which takes a pointer to a string with its length. read_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] Co-developed-by: Boqun Feng Signed-off-by: Boqun Feng Signed-off-by: FUJITA Tomonori --- include/linux/kernel.h | 2 + kernel/sched/core.c | 27 +++++++++++--- rust/helpers/helpers.c | 1 + rust/helpers/kernel.c | 13 +++++++ rust/kernel/cpu.rs | 13 +++++++ rust/kernel/error.rs | 1 + rust/kernel/io.rs | 5 +++ rust/kernel/io/poll.rs | 84 ++++++++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + 9 files changed, 143 insertions(+), 5 deletions(-) create mode 100644 rust/helpers/kernel.c create mode 100644 rust/kernel/cpu.rs create mode 100644 rust/kernel/io.rs create mode 100644 rust/kernel/io/poll.rs diff --git a/include/linux/kernel.h b/include/linux/kernel.h index be2e8c0a187e..086ee1dc447e 100644 --- a/include/linux/kernel.h +++ b/include/linux/kernel.h @@ -87,6 +87,7 @@ extern int dynamic_might_resched(void); #ifdef CONFIG_DEBUG_ATOMIC_SLEEP extern void __might_resched(const char *file, int line, unsigned int offsets); extern void __might_sleep(const char *file, int line); +extern void __might_sleep_precision(const char *file, int len, int line); extern void __cant_sleep(const char *file, int line, int preempt_offset); extern void __cant_migrate(const char *file, int line); @@ -145,6 +146,7 @@ extern void __cant_migrate(const char *file, int line); static inline void __might_resched(const char *file, int line, unsigned int offsets) { } static inline void __might_sleep(const char *file, int line) { } +static inline void __might_sleep_precision(const char *file, int len, int line) { } # define might_sleep() do { might_resched(); } while (0) # define cant_sleep() do { } while (0) # define cant_migrate() do { } while (0) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 43e453ab7e20..78e9fef29616 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -8543,7 +8543,10 @@ void __init sched_init(void) #ifdef CONFIG_DEBUG_ATOMIC_SLEEP -void __might_sleep(const char *file, int line) +extern inline void __might_resched_precision(const char *file, int len, + int line, unsigned int offsets); + +void __might_sleep_precision(const char *file, int len, int line) { unsigned int state = get_current_state(); /* @@ -8557,7 +8560,14 @@ void __might_sleep(const char *file, int line) (void *)current->task_state_change, (void *)current->task_state_change); - __might_resched(file, line, 0); + __might_resched_precision(file, len, line, 0); +} + +void __might_sleep(const char *file, int line) +{ + long len = strlen(file); + + __might_sleep_precision(file, len, line); } EXPORT_SYMBOL(__might_sleep); @@ -8582,7 +8592,7 @@ static inline bool resched_offsets_ok(unsigned int offsets) return nested == offsets; } -void __might_resched(const char *file, int line, unsigned int offsets) +void __might_resched_precision(const char *file, int len, int line, unsigned int offsets) { /* Ratelimiting timestamp: */ static unsigned long prev_jiffy; @@ -8605,8 +8615,8 @@ void __might_resched(const char *file, int line, unsigned int offsets) /* Save this before calling printk(), since that will clobber it: */ preempt_disable_ip = get_preempt_disable_ip(current); - pr_err("BUG: sleeping function called from invalid context at %s:%d\n", - file, line); + pr_err("BUG: sleeping function called from invalid context at %.*s:%d\n", + len, file, line); pr_err("in_atomic(): %d, irqs_disabled(): %d, non_block: %d, pid: %d, name: %s\n", in_atomic(), irqs_disabled(), current->non_block_count, current->pid, current->comm); @@ -8631,6 +8641,13 @@ void __might_resched(const char *file, int line, unsigned int offsets) dump_stack(); add_taint(TAINT_WARN, LOCKDEP_STILL_OK); } + +void __might_resched(const char *file, int line, unsigned int offsets) +{ + long len = strlen(file); + + __might_resched_precision(file, len, line, offsets); +} EXPORT_SYMBOL(__might_resched); void __cant_sleep(const char *file, int line, int preempt_offset) 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..9dff28f4618e --- /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_precision(const char *file, int len, int line) +{ + __might_sleep_precision(file, len, line); +} diff --git a/rust/kernel/cpu.rs b/rust/kernel/cpu.rs new file mode 100644 index 000000000000..eeeff4be84fa --- /dev/null +++ b/rust/kernel/cpu.rs @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Processor related primitives. +//! +//! C header: [`include/linux/processor.h`](srctree/include/linux/processor.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() } +} 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..da8e975d8e50 --- /dev/null +++ b/rust/kernel/io/poll.rs @@ -0,0 +1,84 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! IO polling. +//! +//! C header: [`include/linux/iopoll.h`](srctree/include/linux/iopoll.h). + +use crate::{ + cpu::cpu_relax, + error::{code::*, Result}, + time::{delay::fsleep, Delta, Instant}, +}; + +use core::panic::Location; + +/// Polls periodically until a condition is met or a timeout is reached. +/// +/// Public but hidden since it should only be used from public macros. +/// +/// ```rust +/// use kernel::io::poll::read_poll_timeout; +/// use kernel::time::Delta; +/// use kernel::sync::{SpinLock, new_spinlock}; +/// +/// let lock = KBox::pin_init(new_spinlock!(()), kernel::alloc::flags::GFP_KERNEL)?; +/// let g = lock.lock(); +/// read_poll_timeout(|| Ok(()), |()| true, Delta::from_micros(42), Delta::from_micros(42)); +/// drop(g); +/// +/// # Ok::<(), Error>(()) +/// ``` +#[track_caller] +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 = Instant::now(); + let sleep = !sleep_delta.is_zero(); + let timeout = !timeout_delta.is_zero(); + + might_sleep(Location::caller()); + + let val = loop { + let val = 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() again + // 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) + } +} + +fn might_sleep(loc: &Location<'_>) { + // SAFETY: FFI call. + unsafe { + crate::bindings::__might_sleep_precision( + loc.file().as_ptr().cast(), + loc.file().len() as i32, + loc.line() as i32, + ) + } +} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 22a3bfa5a9e9..dd02acfed6df 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -30,11 +30,13 @@ #[cfg(CONFIG_BLOCK)] pub mod block; mod build_assert; +pub mod cpu; pub mod device; pub mod error; #[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)] pub mod firmware; pub mod init; +pub mod io; pub mod ioctl; #[cfg(CONFIG_KUNIT)] pub mod kunit; From patchwork Thu Nov 14 07:02:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13874638 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.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 937551F81B1; Thu, 14 Nov 2024 07:07:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731568042; cv=none; b=bGNHAqf+k+/LEGQj5NWC/0b3BoIpe/YaGKlaQoXgKH3oRaj2gCrMn8Jd++t9s29NDEXVFuv7W2ixIqR1aT/A/1oql1sQCVZwUrIefoRM1bKUbJ1td85oC6kVEhQ7vRDToUqeb/bWIqHonCFc4ejTKJifX6wZhGRsd+hgvACX6Qc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731568042; c=relaxed/simple; bh=s1fGkXCbAOyGJfqqcT0sczwpfVFOxllsZIzn+hXFv54=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=drBW4fWTEIvSc5MqvOMGdVS1cX1eEK9INFgl7/3lKPK/zMJ/QkaVON1yIrsrr81yurTdqufDaFBRkYwficeocLLy+enz4hbD8Rk1uB0PCJQRN6qtMi15Fz5/RCf06+TCo8e1Z3hB+4pAiNWnoaD13M25JVHzxK5h8f1ccWFzfNo= 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=E8mtXEMl; arc=none smtp.client-ip=209.85.214.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="E8mtXEMl" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-2118dfe6042so1892925ad.2; Wed, 13 Nov 2024 23:07:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731568040; x=1732172840; 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=I0qB0dHVK4fkbUBFWgw/OXByiy+cGpd6DkNTPsTFikE=; b=E8mtXEMl+eWsrwWA+yP90+TfCvFPjP+LAsdEMW6I5NZaceny6HoJKjyRZQkx+5E1Do c6F2/7HvArwnkc6EM+2HfizOfwshR5ZX22IVuBiQKEFnAuvn5irR30p5tJmYT+aaMP4c pXo0IjmxDqTybslQCPvR6R6fi1fvumJNzP9/uOVTVbeOeiNubUHtlRke0JgqCN7Tp+w5 +g1g9p/7CdNaaz3s/Q/awkJrjhe02vlp3F1SNPatBX/iXtz++a/bOQyG2zNc2E0DQeyE toWf4CQhkzAZSusAG50yVzAWUUCh/Ev6TyLkWapHUVvLc+zf1zucsxPHyf830DlRmj+a gAPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731568040; x=1732172840; 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=I0qB0dHVK4fkbUBFWgw/OXByiy+cGpd6DkNTPsTFikE=; b=umBCX68TblXrbTVdGVhNANKfaSe82RrWy8jqT6a3Tr0CH0guPwv+YQnEhwbA+76f9Y SIgU25RrZSBuTFTjDY5NsQiV8M0vdp59ElNU/8tw2WWPBXQmEMinvDMHFrRW7AyKyjVx WQAfD5edK0OISjMoMyFvjhVdKtR+HCS/GC3Shnw+wYlCvg0Dt6ZRGtIaPhWz7XNByjUa MdgoC04RBFf5M4EaFjubabN5qzLeAP6mcFVCRfhBdByHcbQxzWriZNbU9JJB081hJzgK lNcql22b4+Lkyrh4/NBHpfuWJrlDNyRoVDceMQVaipP74cLVhjJESYqw+zwmz4FtlXkH lVpA== X-Forwarded-Encrypted: i=1; AJvYcCXAwLQtwiT/XCG996mcgWeGlWMqlIIF9uqk0etMcbqOCvXLdmfoi8m/zQlDA3wSAmlTgt4DbRVzWKq67wfk1A==@vger.kernel.org X-Gm-Message-State: AOJu0YxZBAck/MaSEQEhal3gst2H6OI+OXJ5JM3WKL+OWbkrYnCvCA0E ka54qKBs2lwQGHUsZgxilzQKgRp32rzg5dXT5THIT3MapfteJCmfc25JwSQE X-Google-Smtp-Source: AGHT+IEJem3Kp38rXmgL6DAz6s4FPxX/o78x5w0/1Bw6VftwfbcnCG7GRH7kBNbdt2lBkrBrIW5i6A== X-Received: by 2002:a17:902:d488:b0:20b:65d6:d268 with SMTP id d9443c01a7336-211aba446f2mr134652305ad.53.1731568040612; Wed, 13 Nov 2024 23:07:20 -0800 (PST) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-211c7d24a00sm4260315ad.244.2024.11.13.23.07.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Nov 2024 23:07:20 -0800 (PST) From: FUJITA Tomonori To: 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, 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 Subject: [PATCH v6 7/7] net: phy: qt2025: Wait until PHY becomes ready Date: Thu, 14 Nov 2024 16:02:34 +0900 Message-ID: <20241114070234.116329-8-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241114070234.116329-1-fujita.tomonori@gmail.com> References: <20241114070234.116329-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Wait until a PHY becomes ready in the probe callback by using read_poll_timeout function. Signed-off-by: FUJITA Tomonori Reviewed-by: Andrew Lunn --- 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..c042f2f82bb9 100644 --- a/drivers/net/phy/qt2025.rs +++ b/drivers/net/phy/qt2025.rs @@ -12,6 +12,7 @@ use kernel::c_str; use kernel::error::code; use kernel::firmware::Firmware; +use kernel::io::poll::read_poll_timeout; use kernel::net::phy::{ self, reg::{Mmd, C45}, @@ -19,6 +20,7 @@ }; use kernel::prelude::*; use kernel::sizes::{SZ_16K, SZ_8K}; +use kernel::time::Delta; 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)?; - // TODO: sleep here until the hw becomes ready. + read_poll_timeout( + || dev.read(C45::new(Mmd::PCS, 0xd7fd)), + |val| val != 0x00 && val != 0x10, + Delta::from_millis(50), + Delta::from_secs(3), + )?; + Ok(()) }