From nobody Mon Nov 25 20:25:02 2024 Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.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 8BB3418C03C; Fri, 25 Oct 2024 03:34:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729827255; cv=none; b=c2i7kuqP6zDKgX7dzkK5XSseGQogh6KnZR8XM3/ToZ2IEzM0KgTjSKkMDvK43FO3S6Q2WMA+zmP9pKmVv9pNqR2kbg6KW5F7wUJQlZUQsoKgcKzZGrasERuPgOGpBp3yieRWEZlwTSrI+KCA5zfUn2y2K2RVAogp/mbKqWv+D30= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729827255; c=relaxed/simple; bh=twzsMo9PnqE2SQYXonN8b7y9524aObqn6wJsaMoGgzI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bZeI15xvRSLN6fJhLK3RDU+eJkvnf3GTy6s8F620AN0bJdnYkw7jJYca2YXdn+OjRyMsRf41RxYcHBpz3xzV0/z8SW4hlwICZt6CMQrK2Hd9DCcoPZvU+I0CTBC5nJwBw2tNG5di8WwGXFEkOLQFRzJ/lU8KrVyzhSLAkU4PgrM= 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=LFLL+782; arc=none smtp.client-ip=209.85.215.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="LFLL+782" Received: by mail-pg1-f176.google.com with SMTP id 41be03b00d2f7-656d8b346d2so1011838a12.2; Thu, 24 Oct 2024 20:34:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729827253; x=1730432053; 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=7o5N7q9DkGBoncN0d1LZtLp06+Ve02cWoasnsmTQcfc=; b=LFLL+782Qik4GERgYlJP18F9a4XSzoQpWSh1XsQYIjRxX6xRxmKTXSAoa1oHNdov2B qARt3gtM6f2IAoV3oTq6TeDHE7qJXqjPUh9S5e+KJrexpZL8lSsKx4bJ8ax7dM2EfD1J VJjP65IvVNeRK1gML2DL13Pabr9Arn2AKS5FqBvkHEPkwdPgTSUzOfit8DspgoXWlz78 26cBHlxivooh57lUBYLxC411De9Xd1CDMSqYai7D+B9ii7VT9LeNN+VjeJLZMzQL3U13 62S8eMqNkKjLTpovWtJTE2Fxtu/1BNcN5jXk6JnRe8fysHDUiNxlieHrSteANoTPiXzV AHlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729827253; x=1730432053; 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=7o5N7q9DkGBoncN0d1LZtLp06+Ve02cWoasnsmTQcfc=; b=hiBoxiFYUHFnxZJ4GLM6Ro8NGAq/BBubFjZGe5o8amH0Nau7Ea2y+RivfJqah6pphS C4ofaEdKYzmS/F72xpTx+dFjbjJG1OtnOh8Ucu2QMznYqytuSAFA77RRNHboRSTlwleF d+JcsdJvhbxdaHummeemRODHYP8CWJEjexxEm8HZyBGMezxUFndakSix7Ti3zNAPAcah UcnSHAHrqJyl9earDoNeZYmyOkTcVqvXb0DLtLgWvZ8UInHlN9tYnmSla27qjzFulTtc 4Y9IlY46hKMH7KLcec76Ah42Agq+IYS4AvWuJ1eD590NqmT1KimK048gLeHG1gQLwp5w 4C8A== X-Forwarded-Encrypted: i=1; AJvYcCVLv/wInCn0BlmGD6B2VEJQd2ejruf8V2a0JK9gBgz7GJQIRzVr/q52n4k00pSCNq9B5utzha1VgW6JvY4=@vger.kernel.org, AJvYcCXRjcTreBt+hEJ7/iUVVEOfoLBq7szcFXDffy41r1BvyV++fqa9Yg6mypcFtT541M5JZDpvsGDmpH7ql/tdFOg=@vger.kernel.org X-Gm-Message-State: AOJu0YwZRw+KPUx4hUbFa6K9BG5Hb/eZd3U3hEUkidzvCYy3YLFjFMHi Xw87KwAyo5gMc873TiyMYxx7vNXrQzknk2Q3TMwvqG3C+8y3XBsj X-Google-Smtp-Source: AGHT+IGjLc7M9f2iO7YVpc18LmagGIS+w52tVqbv90ZJXP9NpG3GgF4k83ml1Oo8YOt0RHNh7QrBzw== X-Received: by 2002:a05:6a21:399:b0:1d7:11af:6a with SMTP id adf61e73a8af0-1d978bae474mr8379273637.37.1729827252854; Thu, 24 Oct 2024 20:34:12 -0700 (PDT) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7205791db5fsm180188b3a.11.2024.10.24.20.34.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2024 20:34:12 -0700 (PDT) From: FUJITA Tomonori To: anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, jstultz@google.com, sboyd@kernel.org, linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org, rust-for-linux@vger.kernel.org, andrew@lunn.ch, hkallweit1@gmail.com, tmgross@umich.edu, ojeda@kernel.org, alex.gaynor@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, arnd@arndb.de Subject: [PATCH v4 1/7] rust: time: Add PartialEq/Eq/PartialOrd/Ord trait to Ktime Date: Fri, 25 Oct 2024 12:31:12 +0900 Message-ID: <20241025033118.44452-2-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241025033118.44452-1-fujita.tomonori@gmail.com> References: <20241025033118.44452-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add PartialEq/Eq/PartialOrd/Ord trait to Ktime so two Ktime instances can be compared to determine whether a timeout is met or not. Use the derive implements; we directly touch C's ktime_t rather than using the C's accessors because more efficient and we already do in the existing code (Ktime::sub). Reviewed-by: Alice Ryhl Signed-off-by: FUJITA Tomonori Reviewed-by: Trevor Gross --- rust/kernel/time.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index e3bb5e89f88d..4a7c6037c256 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -27,7 +27,7 @@ pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies { =20 /// A Rust wrapper around a `ktime_t`. #[repr(transparent)] -#[derive(Copy, Clone)] +#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord)] pub struct Ktime { inner: bindings::ktime_t, } --=20 2.43.0 From nobody Mon Nov 25 20:25:02 2024 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A95C318BC2C; Fri, 25 Oct 2024 03:34:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729827259; cv=none; b=I4hoCOtowzbPisa8ksQ8WIYDdzw7ypPOj/C80Nx4r5gVib+LEAsW6HLzsNk7Kl5s+JtUpbn2y8T3iZsox3vdzmltBQY/1sYv9lKjtEcyZhFtgk6D0VQylezQveUOVfE75gWyT8v3yDW5Weuta1lbERI3Ft4AdJkbZX4CXpN5yrM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729827259; c=relaxed/simple; bh=WWGtuJTu9wvkpnnoH5LcrVExK3FmcpN1yivlxzaEd3c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MLJ+4UhQAFznijbVM6ORa34T1OgJ4JANJEVFp+TVf7Od2xkHS0YNgQ0I09l/NVLsj8arqP7O5wS95XgWjtxDxslRJFPnTqZ3W0HBP4mpp2Z8llchSivUbUNvoc6hRr31feUFLDL1tVJU8SNrEnnGhek7bOl5Jt04tt6ZDPfagBM= 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=B7soFECB; arc=none smtp.client-ip=209.85.210.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="B7soFECB" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-71e49ef3bb9so1033506b3a.1; Thu, 24 Oct 2024 20:34:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729827257; x=1730432057; 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=9mh8fo9bwk1aebkWeaKEj5F1BlHtvnaOEw/7UyLur6g=; b=B7soFECBFz2Goofp+0lJ7pQp539oe1R1JeVd1j8sFj0pZinEGtytKbMv5KB5FtFiab S5NT9ceB1aAsOeW6XCNzWoIK4Xkw98MGk2FXex1u+oSonsd4k42YhOWkOYh5p789AOQK MuJ1EE/L1QLncA89IGAbY2zYDYLILCTKi0jx6or2nS4v1AU4lnceJbclhwF1L2D+y1xe rZWV7UIt+Wa9VMeF3jmz3Nm6j4dHIYwdsyy9wkk+eWlWy9l7xqEUeTmXle+QSWEion5M gU+OqT8OXqnUwOdpglSltje4951KDFh9aCG5rqS8EeWtEqoQTx1KZ6Vg4l5L8mcu/+jm 88hQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729827257; x=1730432057; 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=9mh8fo9bwk1aebkWeaKEj5F1BlHtvnaOEw/7UyLur6g=; b=NeIib7vnkzJeStY0eryagfzvtCjWjVhJeELdXLMsq4wRlKDYEbX5RFRQj+bGfiaZHv +XyiCaXMIVYGTC6BXweL2EuXOfmsH6cyXtrR8TqgbCBoSyOPTrNp/TMA/30k1Anhtwdb r3AiFSTypBakEXcoJAVKtyqXnyHnbFQ+q3cLRq/VIp5/4nbtqbrj15OOqBKrjLT0Ct2X ASDWtJUbw5Vhia+Hp5t1M3DNOjnbvuST8pc3SiKDvR/UZv/SKS1XH75W7bhsNHIXlp+L BIw/NhMU9coYW5v8AM4Is9AdYSRxYFNG0bBo4MByfWJIzqoO1WtgAaPpC3Dq/pNcNqhk 5nDQ== X-Forwarded-Encrypted: i=1; AJvYcCUhMrv7igzVPLK8vUlg6BpMjFTQfXnxFIL7OcVxaswIeMhTrHg6fwfndeXJFY0Ncctr8jn1rl/FqBM11PRmntA=@vger.kernel.org, AJvYcCXSb0swQLXKaFax+uAFnguGxYI4FK5YBWPonznpy2+Cu+xDBIusobn9iO63nzW003HQXhuIb2N/pC6WcoQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxKI7S9ilzd1zUruJqbwoKI7vsHVjvDgpuLodACRPPaINUvkjsT k0Lv07M3B5bBtNtECDem0bXPQwWUejQrwV5kUb4Dtk3PAjHRpbrK X-Google-Smtp-Source: AGHT+IEn5Qc+9HP5rdo2t6yCvtM4U01ZPbrRv09yZk7sTwAUlKD6bKTKT+vNP8qetO6KZzb72CMkaA== X-Received: by 2002:a05:6a00:2ea9:b0:71e:6fcb:7693 with SMTP id d2e1a72fcca58-720452ddbf9mr5562279b3a.12.1729827256864; Thu, 24 Oct 2024 20:34:16 -0700 (PDT) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7205791db5fsm180188b3a.11.2024.10.24.20.34.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2024 20:34:16 -0700 (PDT) From: FUJITA Tomonori To: anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, jstultz@google.com, sboyd@kernel.org, linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org, rust-for-linux@vger.kernel.org, andrew@lunn.ch, hkallweit1@gmail.com, tmgross@umich.edu, ojeda@kernel.org, alex.gaynor@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, arnd@arndb.de Subject: [PATCH v4 2/7] rust: time: Introduce Delta type Date: Fri, 25 Oct 2024 12:31:13 +0900 Message-ID: <20241025033118.44452-3-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241025033118.44452-1-fujita.tomonori@gmail.com> References: <20241025033118.44452-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce a type representing a span of time. Define our own type because `core::time::Duration` is large and could panic during creation. time::Ktime could be also used for time duration but timestamp and timedelta are different so better to use a new type. i64 is used instead of u64 to represent a span of time; some C drivers uses negative Deltas and i64 is more compatible with Ktime using i64 too (e.g., ktime_[us|ms]_delta() APIs return i64 so we create Delta object without type conversion. i64 is used instead of bindings::ktime_t because when the ktime_t type is used as timestamp, it represents values from 0 to KTIME_MAX, which different from Delta. Delta::from_[millis|secs] APIs take i64. When a span of time overflows, i64::MAX is used. Signed-off-by: FUJITA Tomonori Reviewed-by: Andrew Lunn --- rust/kernel/time.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 4a7c6037c256..574e72d3956b 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -8,9 +8,15 @@ //! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h). //! C header: [`include/linux/ktime.h`](srctree/include/linux/ktime.h). =20 +/// The number of nanoseconds per microsecond. +pub const NSEC_PER_USEC: i64 =3D bindings::NSEC_PER_USEC as i64; + /// The number of nanoseconds per millisecond. pub const NSEC_PER_MSEC: i64 =3D bindings::NSEC_PER_MSEC as i64; =20 +/// The number of nanoseconds per second. +pub const NSEC_PER_SEC: i64 =3D bindings::NSEC_PER_SEC as i64; + /// The time unit of Linux kernel. One jiffy equals (1/HZ) second. pub type Jiffies =3D core::ffi::c_ulong; =20 @@ -81,3 +87,46 @@ 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 milliseconds. + #[inline] + pub const fn from_millis(millis: i64) -> Self { + Self { + nanos: millis.saturating_mul(NSEC_PER_MSEC), + } + } + + /// Create a new `Delta` from a number of seconds. + #[inline] + pub const fn from_secs(secs: i64) -> Self { + Self { + nanos: secs.saturating_mul(NSEC_PER_SEC), + } + } + + /// Return `true` if the `Detla` spans no time. + #[inline] + pub fn is_zero(self) -> bool { + self.as_nanos() =3D=3D 0 + } + + /// Return the number of nanoseconds in the `Delta`. + #[inline] + pub fn as_nanos(self) -> i64 { + self.nanos + } + + /// Return the smallest number of microseconds greater than or equal + /// to the value in the `Delta`. + #[inline] + pub fn as_micros_ceil(self) -> i64 { + self.as_nanos().saturating_add(NSEC_PER_USEC - 1) / NSEC_PER_USEC + } +} --=20 2.43.0 From nobody Mon Nov 25 20:25:02 2024 Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) (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 B8C2718DF7E; Fri, 25 Oct 2024 03:34:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729827263; cv=none; b=mhzj3VSJlPhTH6ij+sJhnOssHwNlk4j8LeN4X6KTEJBusN4CDFNZxsMc1uQl9mcwtUShumLvUHvuAKYiUmp1mGmOCj85/N6gGtMxSpnhkBfkfhh8uui1d5vO8htdPAEAE9WmtcJnXx5TNUxqxlJnlnDFqcGXvFKUnWp0C2FzHWI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729827263; c=relaxed/simple; bh=EtK+d2Bje3STOCATrvNiA6gB5neTb9iViHm5w9db8Q4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=J2EIX8Qa3EaiZTYiOLMgmBU8yWX5lRf0oTT/ZSAulnJUwpwUb5JOAF/FmWlICBzysjIsVEP75cSQYvFlG6yOTOv1Wm/hjUaRuxSBxUiM5BOLM1DWUppb/v9GQT7h0xHIUc9TaWJJLgEfedcwdC/zwdImKLtGDP52JCg5D51xDec= 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=ePRO08Ss; arc=none smtp.client-ip=209.85.215.174 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="ePRO08Ss" Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-7ea8de14848so877121a12.2; Thu, 24 Oct 2024 20:34:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729827261; x=1730432061; 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=O+mkaZsnPSyKLPGmS4/PHft/GFdWBAEjk0wbqgKsTGI=; b=ePRO08Sss2o/zfUT7f9SD4GRHYGPtXIi3whtG81QJtQgw7+/SQtkePaLDqElEcFFIb I90gnUjulkJZZf/jd9vZ3Xc03SvCnnypDjrhKAPhvliZHFGblHbPGwKreosRYZ7jRnBU K1z+EDhmbVQLofNgwpZG2I9lGJottf9K4LcYkiR2A7sPct38mTVQ7wzby2VDxTNJhSvd yJFekUw6ZVYI99SBhH8mvmXdOcqOSKG1M9MzSXgH7/GFpGLCW2PGc2b13vAxzi+h9DG8 6uOeGtNfz8xDa+K0JPBKBELnyTcwF+BWJQrtNUaFBsQB0Gd8WDh+bir8K/is6Ark+34h S/cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729827261; x=1730432061; 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=O+mkaZsnPSyKLPGmS4/PHft/GFdWBAEjk0wbqgKsTGI=; b=mjwJ4JcXqpVYk5IlJmpch0S8bEkPMFoh0ypx9n+awN05BMhV/Yliv5mx9BMxdCzrFe WpxBdi0n3c9oTYuGPcYZJljjc5CK15uTcbCZLrvOcTCH8IaK+WOPUdGVD62GxqzCS5oq LgYL55rFrqpyZd3hYc9HVF9y9Hd0fT3X7xm4bsW+FW5R48Nl1f2eSEXZX75Oc3JLxsHO mpcEScmSLUDdvd6KK3pe9TPaONCFlM07wp8gwzuYkubm9H/3vJvupzGz65dsVt83gY4J RBCFYErqrVC7hrEZFKvxwce9gsksVxWaGdiONGzkzrx7tYFSYf+bu9K0UiIigSVHpuyH 9Pag== X-Forwarded-Encrypted: i=1; AJvYcCUv7Lz/q+YaSgDyK45kH6zpGf4Ys9eiYfc7ChpKR7ZV3CGZnnbsSSH/x7Sx6yBGW+LwHomIGJ7FFx2TC45r/Nw=@vger.kernel.org, AJvYcCWrqSUwKCVB9wcxHMDwb4uCZm7kB42tMqIrObknaitNkcLe2qxzqpyieKEZt346qYGcRnd+WvVIEuN9S78=@vger.kernel.org X-Gm-Message-State: AOJu0YxURBNe5Kaf4ngMvuJ1n3bA4SRyAyqXQF4uz5YlaY7UnDNgvvSX 2Gx6AJl+GAnZQpXpVZpb+khBPwKirrRRvivFiIBea2WT40Bw6bo3 X-Google-Smtp-Source: AGHT+IGyfCOgPEOZjm1g0kwLiXPL5cQTwDOEe0Vo+JvYtp44kWIRMIRRGKl06H8fvAWjGdx16rCBhw== X-Received: by 2002:a05:6a21:6711:b0:1d9:28f8:f27f with SMTP id adf61e73a8af0-1d978bad215mr9974606637.39.1729827260898; Thu, 24 Oct 2024 20:34:20 -0700 (PDT) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7205791db5fsm180188b3a.11.2024.10.24.20.34.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2024 20:34:20 -0700 (PDT) From: FUJITA Tomonori To: anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, jstultz@google.com, sboyd@kernel.org, linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org, rust-for-linux@vger.kernel.org, andrew@lunn.ch, hkallweit1@gmail.com, tmgross@umich.edu, ojeda@kernel.org, alex.gaynor@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, arnd@arndb.de Subject: [PATCH v4 3/7] rust: time: Introduce Instant type Date: Fri, 25 Oct 2024 12:31:14 +0900 Message-ID: <20241025033118.44452-4-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241025033118.44452-1-fujita.tomonori@gmail.com> References: <20241025033118.44452-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce a type representing a specific point in time. We could use the Ktime type but C's ktime_t is used for both timestamp and timedelta. To avoid confusion, introduce a new Instant type for timestamp. Rename Ktime to Instant and modify their methods for timestamp. Implement the subtraction operator for Instant: Delta =3D Instant A - Instant B The operation never overflows (Instant ranges from 0 to `KTIME_MAX`). Signed-off-by: FUJITA Tomonori Reviewed-by: Boqun Feng --- 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 574e72d3956b..3cc1a8a76777 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -31,59 +31,43 @@ pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies { unsafe { bindings::__msecs_to_jiffies(msecs) } } =20 -/// A Rust wrapper around a `ktime_t`. +/// A specific point in time. #[repr(transparent)] #[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord)] -pub struct Ktime { +pub struct Instant { + // Range from 0 to `KTIME_MAX`. inner: bindings::ktime_t, } =20 -impl Ktime { - /// Create a `Ktime` from a raw `ktime_t`. +impl Instant { + /// Create a `Instant` from a raw `ktime_t`. #[inline] - pub fn from_raw(inner: bindings::ktime_t) -> Self { + fn from_raw(inner: bindings::ktime_t) -> Self { Self { inner } } =20 /// Get the current time using `CLOCK_MONOTONIC`. #[inline] - pub fn ktime_get() -> Self { + pub fn now() -> Self { // SAFETY: It is always safe to call `ktime_get` outside of NMI co= ntext. Self::from_raw(unsafe { bindings::ktime_get() }) } =20 - /// Divide the number of nanoseconds by a compile-time constant. #[inline] - fn divns_constant(self) -> i64 { - self.to_ns() / DIV - } - - /// Returns the number of nanoseconds. - #[inline] - pub fn to_ns(self) -> i64 { - self.inner - } - - /// Returns the number of milliseconds. - #[inline] - pub fn to_ms(self) -> i64 { - self.divns_constant::() + /// Return the amount of time elapsed since the `Instant`. + pub fn elapsed(&self) -> Delta { + Self::now() - *self } } =20 -/// Returns the number of milliseconds between two ktimes. -#[inline] -pub fn ktime_ms_delta(later: Ktime, earlier: Ktime) -> i64 { - (later - earlier).to_ms() -} - -impl core::ops::Sub for Ktime { - type Output =3D Ktime; +impl core::ops::Sub for Instant { + type Output =3D Delta; =20 + // never overflows #[inline] - fn sub(self, other: Ktime) -> Ktime { - Self { - inner: self.inner - other.inner, + fn sub(self, other: Instant) -> Delta { + Delta { + nanos: self.inner - other.inner, } } } --=20 2.43.0 From nobody Mon Nov 25 20:25:02 2024 Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.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 D07CF18C026; Fri, 25 Oct 2024 03:34:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729827269; cv=none; b=pb2wTVZ7cBuN7RtPFXOXX2WphpGx28LAOqS3MqLoZUc8S9LB5lu7RuDr48BkbMPqE/AwdMAloPCu9BLtTiyDNd5SjWGHDfhNCrKNV+HNb6ui3Fni6CG9mGHhPZVeuas3I0Ovh/uMOrOeLUVtWQhTNhgoG5eIrbZ+OifVY8chFyI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729827269; c=relaxed/simple; bh=mWHpPnrTF6Q0Hm9rpOdF2THfrTVg9CjRiAj6+nSsZHQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SF2KD8OY2WxJ1hQ37vY2TzLZyPMMffjykp/kgx8dSxnt7HDj+L0Bd/IZibURSdZQ6EfRADmf26LzPpyQP6vm1xoVaP9Nlnu510ZYwWdlEcPhvABM8W9LoV7iMJdRuV2ZM/kmM6a8NaFnnMxPnBWCiHRz/6XCZlCar/NAEOKoahg= 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=k7HV722M; arc=none smtp.client-ip=209.85.215.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="k7HV722M" Received: by mail-pg1-f169.google.com with SMTP id 41be03b00d2f7-7ea9739647bso1055376a12.0; Thu, 24 Oct 2024 20:34:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729827265; x=1730432065; 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=2Wl4GcGidfDtEyCvPxvj7s9PVtdkPVOt/OW937eNjmM=; b=k7HV722MnuP3K8DxKZSYSgXxj2prQ0uI00o+dsb/8aP3HEQvqxrqrgrpYHSaz6WO98 r39fq/JApOybN1tDUgMrp9zeFSiC+XbjT74p+YIVbJDntjQGI+66hMn2vMwaJWfDX2gB i/9XYUP6fygq686E9sImFBFD3tEXRqf0NqIso+i49ZgiNRP3mTZ31d/U34nDJirwb0yh UL6+dY/YJazHk1GecPQmPgjqr3DMxWLhwQTk8lYIqbbni3TTJ5UbPc7F0dL3TNWL+KTR al0AY4LAChlOIwVT5/ih6a1mfpu2477mGz4ZSLxUsJZTvir4QIQJBp35a+gOY/a8UK5E YpvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729827265; x=1730432065; 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=2Wl4GcGidfDtEyCvPxvj7s9PVtdkPVOt/OW937eNjmM=; b=lKoUpUcu4rm9ux//fcls4lQOm9eAUBIf5q+9GcFQwd34wrX3ffXsGb6ZPxw+lj9QzB DDTrBOQYaLGGc38uausbeLyVOFGgDcwxeUddoIytCPsEXFTOhNh6+E4MZWvWZcatXIdD hQxrJAkYK+ZgSDETZ6MEouca81vESkUeBdQ1wwS7Oot9PpOKHZYbnay66dwIHKtttity gubIDyNBSlQRh3M73Q6VCYHbQr9RaJpxEdawSDnlUcIVqEtCflHD6ikxwLn21hEP2LGG U3yyb8oaj/HzW8cvp96Goe9Jyo5Z/CHQ+xzh8rbbfrjVd5He9w/xA3axnRKgsN51qrdL TELQ== X-Forwarded-Encrypted: i=1; AJvYcCWl40DgPaDZSBBMCvOyZ32OwvlRdxzdFqE4WsD9SnwjXdIa2iVsqSzIXVDbZGOHjhBOrVyHWXcM09kr/TkE9D8=@vger.kernel.org, AJvYcCXnhFHvMMTg24MpIfIZ/35CDvaTtrI5olqGU3sAeA7xwuxA+h9c9cCAwNamFc8YEoUYp3mij1Isx+KQnNw=@vger.kernel.org X-Gm-Message-State: AOJu0YxcYJ4/1j4X/Mr5RS91irEoGm7TV9RNNP0atQv3Rqa0uYR2Wohp 5hPR8eGyZGvfJBqBXT18pTyLNtzWLOULVe9UtuUHORnQ3IcOv6Wc X-Google-Smtp-Source: AGHT+IENknjlGegsPU/e1TwHHmrvQOxgBVrGDqADb+JbLIbhsoAzJAABlkXR9uMzGPrG7+R6ORm8eQ== X-Received: by 2002:a05:6a21:9206:b0:1d9:21cb:ecdb with SMTP id adf61e73a8af0-1d989d039f8mr4980104637.41.1729827264905; Thu, 24 Oct 2024 20:34:24 -0700 (PDT) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7205791db5fsm180188b3a.11.2024.10.24.20.34.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2024 20:34:24 -0700 (PDT) From: FUJITA Tomonori To: anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, jstultz@google.com, sboyd@kernel.org, linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org, rust-for-linux@vger.kernel.org, andrew@lunn.ch, hkallweit1@gmail.com, tmgross@umich.edu, ojeda@kernel.org, alex.gaynor@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, arnd@arndb.de Subject: [PATCH v4 4/7] rust: time: Add wrapper for fsleep function Date: Fri, 25 Oct 2024 12:31:15 +0900 Message-ID: <20241025033118.44452-5-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241025033118.44452-1-fujita.tomonori@gmail.com> References: <20241025033118.44452-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a wrapper for fsleep, flexible sleep functions in `include/linux/delay.h` which typically deals with hardware delays. The kernel supports several `sleep` functions to handle various lengths of delay. This adds fsleep, automatically chooses the best sleep method based on a duration. `sleep` functions including `fsleep` belongs to TIMERS, not TIMEKEEPING. They are maintained separately. rust/kernel/time.rs is an abstraction for TIMEKEEPING. To make Rust abstractions match the C side, add rust/kernel/time/delay.rs for this wrapper. fsleep() can only be used in a nonatomic context. This requirement is not checked by these abstractions, but it is intended that klint [1] or a similar tool will be used to check it in the future. Link: https://rust-for-linux.com/klint [1] Signed-off-by: FUJITA Tomonori --- rust/helpers/helpers.c | 1 + rust/helpers/time.c | 8 ++++++++ rust/kernel/time.rs | 4 +++- rust/kernel/time/delay.rs | 30 ++++++++++++++++++++++++++++++ 4 files changed, 42 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 3cc1a8a76777..cfc31f908710 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -2,12 +2,14 @@ =20 //! Time related primitives. //! -//! This module contains the kernel APIs related to time and timers that +//! This module contains the kernel APIs related to time that //! have been ported or wrapped for usage by Rust code in the kernel. //! //! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h). //! C header: [`include/linux/ktime.h`](srctree/include/linux/ktime.h). =20 +pub mod delay; + /// The number of nanoseconds per microsecond. pub const NSEC_PER_USEC: i64 =3D bindings::NSEC_PER_USEC as i64; =20 diff --git a/rust/kernel/time/delay.rs b/rust/kernel/time/delay.rs new file mode 100644 index 000000000000..f80f35f50949 --- /dev/null +++ b/rust/kernel/time/delay.rs @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Delay and sleep primitives. +//! +//! This module contains the kernel APIs related to delay and sleep that +//! have been ported or wrapped for usage by Rust code in the kernel. +//! +//! C header: [`include/linux/delay.h`](srctree/include/linux/delay.h). + +use crate::time; +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. +/// +/// The function sleeps infinitely (MAX_JIFFY_OFFSET) if `Delta` is negati= ve +/// or exceedes i32::MAX milliseconds. +/// +/// This function can only be used in a nonatomic context. +pub fn fsleep(delta: time::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(delta.as_micros_ceil() as c_ulong) + } +} --=20 2.43.0 From nobody Mon Nov 25 20:25:02 2024 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (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 B9C8E18C029; Fri, 25 Oct 2024 03:34:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729827271; cv=none; b=n7tKpLvp9Vi5Ix1xbe29xYM6Q2jIWoVqAWMUjOgzEppr3zBO3KFpKDxZe/Gcek7A/7qRyIXIwUlgOfv1UW3frgG5WXynJCGjcxtpUaQN16y295v54rq1XRbZMOyBIugqZfa5BCjFogVZgiUYxbZdeAgoIcRgIGw8AN2B/bCo+bg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729827271; c=relaxed/simple; bh=YZIDna+Nw7Nb23XtlYPw5rT7pMv1qHX5PotS8Nu/1Lg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Xl5mpQikZYaVLyVljocVZHYwhXIrtfr8w7kBGbcsAPt3oEEm1THMqDYiMCakqSlnk2eKIg4XaRCDlo2859+j3CZK+ezwq7hiFbg5ZC7Ep7FLiwp0y58Q16lPMQBSR41T3bO5VlgSw0bYpTs8KsG4mX4o0Z4aTatMvg5paK+LJQg= 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=mb0vxuoQ; arc=none smtp.client-ip=209.85.210.179 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="mb0vxuoQ" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-71e49ad46b1so1067347b3a.1; Thu, 24 Oct 2024 20:34:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729827269; x=1730432069; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=RjDi4l6vEO1+WFS2aicMTlZT3n6TL7cER+Vr/4E6ZEo=; b=mb0vxuoQuU5XF1bjDLkAaztl+jxOpNdh9lXOoF8WleSOR5xUfs42hbwcw8T/5uCKQh siMQZDsSaozrScWF3oamxkZhOIXZVBO+1zvSzu1si9LEhLvOUypBB+2yPU/BRbUGx2Tv HmD/qHrd/4lHIukr+8JjcZCJOheuRRNT4BBcBWyYVXLqQb2wGFWkgDaGtg7xCYoXaW02 JqvdyyQFzwM1sLONEMnP0PiOFQyLxHPvrMq3eC/nq9fl4fEuTk9C/bYZ8O4x+KLwu7uY 3uKqZRSQnc0BYOrgb3khwWnIAoL6R0lBgnUX/kdrGYi7FuUWQp0pOo2sc/4vSGOeCNFO W26A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729827269; x=1730432069; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RjDi4l6vEO1+WFS2aicMTlZT3n6TL7cER+Vr/4E6ZEo=; b=igU13Cl1rGKkdZhCZFg2tG03Y7nrmgltoxFyN6Po/dx2Jvr+VLcQI3WPhf7O3bXSFJ 1MCTu3WLzWpH0BeqF06jH0ORwNXvkDup8NSeSo3BWmn76fbkyzEa/TzLG1Pl9JwnHhm6 CQmQ4QJKddk5kis5Ubwkeep1vbddk78BpO832LVxneyXrM3buai51Gj8iIyLpJuspbwx kw6UH7IPTJbXVVmt2P+9GLmDGMQqrNZlmGXsFybjrZUsDHD1tbe4KT+Dd1dsaUA/RZd0 3aYaI6q24a0TzjcpIlwbvc+qchVjhUMAdD4Z2I9xNvMnKWsdZNClNXNeGAMO9FnA38E0 9VAg== X-Forwarded-Encrypted: i=1; AJvYcCUEKv9GfSnEp0WCsZdxNHATtY9rX4o7gSfSc65Teazvy7R1wjww2kqrAx/EVGp3/FoCQ2RGkFnHfj8snuY=@vger.kernel.org, AJvYcCVkNtNgVV3T0iCykNsKotCW1sXitoluYSvPvZwiKPJT1+mnYTNnAL+DaQLboUSuEkwzwG94n6KaXVbR0C/YlBo=@vger.kernel.org X-Gm-Message-State: AOJu0YxS+Jna7U3OWOU0jgjZI8lCNGA4T1FTEunJulUm5kWyJFjPtWHs CfDFpmfvZYGGS6zi2hexY+rylKIZYjfNyrtcMnGplvPUsrsSsxLH X-Google-Smtp-Source: AGHT+IGj8D98wvdlX2ZQTp8eyWXOuPeE4coGL4RY/0PkUBoLKhcwVaWnypW9CYrjTNiIO7nwt++/Iw== X-Received: by 2002:a05:6a00:22cc:b0:71e:6122:5919 with SMTP id d2e1a72fcca58-72045f8ebaamr5022392b3a.20.1729827268930; Thu, 24 Oct 2024 20:34:28 -0700 (PDT) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7205791db5fsm180188b3a.11.2024.10.24.20.34.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2024 20:34:28 -0700 (PDT) From: FUJITA Tomonori To: anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, jstultz@google.com, sboyd@kernel.org, linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org, rust-for-linux@vger.kernel.org, andrew@lunn.ch, hkallweit1@gmail.com, tmgross@umich.edu, ojeda@kernel.org, alex.gaynor@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, arnd@arndb.de Subject: [PATCH v4 5/7] MAINTAINERS: rust: Add TIMEKEEPING and TIMER abstractions Date: Fri, 25 Oct 2024 12:31:16 +0900 Message-ID: <20241025033118.44452-6-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241025033118.44452-1-fujita.tomonori@gmail.com> References: <20241025033118.44452-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add Rust TIMEKEEPING and TIMER abstractions to the maintainers entry respectively. Signed-off-by: FUJITA Tomonori --- MAINTAINERS | 2 ++ 1 file changed, 2 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 2250eb10ece1..6fb56965b4c2 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -10171,6 +10171,7 @@ F: kernel/time/sleep_timeout.c F: kernel/time/timer.c F: kernel/time/timer_list.c F: kernel/time/timer_migration.* +F: rust/kernel/time/delay.rs F: tools/testing/selftests/timers/ =20 HIGH-SPEED SCC DRIVER FOR AX.25 @@ -23366,6 +23367,7 @@ F: kernel/time/timeconv.c F: kernel/time/timecounter.c F: kernel/time/timekeeping* F: kernel/time/time_test.c +F: rust/kernel/time.rs F: tools/testing/selftests/timers/ =20 TIPC NETWORK LAYER --=20 2.43.0 From nobody Mon Nov 25 20:25:02 2024 Received: from mail-pg1-f181.google.com (mail-pg1-f181.google.com [209.85.215.181]) (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 C68A31ACDF0; Fri, 25 Oct 2024 03:34:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729827276; cv=none; b=FqMSCJRWcY9H5ELAbuIqWJIdGN/beYt1rPvEx2Hegle9dTCHkc/4XWW+v49odj/sLbPRPFVffA/m/FsdJz6N/OSlQI3uKlE2haDBzk3D778HB6kcizq6xv9Aj5RD5qtnODI/EGyhF/HkYmAX7PSQJzHOZLXbQ9WJf/5SJBmbgj4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729827276; c=relaxed/simple; bh=pZsVeffyezjeq+ZCP4S1Z1TvMIkWwVO0ML0qhzsHTg8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Bf4hxavTUwKjbNnJTt543Xm1MA/p0XXuXpp4qt0zSbXVz8u/EAAmYEEoxVotTsqhmCG24qrUzaSdy24QREeecv2yqRQx0F7SepWvg0za2kt2Z3EuwiV0DPACcxZGmmaA0BUPFzCeDQiRwDGBvaYl7b4E6CjuqQPV8tf6i860HsU= 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=Nz7XP32d; arc=none smtp.client-ip=209.85.215.181 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="Nz7XP32d" Received: by mail-pg1-f181.google.com with SMTP id 41be03b00d2f7-7e9e38dd5f1so1152372a12.0; Thu, 24 Oct 2024 20:34:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729827273; x=1730432073; 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=Z1q9ggByLgvF29xNghA+oLeekFn/dFvbYSRczZS/Rr0=; b=Nz7XP32dmmFSFZ4i2h7KnfPiYY7l8Z/T5F0YDuXnedc4VFICoBSOdW1gxw/Hbgvgl3 gDC/sX7P5cbaa1YlgVsP1qpmwlDi4YBI1rjHlJ1GpMbQZ+1HoDQIhxjBCNM3vxA7WhLd ze/V0JyI3OfS20D776J6rxhm8AU+qwBU2rydDX8QEvY67CI+uSWC3euJ1v9KAmwpNpEy B1BRM72YHmniMvimCuoioSxMSZPFSeNh7GY1eRZaQZmji3kQg3ooxnlspFe8PC1OlfqC w6crVx+Z45OeyXnYZUSJK9SyKNz434pjWRZDWkbp/keSbFCXZaWHLAw/HteKFDP8uaF4 s7fQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729827273; x=1730432073; 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=Z1q9ggByLgvF29xNghA+oLeekFn/dFvbYSRczZS/Rr0=; b=mrBJ1CiEYGoV40J5RIgFNdArzGO4rrJPGr8TRz0qVGDLOJi256ADV40WpFvxBYkocu sYclpVzUDZ5R+PVHp1OeqLEQ1BnniBZrmAZPj2kVx10LQvLf6mXEOyAEUmqTQOafi82E qbfbOQ1WEuDnRPZNCkHZgj5Z69TSJ1tehJyBcdgXs1A7+t1B32A+gOfodLp8iiXm/zER qwtBGn201f+EmYc5b67oIVFT1sshz1jBXdn3QWqD/ykHgUxqy+ogipHR/bVckTmZMHW4 NyCxzIJAUEOixBmL+F9hIvuktA4m4COonSIaU4jjQg68SR4H5GBNUZ5xEL5Dlxn/r8Sk qIlg== X-Forwarded-Encrypted: i=1; AJvYcCUXMrlhgkxlVSOZsz5ojhDm5MMIC9uYmfNKbEDo63KSjlJB0AR8uKMSghluNRN8PxSMsKSXTWZNaY9j5KQ=@vger.kernel.org, AJvYcCWNZsYu7PX7TAhlbD7ykn6B8gg0vn/f4einP+smBAu1Cf/ORK1mXe5xLiLqlhc6Nga06U9B7r3dBjmWrvCtkYg=@vger.kernel.org X-Gm-Message-State: AOJu0YyPSWObFYKFYv/LDwjrRk3tpGygJHJbc+S/Myk/90/wvKLOIL9+ u1swub5IbrBUFXaRjKoGZbm5PBiRv6Jp34n1yAy1anyOe6XujQif X-Google-Smtp-Source: AGHT+IGIUPkN+OqnxUUcmRWusqknbhO05BPVVFPbEuKkJIMkAd+D/2fn/uzB7S5maSTcz6zJeA8RPA== X-Received: by 2002:a05:6a20:db0a:b0:1d9:a9b:28f0 with SMTP id adf61e73a8af0-1d989cc8f66mr5807282637.34.1729827272976; Thu, 24 Oct 2024 20:34:32 -0700 (PDT) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7205791db5fsm180188b3a.11.2024.10.24.20.34.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2024 20:34:32 -0700 (PDT) From: FUJITA Tomonori To: anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, jstultz@google.com, sboyd@kernel.org, linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org, rust-for-linux@vger.kernel.org, andrew@lunn.ch, hkallweit1@gmail.com, tmgross@umich.edu, ojeda@kernel.org, alex.gaynor@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, arnd@arndb.de Subject: [PATCH v4 6/7] rust: Add read_poll_timeout functions Date: Fri, 25 Oct 2024 12:31:17 +0900 Message-ID: <20241025033118.44452-7-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241025033118.44452-1-fujita.tomonori@gmail.com> References: <20241025033118.44452-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add read_poll_timeout functions which poll periodically until a condition is met or a timeout is reached. C's read_poll_timeout (include/linux/iopoll.h) is a complicated macro and a simple wrapper for Rust doesn't work. So this implements the same functionality in Rust. The C version uses usleep_range() while the Rust version uses fsleep(), which uses the best sleep method so it works with spans that usleep_range() doesn't work nicely with. Unlike the C version, __might_sleep() is used instead of might_sleep() to show proper debug info; the file name and line number. might_resched() could be added to match what the C version does but this function works without it. The sleep_before_read argument isn't supported since there is no user for now. It's rarely used in the C version. For the proper debug info, readx_poll_timeout() and __might_sleep() are implemented as a macro. We could implement them as a normal function if there is a clean way to get a null-terminated string without allocation from core::panic::Location::file(). readx_poll_timeout() can only be used in a nonatomic context. This requirement is not checked by these abstractions, but it is intended that klint [1] or a similar tool will be used to check it in the future. Link: https://rust-for-linux.com/klint [1] Signed-off-by: FUJITA Tomonori --- rust/helpers/helpers.c | 1 + rust/helpers/kernel.c | 13 ++++++ rust/kernel/error.rs | 1 + rust/kernel/io.rs | 5 +++ rust/kernel/io/poll.rs | 93 ++++++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + rust/kernel/processor.rs | 13 ++++++ 7 files changed, 128 insertions(+) create mode 100644 rust/helpers/kernel.c create mode 100644 rust/kernel/io.rs create mode 100644 rust/kernel/io/poll.rs create mode 100644 rust/kernel/processor.rs diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c index c274546bcf78..f9569ff1717e 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -12,6 +12,7 @@ #include "build_assert.c" #include "build_bug.c" #include "err.c" +#include "kernel.c" #include "kunit.c" #include "mutex.c" #include "page.c" diff --git a/rust/helpers/kernel.c b/rust/helpers/kernel.c new file mode 100644 index 000000000000..da847059260b --- /dev/null +++ b/rust/helpers/kernel.c @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +void rust_helper_cpu_relax(void) +{ + cpu_relax(); +} + +void rust_helper___might_sleep(const char *file, int line) +{ + __might_sleep(file, line); +} diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs index 6f1587a2524e..d571b9587ed6 100644 --- a/rust/kernel/error.rs +++ b/rust/kernel/error.rs @@ -58,6 +58,7 @@ macro_rules! declare_err { declare_err!(EPIPE, "Broken pipe."); declare_err!(EDOM, "Math argument out of domain of func."); declare_err!(ERANGE, "Math result not representable."); + declare_err!(ETIMEDOUT, "Connection timed out."); declare_err!(ERESTARTSYS, "Restart the system call."); declare_err!(ERESTARTNOINTR, "System call was interrupted by a signal = and will be restarted."); declare_err!(ERESTARTNOHAND, "Restart if no handler."); diff --git a/rust/kernel/io.rs b/rust/kernel/io.rs new file mode 100644 index 000000000000..033f3c4e4adf --- /dev/null +++ b/rust/kernel/io.rs @@ -0,0 +1,5 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Input and Output. + +pub mod poll; diff --git a/rust/kernel/io/poll.rs b/rust/kernel/io/poll.rs new file mode 100644 index 000000000000..ef1f38b59fda --- /dev/null +++ b/rust/kernel/io/poll.rs @@ -0,0 +1,93 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! IO polling. +//! +//! C header: [`include/linux/iopoll.h`](srctree/include/linux/iopoll.h). + +use crate::{ + error::{code::*, Result}, + processor::cpu_relax, + time::{delay::fsleep, Delta, Instant}, +}; + +/// Polls periodically until a condition is met or a timeout is reached. +/// +/// Public but hidden since it should only be used from public macros. +#[doc(hidden)] +pub fn read_poll_timeout( + mut op: Op, + cond: Cond, + sleep_delta: Delta, + timeout_delta: Delta, +) -> Result +where + Op: FnMut() -> Result, + Cond: Fn(T) -> bool, +{ + let start =3D Instant::now(); + let sleep =3D !sleep_delta.is_zero(); + let timeout =3D !timeout_delta.is_zero(); + + let val =3D loop { + let val =3D op()?; + if cond(val) { + // Unlike the C version, we immediately return. + // We know a condition is met so we don't need to check again. + return Ok(val); + } + if timeout && start.elapsed() > timeout_delta { + // Should we return Err(ETIMEDOUT) here instead of call op() a= gain + // wihout a sleep between? But we follow the C version. op() c= ould + // take some time so might be worth checking again. + break op()?; + } + if sleep { + fsleep(sleep_delta); + } + // fsleep() could be busy-wait loop so we always call cpu_relax(). + cpu_relax(); + }; + + if cond(val) { + Ok(val) + } else { + Err(ETIMEDOUT) + } +} + +/// Print debug information if it's called inside atomic sections. +#[macro_export] +macro_rules! __might_sleep { + () =3D> { + #[cfg(CONFIG_DEBUG_ATOMIC_SLEEP)] + // SAFETY: FFI call. + unsafe { + $crate::bindings::__might_sleep( + c_str!(::core::file!()).as_char_ptr(), + ::core::line!() as i32, + ) + } + }; +} + +/// Polls periodically until a condition is met or a timeout is reached. +/// +/// `op` is called repeatedly until `cond` returns `true` or the timeout is +/// reached. The return value of `op` is passed to `cond`. +/// +/// `sleep_delta` is the duration to sleep between calls to `op`. +/// If `sleep_delta` is less than one microsecond, the function will busy-= wait. +/// +/// `timeout_delta` is the maximum time to wait for `cond` to return `true= `. +/// +/// This macro can only be used in a nonatomic context. +#[macro_export] +macro_rules! readx_poll_timeout { + ($op:expr, $cond:expr, $sleep_delta:expr, $timeout_delta:expr) =3D> {{ + if !$sleep_delta.is_zero() { + $crate::__might_sleep!(); + } + + $crate::io::poll::read_poll_timeout($op, $cond, $sleep_delta, $tim= eout_delta) + }}; +} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 22a3bfa5a9e9..b775fd1c9be0 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -35,6 +35,7 @@ #[cfg(CONFIG_RUST_FW_LOADER_ABSTRACTIONS)] pub mod firmware; pub mod init; +pub mod io; pub mod ioctl; #[cfg(CONFIG_KUNIT)] pub mod kunit; @@ -44,6 +45,7 @@ pub mod page; pub mod prelude; pub mod print; +pub mod processor; pub mod sizes; pub mod rbtree; mod static_assert; diff --git a/rust/kernel/processor.rs b/rust/kernel/processor.rs new file mode 100644 index 000000000000..eeeff4be84fa --- /dev/null +++ b/rust/kernel/processor.rs @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Processor related primitives. +//! +//! C header: [`include/linux/processor.h`](srctree/include/linux/processo= r.h). + +/// Lower CPU power consumption or yield to a hyperthreaded twin processor. +/// +/// It also happens to serve as a compiler barrier. +pub fn cpu_relax() { + // SAFETY: FFI call. + unsafe { bindings::cpu_relax() } +} --=20 2.43.0 From nobody Mon Nov 25 20:25:02 2024 Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.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 D339D1B0F1C; Fri, 25 Oct 2024 03:34:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729827279; cv=none; b=gortbliKT+8oii0tTPaIbs39tdBiIiBJ/VjTXJTZcddFAKprLQdRjlXKlddyxIHXVNUGSraitcl8F8pHugGqC0FtH4Go9gjvt1yQsXO3KnJa4J2qMfqWIZivRUl2Xfvvd7r3hVUgtgm74uXKDPY22AJdVCM++SQP1niMnio+BkE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729827279; c=relaxed/simple; bh=jB92JpeDW2yhP2VRKGudKbbfDREsNE6Gpy5FBMVK5aM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tckyOhPpPKUS6sI3a5I23tv2nzvWtHdH53BW13lNmVqc0vdOYExH9l3a6uF+0tH6APXKUg02GPe+Ez3/IfNVPmlt+qicFHE9cf6inmAGVIGqPTFpgRBRvJfW4QgF2dyVvNm9RA48m1vwsRI056fkn6jP2/727sF2xjmOk8WsnUA= 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=QbgCZRqK; arc=none smtp.client-ip=209.85.215.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="QbgCZRqK" Received: by mail-pg1-f178.google.com with SMTP id 41be03b00d2f7-7ea8de14848so877201a12.2; Thu, 24 Oct 2024 20:34:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729827277; x=1730432077; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=mK2RJ1Xug5oMAzwhSCqVHPpqDu7p/3YqJqaxy25r6IM=; b=QbgCZRqKte0cIj2PmXXi1/D/AM/ld59kHK+Rz4IHb6EA8ztM1vEPQUVtD7yK7KuZA+ F7DG9W5aWUimm0B8GAstODa5WstGSp7FZe5NKffesTEeSqE9dVK+jQ6vLs+K6GHnrHQg sbRSzh2r8zqrZe9eP6vGmj7AtQMnIE4HY7Adtz3lgvZkNfe3lok0WxH8VWjCTy4rZnN7 uG9M9yv1rNzbPiMqv6m1h3f8Lo960m2oJsPBvcoivqEs6RuCxzruDgNPydY7MiJ1t8LZ 0xOh6pZgPzMbvy3iut2zeheiUcwaf3LegSaNxMchAhaK2t773jSy1/E2ooyl2oeZ7RKf WkOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729827277; x=1730432077; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mK2RJ1Xug5oMAzwhSCqVHPpqDu7p/3YqJqaxy25r6IM=; b=U/Y357W9qT5FkuuQDeg0fhuTt/KbOeGd97/FsppLnIsYOaejuq/Gv41hGpwFNSxkNt XvGscEj4FnIDqnVSjnYh5Z7dfg6yNwp8lZjnUnFgxB4WN87PKJZs+aXVTE8w6YceAxHi GaQQZvvfnU9FdwdIXo9uuM3YIR4IfAG0BQhkLg4VV23oCVkhzGdz7SyoMAE5UWSd/J2g HP2Q1YnCNRc5iBiVx+IWoVB8Qy5v2OMndhwZKKw7O2oFObPjScIaYxx3g7jsCLIOHZ1F wzwQ+MCFkLDiLAu7xhU/txQc/iEtZomQ+3eaKESt0vq5AWcCPdtP4S3UMq2UHu67Kitc rF+w== X-Forwarded-Encrypted: i=1; AJvYcCUi7L3vrYX9zAdCZnAzS8T6ItuWrAETnSY4496dDfr/BUK43+ebj5XIWCoDocioksmmfTS42CIbh5PyVTRthN8=@vger.kernel.org, AJvYcCWwYw1SPPqMgEZVSfVIkvWUPBvp5b0LtcqksQjbFqKSBQdgQVuBN2i1j0z9DtPCs7ocNhlcwOsRjf1ZyUo=@vger.kernel.org X-Gm-Message-State: AOJu0Yyv85pUFpkYjQMA8eBSHoT1yjRdeMiiYJyL1CfGZTrzvpL8hb7Z c0hgmBw2qFKkc9/k82kxg4/VlqUXYB+WFiEHFxTy3FTb6xsy6l2KeC83iCVj X-Google-Smtp-Source: AGHT+IEH7dK1McuvR7hzSWJAMJ7WGlTHb2AlplhCtkFMzCX87+IiwGmqetTU9HQS3ZVTMuAz2d+mnw== X-Received: by 2002:a05:6a20:1d98:b0:1d9:4837:ada2 with SMTP id adf61e73a8af0-1d978bad18emr10579527637.35.1729827276975; Thu, 24 Oct 2024 20:34:36 -0700 (PDT) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7205791db5fsm180188b3a.11.2024.10.24.20.34.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Oct 2024 20:34:36 -0700 (PDT) From: FUJITA Tomonori To: anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, jstultz@google.com, sboyd@kernel.org, linux-kernel@vger.kernel.org Cc: netdev@vger.kernel.org, rust-for-linux@vger.kernel.org, andrew@lunn.ch, hkallweit1@gmail.com, tmgross@umich.edu, ojeda@kernel.org, alex.gaynor@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, arnd@arndb.de Subject: [PATCH v4 7/7] net: phy: qt2025: Wait until PHY becomes ready Date: Fri, 25 Oct 2024 12:31:18 +0900 Message-ID: <20241025033118.44452-8-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241025033118.44452-1-fujita.tomonori@gmail.com> References: <20241025033118.44452-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Wait until a PHY becomes ready in the probe callback by using readx_poll_timeout function. Signed-off-by: FUJITA Tomonori --- drivers/net/phy/qt2025.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/drivers/net/phy/qt2025.rs b/drivers/net/phy/qt2025.rs index 28d8981f410b..f7480c19d4cc 100644 --- a/drivers/net/phy/qt2025.rs +++ b/drivers/net/phy/qt2025.rs @@ -18,7 +18,9 @@ DeviceId, Driver, }; use kernel::prelude::*; +use kernel::readx_poll_timeout; use kernel::sizes::{SZ_16K, SZ_8K}; +use kernel::time::Delta; =20 kernel::module_phy_driver! { drivers: [PhyQT2025], @@ -93,7 +95,13 @@ fn probe(dev: &mut phy::Device) -> Result<()> { // The micro-controller will start running from SRAM. dev.write(C45::new(Mmd::PCS, 0xe854), 0x0040)?; =20 - // TODO: sleep here until the hw becomes ready. + readx_poll_timeout!( + || dev.read(C45::new(Mmd::PCS, 0xd7fd)), + |val| val !=3D 0x00 && val !=3D 0x10, + Delta::from_millis(50), + Delta::from_secs(3) + )?; + Ok(()) } =20 --=20 2.43.0