From patchwork Sat Oct 5 12:25:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13823264 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0D5C01531C3; Sat, 5 Oct 2024 12:26:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728131181; cv=none; b=aTb+pn/TmDbkUWtmpv6CyYunpIilg6bjYy51jVNlvDE6Qv3CEXPOoOV0uoqKDLNwKVInnBMzwyKJYfXtdS5b/IDGGdTozdVKAOjfjGl4DeOHedAvOhGvUPbPeKpj5WcwwcrdtYG4/j9XVZbUr+7+nupD4zknffeXVJS6hseunN4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728131181; c=relaxed/simple; bh=+nc/Z5V3f5KGTXPQ+kwQEPon7lqKacKesqid2l/Atrs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=COoNUe2nmA0jMSfsla0/G7rH/I9kbzZ2tt8KyMDQdyjG6G6k14rjDsgCz7bkvWgK7ic6rm4IlKoSyEScm9mdhw6ssTjH+rWmXCDtf5D4LJ+B+XbaIFkch0Jugtdsq9tEoKT6JBek5ECeyX/5wmSWcTfbV8YZTcI/6ugbiLC8ro0= 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=THCf9Qdk; arc=none smtp.client-ip=209.85.210.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="THCf9Qdk" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-71dfc78d6ddso2851b3a.0; Sat, 05 Oct 2024 05:26:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728131179; x=1728735979; 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=CKGy300dtIv/lfmSvdg2mAnHWaFMshcSd90+qpYaYEc=; b=THCf9QdkEVyF05CcA1c0DDqhVq4F5cZl4daXcMj0/hNOAl3QWIUcPETNy/AHUHLZFQ iNDfG+pSBzUfaGsNYzL+9CXMknUIOd3M2Aw7oezIRFwHf3P8FpEsGNuuYkIXsTJNjE3S MDBvBbXQMIPqjW+MHuDRtht32tnWl05SSoo9HPSKBVsnNyUHgMeeFM9HJg16VLjgxWNr A1yKMgNT6lqkSEkPwnB336VoRA3DL5lVAsRjbbRa7/fgvuIJDWxNQ9Xahjhff1uQi9au nMIgapzMsxPjWE1QKeB7GTNXKKpkwx342U1b86rIGRVRrccX2akAgeg4uIWd1lmgI+tn q4dg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728131179; x=1728735979; 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=CKGy300dtIv/lfmSvdg2mAnHWaFMshcSd90+qpYaYEc=; b=BhlYZUYTn9+AUAHhxg2TOYYFlJQaH+ODw9iGmpbE/HT5STmeB6XU0Cx4L/2NewezEv rKu68toqsasSxG4e6g69Y0tQ+0eLjVBgU6cLi57TR3YQkPUxx2WxynMqnvfmv0+l5Vlq Pd2Leh7bhXuLhdg3dWRVCyU26/7gS9CUaWcqeZnHaoVxoHFAxShCE7qfqDeSovs+4QPx 4rw9loT7l8oydhXsitmbpC+BcpftGxrgSfsQmAK5jF3Hc8oJTgPnramjTy38mg5byrZW rKuU0bZLGtc5ku3eAshLs2jQYk6QIL0AD1QbMfooFVEl5okBevQmrhnjiULA+B0GBDIq irMA== X-Forwarded-Encrypted: i=1; AJvYcCWd6bwiadEhXGLsNOuc0Wa5woMs20CSO5Qk9qXjjscoBhxUBuim8P5EAMqLMz17sQLDOgFWcxFi72CFrJg=@vger.kernel.org X-Gm-Message-State: AOJu0YyC7lvjgj1DGttGP3kFM0M+ETAg5AcsvczdMspyZMz0y5+OesLx CEbuRmWqOTyBLLH+spv0YlvGUCGWXEuyhUbpW8O/yJumDn8L+Xv775ECuX0H X-Google-Smtp-Source: AGHT+IEO1cM5AenyITIpMoldc/U4hQBqlkR4S7eRUdgYqpXiG+HhIfXVdaOOdti14V+pqK22v0iCUg== X-Received: by 2002:a05:6a20:d70f:b0:1d6:e852:2362 with SMTP id adf61e73a8af0-1d6e852236amr4434484637.35.1728131178997; Sat, 05 Oct 2024 05:26:18 -0700 (PDT) Received: from mew.. (p4468007-ipxg23001hodogaya.kanagawa.ocn.ne.jp. [153.204.200.7]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0cd08besm1397878b3a.79.2024.10.05.05.26.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Oct 2024 05:26:18 -0700 (PDT) From: FUJITA Tomonori To: netdev@vger.kernel.org Cc: rust-for-linux@vger.kernel.org, andrew@lunn.ch, hkallweit1@gmail.com, tmgross@umich.edu, ojeda@kernel.org, alex.gaynor@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, arnd@arndb.de, linux-kernel@vger.kernel.org Subject: [PATCH net-next v2 1/6] rust: time: Implement PartialEq and PartialOrd for Ktime Date: Sat, 5 Oct 2024 21:25:26 +0900 Message-ID: <20241005122531.20298-2-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241005122531.20298-1-fujita.tomonori@gmail.com> References: <20241005122531.20298-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 Implement PartialEq and PartialOrd trait for Ktime by using C's ktime_compare function so two Ktime instances can be compared to determine whether a timeout is met or not. Signed-off-by: FUJITA Tomonori --- rust/helpers/helpers.c | 1 + rust/helpers/time.c | 8 ++++++++ rust/kernel/time.rs | 22 ++++++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 rust/helpers/time.c 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..d6f61affb2c3 --- /dev/null +++ b/rust/helpers/time.c @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +int rust_helper_ktime_compare(const ktime_t cmp1, const ktime_t cmp2) +{ + return ktime_compare(cmp1, cmp2); +} diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index e3bb5e89f88d..c40105941a2c 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -81,3 +81,25 @@ fn sub(self, other: Ktime) -> Ktime { } } } + +impl PartialEq for Ktime { + #[inline] + fn eq(&self, other: &Self) -> bool { + // SAFETY: FFI call. + let ret = unsafe { bindings::ktime_compare(self.inner, other.inner) }; + ret == 0 + } +} + +impl PartialOrd for Ktime { + #[inline] + fn partial_cmp(&self, other: &Self) -> Option { + // SAFETY: FFI call. + let ret = unsafe { bindings::ktime_compare(self.inner, other.inner) }; + match ret { + 0 => Some(core::cmp::Ordering::Equal), + x if x < 0 => Some(core::cmp::Ordering::Less), + _ => Some(core::cmp::Ordering::Greater), + } + } +} From patchwork Sat Oct 5 12:25:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13823265 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DAE9A1552EB; Sat, 5 Oct 2024 12:26:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728131185; cv=none; b=VCWI+6n5Xz2VEFZu7+kyr3VrfOZnecNAALeY+mTVYN3pH2sP6wFFypOvZ+w6O67YUda7OzYNNaBVzpEFQIL1IH997qu9inav0oQc8yZUPFEkWYnIwxYYWeRxYgZZRBvMP2Qo3efrQsmEGUMRSRhYbwMT23NJvoEYU8OR8lS8vEI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728131185; c=relaxed/simple; bh=JNqDJ7f9UtL2t6PrWNiRsLuZ8p//mFWPbl9Ykus8zqo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XvvndRojryn5Hf3iaWbik7d/WKm1LVtZ29+2/0K8l0j5pdk3GR52RWzU8nPcW1WOyLd9eiQIEdUMW0SlbmAcaCRJQc4OcoWtvG66bwKtV5rHK20D+qnv9Zj+MiVoijawhPTP7iVkXeaY2KS8OF7T5QN5Y9uOlyARZerYG0u7yCs= 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=FS0lBMEo; arc=none smtp.client-ip=209.85.210.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="FS0lBMEo" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-7179069d029so2305377b3a.2; Sat, 05 Oct 2024 05:26:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728131183; x=1728735983; 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=Tvk/vZ/FBEf3YBO7h5QV1zQaPOZaXxyH6QMku/LV1UE=; b=FS0lBMEomQpxXYdMMJxmn81uDVaG2XIMKYlFCDOQ3XwvYKC/oBO7JOu+he3SKQ9SAQ VH73tvd5LxVYylzCJB6gBDJTjk91o0JXyJMX5k00ViI+G3JpUq6RBxLrIT51FxsT+IkC KfmnOkgiIxJp06k7qqcjQOVH0hrwl9knhJZY9uZVmpLDfWl9sfxLEbf7HcpwkHJqwFT+ kPbLpbwBZMrsqiM6I8iu5zv0aIR9/tFp94n7i7ILc4YawJpi6kDnTNiknvtAOZCm4t+O 3qNHbk8uX0+aSAnZC7uJuLYS6cEDIWDKPj1zsis5L9mdvDugnN0WBK8ON4NTGdxv2trp j8NQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728131183; x=1728735983; 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=Tvk/vZ/FBEf3YBO7h5QV1zQaPOZaXxyH6QMku/LV1UE=; b=XOxfx2bS2I05TpoXiR1Rzpp1L418/WFpb2/lzdU1v/okGeVXVAYFEO3gCBYhwLSsfy yX06DlnCD+Q6jQE/49H89+dlBYFlbEGVlNvhHuoSC3Epe9ZyxxKjAimMeGVMMyumDOkm KGMUDW+ZiKJVUx4RgQ8LIS+e3ey0KQ43RyGMPpuhuDcAUYzcMTK9h3PfTUEQffVLxWg9 qTTOmmNMSF7kPAP/ihL6SDRUnSw6qFpdXp79zcuVZQutG8NjChD+v9UDGtxdXt5jv+nL kBI2ApdZ+7yLbnfno8j8L9HFXqhUAqNBlCPK2LLRjksWnECn06lAfpTJ3/vkhPmB+K9n zW5A== X-Forwarded-Encrypted: i=1; AJvYcCVCviQNsIIaKwfdx/QH0KmM2hQf4DQXC9JLJF7hepL4d0vL2TEVmLW/ut8XgzAUBzeDaXdkSYqg1hQyt1U=@vger.kernel.org X-Gm-Message-State: AOJu0YwJMmYK/D0HkRcbaXiZ0h3JvfbV/gkOxNSUF1SBX/O5/8YUesYH rPRsJZG/kho7G4bDfywoJT1rJDohMIJNz5kiUdtMRWB7uJA7vWHpuSOoV/Yc X-Google-Smtp-Source: AGHT+IFVQJR9Cm7+64FWR6kRSvxRdFLHQ+iNTReOqxqohFrFYjZvpqPNfuGQmV2v9HjjgzYxo/GMXQ== X-Received: by 2002:a05:6a00:178e:b0:714:173f:7e6b with SMTP id d2e1a72fcca58-71de23a691dmr8474920b3a.2.1728131182885; Sat, 05 Oct 2024 05:26:22 -0700 (PDT) Received: from mew.. (p4468007-ipxg23001hodogaya.kanagawa.ocn.ne.jp. [153.204.200.7]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0cd08besm1397878b3a.79.2024.10.05.05.26.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Oct 2024 05:26:22 -0700 (PDT) From: FUJITA Tomonori To: netdev@vger.kernel.org Cc: rust-for-linux@vger.kernel.org, andrew@lunn.ch, hkallweit1@gmail.com, tmgross@umich.edu, ojeda@kernel.org, alex.gaynor@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, arnd@arndb.de, linux-kernel@vger.kernel.org Subject: [PATCH net-next v2 2/6] rust: time: Introduce Delta type Date: Sat, 5 Oct 2024 21:25:27 +0900 Message-ID: <20241005122531.20298-3-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241005122531.20298-1-fujita.tomonori@gmail.com> References: <20241005122531.20298-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 Introduce a type representing a span of time. Define our own type because `core::time::Duration` is large and could panic during creation. We could use time::Ktime for time duration but timestamp and timedelta are different so better to use a new type. Signed-off-by: FUJITA Tomonori --- rust/kernel/time.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index c40105941a2c..6c5a1c50c5f1 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -8,9 +8,15 @@ //! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h). //! C header: [`include/linux/ktime.h`](srctree/include/linux/ktime.h). +/// The number of nanoseconds per microsecond. +pub const NSEC_PER_USEC: i64 = bindings::NSEC_PER_USEC as i64; + /// The number of nanoseconds per millisecond. pub const NSEC_PER_MSEC: i64 = bindings::NSEC_PER_MSEC as i64; +/// The number of nanoseconds per second. +pub const NSEC_PER_SEC: i64 = bindings::NSEC_PER_SEC as i64; + /// The time unit of Linux kernel. One jiffy equals (1/HZ) second. pub type Jiffies = core::ffi::c_ulong; @@ -103,3 +109,61 @@ fn partial_cmp(&self, other: &Self) -> Option { } } } + +/// A span of time. +#[derive(Copy, Clone)] +pub struct Delta { + nanos: i64, +} + +impl Delta { + /// Create a new `Delta` from a number of nanoseconds. + #[inline] + pub fn from_nanos(nanos: u16) -> Self { + Self { + nanos: nanos.into(), + } + } + + /// Create a new `Delta` from a number of microseconds. + #[inline] + pub fn from_micros(micros: u16) -> Self { + Self { + nanos: micros as i64 * NSEC_PER_USEC, + } + } + + /// Create a new `Delta` from a number of milliseconds. + #[inline] + pub fn from_millis(millis: u16) -> Self { + Self { + nanos: millis as i64 * NSEC_PER_MSEC, + } + } + + /// Create a new `Delta` from a number of seconds. + #[inline] + pub fn from_secs(secs: u16) -> Self { + Self { + nanos: secs as i64 * NSEC_PER_SEC, + } + } + + /// Return `true` if the `Detla` spans no time. + #[inline] + pub fn is_zero(self) -> bool { + self.nanos == 0 + } + + /// Return the number of nanoseconds in the `Delta`. + #[inline] + pub fn as_nanos(self) -> i64 { + self.nanos + } + + /// Return the number of microseconds in the `Delta`. + #[inline] + pub fn as_micros(self) -> i64 { + self.nanos / NSEC_PER_USEC + } +} From patchwork Sat Oct 5 12:25:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13823266 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9AC8E14F9D5; Sat, 5 Oct 2024 12:26:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728131189; cv=none; b=rI3uTEfKlgymzsJAlZWRrCDr7QxQSupRZE0vIPIakEQL0yBG9EoTCKNm4zXeKRsjr/AGfwOJMEF9S2Z1emdUcDWcLec2/fKwoyZ+h4kJapvEbC17ep8kiprT5xOog/gpS+j6oxGmZiStlcPEyxPVGxkQU8uX9KaRhZ4wBBAFynY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728131189; c=relaxed/simple; bh=KcAeSONx/DiMxyfcnJRledcFg4LXf3lzROgvJ3YPsZo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mEzgnwt9lys8lqVhWufsPprqj0gy5ODUcQZ7RK8oevJoXk9I0hadO9vyhCRGWra1jgji3yUWd4kCQN6ctt3kXb0LYEBA1SzX25yu20adp/Oav2/HmvRhfgkBW7pr0yyhihx16FAGZXy6t5TqnN2UdGY4FjgseSHOy6GJLCjSBnw= 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=IyDXU+L/; arc=none smtp.client-ip=209.85.210.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="IyDXU+L/" Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-71dea49e808so1254928b3a.1; Sat, 05 Oct 2024 05:26:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728131187; x=1728735987; 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=kR/9kqLqTs0B22yZKmgz+3pmNIwgLpkKQm87ew+rvAs=; b=IyDXU+L/7wrr3/mCEFiK1ikS/aR8PSoF0Zp6iNS6Zvqnw3uvxysvGDSeUG9QAfjZdY bPHddtvmwd7cyl4OGWlJeuk5a/Qk51BVCwgHG+GTkngUSq4/53Eq2dH2ZxIIHFZc8T81 KBmMqkS51gvMlgkLHyUqNgIW+iqKskGZRVt60sSBO4+xmHbfei8tYCiixgo26s8ZlBiP XUyhhfJYh1o+ITY9w6y/UkN4Q9/tsGAiPWpVBoiRqP63cf1bMRgg7MDaZaSMAsDxy1xY lj+2TUEXk5wDnq0nw5nfRSeDeaKWT1ndeAJd+2b3MDgYuWwPuNX7vDzVY9mDu5/oKjCT 63yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728131187; x=1728735987; 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=kR/9kqLqTs0B22yZKmgz+3pmNIwgLpkKQm87ew+rvAs=; b=jwOdKu3BKCghxFBAqKvm9U29N+pk5rjKuWgJKB1G6JLP1+BLHpAwK1pS9j2KieHoQx aCA5oJnX9wnL6Zn/GbXQdW2FZrN0Degu/YBEHgTrXbK6K3goNR9DOz6oI6FQfxsWc4Gb vdvbleeTplhegIhdWt07bk4WaAtoxLHInVv4mfNxD6bO+hJBEIys+lkCz3Ya3Iu6TVlM QJq3pIMqxcgdMATKMTf+m7VzY32L/3o92anx+7GE8hVm+DiEI2jIV4zEJTzCkDn4+gdI g56K0ztMNWCjCt57Hamx16lHMlGNkcgji/rlBWF4b+dIZ4A5HLpnZSF9BU8XTX0SRON8 carQ== X-Forwarded-Encrypted: i=1; AJvYcCX3bcenvdqfWDBpeB6imI/LYukMsw+26nkRnFmTNsA8lspVnswUAk9asgdyERKANzjkYphoeLJipCWnKTE=@vger.kernel.org X-Gm-Message-State: AOJu0Yxt7GepTKoSEzMvfxFJmAWZRTmjyuezmCLdEdoYUnV1Cv3bKteb wZRFI0R1r/FGzOo9GXE7MJ3xUbbUmZW9b3xSdMDTPesWiIA/iQwJfZUVpq6f X-Google-Smtp-Source: AGHT+IGM/R6UCUnLz5gHhmt80Zj8TRtxwkmL0KFhW4UUxT4flbS3jpdytc4Y6DzO6Z9LcUNf2SW8Kg== X-Received: by 2002:a05:6a00:2e27:b0:717:87d6:fdd2 with SMTP id d2e1a72fcca58-71de23a89damr10684913b3a.4.1728131186738; Sat, 05 Oct 2024 05:26:26 -0700 (PDT) Received: from mew.. (p4468007-ipxg23001hodogaya.kanagawa.ocn.ne.jp. [153.204.200.7]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0cd08besm1397878b3a.79.2024.10.05.05.26.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Oct 2024 05:26:26 -0700 (PDT) From: FUJITA Tomonori To: netdev@vger.kernel.org Cc: rust-for-linux@vger.kernel.org, andrew@lunn.ch, hkallweit1@gmail.com, tmgross@umich.edu, ojeda@kernel.org, alex.gaynor@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, arnd@arndb.de, linux-kernel@vger.kernel.org Subject: [PATCH net-next v2 3/6] rust: time: Implement addition of Ktime and Delta Date: Sat, 5 Oct 2024 21:25:28 +0900 Message-ID: <20241005122531.20298-4-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241005122531.20298-1-fujita.tomonori@gmail.com> References: <20241005122531.20298-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 Implement Add for Ktime to support the operation: Ktime = Ktime + Delta This is used to calculate the future time when the timeout will occur. Signed-off-by: FUJITA Tomonori --- rust/helpers/time.c | 5 +++++ rust/kernel/time.rs | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/rust/helpers/time.c b/rust/helpers/time.c index d6f61affb2c3..60dee69f4efc 100644 --- a/rust/helpers/time.c +++ b/rust/helpers/time.c @@ -2,6 +2,11 @@ #include +ktime_t rust_helper_ktime_add_ns(const ktime_t kt, const u64 nsec) +{ + return ktime_add_ns(kt, nsec); +} + int rust_helper_ktime_compare(const ktime_t cmp1, const ktime_t cmp2) { return ktime_compare(cmp1, cmp2); diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 6c5a1c50c5f1..3e00ad22ed89 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -167,3 +167,14 @@ pub fn as_micros(self) -> i64 { self.nanos / NSEC_PER_USEC } } + +impl core::ops::Add for Ktime { + type Output = Ktime; + + #[inline] + fn add(self, delta: Delta) -> Ktime { + // SAFETY: FFI call. + let t = unsafe { bindings::ktime_add_ns(self.inner, delta.as_nanos() as u64) }; + Ktime::from_raw(t) + } +} From patchwork Sat Oct 5 12:25:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13823267 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.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 82CDF14F9FD; Sat, 5 Oct 2024 12:26:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728131192; cv=none; b=KF0ytc9qWVbqjoraNMHExDeo0Z3v9E+pZaSFnC8OOKP8cZ9dKrcK1HTpJpql5zQiTjJjTYPsdkKrE/C/WFwGyj9srmahHkGi743AzEytQ3CDaKlYoKO0Mvy8vUL/kgxY/cifa1x2JgRMyilNYDQdSCODRIgPVmUQs8sNaCv32N4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728131192; c=relaxed/simple; bh=wKhSL52KtyNQuFpvg1KAEd+hgO7B46PWjlKb2ik9skw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=D5w2H7edGjDiSLSKQX2druzgl3cKgQue8Sg9NwYTJamOscHcYDJPm4rUydfIv+nDm7Ovlt876UAUQ0WFZNRB8ZuslxT9owCyKOSHTBqRFo/mejHvAXtvsoogR4+flVbxzo8vSGuHLqaxnixHps1khszibVjdsSR7Fdz03gEFckk= 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=i6MnbTH6; arc=none smtp.client-ip=209.85.210.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="i6MnbTH6" Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-71dfc250001so22907b3a.2; Sat, 05 Oct 2024 05:26:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728131190; x=1728735990; 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=rp/njmks6r/M/HAsaNDxOLvQZ0JDeDbotfFoRfrRz6U=; b=i6MnbTH6N93gj06U2aFaDOqhxK1rjbV9YssrqD1stiSLG/xSGvt8yxSVKGAziA11J2 jTrLOF4qNuUslkEOsgq2FlGXq6+YMI3GTBSS1W4fbNHL7jIv1q3KS1IO/f0PRsh+pgMq YnKL7nNVQbcSVJ8gp5Lh+6BpfCmpAmLGH8r6i2EdfBy6d286ltcZSZlNZ/W+lTAPyVRs c8ngndZanLkTwg6M8k3xKW1BD1uCnDq7cCP4lc67fjJ3y3BFITK+jbvGZ4RgJenhHEb7 JV/F6/xvYlsZD4HYMJ2Vn8yHyGO7Wp/ORIZ3Lx8MBtNC6Z/x5rj+uIUOaVs9QLzR/dGd vTIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728131190; x=1728735990; 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=rp/njmks6r/M/HAsaNDxOLvQZ0JDeDbotfFoRfrRz6U=; b=oCBi3UgNIiBNYhKlP+od4eQf+ud4DdUH0cF6Kqh9P9wBCTuMsh6r6C5MJfPzUcOsf9 j6vWiY0ZJI5iq8AnS6Lpdr0fjarfTuWzzR3viTt1WVRxxGeE+JTXCGcRV6qHZCRSyC8R nPXZinY3y6tziJr+R7UaSgxmFr0zVSt9rcEgnTdD4cKhlyT9YboDzJBSccblDH1FNbDb hrEXuKwL+lNLI4NOyIsIULd7nIG8ZWMXDCa4dGCPx/QFx6Npqte9trYCw42rcVbcjsTf BTMGdkR8TKLjFPhvYG1KBC8N+HjEsLXXFvOBVOH9S6CFFAbvwUzhkWtCRt9y5qpdPX5a 93Qg== X-Forwarded-Encrypted: i=1; AJvYcCXNudWsx0eayk17TXmEHsfvnTRbyXX1RzxPLQwtqIsZllayhJcJxPRM5MCVQJhZf5V1iacbo3IQ+Y2yXms=@vger.kernel.org X-Gm-Message-State: AOJu0YzOyBNAvy6jaGkbRbK0r8/Kyq+RZvWqxp4kyWBSDDJboLzO1Hjp h9HarX9zdNX8X+VvyIlJxxeGCB6TRSgHJykUjHpQAzOoQkFNO3Fh0nuFRxJp X-Google-Smtp-Source: AGHT+IH+zyWHTg8WIGpAjbBHwrRfYYIR969NvgSDZjOZUwIrdguNxSgSqM8ScBgGTurIZByAW2lTPw== X-Received: by 2002:a05:6a20:cfa6:b0:1d4:e418:c2b6 with SMTP id adf61e73a8af0-1d6dfa23c3dmr7582407637.10.1728131190554; Sat, 05 Oct 2024 05:26:30 -0700 (PDT) Received: from mew.. (p4468007-ipxg23001hodogaya.kanagawa.ocn.ne.jp. [153.204.200.7]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0cd08besm1397878b3a.79.2024.10.05.05.26.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Oct 2024 05:26:30 -0700 (PDT) From: FUJITA Tomonori To: netdev@vger.kernel.org Cc: rust-for-linux@vger.kernel.org, andrew@lunn.ch, hkallweit1@gmail.com, tmgross@umich.edu, ojeda@kernel.org, alex.gaynor@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, arnd@arndb.de, linux-kernel@vger.kernel.org Subject: [PATCH net-next v2 4/6] rust: time: add wrapper for fsleep function Date: Sat, 5 Oct 2024 21:25:29 +0900 Message-ID: <20241005122531.20298-5-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241005122531.20298-1-fujita.tomonori@gmail.com> References: <20241005122531.20298-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 Add a wrapper for fsleep, flexible sleep functions in `include/linux/delay.h` which 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. Signed-off-by: FUJITA Tomonori --- rust/helpers/time.c | 6 ++++++ rust/kernel/time.rs | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/rust/helpers/time.c b/rust/helpers/time.c index 60dee69f4efc..0c85bb06af63 100644 --- a/rust/helpers/time.c +++ b/rust/helpers/time.c @@ -1,7 +1,13 @@ // SPDX-License-Identifier: GPL-2.0 +#include #include +void rust_helper_fsleep(unsigned long usecs) +{ + fsleep(usecs); +} + ktime_t rust_helper_ktime_add_ns(const ktime_t kt, const u64 nsec) { return ktime_add_ns(kt, nsec); diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 3e00ad22ed89..5cca9c60f74a 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -5,9 +5,12 @@ //! This module contains the kernel APIs related to time and timers that //! have been ported or wrapped for usage by Rust code in the kernel. //! +//! C header: [`include/linux/delay.h`](srctree/include/linux/delay.h). //! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h). //! C header: [`include/linux/ktime.h`](srctree/include/linux/ktime.h). +use core::ffi::c_ulong; + /// The number of nanoseconds per microsecond. pub const NSEC_PER_USEC: i64 = bindings::NSEC_PER_USEC as i64; @@ -178,3 +181,16 @@ fn add(self, delta: Delta) -> Ktime { Ktime::from_raw(t) } } + +/// Sleeps for a given duration. +/// +/// Equivalent to the kernel's [`fsleep`], flexible sleep function, +/// which automatically chooses the best sleep method based on a duration. +/// +/// `Delta` must be longer than one microsecond. +/// +/// This function can only be used in a nonatomic context. +pub fn fsleep(delta: Delta) { + // SAFETY: FFI call. + unsafe { bindings::fsleep(delta.as_micros() as c_ulong) } +} From patchwork Sat Oct 5 12:25:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13823268 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 65810158543; Sat, 5 Oct 2024 12:26:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728131197; cv=none; b=jPHLuSbDiENbd8QifnW8UEGtoTFiMQ4SeXn8kC9hxUUSTEBpRU2ZPlCnds1DWqV3Ii3Soy5iYm3nwNMPXrOox7mz+EtK6vASeNd7wTEFQsDBzUVhonfsV3FYWG01w9FapIdV1jgNOyBNGrZQqOVXc/ewPi8JVQDsnmB5nocS6Hc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728131197; c=relaxed/simple; bh=wmifuArjCCTGvjSdMIu1qn48E23mkhTjGw32XsdaHWc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RhD4tCO8uzWhbYAFarOGeuefGLc37HRsGYIhX6WmgKHHYow7gOcy+VOfaKqrwecCZfK6Q+g8laZ6PJ7lmd2U/Bx9eBqMjjdCfPR4YgpXy6ND6dryiAC1h5TSqryl7IfqtVfo3FeA9SgGCbEqiAYuaKtZNcC9Q1C+d1vf84G8LVc= 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=HtnKjnjf; arc=none smtp.client-ip=209.85.210.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="HtnKjnjf" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-718d704704aso2728006b3a.3; Sat, 05 Oct 2024 05:26:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728131194; x=1728735994; 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=/ouHlAeMaJ48AE4PPh3HomWVu44dqd04FLhO9b4zhps=; b=HtnKjnjfA6aiJvaIgJnW0VhxQrfMb0TBGdWgpQxmKiMP896DMYFkkHcVgcsRAMXcfk 7Dj6TNstyWDVSLlNGcXasLf0DVqPQvTVURzOsogAXnWWtFUKBOauucuXgB70Gp/QsJBS Aa8DADs+Doft7AMkwfHDNHiZXHT+EGlxzxk3CwRJEx7N8CO0rmRrLtCGieTPAivIIj6x OmwPngVrh0a6Xbw/lRpAHxLyD/nK5t09T48PKXfjFHVzrN5bkA2mANUGlva/sc77DeAO 05zqaL0Fczh/HoQz3P2mCyZrTnh3g38y3B3AgUtorG/63C9w2WhBjgW6AnNj7yqDaLlT 7sGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728131194; x=1728735994; 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=/ouHlAeMaJ48AE4PPh3HomWVu44dqd04FLhO9b4zhps=; b=NwRaQcMmjYCxB591BveVRUL3ZwA35rWS7rl3KJiPhtmqAbktRFhFhsweR6JEPFU6Te Rit83u9WEknLZlWVMgFLNNpBD8+nArtHgYr0CixVe9BvvhQxLEO/PBMM4QqlqmagwOk3 JZQZYoH7jn6xXLbMGTJRO/db/wKVoM5VqUif3dm1TRY3xwmm/OwShnKJbF3Y6P2PCbdy DyxHAo5NYl5xhJtTUqnNYkS6loJNRW7g3L1Zo1/rJpfVzVAY1uxfxxjZkm1OPzb9+GNr ifn/leQdkTdrWotnz+s5npcBuU3Y3rQLCj7ldbjpUv6JHDAYuLmRnlA2KBx44Kgo+kNO xNNg== X-Forwarded-Encrypted: i=1; AJvYcCUCWOksICo/vmLvV8IT9LcpczppeFKHcP8M3/B7vbRMUOfXpZy7DD0pk/vA7TLDl28ccLjE8imoOdsYwr0=@vger.kernel.org X-Gm-Message-State: AOJu0Yz7KR47D2kDAG+R4Jb5eHqpBIohDvh3qMjLu85YjsOkFysJmykW l8cX9WfZJnV1+46a1SVzWPcs8n696fLpp9CtZ3Cd42s+XqDzpYs/Fl7jZ0KJ X-Google-Smtp-Source: AGHT+IFsB+9ZpruAyYt2o9loiV34xg9sF7pGzkPdB1Fsmt7tD4LbtvtKfFEblIBhaeWRf6bRrxtyEA== X-Received: by 2002:a05:6a20:43a0:b0:1d3:293d:4c5a with SMTP id adf61e73a8af0-1d6dfa4278bmr8562405637.22.1728131194349; Sat, 05 Oct 2024 05:26:34 -0700 (PDT) Received: from mew.. (p4468007-ipxg23001hodogaya.kanagawa.ocn.ne.jp. [153.204.200.7]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0cd08besm1397878b3a.79.2024.10.05.05.26.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Oct 2024 05:26:34 -0700 (PDT) From: FUJITA Tomonori To: netdev@vger.kernel.org Cc: rust-for-linux@vger.kernel.org, andrew@lunn.ch, hkallweit1@gmail.com, tmgross@umich.edu, ojeda@kernel.org, alex.gaynor@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, arnd@arndb.de, linux-kernel@vger.kernel.org Subject: [PATCH net-next v2 5/6] rust: Add read_poll_timeout function Date: Sat, 5 Oct 2024 21:25:30 +0900 Message-ID: <20241005122531.20298-6-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241005122531.20298-1-fujita.tomonori@gmail.com> References: <20241005122531.20298-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 Add read_poll_timeout function which polls 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. might_sleep() is called via a wrapper so the __FILE__ and __LINE__ debug info with CONFIG_DEBUG_ATOMIC_SLEEP enabled isn't what we expect; the wrapper instead of the caller. 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 | 70 ++++++++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 1 + 6 files changed, 91 insertions(+) create mode 100644 rust/helpers/kernel.c create mode 100644 rust/kernel/io.rs create mode 100644 rust/kernel/io/poll.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..5b9614974c76 --- /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(void) +{ + might_sleep(); +} 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..d248a16a7158 --- /dev/null +++ b/rust/kernel/io/poll.rs @@ -0,0 +1,70 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! IO polling. +//! +//! C header: [`include/linux/iopoll.h`](srctree/include/linux/iopoll.h). + +use crate::{ + bindings, + error::{code::*, Result}, + time::{fsleep, Delta, Ktime}, +}; + +/// 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`. +/// +/// If `sleep_before_read` is `true`, the function will sleep for `sleep_delta` +/// first. +/// +/// This function can only be used in a nonatomic context. +pub fn read_poll_timeout( + mut op: Op, + cond: Cond, + sleep_delta: Delta, + timeout_delta: Delta, + sleep_before_read: bool, +) -> Result +where + Op: FnMut() -> Result, + Cond: Fn(T) -> bool, +{ + let timeout = Ktime::ktime_get() + timeout_delta; + let sleep = sleep_delta.as_micros() != 0; + + if sleep { + // SAFETY: FFI call. + unsafe { bindings::might_sleep() } + } + + if sleep_before_read && sleep { + fsleep(sleep_delta); + } + + let val = loop { + let val = op()?; + if cond(val) { + break val; + } + if !timeout_delta.is_zero() && Ktime::ktime_get() > timeout { + break op()?; + } + if sleep { + fsleep(sleep_delta); + } + // SAFETY: FFI call. + unsafe { bindings::cpu_relax() } + }; + + if cond(val) { + Ok(val) + } else { + Err(ETIMEDOUT) + } +} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 22a3bfa5a9e9..7b6888723fc4 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; From patchwork Sat Oct 5 12:25:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13823269 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 00757158A1F; Sat, 5 Oct 2024 12:26:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728131200; cv=none; b=Id/WQaf81HBUnoZux11zl8WFL2QGS/C+cWao8Ynvf4P5pjHDinRTqhNxrS5h2xb7AGv8qE5y+uKYuvnP3ADcIVkMvVeYv+RM8FvMf56TZy4vOmVajjn1fPmOdTPMKq9AQHdSIf/3mmWjipKVBGFCFXOEBHb+K2vuPxMPRSGAoko= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728131200; c=relaxed/simple; bh=fqFACiSqMZBgnX0/lu/Hs1iRXNUzcVy+wdWJ8PqhZLU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OdZcdnjpgyhnrhRYuWsFs6sslrbAMQL0S58bUkApsZ+XgXqeqbMfB11hf10AxlAi2plNEZbRD2dszHZURBh9J6OKGDH+CXzVRSgAIVLzFMv6nnVmlJyQoyNkvP8nDdBxX8+WAejRLLyLjwk8irfjVZFQ1b17FDwrgjB/0p9sIV0= 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=mPCTYc7D; arc=none smtp.client-ip=209.85.210.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mPCTYc7D" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-71de93c918cso1103068b3a.0; Sat, 05 Oct 2024 05:26:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1728131198; x=1728735998; 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=XdkX6wuCkXTawGfq5yedKZKwPh+awCwS1ZCSgtCWaZg=; b=mPCTYc7DPzBBAFJgGqZmI5y5b4f1Us9EJkNuZPb8hAdK23zBTPfC2/U/QYBAo7LeeI hgounnmrLHkZ3WRLySuBFtUasLRAJIR6ZeLQcd6OCJXpJLK0J3fzd1ium+ZMwpeFSROf B8GMFTmD2l+Np8MgZeGQFg7DPGowPADcvvWXDdlHgx0GUcxTMWdLfy7az2SIJohal5x6 UioFC5DBJTDuEf1q2rO0YqiieLOCD0U10nPKTMZktFYu9FALRyQRKmKA5w/qktrHnCq4 jVOoamKewe9bz0ACllT8BPNrJr6T6a0IURSujlczPqVjFxEG6LAFqduLuwsTgXZzp0KI la3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728131198; x=1728735998; 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=XdkX6wuCkXTawGfq5yedKZKwPh+awCwS1ZCSgtCWaZg=; b=XXY3ENjv1NGlcuLXOYkINO1nl74YwCYcbVUPzJ3ipZtWov7nOBeFSnCMGAr2kl+AfC 6AyGLDM3RjJxhf2ou7gm9ZH//SYV5aDrzOqfoPykgkM5RBgdKp/taDGbsFlcUCHbjdwt Kjc9zkV9T0cwInjS+S7VjuK/XsfTJeMrHZtkia95/pJjKcjgiZTPQ0aefQrikiopavE5 ZqTDN6CHm7U607l2R25YVTOils1jw+8B0U1XZ1gxUFtmNVYZI15glBDH6qoCEHTynSA7 8trIlaBdwpuCiXxcwmOkpH4vwtkiwgQ5pVXEzp0pcLpD2otPAeCWWuKirSG0Yv7vfqp1 +AyQ== X-Forwarded-Encrypted: i=1; AJvYcCUf38y+m/bA2Useul6qCZoD9laeCk72N7uFpqsJvgZED7ueywFGF4/WqF869VU5DR8RT882mjUdw3syiag=@vger.kernel.org X-Gm-Message-State: AOJu0YyKrnNXXRmiYIOYGz6y08/EW44tNghOFmlYu0xCh2TcUk7T+oGg CEiJefPYhQxKPtVS+xIyev6LESPIiipGKajuByuv7ukoo9Fd+BVF7TUzzgnJ X-Google-Smtp-Source: AGHT+IFZZxrOV3jNdJQFi3xVOSpRCRDXWy+64wpkzTQY+OA+CMx4Xc/JuC5z4UHhSJ6BHkfOpK/VtA== X-Received: by 2002:a05:6a20:c70b:b0:1cf:e5e4:a24e with SMTP id adf61e73a8af0-1d6e03732e2mr7755425637.16.1728131198157; Sat, 05 Oct 2024 05:26:38 -0700 (PDT) Received: from mew.. (p4468007-ipxg23001hodogaya.kanagawa.ocn.ne.jp. [153.204.200.7]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-71df0cd08besm1397878b3a.79.2024.10.05.05.26.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Oct 2024 05:26:37 -0700 (PDT) From: FUJITA Tomonori To: netdev@vger.kernel.org Cc: rust-for-linux@vger.kernel.org, andrew@lunn.ch, hkallweit1@gmail.com, tmgross@umich.edu, ojeda@kernel.org, alex.gaynor@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, anna-maria@linutronix.de, frederic@kernel.org, tglx@linutronix.de, arnd@arndb.de, linux-kernel@vger.kernel.org Subject: [PATCH net-next v2 6/6] net: phy: qt2025: wait until PHY becomes ready Date: Sat, 5 Oct 2024 21:25:31 +0900 Message-ID: <20241005122531.20298-7-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241005122531.20298-1-fujita.tomonori@gmail.com> References: <20241005122531.20298-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Wait until a PHY becomes ready in the probe callback by using read_poll_timeout function. Signed-off-by: FUJITA Tomonori --- drivers/net/phy/qt2025.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/drivers/net/phy/qt2025.rs b/drivers/net/phy/qt2025.rs index 1ab065798175..b5925ff9f637 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,14 @@ 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), + false, + )?; + Ok(()) }