From patchwork Thu Jan 16 04:40:53 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13941193 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.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 C9BD74A01; Thu, 16 Jan 2025 04:42:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737002556; cv=none; b=DKzaHU4bahLUPjQS/mIfCySz6y2zzZkr9T2g2CK7VZXWmJ/GlpS4fNBArJBT9CdQ70a+0A9Pdb+LD6m1pKkj4g2pAmvIJ5TfwwnQ1OMONO5OD/7GZn3SFATeOKGAlMVJgo4jsG//dUThlYYGNpxb2l548c7I0ZY+0R1A38SpYzo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737002556; c=relaxed/simple; bh=Vc+8HUJI9ajHvEkwsTV8Xxd958VCwxmLyh0VR+NP/iw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UmPHa2CQcb3BAi8xyCzK+2z8/gHFi+GUPua6vzk5weLVKazpbRgMtxSTG5NcTnAjeDZodIkeWyWuR/3DmXo+Cxg5b5wR1sirGspp3vFukCX0gWttIRNI3MKNWTtutYaT4xyGN+c81EivXwOJqxe5GvIdwhIg7SjPfFzkuSUTYJs= 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=IFKn9xS6; arc=none smtp.client-ip=209.85.214.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="IFKn9xS6" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-2166651f752so9952585ad.3; Wed, 15 Jan 2025 20:42:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737002554; x=1737607354; 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=HHj7ri5OVtg4QKKoVCyZMM8tEP6V7lzENuUM05/wz2w=; b=IFKn9xS6Lj2LzYxsvSC87llixlyndwnfW3kINiOlX5m8J2gBFf79sZ9tjkdLbf+/UO lL0D4qQ1eEHMQxYavwDtP4SSspHAn1TCmxTq+cEv2WPiTK2Wl/R+qxG1JjJnIkniNKNZ O4wLSqbleF4bjzUn3Yn3swL+jMjCXReAK/UrDFshA1uhXy4EacEoYWhM89gqIKb9/cws vzAkDJDaCEluiKgdSwKvo1PuwcNA5KPujtja+dgtJx6pOCJQ/dDcVuZlhy1h0riaWt7B 5JkbZwMHC+oHZSPwaeMcbLVpMl0L/zsTeOQhvKyxQx6BI6KMtOzcLlUKQfaXRyXCwo5i ECgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737002554; x=1737607354; 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=HHj7ri5OVtg4QKKoVCyZMM8tEP6V7lzENuUM05/wz2w=; b=GHcr2pKRGM5MWSFeNnvWLQvHBV3DdVQbMdqNh3TZGMexEAESKezc0D8FtD21q8EVxf j4T2d9eFa6ibHeXln94Ch1YH2Dd0Q0V82P19XSOzmj2jn9WQ+Oy6As3DUIalx0IEsPA9 gHaEgKrCuFbGwJIiTbXlhTtbr8S/1ymDqqU/g/X3yhxijKA7mXV13mMOTaMmTO7yhWMx qQ2GjQ8OOQeIszpgoD+RNDvE5f4OPJjw40zwKmyXEk4BOK5aeI+WTzby7Az7UtQrWcHa us/yjCNoi/xJJEEXZlUSD3ZLOdwTnOey6SzReMa38riaRBb/iZmfziV3eJmorpIFtszs 5itg== X-Forwarded-Encrypted: i=1; AJvYcCUBftUw7NNA4rlqAyD7+MiLNpwiMn2OECS9rzpxuh36wCHlC2bPqtRQwivWJHDO6UozQzEEkB2tw12SnfJckmE=@vger.kernel.org, AJvYcCUgpdhQQBkqOPaH/zLGjNT1PngD3YLNXcKKzFvHKl5eQXSoILp4WZwWYQOCq6AUWfzvtbZlGIY=@vger.kernel.org X-Gm-Message-State: AOJu0Yza1Za1NX2JxDPwszh2vNoV8GIPzm/x56Cn60ozU6lysTKCDxKg A0IkR5jRHgt1l4UCDRTEyzCiZeYaxdUBoLgT8CIfehAnoYt6towBr52IaGij X-Gm-Gg: ASbGnctN1Y+X7I01b0WOM5zDMHp1lyd9ex3z60XuVDtJnFoSxBGpgNfvMS/q4za6pIl NIO6+lTFhnOcPSeswcZmkLPnZCv8Lqzcr23D0R0MKv+2LOMNuCT3LVSqMyt4a6dItCCXU2Hgo/s beS1WO9jQNn4f+Plds95jsz+QX2bghstd3xqEyYpftZvjGnTNVWm/jrC98rmNF3Ztx/xsUAc1Oy 4vYOBfhna2BYUNDoWKULyF2RJH5P4f2hBdxLcMFMORJfTnxK1IPhVGMUt+4DzgJZZqKnCPmiPHj rXf2/dC7gKiLrgoqM/p9PBRcQ9JRZ771 X-Google-Smtp-Source: AGHT+IFkYf47GJ8czgv3H9k2ZXcQWB9V+Rk4TOoFndwTvzrxeuRKQY/eVQ3xbMHzo4e6xyawjaTXPg== X-Received: by 2002:a17:903:22c4:b0:215:3661:747e with SMTP id d9443c01a7336-21a83f48c2fmr407037165ad.8.1737002553891; Wed, 15 Jan 2025 20:42:33 -0800 (PST) Received: from mew.. (p3882177-ipxg22501hodogaya.kanagawa.ocn.ne.jp. [180.15.148.177]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f217a60sm89161045ad.158.2025.01.15.20.42.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 20:42:33 -0800 (PST) From: FUJITA Tomonori To: linux-kernel@vger.kernel.org Cc: Trevor Gross , Alice Ryhl , rust-for-linux@vger.kernel.org, netdev@vger.kernel.org, andrew@lunn.ch, hkallweit1@gmail.com, ojeda@kernel.org, alex.gaynor@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.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 v8 1/7] rust: time: Add PartialEq/Eq/PartialOrd/Ord trait to Ktime Date: Thu, 16 Jan 2025 13:40:53 +0900 Message-ID: <20250116044100.80679-2-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250116044100.80679-1-fujita.tomonori@gmail.com> References: <20250116044100.80679-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 379c0f5772e5..48b71e6641ce 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 Jan 16 04:40:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13941194 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.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 1A2CB86323; Thu, 16 Jan 2025 04:42:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737002563; cv=none; b=s24yh9GYWJdD3oxRLvZuHKiSSZ8ws+jH1elBSQCF31HDR3SIVn3/KkneN0YdgUW2XjbrBgnrS5JgUqoBlPt9AC1m2fsxJpZ6ZNcYROhR9MtythT4WoORjWWTWlrd9NibRLUAj60upSATnQ1Zf2PKjtOlmX7cPRpI4eFsEFO1aaI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737002563; c=relaxed/simple; bh=CNoU4j5DemidNSsl2YlTp/ZxnKxH4rxzD9POXhZ+6rg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mEPhmeUtKrd2KCu029XykCp0sz5ldZ5BrQEUV/kANpYgQJb59KefTFnEy8/XE67wNXzKegPnyztKHo2E6TPfwaOuX+zynBCzoMU3+mIrWVrF3W/R4IGTivc9mA9DD1z1NcB1Z9TqrSHaBAxe4YmFYvCy1cf2CE6gJ5sbMkNUehQ= 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=iBMGVxNc; arc=none smtp.client-ip=209.85.214.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="iBMGVxNc" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-21634338cfdso10385255ad.2; Wed, 15 Jan 2025 20:42:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737002561; x=1737607361; 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=JybksRFzIQq0QOOnjY7EbBhGN4P7Dv0rI/iDUoagoPM=; b=iBMGVxNcV6A1rmgVHFY75TG/d9hIL0JDQEv9k5gu2nGwoCfsFuKe+wJte/zi9zSlbi 0t3foy+uIcDjkklo8st0B5BS7zZcJoI24TQ+NcmQgfOVHealu7tGuAYtZ7x6RKLIjYJt jx5i/ATXVGQ7neZOKXtgfqN0cWl5BP3UOobUrvk9HDK2ugZr4peYmkU7mL0SbMQ6Sg8N PfEBY3OujtB9tHERENduz+MqQ5BPXQjwI0fPf6d8vYb69F/nJbg+xbbwqJMp/PDGsvgb ch/H8+m3eiHpCRUb2M0/cbMMqMXPl2mkVqhym/X+VLDzgkEnFdC99I5o+xw7DfKWcHqF C5YA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737002561; x=1737607361; 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=JybksRFzIQq0QOOnjY7EbBhGN4P7Dv0rI/iDUoagoPM=; b=QtI8NLM9YlAyCpVm8JF6vToqrCZs1e9XPF7PLfaeEQlqRyxqSQ6flxiAQIg5MDcv7b I6OooWgqKW10ZuaAjGfGP9zZc80ZoGRUTfT7/hZpsRDH05BdWjeLiHbdKc7WTY4nqsKF 1k1MuIyT6gbSfV50s0KxsJfaQduCC3A1/tGdqJwyaS1KZ1oRW+mxWVkcXrZ65rHOZ6Sv CZc6lPTeUw+4B1TwP5mq6z0aI0wICFiHZfEzJ28PAAURofwRv855M41ac+WIPGqdT4Ce +ahM3+fs+eEujAmvgPgMZjTiJKKzerqNji/h6g+VvhMMkNmPZmTSSj58vFpu+hTnYaYy EAJA== X-Forwarded-Encrypted: i=1; AJvYcCUNHscYRxbYdcB6R3dCoZpSl/Q5Msd/AxBCXTRQFlkPcRKGLbjbw7MJwdG6q4WfAp9CM2yeTZTB6Depueog1a4=@vger.kernel.org, AJvYcCUzARaEUDTF9bbWeCHO1Z1o+NtGAyJwexcI5DO1cBFQ3PXrNv+TefVYKt+vDrZ5gZshtLw+FaI=@vger.kernel.org X-Gm-Message-State: AOJu0YwMUL3U69gXVByRaNinCC4qppx4k3MqNXD1Tx3c+iarOm3cAhrD dAb8xERCBlj1kdLCuLMgLxZGO7SmUYG5FCQwHm8xJXioydTjTmKGC6ZXf+Gc X-Gm-Gg: ASbGncvfJTZQtR9qboQA7y3vZazN9Pf1uGizCFYGKQedpgECiVpKWcjfJryckbltvvv bEaxON6/07+U7UOhdf2b5Jnk63hYoacX5PjGvb3DVB3wCNCJ1r753939K3koU/pGd7Xju37/8Bj oehMKlE+cpkUuFzpW+sLi2PfvEQtrq1E4pDiS60lPM18P1QDpuz7l7BhAcxgtkmPoUoONjCUc/x 1+u75oA6HIUISxpHqBg+oJlhLXsq6R5gJ9/ftItiLxwwtRC5EMwCzbshI7XEw1aL+BbS28HXL0g A4ArftnvMeu3gp4GPXwWrTworYO/DE0D X-Google-Smtp-Source: AGHT+IEr2vOJf92WyBIfEJbp1W0orVXFV9U/V2tiUnsAO/7H9HpuqdhAioJi1KdurZDvqN7TgJEnuw== X-Received: by 2002:a17:903:2301:b0:211:3275:3fe with SMTP id d9443c01a7336-21a83f5243cmr268617295ad.17.1737002561053; Wed, 15 Jan 2025 20:42:41 -0800 (PST) Received: from mew.. (p3882177-ipxg22501hodogaya.kanagawa.ocn.ne.jp. [180.15.148.177]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f217a60sm89161045ad.158.2025.01.15.20.42.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 20:42:40 -0800 (PST) From: FUJITA Tomonori To: linux-kernel@vger.kernel.org Cc: Andrew Lunn , rust-for-linux@vger.kernel.org, netdev@vger.kernel.org, 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 v8 2/7] rust: time: Introduce Delta type Date: Thu, 16 Jan 2025 13:40:54 +0900 Message-ID: <20250116044100.80679-3-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250116044100.80679-1-fujita.tomonori@gmail.com> References: <20250116044100.80679-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 Reviewed-by: Alice Ryhl --- rust/kernel/time.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 48b71e6641ce..55a365af85a3 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 = crate::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 Jan 16 04:40:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13941195 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.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 653C8EC4; Thu, 16 Jan 2025 04:42:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737002570; cv=none; b=N2gPBakSFcUxDASgj5yB4FBqInhyiFNVP9LWZHKnGvDvm0PSMFiojJ6j662CAXl1zJlQHlaBXefnkat4PgEyWcLJls+wrpVSUqZTwFtBZXMYfimP7xzwAGi4jB90wLL4JL5Bl0g7ZbW0nS8wIkdQdJfLUhVb0+lYXWArF2562+g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737002570; c=relaxed/simple; bh=ZXb/0NiUcW1nSbcWaHbSM4MFinXMLJootRsCvXQzwhg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QAhkNm/TVJ3HjXd1yGl/Mtb7M3N/cK1Nh9jBz2Xz2aHFwDZkGn44iDoQQVNlgKM/VlW6xNzz7RE34N25e4nntIL4gfkf14hWZ53sVo6VO2LlUdT1w6GNYlRvsTuJ12XFJO8idG6wOuVBOnFGw90wGjhQ8omXeYXgay9gQODl4Cc= 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=IhGB1PYJ; arc=none smtp.client-ip=209.85.214.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="IhGB1PYJ" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-21644aca3a0so9879245ad.3; Wed, 15 Jan 2025 20:42:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737002568; x=1737607368; 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=+lsSO3XnrzhExT+J3VC9DmPLXbd0JuCQEKEpxIJs/0E=; b=IhGB1PYJyXoc+gnS7vRHzN+C3tFKIXx+CzXkA/OxljUiYOcJ21CSexOx4oGrOL+sBL /UUBZob9PjDKPdZJ//+dSDTa9UZrILfMAQsUwrGDsmYiJyVQpf9c6htqbWBE+x0Y7DwU TWpwdsdyM9Mx/DbidwwKP3mJYVGUp62RwB9HPtlWaheodNbzj3GgVDwDF+fkbTERhnho 6B+C0kKMm8tAUeaowrpXkhVnGRB3gM3+ZGbWOWmecsbYTW+5zOk0zAuToCZfBu48iRuC 2gCB0lsDNjyBWjOIwkiu/S6H3JSuzkqMiFC1iJdJPeDqV6DQjbQKZQ43abY+6dw+FThR u2Sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737002568; x=1737607368; 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=+lsSO3XnrzhExT+J3VC9DmPLXbd0JuCQEKEpxIJs/0E=; b=m28w7+Y9rp6aPRnLI5wiMY5WaCpT9/bz7FtTVeRv49E5yeBCoeE+DyNCnCcI1Rw0hc T9Wxe0m41x+qfRbfGQjK8lcKvHIPkLGNJpDUDrOYC6xB2ZN7eDpgQR03vrGgZ2wh2tTV bYXDPclW+pzqgMirJZlC6PgyRglLDkIMnoopszI62D7JW7EMbhWv47x4hg5+HsHTVcaj FB3d/66JaBf62J9qgBXTjuMqInYJ8RYiZPQ/3y3cxcpKskUWm/8ixZZ69YXnbtoheDoO t4DifbNO5U0ikFierc1AkRxmSLcNvos3P7ee/ItzUSiX21AvFIIZwv9rNtMPGO+N4kdP OZZA== X-Forwarded-Encrypted: i=1; AJvYcCU226tQQu0P3oUG3nZkQwTE0xqjQZQArRrnDfiV0P07vV3Q7A430MBRxZG3frjS7DyZn26GO4l+M18F19zlWBY=@vger.kernel.org, AJvYcCXOARYdMn+llX9sixDlMr9CU/eelN/0CUSoZMRlIegNDdjUUfCXKFKCHdlRgv6F3OEL+55BxcA=@vger.kernel.org X-Gm-Message-State: AOJu0Yzup9q9MYpEXv7WXjnCtOz4QJHyVuFnKFUULRg0UC1OhzCtVKIM lBB1u+YoHEiuRO9J/TPecVnfxQhDcRRQsELZpQOIXfGmcmHFCJBQFbt+fJXN X-Gm-Gg: ASbGncsjtwncZc9t71Ie915noKt01ceC75QTPBSzDVvzrVOWsi6Fg+RngD+2e/LpCPV SyueCVu/Z4Hlh/8rxt2pmyz5FQ8d+YXI0TNSrtkhSjcq9MqCfgYJXMZxRS9zGQ6h2ELe2sfPrz0 g52w9jbFX/ivI5e/DJIrhw6/iALc+slHH7RohV0C3+tJElGs5BsKIaEOClm/20W/8oNO6CRgDOc vxLT7ML33BthHOVYPv0cnlYDKRmmQhBjFMCJG0blyI8oZDZKHjLQUmv1re/LyBsO34tengAJXN/ cx39+KOYcCcOZ8sIMi7vJp9jAHGvCp1h X-Google-Smtp-Source: AGHT+IFoRbOB1iCutLQdzx68G+wIpR567sAM2ad+/yzfbp0H/NK8qiOzYGirpt1+1g92EyxWS8i8QQ== X-Received: by 2002:a17:902:cec3:b0:211:ce91:63ea with SMTP id d9443c01a7336-21a83f56f9emr492179465ad.15.1737002568451; Wed, 15 Jan 2025 20:42:48 -0800 (PST) Received: from mew.. (p3882177-ipxg22501hodogaya.kanagawa.ocn.ne.jp. [180.15.148.177]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f217a60sm89161045ad.158.2025.01.15.20.42.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 20:42:48 -0800 (PST) From: FUJITA Tomonori To: linux-kernel@vger.kernel.org Cc: Boqun Feng , rust-for-linux@vger.kernel.org, netdev@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 v8 3/7] rust: time: Introduce Instant type Date: Thu, 16 Jan 2025 13:40:55 +0900 Message-ID: <20250116044100.80679-4-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250116044100.80679-1-fujita.tomonori@gmail.com> References: <20250116044100.80679-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 55a365af85a3..da54a70f8f1f 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 Jan 16 04:40:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13941196 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 E9E58152532; Thu, 16 Jan 2025 04:42:56 +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=1737002578; cv=none; b=HDnPrQjKjKO6DlUyKqZafUv/swsG4cSXdlvldjqcyEBqZLX+m+drTBiJ7UEXJeiulcsDhSh4YdeCBjRIL6uCr1BH/XM4PSwpjCp9NgJnmtSVv/u7PI4TBKrj6pD2lCnlg0nR0aldvibwd4QjySk5Cph+0+fcFoArRW4D7g1dfyc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737002578; c=relaxed/simple; bh=O+kqL+5YpnCiY6731vR15kXmZyQfCfEoPbqW1DePO80=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=B6xyulPI/XEbgNHeXhv86ea+6bZbqYz39MfFYCeH2qYyRSuLAgobSGu9lxE6hyRzcxNkfTbTqUEwBDuhq7/bVssquVFq7o83H5G88f96HFuKbnQjZ0WxyUxSt+FNUVbHEZzO82x3CgyGeF5qXSNMyBLcTur/d7JQLTMtUiLDa+4= 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=HCTT1sPq; 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="HCTT1sPq" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-21654fdd5daso7335345ad.1; Wed, 15 Jan 2025 20:42:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737002576; x=1737607376; 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=tG2rfgEzsdeXdQBcUUB2Ws17wlCYVB4wC33Z9prKhtg=; b=HCTT1sPqt3xvCQFnhS8PRc9owhQIUShOaDC8ViHgxEYGT6v8pRFBsGrIjystVv7Aks LPm8AZm8lxbkl+Ite5mjUTtD8SKifxDCFH0Wsayb8wvfhAMnP1bHPeQakDc9OvN9axp5 yxs0RmiK7Fj29bhhpSkHNOF9NptdJRyO/KL+UbRJs8H5tMkVViICCq0kYtBdkJKaP0TT UJwSl2LJElF7fdIl56fsiPttmTOHYbMao1Kn0wuMn2Km2y9Y2QNuPQLFBqXD5xh/25HY l//vDLfgLivFGb3iX2lsPuzLlxCjrfYYiess+775ejiQVik3QVErMaJ92HUd869rJYAD r7ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737002576; x=1737607376; 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=tG2rfgEzsdeXdQBcUUB2Ws17wlCYVB4wC33Z9prKhtg=; b=hd6zY0kEATfYbLzBBfXt4BGDBQmuBMykDvw1YE7Ur65iWs1F3sYmDH/5oDvC11jXdy j38zBVBCqQATtdxy5EoySHMGWWx8d90Q2+5OSkecp5kFvsQrQJAPToveoDv3NIe9yLRW K5uY9R1IPJyc1L3USyYc5+n/8Nmb6dbvfjgz1gy3682pjLu/YY/eGI3p2IBzdzR72N6x KH1GjBuAQIUJq2axWbk1tz4LxzBff4vjdkhHNrFgmpwB2zy0sZbtaV4kl01U2N8/NwzO AkjIJkh66KRbVDinNzUPCDLpuMQbKERBiDfWxXAtWlDFuvKfdIxyQQRfooygrNtkcm6m omaQ== X-Forwarded-Encrypted: i=1; AJvYcCUUyP8gz+QWxxYp6zL0yUovGaQfs/U6RgJS3Ld6We5jvN9Xf84DFFT59lae/rbGtM2B3cUq1cE=@vger.kernel.org X-Gm-Message-State: AOJu0YzwGgOrw4T5GrbeHGx2p/IugifQyyUR84+qeeBN7of8mSat62cE o2JEwJWWPhCFRCA26YZtXjcXrRgAd585eddmXT150X1LaoT4qGO8h4C0Lkq3 X-Gm-Gg: ASbGncvFCDq5747dWpg97lk93luKA3SYH6c1Y31iZV7FzipFm/HOdCgK5eNDVCwPjub VK3BDgxDDAEnYvO93USEa+bGKe/Zkl60BYTbVftEg8oI4TWD+RUnFNVwjnVSRu8GAi7zZb4zMML shyI9j1G5ZI8zOlZGa3TLeGZH59E19GIb2qwDpQTHYHbxfRK29G7N28laNDb4iSoUmRALfcmLs4 BFtzUIcH32M+gTyb6KfJSa1QxEjSrr3sudfVv7lCfyXEAXkjdOIHU/mlRlHvqROyl/akAD+h+vI BYbSUDJx7hSVJVCMTIEM1AGJMSVrM20I X-Google-Smtp-Source: AGHT+IE2kXmwBJ24RxcaisRLSIyxRU4mEX0ssGEXixCP4azmT5AeNYwOMqu6fbffIqx+JO2OiYe6eA== X-Received: by 2002:a17:903:2285:b0:216:386e:dbc with SMTP id d9443c01a7336-21a83f54a7dmr463185215ad.13.1737002575812; Wed, 15 Jan 2025 20:42:55 -0800 (PST) Received: from mew.. (p3882177-ipxg22501hodogaya.kanagawa.ocn.ne.jp. [180.15.148.177]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f217a60sm89161045ad.158.2025.01.15.20.42.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 20:42:55 -0800 (PST) From: FUJITA Tomonori To: linux-kernel@vger.kernel.org Cc: rust-for-linux@vger.kernel.org, netdev@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 v8 4/7] rust: time: Add wrapper for fsleep function Date: Thu, 16 Jan 2025 13:40:56 +0900 Message-ID: <20250116044100.80679-5-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250116044100.80679-1-fujita.tomonori@gmail.com> References: <20250116044100.80679-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 dcf827a61b52..d16aeda7a558 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -26,6 +26,7 @@ #include "slab.c" #include "spinlock.c" #include "task.c" +#include "time.c" #include "uaccess.c" #include "vmalloc.c" #include "wait.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 da54a70f8f1f..3be2bf578519 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..db5c08b0f230 --- /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 crate::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 Jan 16 04:40:57 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13941197 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.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 F2FCC158D8B; Thu, 16 Jan 2025 04:43:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737002585; cv=none; b=CVCVnNSknY4NGPwtKfNvgPMExZw/nZbNYmZyBFVMgiDSCu7zMhHmc5aVZjsYZNv2QnKWs2llEtxzBfCmiUQzRAkg5dr9ldS/8ujHJftQYV3jfnGCspfoFgqv9Cm6TF5eetWJnCTk8CCBMzbpCM/ob7ZYV4mc/BitW6b4i6hGeLc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737002585; c=relaxed/simple; bh=QCMniBBTurKlsiy0RrGlj3k+lcWNbYodF8V4zYwePL0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QCQzv8FIRccxkUljELaIuSVWbMmHvi5IKkyJTNg6gZhrPY2YYzyNONST+Wf26S0OqYgYaL3jGLxvYcNbgbilgLHdR4P0fcu7gS4sIuIcLLTcgjoettU0802SoZ3z4/V3qHYT2ajQWst1WEwERs1kimrL5lZwezbYMpHpC3ooj6A= 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=IMx2pg9U; arc=none smtp.client-ip=209.85.214.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="IMx2pg9U" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-216728b1836so7058015ad.0; Wed, 15 Jan 2025 20:43:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737002583; x=1737607383; 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=sR/+/dZXDRQ7/mF/Sv1q8TJwB25bHgFlrdQuIhySF5k=; b=IMx2pg9UXc31ArQimcDPm7mGVD7BKxNJLRQp22N+f+NubQLaXQ6QggoF+4vOA7rkze XyWUNMf5gtdcmBA9njWx4rsucp/LPlBwFs1LC4+hcvJ+mn34wqKMdxrWIbNJONzr9fYp wDMrC4aYh+V4eFVFdif6sJleVX/sn+7autgkpL2HTwgkc7XKwcOtIHs6snwPFptGIUMM hwKXZItehFuNuHxeuo3LCv4+zzFNYWqeAGmZabpLc5hkm0nyS55xcgTxepfMb2tMCJt0 H2ESFG+/+1wKoDTSwQPcnoh9sWnkOHq8gM7is6oenjGqhu25KHo5hwpk/wO8LsDLWvPv qNoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737002583; x=1737607383; 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=sR/+/dZXDRQ7/mF/Sv1q8TJwB25bHgFlrdQuIhySF5k=; b=cZ4xilK5YuER5pSqWB6UCEyRjGxo0xqxDZvlKp+UCpSAesJAiJUwwK2kst1D1bCBnX PPtXAiNVtMXYrcFUxoKl2gXMt7T/YbERptukBumduzDCjYTPpwxGj5eulHrS6YzsbVN/ MFBFu5V9neVWjAos05j6VPhD9LXOKhB51C0wRLgJr1OxXyduWCUeE67zGD8xvEcn4Sys UwBPkPB/i+0Cej1XDpWFBmXgDoKh7GhPgkWYSSTTevQTDYaiE88TWy8zVH+CQCOXXVc9 bcdEqph1vGSllNRABiv7OX6QXMQCrxLiLVdfg1GoEEUumIV9c6JU5Ru/tkzIo+5atjEi C1VQ== X-Forwarded-Encrypted: i=1; AJvYcCX/8OXN68VaDE1WfOybyM9wp6WPHLlvWGouG3BswdrFPrXzCelUnqVapFI1nSkymvEgWa8cK7k=@vger.kernel.org X-Gm-Message-State: AOJu0YwapblyfCkmRLDg1Q6IiFHNGJLNjhdfhhjN+1CfDKR2IJb+s4Wb fgsbTq4G8rWyJAOqTbYQS2OCnqBTRdu8yk/1RE3Cm5S3ARG2Y2egPNgXLzRm X-Gm-Gg: ASbGncsoADUbb6bNtden5oTh/XM3ZRKfPx+b4TVtMv+7m0WgSFOATzqAeg7c3YaUOrg Jts6s1Vi9XW/Qin2sXDtvD8GHhePmvJSJX0StSxHpKO48msd35mpNGKjjYbVIc6bNJnpijh+cbl 6SkKaASsSZZutzGpHXohln27fQo4vJkIEBykAQUTFnvJB7jO8vcixwbP2w0iMBg96K9oHoxDwcP iIwjf/+zIeW/R4FEk7s75Yxkbw0a41A97sIonoZ1S1wL0zVSnAD3xmfbm6c2Lu1azYzM/zpc9I9 dvtxdAVuvoAnxG1YSJbczV7IMc9jPe2U X-Google-Smtp-Source: AGHT+IFpmhSnVVzlqkvJXV3QbkNm28zgOpZwKPS/NqCuF5ndEvlcjPRKOS3coyLV++pkNSPPKVrIlA== X-Received: by 2002:a17:903:2308:b0:216:779a:d5f3 with SMTP id d9443c01a7336-21a83f546c3mr540495325ad.14.1737002583007; Wed, 15 Jan 2025 20:43:03 -0800 (PST) Received: from mew.. (p3882177-ipxg22501hodogaya.kanagawa.ocn.ne.jp. [180.15.148.177]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f217a60sm89161045ad.158.2025.01.15.20.42.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 20:43:02 -0800 (PST) From: FUJITA Tomonori To: linux-kernel@vger.kernel.org Cc: rust-for-linux@vger.kernel.org, netdev@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 v8 5/7] MAINTAINERS: rust: Add TIMEKEEPING and TIMER abstractions Date: Thu, 16 Jan 2025 13:40:57 +0900 Message-ID: <20250116044100.80679-6-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250116044100.80679-1-fujita.tomonori@gmail.com> References: <20250116044100.80679-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 baf0eeb9a355..77bf1d2e6173 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -10251,6 +10251,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 @@ -23643,6 +23644,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 Jan 16 04:40:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13941198 Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.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 A1AEC142E7C; Thu, 16 Jan 2025 04:43:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737002593; cv=none; b=vBYirpnZbp+qVSOHjxen9woNIuSbpWuEWcEN8aMh4h3PDb3jtiqafknqOhv3ILr1GX3dMyFunJ9C7gkyVkf8qHymgWT7sE0ZOpXQ1SXeXc1BqIz3bFtq7aOZ0Gd5iFleX5pKe5No1maNz/UCJDSQgcFv70O/GxHlhwTUyQtJF5w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737002593; c=relaxed/simple; bh=Nkj0xAr5LhZxDS0qL6phA41kZCrKM2p/3hWG0MI3ao8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RHwn85v6wHYSrK6cgSJt78+aobujllHvfq/W5DYQrnhgYu5mVYwv0IfdGnr3/gH/wUFSiHVRKXFF/dw17uybICtxgw9ah6rX9dHYlJlrEiawn+iJNkwCytf670aWaQ7MSfhvsdLrOqiOdg/Brg4StSm8oQSUnqhoJw/kePXS/j4= 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=hcvcyGrF; arc=none smtp.client-ip=209.85.214.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="hcvcyGrF" Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-21628b3fe7dso7485615ad.3; Wed, 15 Jan 2025 20:43:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737002591; x=1737607391; 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=fZvnsgo44tIQ3RPgSzc6JsAoEmnciNW5ZiYm/3odSKc=; b=hcvcyGrF8RFq+QIaZMOtHKEPzYNem+uC1YKaAa5uYSj/kNlJkFPUMaZZAuR9IiC3f9 gt+G88nu1DVWx2UCmN05xZXPVgsYEW4x+62UkUByucmXLmkRqlvDoJmmdX923QR5LYya xn5M+QAYgo07VJP5RU9XZCaAfhc6gjCpjSpWurDQh4KiNKTvWF9AiT6QYAXoKBnnhlSj fZ+gaDoLOx3hLJDfLXhEpCprVNyRUcUZ/tgVU/FvEteTtupVsBq/2s5aopQQEUZftMKC MTDsJOTDwVZ0IgwCHbDJ9sDvRMeFieC1r7gCMvs4zxwQk0XuxYuluJz1oVxdgm5c2EeF LHVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737002591; x=1737607391; 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=fZvnsgo44tIQ3RPgSzc6JsAoEmnciNW5ZiYm/3odSKc=; b=XjtkkoHDeB7IbaYBqkfHUF1MAOyEf/GN/x83UveA56VDadYqy0AaeItFWvbNmu4NtK Zwu6Xc1eDv/rNkOEm3cVbnFL0CLb/cMpQ/idR11ECB78aYCq0+XAnRytUCJcJC7k9xKR M9hdDH+fknmjSpWe7RC314Ao5+wybPaax8DNZBNHy2NHXWdsXcW8QQmoSCv5AcOnZvEZ xkld66lo5ijLVYHfnUZ4GNFJiQgiDKARUW03wZ1x/rxQKR9gr3WMrh7xWHx88ftwzsZn Bd8eYkdzCaReWVX1cie5vOmlogTvZHbEue4IjEQbXUp2PjWEeYuTbZoina9BzcCsOACs dwJw== X-Forwarded-Encrypted: i=1; AJvYcCUj6ywg7FylfjFYGNvjgXojDbECqN+zeTFzLopjLnU+MMKw564fj/uX58Mf5gVK6KAb8SS5DPYdwm+qUFDs+CY=@vger.kernel.org, AJvYcCVK/C3sWkFBjrQ11aA+l03SJPSFCd+T66sQGeGBDfW9rM+bCR6wcsCqU8eVP5F/3AhieGORlwY=@vger.kernel.org X-Gm-Message-State: AOJu0YwY010N/8IxdX0SpGM/S4MdlItHYMdQQzJ/JtKA7QXzo0E6JAlQ amfV38BzvyJbr8tIaDMOjS+PTjAQBqNpApRcg5apGT1rQLbUhCRf3ttvCB+t X-Gm-Gg: ASbGncuZAk6KvM5mJeizhdiLHH0QmBLAoBPVaFx6en65HIKNl4Rvzv+yxMifd60qd0Y 8a2Ev47djgRdIj3Q+YC8jAa4xfck205Bt0IFyJv6bAT4WyvjUsFKEfxjohBcxPm+3KQktT4KrKU osd222sU7fOlQED08SFG8nQnRLWo2xYijSfXCWW6GxaYASrFXT9LWn6W7SsW1gYrIdbeAxnj/VX QNTTjsxRml/uzSNXzatKXKumkRkyEfXKMeMI3mRIaHO3wi4/iIwmHXhfiopMT2xd57FgwKhz3bL snHtnGiYb03pxT7eXD1IKLQDlnqeE1ai X-Google-Smtp-Source: AGHT+IHexLKREhj6vDPzqKUgGrdzSuLDrW/rSFuU3b4flGjuVCZlpQsV/fxLvkcxp52ts7eNO5s1Vg== X-Received: by 2002:a17:902:d48b:b0:216:26f1:530b with SMTP id d9443c01a7336-21a83fd0bd9mr445694035ad.51.1737002590454; Wed, 15 Jan 2025 20:43:10 -0800 (PST) Received: from mew.. (p3882177-ipxg22501hodogaya.kanagawa.ocn.ne.jp. [180.15.148.177]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f217a60sm89161045ad.158.2025.01.15.20.43.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 20:43:10 -0800 (PST) From: FUJITA Tomonori To: linux-kernel@vger.kernel.org Cc: Boqun Feng , rust-for-linux@vger.kernel.org, netdev@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 v8 6/7] rust: Add read_poll_timeout functions Date: Thu, 16 Jan 2025 13:40:58 +0900 Message-ID: <20250116044100.80679-7-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250116044100.80679-1-fujita.tomonori@gmail.com> References: <20250116044100.80679-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 | 28 +++++++++++--- 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, 144 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 3e5a6bf587f9..d9ac66dc66d3 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -8670,7 +8670,10 @@ void __init sched_init(void) #ifdef CONFIG_DEBUG_ATOMIC_SLEEP -void __might_sleep(const char *file, int line) +static 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(); /* @@ -8684,7 +8687,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); @@ -8709,7 +8719,8 @@ static inline bool resched_offsets_ok(unsigned int offsets) return nested == offsets; } -void __might_resched(const char *file, int line, unsigned int offsets) +static void __might_resched_precision(const char *file, int len, int line, + unsigned int offsets) { /* Ratelimiting timestamp: */ static unsigned long prev_jiffy; @@ -8732,8 +8743,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); @@ -8758,6 +8769,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 d16aeda7a558..7ab71a6d4603 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -13,6 +13,7 @@ #include "build_bug.c" #include "cred.c" #include "err.c" +#include "kernel.c" #include "fs.c" #include "jump_label.c" #include "kunit.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 f6ecf09cb65f..8858eb13b3df 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -64,6 +64,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 545d1170ee63..c477701b2efa 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -35,6 +35,7 @@ pub mod block; #[doc(hidden)] pub mod build_assert; +pub mod cpu; pub mod cred; pub mod device; pub mod error; @@ -42,6 +43,7 @@ pub mod firmware; pub mod fs; pub mod init; +pub mod io; pub mod ioctl; pub mod jump_label; #[cfg(CONFIG_KUNIT)] From patchwork Thu Jan 16 04:40:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13941199 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.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 989B64414; Thu, 16 Jan 2025 04:43:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737002601; cv=none; b=PgkgwZhB43YcRmsYSZTe/r+6ONf1Rwm8dz6murpAUxRodmggd7xmHa4R+sFBQnTINJuYBOZ8K8er7orzece9mxVVjKcgip3DlN6UpHP5R0isOjzCw8Rz8OTZht8Z5HKfoicDdZyM8JNKidNmaCzKUDvEmbEPkIJI7FAQsd4cmAE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737002601; c=relaxed/simple; bh=re0jrUWb22m9wVcyDHNMZvdytL9xnLR8R6MQwhWR7x0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uhSu4OCSRuVXLYK3vdY/QWZ+kUuGwLhxNQ1zt9PdbisW7BQKAmoa+9wXyF46HAwptn9ybpK5bbb/crvIhc/F/Cd84LAjVbCwbe6kYqMBTSFQ++0SMAQV+rjQjjyWNfsF8QoVU/1wFngvI1uC5jZT98OeHrSAuZRc71G1/XELYyw= 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=QG9qy3G2; arc=none smtp.client-ip=209.85.214.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="QG9qy3G2" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-216395e151bso7690265ad.0; Wed, 15 Jan 2025 20:43:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737002598; x=1737607398; 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=2SHfADerOSbD2n5XWOFlbp5M9r6bMFwlpcEuA4NEOP8=; b=QG9qy3G2zVXW3GVCk6gVzwb/UZdB5r1BCXatQ4ggy+A1XY1y2y4K6DF9O35P2KZ2UD be8mddwOeiLxr8Y8H6uPuotZeXdl61BisHN88Ub0hXlpJROS/20xA296PrY+kPT3hA3x pU00J+SsCLB4SbFJKIg/IOVcrxC5ppQ8nILB231m2yWOXFh6r6tIWUnRnkMWRvFm8QXX wBOjNSapWDQNBhnt6FRfNWJwpnQ6+mLDRLUiRZLrw5zhVtyKCn2kfn7LL1Het+Qj9F6T lDbvMEBCu/qDR49hHAc4Es4xzGdTI6SgZ3zVszC+TrBgIxPkYbx8onvIlT/VLyVAZLeE oRZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737002598; x=1737607398; 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=2SHfADerOSbD2n5XWOFlbp5M9r6bMFwlpcEuA4NEOP8=; b=WlX5AkekJFmfrZ4/OC4xMk9BTs8AAiOYCDO8Qx8yB04Ug6ARFh/4+/bwo1qYN4RBRd Yy55lAprULCfVCvbmAze/+DWThFd2wvqS4np+B5C4WSMAnOqrjfDRfwCx2avfoNpMnMa jSqbcZlHLB5cNyNYTJFsBpX3zkSdIZePGnZxjaK1k1+ApeAot++a998tCIeN4sBDiGK1 DeUjUVq7W8Cx4mrnFDEgXexi4bIM7B92jtbwl+tno32xqwxpg/fZVJXhuGoGqpRB/wu7 E4PoUZ2dgEQA1W3rRSp6rQEFt38Stmv9pUEaBLIB7PysttytQRDEIUvch5qebYC/v8bm 6Ugw== X-Forwarded-Encrypted: i=1; AJvYcCUBOfnGxmgMeNj0fyw9etiZIalJye6FR5FJoVZmxcJzc8MCtyClQbU/FDrhW0kdxwQ5oGZfJSnyA5rEq3esZT4=@vger.kernel.org, AJvYcCVDiM18qX6rloX/QvMAhbRHixOAz2HRcmpIQgVtFp4QxkCRxn8vXLzTlq9bBvisv2oMqhfnn/k=@vger.kernel.org X-Gm-Message-State: AOJu0YzgIW0l+OxTh/0kh9ncItwzU0pWYtcgjKu6I6EtNmlFBaQOG8BD ViWlPTTpjS+hX1IAOvuv3O/JXHpQMs1+v6F/vmOZm/lyC8n2HMwPSpl947xM X-Gm-Gg: ASbGncvSuFVZ9Svrsmd/PpA84F+hzZxeKC3UHQdFKJBQap3wifHdcRfQwsPEgs+UteL ciu19wtNXfmBob6F5D++UFExX0T7NT7QPuXA46dDgHM5cXhMd83zc+nlFwaTops0/vl8yEBA7kH dyQsF1xndnv89YQ70sHZ/2M6bp28MFkAy1OXMU2MKfh3RTBPZmM5umEDz0x1M//kCEiz2iTo13w B1d7sdX602jJ5uoGtNGFJxTE9vSIMLgBT6SUct9edNrRooLqFAZGlz6Lq6YMoJnMHqwND9D+c1V V/Bn8Xm+KWJuA++YPFTUuUESfqAvJbbB X-Google-Smtp-Source: AGHT+IF1zYWuvY6J11Gv/X0oddWEerWFkZm3r8Y53udA9sBWenUxWgEyVzqUXhp519omHjlOYKPMww== X-Received: by 2002:a17:903:1d2:b0:21a:7e04:7021 with SMTP id d9443c01a7336-21bf0d1c68dmr84654725ad.24.1737002597672; Wed, 15 Jan 2025 20:43:17 -0800 (PST) Received: from mew.. (p3882177-ipxg22501hodogaya.kanagawa.ocn.ne.jp. [180.15.148.177]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f217a60sm89161045ad.158.2025.01.15.20.43.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 20:43:17 -0800 (PST) From: FUJITA Tomonori To: linux-kernel@vger.kernel.org Cc: Andrew Lunn , rust-for-linux@vger.kernel.org, netdev@vger.kernel.org, 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 v8 7/7] net: phy: qt2025: Wait until PHY becomes ready Date: Thu, 16 Jan 2025 13:40:59 +0900 Message-ID: <20250116044100.80679-8-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250116044100.80679-1-fujita.tomonori@gmail.com> References: <20250116044100.80679-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. Reviewed-by: Andrew Lunn Signed-off-by: FUJITA Tomonori Reviewed-by: Alice Ryhl --- 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 1ab065798175..f642831519ca 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(()) }