From patchwork Wed Oct 16 03:52:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13837691 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C0AAE187877; Wed, 16 Oct 2024 03:53:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729050823; cv=none; b=rh1Xe/aXeuw8ZFKJ0XqpqVou/4w72nEZEEOZrCIboYWE3O/8/ZgH2r6YdXx/21n60+quKduyEbxrXVM3uy75bxyxSGJL2Gnn7KqD2ODS7QA9ClVC0yw8qh0vT2Ww8UcgnZibGQWi0SN+EZOw3ENfoZ7VEtUVCJ2Qa9sn6SN14YA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729050823; c=relaxed/simple; bh=sdOO7l0VkcV1+c4pfvjDnSgt/7HmqQdukwcZTyA4Bgs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BWs/kV947cKz/q+lAbPK44fZ1gC/3+n85VQZh3Xaywuwq1ZG/EM7nAySOHOcLvroHWQo1kMYfJoHnInZp0BYYVtw/o7rB7tBgxeCBuI3R/KQc0qd6Xqd5qtiRKNuwuVWZLYJYCXlHNlZYmVeM1+bvbuxPFOdS43Cy0HQ5aVbE50= 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=eXSzJSmc; arc=none smtp.client-ip=209.85.214.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="eXSzJSmc" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-20cf3e36a76so25988825ad.0; Tue, 15 Oct 2024 20:53:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729050821; x=1729655621; 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=PcGxqIvQqc6G0ynE34ZXsyZnUox+rF7yhwSw41f+KIs=; b=eXSzJSmc5u/9EOKRY5/vsGt2O98clbojEpuSN8xMcU/xs5uU/LqXk03wZcpKH6lZnN Ofw4BF4VhxnN5lvoM/muS/ycqpKHqafTKCcfj2LCivNMbrKHB0XLEJaBCjlVDBBXrx35 wuLIwwjygQwYkJFccbcH4K5mw0Apa2AsMNIoScOaIUVwIB7CDmFaxxfmda7gOI6iJpHw /TTdyStWyPvFKCTGamiS8YHNnJ5iKEsyOOiqlAOkiahLAjq9+IBC5hJ4Gt8Ku0HH4q2C dJj9sYaniQyKKJhQ4awgG9nwV49UC7ItjCYR0FyZDdTWnV0d1RBsgc7JJSEYURAe3rYy g0Ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729050821; x=1729655621; 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=PcGxqIvQqc6G0ynE34ZXsyZnUox+rF7yhwSw41f+KIs=; b=sFVKE1qtPbiSYyGrJJ7yoVjO/mRkfwfdumKpd2mBoZDXqN/i3Ji92DHRQAcEKwLjke ns34VDXVas00k/N1DZCEriLowkC0sQ/3vYUll98A5G8QZLadR5/OkBiLrX5yEL5AoZv1 qCFUe0Lxh6+zWtSPPLgv2BNeCZBRdLLQvu61RHZWRnnwcP21Ze8YwkLUqprvzHn0syWt Nf4oJE7/tJZpY5BfQ+hqGDv9Cw70zRDSvXu1MczDGr2Rl9gRravpCn1eITVOOvZjdZmO 9oTEUcvZr1Rb2/spnNL/IwYxhDidBlijFqRbuOoEfOFPjpLJ83iMQV21O8sp/YSopHxg PNTA== X-Forwarded-Encrypted: i=1; AJvYcCVlG0qK4ilst/P/nXt/dUP3YOuKBBZB+BDIJ7G9ojZe1BZY58HJiLtGgcXY8Eo8OUkFO0tmnKVpSaDHm9o=@vger.kernel.org X-Gm-Message-State: AOJu0Yw1ajno8ookkLy13QRHEXrjlnpfHp2uZfYkQP1iGbu1Gua+hRvS 4zBmvcFu7OW9DBWWWooGYB0w8VT6c1nGYmWR6EktoJlm97DKQpPsyvJup735 X-Google-Smtp-Source: AGHT+IF42EEDWe2xyQ+/4NTnG61Wd4qxON0nU59qp7Aqk1dUHUJ3Fc5OSnVCc1WYCiCrhT6WxlChsg== X-Received: by 2002:a17:902:ce92:b0:20c:98f8:e0f5 with SMTP id d9443c01a7336-20ca169e8cbmr212371145ad.43.1729050820686; Tue, 15 Oct 2024 20:53:40 -0700 (PDT) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e392ed1a4fsm2885691a91.17.2024.10.15.20.53.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 20:53:40 -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, jstultz@google.com, sboyd@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net-next v3 1/8] rust: time: Add PartialEq/Eq/PartialOrd/Ord trait to Ktime Date: Wed, 16 Oct 2024 12:52:06 +0900 Message-ID: <20241016035214.2229-2-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241016035214.2229-1-fujita.tomonori@gmail.com> References: <20241016035214.2229-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 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). Signed-off-by: FUJITA Tomonori Reviewed-by: Alice Ryhl --- rust/kernel/time.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index e3bb5e89f88d..4a7c6037c256 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -27,7 +27,7 @@ pub fn msecs_to_jiffies(msecs: Msecs) -> Jiffies { /// A Rust wrapper around a `ktime_t`. #[repr(transparent)] -#[derive(Copy, Clone)] +#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord)] pub struct Ktime { inner: bindings::ktime_t, } From patchwork Wed Oct 16 03:52:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13837692 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) (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 DD622189F57; Wed, 16 Oct 2024 03:53:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729050827; cv=none; b=tTPt+y450fQJ3z4n69mjNVhv4hUYjkn46u006HLRVUnKnm+rCFG7iTtZQc5pR26scstsWtd9otm/khymPiDampj8eoFbhczCZlin1ZYmPxI8UJ6mrXT0EI8+4IAG1ZY6KQ6D7QD/iAPvcX3VNbG3sDfyhIfhcXEzmLsMGDFVwTM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729050827; c=relaxed/simple; bh=XI/dyKxyCoddGtpKIJe6JLnuD35Wsjc8XEV+pb1YPb0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UNawTScpiDpF0Y9pp1ttjNNMv3FIztn0Z5Y96Z+Xywjg5Sbo8LgGO8hi1BUEXPGFL3Qr8PujKT50usDdsO2SR/auJq8nnVzYQYoeUi/grAfGF0ydGUnkBXlEEQ7UNESu3R/sP8LmsOL3B1ivvb+gUSoxy7yiHXWfaJFuG4mMCpk= 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=YlYtNLoS; arc=none smtp.client-ip=209.85.216.46 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="YlYtNLoS" Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-2e2d1858cdfso3949839a91.1; Tue, 15 Oct 2024 20:53:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729050825; x=1729655625; 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=cLHaMTl/OaXFHW2jFEe7sxZ89COHw7ReQgLHZorTXMI=; b=YlYtNLoS1TXicTMT+FCQ5Y/Trn2imQq1eiO0MVPqMhkAPpQDbIOHsWSwNfLR4Kv2Eu DxrrjQ0/tn6lBF8XZttXlEXjLkct6xw7Hj+eVImjN8YYOJ5JtfsrSPiS/FMccwoFAFmN E5YNR2yc26GzYSkSVnc/WJwzZH+xxtLA3NLpsTB20MAr0h3yJltlu/R7uJPBmwVN9f9Y Jw95xTE2V6xXXiI2MC5hUb0iijH9umLv2ZPRBBO5gG0f7Rb5qlFtrIQowAntwCFTLzux Shcx+nV3/B//F1V1kyoSEN2gWVRqU/5R8ExW8RE/vyzMyHZ/mCXF6yVxGEgAsTKxRzKp /uRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729050825; x=1729655625; 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=cLHaMTl/OaXFHW2jFEe7sxZ89COHw7ReQgLHZorTXMI=; b=QaGfswWQaxqCIcV/LbAy7EdKZvZTeeVrHSzfJ55m66QErQywfAOCSV8u2U7pi56Q2u eqt6MGZP/ErHaESgO2fSKcZtkcuiEWeNXgHtyG2vxzoJ+vzrIalCJo7il51JHfapNt6G j0rWq3vrlxx/D3t/fVscsYA9zeIrhn8qrTezjRc/g7/g+oyVMYZVPx1euzFQvXtUK4+i kAC+p4tG95CYgITssXfm1RtkZn7kIbdeiGf7/fI1iHXEoxlYXESUysoFd6m6lxkX0v1Z bYYyNGQn1vv5k/UxCvP6YdfuUjbNgSb0vFKhlmIBBCyOpa2LWI7nCnu7MutJZ6CiodIq zGHA== X-Forwarded-Encrypted: i=1; AJvYcCWvv23HY0AJHpwdI++Vp3fKKXS6dM0YLHlFyxyWVgcxTo6Y8UTnrneurc4mr0mFcSlhL0HfNk0uZP+rLXY=@vger.kernel.org X-Gm-Message-State: AOJu0Yy+utyafuu2BeDeUBuJWmSl7+ETvtSy+d5TXJWImq5d5fcaonvn lx2Ero6w/E4FV9oeZBqHCsVYWrohZeU6suKkSrUxTPrBtF4/izb0YUJ17y2t X-Google-Smtp-Source: AGHT+IEgKH2ShojJVIfEMs51cddGgBeRJfBJxZiCq/Vo2hQCTzQJ54d8PUKdqtT2ctc9egSBNnYfKA== X-Received: by 2002:a17:90a:b00d:b0:2e2:c98e:c33f with SMTP id 98e67ed59e1d1-2e3ab7c78a3mr3231821a91.1.1729050824834; Tue, 15 Oct 2024 20:53:44 -0700 (PDT) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e392ed1a4fsm2885691a91.17.2024.10.15.20.53.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 20:53:44 -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, jstultz@google.com, sboyd@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net-next v3 2/8] rust: time: Introduce Delta type Date: Wed, 16 Oct 2024 12:52:07 +0900 Message-ID: <20241016035214.2229-3-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241016035214.2229-1-fujita.tomonori@gmail.com> References: <20241016035214.2229-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. 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. Delta::from_[micro|millis|secs] APIs take i64. When a span of time overflows, i64::MAX is used. Signed-off-by: FUJITA Tomonori --- rust/kernel/time.rs | 74 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 4a7c6037c256..38a70dc98083 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -8,9 +8,15 @@ //! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h). //! C header: [`include/linux/ktime.h`](srctree/include/linux/ktime.h). +/// The number of nanoseconds per microsecond. +pub const NSEC_PER_USEC: i64 = bindings::NSEC_PER_USEC as i64; + /// The number of nanoseconds per millisecond. pub const NSEC_PER_MSEC: i64 = bindings::NSEC_PER_MSEC as i64; +/// The number of nanoseconds per second. +pub const NSEC_PER_SEC: i64 = bindings::NSEC_PER_SEC as i64; + /// The time unit of Linux kernel. One jiffy equals (1/HZ) second. pub type Jiffies = core::ffi::c_ulong; @@ -81,3 +87,71 @@ fn sub(self, other: Ktime) -> Ktime { } } } + +/// 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: i64) -> Self { + Self { nanos } + } + + /// Create a new `Delta` from a number of microseconds. + #[inline] + pub fn from_micros(micros: i64) -> Self { + Self { + nanos: micros.saturating_mul(NSEC_PER_USEC), + } + } + + /// Create a new `Delta` from a number of milliseconds. + #[inline] + pub 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 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.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 + } + + /// Return the number of milliseconds in the `Delta`. + #[inline] + pub fn as_millis(self) -> i64 { + self.nanos / NSEC_PER_MSEC + } + + /// Return the number of seconds in the `Delta`. + #[inline] + pub fn as_secs(self) -> i64 { + self.nanos / NSEC_PER_SEC + } +} From patchwork Wed Oct 16 03:52:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13837693 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) (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 D412E18B48F; Wed, 16 Oct 2024 03:53:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729050831; cv=none; b=OIxRLTgUKoy4S7uxqta3R9gECqy/cD1hzNWSoA4zswH1kWt2NTr+Iis+Hci8TVjEdQ92LsgeXyJr3vLGes1OdmKjMcbC/QLkE5PsgWvxW/cUhtGLYB7PDASnrlBke04sped/BwT1wRlfzvlGoVhKe4krBobDQ4M198ZrqZbshEM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729050831; c=relaxed/simple; bh=oO8kIoSHWWi7gATVjc5bDLg8mq2ATSDBxjtZ1uzqZCA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AulVPxbepM02+SoiGntZsWDoWj6m1r4lQ4IKOsdm1vxVulQgnbTLvgXqNVrGCscQD32vzUZk3PoyUTWiM5EJR6HeuoD9LFkwCLZpUB4qeMY6yyhHba6GzUcDmij4CxW84pRgmmd6oiq2y+QPr4ibtgIH/XDNLrFo6EaJILd+LYQ= 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=gHpJwzws; arc=none smtp.client-ip=209.85.216.47 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="gHpJwzws" Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-2e28b75dbd6so4321928a91.0; Tue, 15 Oct 2024 20:53:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729050829; x=1729655629; 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=PHwlHcER/lC/9sWgy6T/4Ohgbzy5c4D+iN/+N5JPfic=; b=gHpJwzws2tuiRs3M+wlPd8vmxEyFpZv65wtYe6TPHY/TaTA4rBncK68oe3CKrJdABl BHEiOPcxq1TFczoFnT1fKhlC0qetw40zDy+QV5ekZ7b+/uwlzkNdb/J95z2pMgWOZQx8 E/x2TvzRLqn0Kdu8ep9Rp1OxWXNktIID3K87j8sB4NYsauAWFIDAhWzL7LjGncO20NBP OY3mKZxuvY3xYvZmP5Y+OOT/L9qrEYLjLU79/GwZIiBso2kWUFQVB25IZ9Om6QyXffOL Rgc+JLedfGPcSLwL1tBl97f+jYfuhhOZYSNMdzoiU1RjhhMZw5PcIAT/C/aCuqtmbpBR NTXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729050829; x=1729655629; 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=PHwlHcER/lC/9sWgy6T/4Ohgbzy5c4D+iN/+N5JPfic=; b=jFwx1fOHDomwetcNnGO4TPPEFEd4YwwAN9+ZpSsLv8Aht0T3yjBAjLV262ClX5tBsn f3n48JbPO6+uJrDrFePBaZo9xXgPbELuZ9BVco7NU7/IDX03v2NqqozAKeXqjwK5kOd/ FKN66S/rp/XZg5ezl3TpNXpFDsH7tanVVGKVT44wmkomyH9upvOZZNra71k91rFvaOCZ 3tXH8N+EyZSoCBTa656jkk2UFDGQ4jTOgbPa+R3Z5l8bI58UsaLIMVdQFNE6RUCeFdBX +PlL8sAGhGve4OSRF0h/la4QQmg/l1GXNFCB53M/b4o830sDBa+ziQxH/XsGVXYPkq+O cHNg== X-Forwarded-Encrypted: i=1; AJvYcCWsz3hASlVGMiNGs0S4Ir88gkNp1kx7YpwwO/Ytx3zptVdReq/5JOhPqlLZ43v/3kEhC7bhNScITIgPybM=@vger.kernel.org X-Gm-Message-State: AOJu0YymOP3+EgjeZpI2ms//oaSG/kzGAqicuola49vqTgnzTsQJr94X yoHs0kkiJbpImKEkLbfxHt5WFRapPxrTwW6Bb7mvYlphiwqsZp0DmCQyX+sn X-Google-Smtp-Source: AGHT+IFuOalD9KF2WAnb9fVbVduiq+Te+F77S13d6FsanJftfnhzfksVT+LghStx3FKQWiNlidzggA== X-Received: by 2002:a17:90b:224c:b0:2d8:3fe8:a195 with SMTP id 98e67ed59e1d1-2e3152b0100mr18083336a91.4.1729050829007; Tue, 15 Oct 2024 20:53:49 -0700 (PDT) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e392ed1a4fsm2885691a91.17.2024.10.15.20.53.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 20:53:48 -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, jstultz@google.com, sboyd@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net-next v3 3/8] rust: time: Change output of Ktime's sub operation to Delta Date: Wed, 16 Oct 2024 12:52:08 +0900 Message-ID: <20241016035214.2229-4-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241016035214.2229-1-fujita.tomonori@gmail.com> References: <20241016035214.2229-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 Change the output type of Ktime's subtraction operation from Ktime to Delta. Currently, the output is Ktime: Ktime = Ktime - Ktime It means that Ktime is used to represent timedelta. Delta is introduced so use it. A typical example is calculating the elapsed time: Delta = current Ktime - past Ktime; Signed-off-by: FUJITA Tomonori Reviewed-by: Alice Ryhl --- rust/kernel/time.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 38a70dc98083..8c00854db58c 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -74,16 +74,16 @@ pub fn to_ms(self) -> i64 { /// Returns the number of milliseconds between two ktimes. #[inline] pub fn ktime_ms_delta(later: Ktime, earlier: Ktime) -> i64 { - (later - earlier).to_ms() + (later - earlier).as_millis() } impl core::ops::Sub for Ktime { - type Output = Ktime; + type Output = Delta; #[inline] - fn sub(self, other: Ktime) -> Ktime { - Self { - inner: self.inner - other.inner, + fn sub(self, other: Ktime) -> Delta { + Delta { + nanos: self.inner - other.inner, } } } From patchwork Wed Oct 16 03:52:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13837694 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (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 5611018B48F; Wed, 16 Oct 2024 03:53:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729050835; cv=none; b=Ez/7hMvq0/FRUxR4IQR/JSAGsvJ8q2sR3W+P/jI+96ee52E5j+Ppmc1Q+MzXkVLXfYy5pZ/Owkn34zUpPOq3tX1jBegpJxjCtekGyS6QqGWQ5dBMulbOkTYA5CsZznu7OuKNFWZKqn8lPz05Es9CHmcBBXJohJzbmfQjyFGkL7U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729050835; c=relaxed/simple; bh=IcMWuPB8VR64rDLYBYOGRGA0VJQ3fRc0RASHVWzNl3Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ceTql4xMi5leuDl+VV9WtQam1Apl92YtR37YadFqHJVDIbaqdoPEyCrDGVKxVUFZegfdUhe/GND/9UPEwrSuzhIGYHHDZNVnfx6FjRqMFIVfHPkvMJPzCYSXmzwbr42oCA1VoVPZ8hRA5qnWXDxXItei/N9pPFA7NfiViDq+SHU= 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=k75JJuPE; arc=none smtp.client-ip=209.85.216.52 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="k75JJuPE" Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2e2e88cb0bbso3470274a91.3; Tue, 15 Oct 2024 20:53:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729050833; x=1729655633; 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=0j1SQRGmCZOe+0FnzFGm8ZqtXpWgNmgq2ZQqT19Tg0c=; b=k75JJuPEmkW/5f/xQ3WHDc5M6+3rQ3ign+Wap3FNf5/2zgehUwIJOiBbRXBScmdcXq ZHxKltuRk/XNjVCuf2Z3Dt8lMOdR651iQT6bT7TG3/amDLKn+CqyQuvfaSycu//wuOAh VSWmAmxTlRA69j2Vnw/MOzJpjLZ/WnK/gPuYrPK/YqjOojDHLuMHPqseBfiEyDqbzHO4 QrWfniNlAo3ibYj3NPsWiUar/Xz9v/u+n/J5M1NrCxDI8/d+oAzzcLDSpxJeDCg/GY0N H9xQ+hBjCcO4jH5011tjcEESwPYkFCycMSUqC+olQoncybVlvNIzkBeBewEjHJtYKKG9 vkXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729050833; x=1729655633; 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=0j1SQRGmCZOe+0FnzFGm8ZqtXpWgNmgq2ZQqT19Tg0c=; b=Kr9+fBscGpHYlV1gO60TxwvCiakOq8ex+gXR+wLNi9uN5DAfbXefWLu3lwBvP5+ho9 WfB+9i76sRDwphKpNVLVuWFhe9S+7zDTjg9J6mr08ygwT3gnwMlJFUWn47227Xn32a7g RYEY1VfinDUhtJ1iIpllYAl7JNqeFbP0fUqSRDCjj1hwu7XzYsNpcVFYy5coxblit6uQ DzgBH7s/4nwIwfNEN6OSnCSrUNFhb5Ek9Lwp2bWQZ0P5qfWd5igD16WyDbnsJRr2Zfxp 4coEC+PRlzw8gAdSxdPWkJuiP4agS6xBwz5r5WCWa8oF9Lgqydhbxxajo7KCqzB+93N9 H+Cw== X-Forwarded-Encrypted: i=1; AJvYcCU6ryejA0jd+AUpdKpfvBCdLBl1lQ9GhMZaXHF4FQ+nLxyZg6YvdbQxcHBwY6leoz0+dN1yJjwBW/EySSA=@vger.kernel.org X-Gm-Message-State: AOJu0YzAUu8TYbqpLqG3h8K3WkziGaS9Sa6sVOzwUU4AAiGfy5hcK+FO TT8gOZhAqXuH3NAFtJGnetl17yg5bk2UXerCp+BmeG7MqmCa/Q6tcDal+qCF X-Google-Smtp-Source: AGHT+IH6giL8Trj0ATYvjOWORVSahtJgstNR2ZjYBD2gOGpvAMUjXOAIwhRHF+AKVU2DWRwuIWxZUA== X-Received: by 2002:a17:90b:4f81:b0:2d8:82a2:b093 with SMTP id 98e67ed59e1d1-2e3ab7fb661mr3079549a91.13.1729050833148; Tue, 15 Oct 2024 20:53:53 -0700 (PDT) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e392ed1a4fsm2885691a91.17.2024.10.15.20.53.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 20:53:52 -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, jstultz@google.com, sboyd@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net-next v3 4/8] rust: time: Implement addition of Ktime and Delta Date: Wed, 16 Oct 2024 12:52:09 +0900 Message-ID: <20241016035214.2229-5-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241016035214.2229-1-fujita.tomonori@gmail.com> References: <20241016035214.2229-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 typically used to calculate the future time when the timeout will occur. timeout Ktime = current Ktime (via ktime_get()) + Delta; // do something if (ktime_get() > timeout Ktime) { // timed out } Signed-off-by: FUJITA Tomonori Reviewed-by: Alice Ryhl --- rust/kernel/time.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs index 8c00854db58c..9b0537b63cf7 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -155,3 +155,14 @@ pub fn as_secs(self) -> i64 { self.nanos / NSEC_PER_SEC } } + +impl core::ops::Add for Ktime { + type Output = Ktime; + + #[inline] + fn add(self, delta: Delta) -> Ktime { + Ktime { + inner: self.inner + delta.as_nanos(), + } + } +} From patchwork Wed Oct 16 03:52:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13837695 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pj1-f50.google.com (mail-pj1-f50.google.com [209.85.216.50]) (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 4971A18C02B; Wed, 16 Oct 2024 03:53:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729050839; cv=none; b=H8I85+bEtuOYNLthRKCassvaqH6Blzs3OHd91M2nLGzjz7NqTr/5arTsdBH9DvBLNhpis5k9BYfNDigdP3FMt9xs1JyUp9Ur+oe7h9rEpCHYvYQPr7dRNtAba7Ghg0N/2a8W6CJt/7ds2jdKeKH2L/gNm3V3fJJGC8URwwGFVJg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729050839; c=relaxed/simple; bh=gL3wRmMknlT+8W9oXhS5GNiPGVIJ3Au148RU8vApvBo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UHmITJ/aWPsoNtHh0UsNnROZHTbhMFu7mLIOj1iEzrZtB1aaUo6Rc5O8H4GvtGppnIJxEBwZSzs5jsAS1ROKxnkNY+/Xl7mJKg5f5A02QDYXw2AJe2mzwe8iRbC15S9b+MkhXBChz6BSENa+Z6Rq6ivVbcd+pNyoO1PHIe8c+Tg= 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=c/csWfRZ; arc=none smtp.client-ip=209.85.216.50 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="c/csWfRZ" Received: by mail-pj1-f50.google.com with SMTP id 98e67ed59e1d1-2e2ed2230d8so3568130a91.0; Tue, 15 Oct 2024 20:53:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729050837; x=1729655637; 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=509eXO9XAU0pFq5wqNoTY0iWcoToJ2G4Fpozd36voIY=; b=c/csWfRZLuaz7gdcaOYszDb+vobCNUZelAIdHeTbLyi8W08SyLqGJGUv8EOKfEuvm2 b2uORb3J43r9D3Q/mNiYKeWtsUWJ4qO7NeWAe+xGDBAmybJWWWvTDgN1icFWKGggNUZm Qku2F0+vJf9/7MjdIkVYY2rzfako0+GQXozyt5n6k1d0m+UVkCGOVeweZ5UH1V/Gr6zu 3gyORBeThMRTBqeRZ9ERNxB/ce2V3nZoLWliFHKl8eg1UqT+XHb8POYe0d7+1IyPgM8H km7xZun3SetMCis5IcBEaca22z+9htfEkhXM1sNiNQKvzMZ0g1EIsmPp8qOcKS/pPrJa JqBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729050837; x=1729655637; 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=509eXO9XAU0pFq5wqNoTY0iWcoToJ2G4Fpozd36voIY=; b=wsHf/O9T/azVW6Off3wDpipV0mO/xv6a5mIZDRhmttpKFW5LwtXTehKJy+cZvIANZh nmUeZ0Ox6K4vVic0Eb3XuhihJiQi/yls2A4xkteWzPmTs2e0tRzsstFhau6V/k3cCz6k xz5Pb+syzivDIQGct20LacfcgQARvE8UyxGMhwbUo+FVZNEdTEyEoTul+/BowIMPhhnO yTes3J9iG4d1thfjMiQAeRtokg9kmVS3wN2H74KkB4de76vH4LHbcuf8pR8RKi3JnULQ LfxPocUdDcWcr+7pbjtP90qMFDLaSAnyFnRMt9rVpKXI0pjCClV+GzJUVJBE4wOmjEo0 lZFA== X-Forwarded-Encrypted: i=1; AJvYcCW6uo6JEL3VDSxMR2jHLB3gc8iBjjgN54I0Ll1sH2ydUDm2S9OP+IW+79jJFZsiMFwIEivv1pszFVC2gzE=@vger.kernel.org X-Gm-Message-State: AOJu0YwlBgclHL+r31sdzt0vMkf7KrGSejAKxzKDOOwyZaYJ8DeEEHQ9 ySHIHnvtKiRrqUP3Qk3Gp0KTaEGrR+IY8qttUPQXLGA5IRUGM78/O3A5m5q1 X-Google-Smtp-Source: AGHT+IFg7rwqV8upC4BWPoICnCtWUGDEBfoBMAy3BAbFXgso/4Q4SFSyYcNXlU3zi8flIPsJUksSWw== X-Received: by 2002:a17:90a:fa88:b0:2d8:8430:8a91 with SMTP id 98e67ed59e1d1-2e3152c0653mr18771963a91.10.1729050837327; Tue, 15 Oct 2024 20:53:57 -0700 (PDT) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e392ed1a4fsm2885691a91.17.2024.10.15.20.53.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 20:53:57 -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, jstultz@google.com, sboyd@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net-next v3 5/8] rust: time: Add wrapper for fsleep function Date: Wed, 16 Oct 2024 12:52:10 +0900 Message-ID: <20241016035214.2229-6-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241016035214.2229-1-fujita.tomonori@gmail.com> References: <20241016035214.2229-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 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. Signed-off-by: FUJITA Tomonori Link: https://rust-for-linux.com/klint [1] --- rust/helpers/helpers.c | 1 + rust/helpers/time.c | 8 ++++++++ rust/kernel/time.rs | 4 +++- rust/kernel/time/delay.rs | 31 +++++++++++++++++++++++++++++++ 4 files changed, 43 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 9b0537b63cf7..d58daff6f928 100644 --- a/rust/kernel/time.rs +++ b/rust/kernel/time.rs @@ -2,12 +2,14 @@ //! Time related primitives. //! -//! This module contains the kernel APIs related to time and timers that +//! This module contains the kernel APIs related to time that //! have been ported or wrapped for usage by Rust code in the kernel. //! //! C header: [`include/linux/jiffies.h`](srctree/include/linux/jiffies.h). //! C header: [`include/linux/ktime.h`](srctree/include/linux/ktime.h). +pub mod delay; + /// The number of nanoseconds per microsecond. pub const NSEC_PER_USEC: i64 = bindings::NSEC_PER_USEC as i64; diff --git a/rust/kernel/time/delay.rs b/rust/kernel/time/delay.rs new file mode 100644 index 000000000000..dc7e2b3a0ab2 --- /dev/null +++ b/rust/kernel/time/delay.rs @@ -0,0 +1,31 @@ +// 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. +/// +/// `Delta` must be longer than one microsecond. +/// +/// 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_nanos() + time::NSEC_PER_USEC - 1) / time::NSEC_PER_USEC) as c_ulong, + ) + } +} From patchwork Wed Oct 16 03:52:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13837696 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) (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 5BC6118C90D; Wed, 16 Oct 2024 03:54:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729050843; cv=none; b=eXbGlXLQtxj11qgS2XaOJu1SqBmQEKqQBerJ/oaIBfOkaY01fkadENUDfl+HuXUEUmdH6sWx1UbmH+bXJNKaGblUC4YiuMxLRrIPwWYAWAQSiA1ry2oqHbib9gGr5TCmSpG1FLcDs7PqC2BzvyDBn04qk5tV6GTrRcT78GdPaXk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729050843; c=relaxed/simple; bh=VgLWI+AjLBx8Ja7xEtqgZgZBMA3LJvFtYSnH/F5Vxc0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Wr7TomvgLoxfzS/Ucy4RmAYiveN1Vjoi2HWjQUt0baMW8iCRe+/ilN3iPrIS5ZyYlKjHrhgFGLvqXcAPg8ldZemcki5FBCSeIhAYUbVUfmjbHFn6LcWlYOU5USpJexyDoNyizTz6avHPCc8pK/tcdT3Up3R2w6fTcvYAAgfkgUg= 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=X+foAIVR; arc=none smtp.client-ip=209.85.216.42 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="X+foAIVR" Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-2e34a089cd3so2617594a91.3; Tue, 15 Oct 2024 20:54:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729050841; x=1729655641; 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=Woj+2pHK7bC5CLcfrqEjzTTpY7289fmk2YwH0hzw958=; b=X+foAIVRy8l3MTP+fBgNcP8f5/zihesZdq7zCov0q01NEhqUlz4oZP5OSXxUCPMfwF 00dtvUDrus6mNx8pDF7SIK6Ei3J3Kn71JIB7AtWeqR2V2WYnKoVEZ8SizI7Aw9z8Ng5G U6hmsUNC56L+103OpZun3mJ0+BL7dDN0im2j6cjXVA5yW0VAU1ytP63hCZ3hOWfev4Yb 8y7WFv8lz68pwzdkgHcdbMxLjfNulXUQPGU1/10oPYvtvKP2UnyJB8ApGU2CCVMwG0bF yHfTLIxCpqsrP87VfB2PUc8KAD2ap+p4KazL9zvkuNe5sXbUawuqAj43N9Njz3rGXoDo U/jw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729050841; x=1729655641; 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=Woj+2pHK7bC5CLcfrqEjzTTpY7289fmk2YwH0hzw958=; b=jmpopJU9k7LhBkjzxakjlLkOQOI89izNpCYVZqCUitxlAImmA0mwAfpN8llX6E0cdI HUcjjR/75LIx+xGPIvW0GiyoqK/CY19WtWbo3doRkbGRg+OEpwx7xC8xL9lBZ8aCQ+yz neWTHt0eO/3/97Ed2CRxi9nT2Rg+O2xjV5t/O6OwDDnj/wWo1+QHera7p0gUvde/2drN G/Gr1D2ayxU2gxe8drYbzkRw5ayH4VqvaW+ztWYpYkBqzfXIitr/aMfzbEmiON5gKqrk 2CqrSyG1RQpnvGzVQt7S7VTsfze4NC4IInHKOX/bNTs2Kny3F/0zJ49AM7+7bBVGlxl7 C7AA== X-Forwarded-Encrypted: i=1; AJvYcCXHyRmqkjoajbOfVU0NQq29XDf35snaUNz7n7S/OGgjUl3fJNLiC63sAhLLKiVGXx3Z0zidCd8IWq2hcXQ=@vger.kernel.org X-Gm-Message-State: AOJu0YwXHmh/SN5ZtoItdOpkBqyEAlVntjyn9mhOg5PwcqYecAP2pwhf q0y83Rb47B+AwRZRdfi+Za+LUvIwJIIEF8AGiKAO7I9c2q2Jfg/jTUlsETZa X-Google-Smtp-Source: AGHT+IHMmqygum/tEmic42aO376l8Z/b8aPxK63tGMO9TYQnXDAdlDD1YCniFmgmLGT5hcdUiZIDog== X-Received: by 2002:a17:90b:224d:b0:2e2:bb02:466d with SMTP id 98e67ed59e1d1-2e3ab8dfb4fmr3211675a91.33.1729050841450; Tue, 15 Oct 2024 20:54:01 -0700 (PDT) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e392ed1a4fsm2885691a91.17.2024.10.15.20.53.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 20:54:01 -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, jstultz@google.com, sboyd@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net-next v3 6/8] MAINTAINERS: rust: Add TIMEKEEPING and TIMER abstractions Date: Wed, 16 Oct 2024 12:52:11 +0900 Message-ID: <20241016035214.2229-7-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241016035214.2229-1-fujita.tomonori@gmail.com> References: <20241016035214.2229-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 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 d678a58c0205..faef059c16b1 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -10172,6 +10172,7 @@ F: kernel/time/hrtimer.c F: kernel/time/timer.c F: kernel/time/timer_list.c F: kernel/time/timer_migration.* +F: rust/kernel/time/delay.rs F: tools/testing/selftests/timers/ HIGH-SPEED SCC DRIVER FOR AX.25 @@ -23382,6 +23383,7 @@ F: kernel/time/timeconv.c F: kernel/time/timecounter.c F: kernel/time/timekeeping* F: kernel/time/time_test.c +F: rust/kernel/time.rs F: tools/testing/selftests/timers/ TIPC NETWORK LAYER From patchwork Wed Oct 16 03:52:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13837697 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) (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 B698718CC08; Wed, 16 Oct 2024 03:54:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729050848; cv=none; b=tGnq4qpuDi23p0r0Jz5VRlV6SCdcxtZa+3Uma4vRoQOdPpUWYbo+J8/nNSmZ1T/exyUFzIDh8mluWunu1F8TpCICL+oaVJBYLarQL50HwDcggrmbhAHC0JJRpfjh1fVCOW7ravkmqOfdNf3HEd1AqOYedruKUNZnjMDfZGWmJEg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729050848; c=relaxed/simple; bh=GCzvw7ibLiDxSGnzi/RvsU+Em4kHjqiyYpGXRBJyP3g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tbYgR6kNjTM0Yfep8E4uHWY+MKEyZaOilr39PPauUAkK1GVKiknL0ce0/lNg4QoG8WczNn3ThecQeYRFvV3/UXuPPF9rI67w8BzuZxdMXtjjkSTT55bRhoOJu76RyERSlLJOAjybHkV5IsnDT+2FqCjm0c1+wgaGm+XtJbKY9zI= 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=YwCHxMAs; arc=none smtp.client-ip=209.85.216.45 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="YwCHxMAs" Received: by mail-pj1-f45.google.com with SMTP id 98e67ed59e1d1-2e2bd347124so4062111a91.1; Tue, 15 Oct 2024 20:54:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729050846; x=1729655646; 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=ewjM75y6/WI7rdufDQw0McoVQ3kSSWYYvv4Agerd34c=; b=YwCHxMAsGDwc1xKQ6tYWEy/7DQ/5xRseRZDRwkwQ7UHKqGtwM24d9NrL/En5DYpArT yk8odAjhd2ki0DtxmbQ/STymtxWLWeXJxn5qJTKmsb1Ebzno6belZ0yO+5VU+rmGPMkZ nKIg48uJn7OHmuSFcCENAQH975rm+yVzC5WpODIsCQltf1LSrKEDn+xk/s4vJiQdToED S1C/D/fYCdoO40OrpFnk6Jwe4zrJYrJtweg5MinYqCohK1NifhF8E6cJRj4Zoln7+Smg OEK4VPoa0+H2WX+OmIaLYnKpPhpq0i7NkfF2tomiWLZJZbYX+VusAkdsPCsqneg+8wI7 NR1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729050846; x=1729655646; 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=ewjM75y6/WI7rdufDQw0McoVQ3kSSWYYvv4Agerd34c=; b=KUu4267eQYle+fcmnLk5NdrZQacexm3WwLk8mRhjzHdNIRa4cVkf9aDoC2/JCAWd61 gSxB7j4ijLBwdgoXpKa2KzDwg3JaBmQoHGAkIamXpHAI1ScMxbSKY4jOKy7O8NVQNQ0P N5oPncU5KqIdQ1M0LDBUN9WUDj+FEe71mlB/Ev9oUnmHBGaEsJ6asBe6R4W/0LvuMJba 9Y2TYu4Tr2sxsgycVK2C8mcUQJsouGE82IYeP2h+4cVJXBOZwaCEOrKHGPDKLfc+k33M fgVJb8bL97I/lsrQakWgZkvYTCC/LpFtJfbXCBSoBWBgr88g9q129K16bQPZmvqOphOr 1hAg== X-Forwarded-Encrypted: i=1; AJvYcCUmrPZL5zsCc/Qtk3/9FDHMbnXFNaK1iosZleIFz7q5CXMfoPmAI37RRlhmM84Hj4FB5k+Ia9foeVvJAgc=@vger.kernel.org X-Gm-Message-State: AOJu0Yw53kR8b5IBZqr8tQ7HjkNFdLyCmmx/zR/12oPdct6pz5pa1vi5 TguhbPBv9corfm2zVqUK9Mta5C7CkxzBMz9yvXXlFN8LtPJCAjzj30nM+Ijk X-Google-Smtp-Source: AGHT+IEFV09BdCjpt0eaoF+ix5VsqcfzYDWe6+Z7ZWqHpTtX/c7nJYEQE3SCOcVBhEKExYGxThv4qg== X-Received: by 2002:a17:90a:6788:b0:2e2:ac13:6f7 with SMTP id 98e67ed59e1d1-2e3ab7c42cdmr3010451a91.4.1729050845654; Tue, 15 Oct 2024 20:54:05 -0700 (PDT) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e392ed1a4fsm2885691a91.17.2024.10.15.20.54.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 20:54:05 -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, jstultz@google.com, sboyd@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net-next v3 7/8] rust: Add read_poll_timeout functions Date: Wed, 16 Oct 2024 12:52:12 +0900 Message-ID: <20241016035214.2229-8-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241016035214.2229-1-fujita.tomonori@gmail.com> References: <20241016035214.2229-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 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. For the proper debug info, readx_poll_timeout() is implemented as a macro. 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. Signed-off-by: FUJITA Tomonori Link: https://rust-for-linux.com/klint [1] Signed-off-by: FUJITA Tomonori 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 | 84 ++++++++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 1 + 6 files changed, 105 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..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..d4bb791f665b --- /dev/null +++ b/rust/kernel/io/poll.rs @@ -0,0 +1,84 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! IO polling. +//! +//! C header: [`include/linux/iopoll.h`](srctree/include/linux/iopoll.h). + +use crate::{ + bindings, + error::{code::*, Result}, + time::{delay::fsleep, Delta, Ktime}, +}; + +/// 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, + sleep_before_read: bool, +) -> Result +where + Op: FnMut() -> Result, + Cond: Fn(T) -> bool, +{ + let timeout = Ktime::ktime_get() + timeout_delta; + let sleep = !sleep_delta.is_zero(); + + 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) + } +} + +/// 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) => {{ + #[cfg(CONFIG_DEBUG_ATOMIC_SLEEP)] + if !$sleep_delta.is_zero() { + // SAFETY: FFI call. + unsafe { + $crate::bindings::__might_sleep( + ::core::file!().as_ptr() as *const i8, + ::core::line!() as i32, + ) + } + } + + $crate::io::poll::read_poll_timeout($op, $cond, $sleep_delta, $timeout_delta, false) + }}; +} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index b5f4b3ce6b48..d5fd6aeb24ca 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 Wed Oct 16 03:52:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 13837698 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) (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 AE21E18D62D; Wed, 16 Oct 2024 03:54:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729050852; cv=none; b=IonzpMEK6HRF0sPOokFoZqeXZfq/Ynzz7haQv0c7madz2o7rDTNufqVvg8Pi8279vBciLvqx6BixHV6tNwzy7uV7U2bVqtRhydY2/6SdnLoWvYo0SYb+g+xPrhzxNyNw0z/4JRsGimxFr69qyL4ZfHI1y3JOSLpfii9Wp6DdAjw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729050852; c=relaxed/simple; bh=jzFKSh9o+bumGj3TnLtbfXkFEwNCdfMJAShnlXD3DRY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PkgmePk8Aj+XpsAk25TxeV10lydIYAO3+VqVqPpxdwCdlwWBqd1Rb00b7rUM2ZbixyD7cx5ivyGIV+Fpidi2MMzNTtnz6EZJIAcFi4at4KQOtC+wbVTBsgOjT+0f4sJL0H+sG5JJlzD0XXL6g3fiXvWiLsOeRnhq2kmF9XrIgEA= 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=ChOkcjtv; arc=none smtp.client-ip=209.85.216.43 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="ChOkcjtv" Received: by mail-pj1-f43.google.com with SMTP id 98e67ed59e1d1-2e30116efc9so4043998a91.2; Tue, 15 Oct 2024 20:54:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729050850; x=1729655650; 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=Wo+ebOBA6uOgnJrPTZr9hFWwcZfDvc+UeNZE4w2Oi2o=; b=ChOkcjtvbKEJIuuNmjTbYv4lesPODnibuLey+sRCcE62wMf0scg4gWGnhy1Pe36fEr vJyoAUzzHr8NfWMVHzA3y2Bd+Rc/8BNVwS68XdiwUpGu7tD/be5FesqF7BZXv4DDMi5L lhD11e8y8kXlrM9najhOdE0VDDwioZAJB5rOde8uV+KJhn8eOQmYdluR960yCK7zIUVk XR0HOIMNw3kQuy3fh6VR+NoF64FqauNLtqGEymCy3qbAmDz3DCaEGuflRnUIY66i4aaT Ri78itSchzZeiZvqO8yiuF5Phzepo7BHzSsZj9HG2VFeewAr/inVfMfw0DGQ/bbQNUcE EuIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729050850; x=1729655650; 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=Wo+ebOBA6uOgnJrPTZr9hFWwcZfDvc+UeNZE4w2Oi2o=; b=JEdPmFx1uHTOai7sLoXpEtB70bRDlTp54lI4N29YrC38HnL56uxfEMu6a9tJb/QKs4 OhaMGePlxvOjCKW6BPcFCr9+9NDz5YEoHqIYFxsRaDLGpj4KnwYJIlzjfKqfjNLz1gq8 qVBGEG4r+nL4D9hIXiwcDUQnafS1kK9Phndt2HLClL3BQaH28/N+rD5Ryo9dqW9tnYNs bXEBDF06UzpqMT23EKs03Ac7i/aVs0BCZ08msPApLPz42rOVGJ1p4hr5RufeX1LO7/wv A910wpAM1ynSuST5KrZ6Ww5ozQ9OXcmgdSIDZuTtv7Kb3iJBPuSWol9goaIDQkPvO8uQ 9T/A== X-Forwarded-Encrypted: i=1; AJvYcCW6cBKOiIWHVM2ZWqR0EdtnTWOMlOIvGf7iBnJNDzTBAjSBU0V3jzyIw4znTHvt8RjVZEC8z0xMmfojbok=@vger.kernel.org X-Gm-Message-State: AOJu0Yxv8SOv/OjBb5Xm4VoZ54vxB+uOWPt5kM5CpGmuaeRzfG8P4e9N 68lySxLh9jvPx9AY3jWWAU2tKtmsIhy6OLHSkqnG3G8exQfPD3tVTKqNZhY7 X-Google-Smtp-Source: AGHT+IEA2CCbHvwGf03FPLUkUraMS05ikMuK9tpmIBZQSsxZM5NDDWHrubA89P/w0fE4ZMPfSNlTcg== X-Received: by 2002:a17:90b:224d:b0:2e2:bb02:466d with SMTP id 98e67ed59e1d1-2e3ab8dfb4fmr3212058a91.33.1729050849805; Tue, 15 Oct 2024 20:54:09 -0700 (PDT) Received: from mew.. (p4007189-ipxg22601hodogaya.kanagawa.ocn.ne.jp. [180.53.81.189]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e392ed1a4fsm2885691a91.17.2024.10.15.20.54.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Oct 2024 20:54:09 -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, jstultz@google.com, sboyd@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH net-next v3 8/8] net: phy: qt2025: Wait until PHY becomes ready Date: Wed, 16 Oct 2024 12:52:13 +0900 Message-ID: <20241016035214.2229-9-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241016035214.2229-1-fujita.tomonori@gmail.com> References: <20241016035214.2229-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 readx_poll_timeout function. Signed-off-by: FUJITA Tomonori Reviewed-by: Alice Ryhl --- drivers/net/phy/qt2025.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/drivers/net/phy/qt2025.rs b/drivers/net/phy/qt2025.rs index 1ab065798175..dabb772c468f 100644 --- a/drivers/net/phy/qt2025.rs +++ b/drivers/net/phy/qt2025.rs @@ -18,7 +18,9 @@ Driver, }; use kernel::prelude::*; +use kernel::readx_poll_timeout; use kernel::sizes::{SZ_16K, SZ_8K}; +use kernel::time::Delta; kernel::module_phy_driver! { drivers: [PhyQT2025], @@ -93,7 +95,13 @@ fn probe(dev: &mut phy::Device) -> Result<()> { // The micro-controller will start running from SRAM. dev.write(C45::new(Mmd::PCS, 0xe854), 0x0040)?; - // TODO: sleep here until the hw becomes ready. + readx_poll_timeout!( + || dev.read(C45::new(Mmd::PCS, 0xd7fd)), + |val| val != 0x00 && val != 0x10, + Delta::from_millis(50), + Delta::from_secs(3) + )?; + Ok(()) }