From nobody Mon Feb 9 08:33:36 2026 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 580D832ABFF for ; Tue, 21 Oct 2025 09:27:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761038880; cv=none; b=C7XYTRAOupNsf3LPky9S8xMOsY5PMGt5M7/AaemjyJEljMXXISPfFn8BICLzQ89g2jdE3i/596/IyxEYbCssOXug6MiRumFvMjH+BV5kkGrNCH7COwmI03PKAlKhZYXT1hJRehecHKhOofQDJYBE/T+LH+XVOaTtJHELYUmxL0M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761038880; c=relaxed/simple; bh=3/K3GDSMOD+085rTVXVIBdxxT7kof4c6WZKbETezSt8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=egq0jnj26XtLBgS9RuAWXxL7AF2shutWcnzLyGW58HS9NWzXNbouD4Y9prE22FBNj290+2INAY8LhMCig0pB0L8qK6Zhah939tjWWKM8jAqtXeV3ws0YdVE44Nn96XVpemCZcbaNHxbrOiYBNnUK39vfj3g+sAv++hCtGB9eXZk= 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=QiUJk9Mo; arc=none smtp.client-ip=209.85.210.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="QiUJk9Mo" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-77f67ba775aso6948566b3a.3 for ; Tue, 21 Oct 2025 02:27:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761038877; x=1761643677; 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=Z2NlzGd5x98kMuQNMDYXrVfdPQkIyShWAfGfbxPh7uc=; b=QiUJk9MobWfmTaOyNULUITKOfyQNOg6x+qroulBGq+kwGEiCtYPNrq+PQ5b2jW8Wdh NxSoffCoXsDlKoK6/FNxczJ9WEshR8U4zvCeGz5Vh/RSMyFyocWKkhDnynfSEZt5Dpj2 GhaCwxK2wF7tykBzxsvxfM3+ALfLNE3fQj/jmUAiyfXLLQLHvPDu9dyhYMEtW9fE9YB7 KHcvy1VdkSAuUtemccCVEOzZoE8Z0N+64Pf/hAuRXLmjiHaerXKZHU02/gdVAd/zKpF2 e5x5vndYRl2MxwiDOGagfakDN9ZNFuqwIarMRAj3fe5USi2flg2vDldYI0gMv2d+NQEC 2G3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761038877; x=1761643677; 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=Z2NlzGd5x98kMuQNMDYXrVfdPQkIyShWAfGfbxPh7uc=; b=qG85QXc42RjG0yuLOzzUm5UEf+90nIYk2elTjzcNKgJ9CcCpQCiTOmWfOqnEthqPTW zDRbRPXfiW9D80+EGUf9OG8LnRRO4v3TuorQc1pInbaCpgncdNM/flHOVIExLWWXOs0g AEQO/+PpY7vC32HNrTzJrYs7b5lS1DwBlNflib3NVqXs7KCDKWhnQS1hhVjXmhyHByiR oHFKtmpS3tgx+oDVm/0du0GNqKX5X1+If9YSttFb1JFSzHI1TIT5+Al2alULitVZz8Ii Xv2Nz+kEY5b8bROxIozTA4c3J0Xth1S+TwF5hyhvGGVKzaKNqmG4V5OoktwLy1/lUFz2 7xKw== X-Forwarded-Encrypted: i=1; AJvYcCXYydSdeY9WpqUVTvXqEx9/ukZVxrBSpkiSzEJtxMiMqumKgcRdXv3hLO/+FrkEmYE6EWGTjTxPq2fnn3Q=@vger.kernel.org X-Gm-Message-State: AOJu0YwkeR5pC5M5gmxFnl7LU1d8x/Onf+EV+xfcaqumKeIuSb0TuHFE OHFeXJMocTQXSoOEIJ86Y7tVaAmW45EH1+UO6apq4lwHYF7z9jrD/g+m X-Gm-Gg: ASbGncu6Rv+cxp2QL8UHif4hqSEJ50zowOjNaZQ3FiuFbExk8EkY23nmJj1gA/DEHSK jZ6o713aFAtgI1Lj8cXWtTfZqdxj03FIEFjkLDoQ2lPgw/MfGLTrHVA9LQ5th4KACooRskWQ8Ir c5V/5GotyMsJqa1u3OFgDj4hzHrpdEjSN1ZJOvoAiMPhZe7zHSOQ2oFVFG7qgOdHqcF1Vrq7v/G cERMgW7dK7JrPxE3E51be00gGNIY7aDScf2PMNvLyqWQll4XE5H3tG4WTlkKTDkjMlD059wZRw6 jz6TrzdYGr8+wwf4giTsxC/s0XNCTU5jj9vjPTPP33SKV/XYGGOTwPCOCzxwwbkknQMr33QVwb3 dbAs+1i0Ki5rIld6/IyLt0Ad1NbGkr8nGW06bZMKFGw9uIZLmLEKDsQwFG0HoTCFfiJzxz9zfri VNo7q+mPtcDlbn6tRm/iICtUwNziBxHV1X2UNd3TbYSIE48gTgrzci67VBZYk02h06MW9yhg== X-Google-Smtp-Source: AGHT+IG84QcMXftLvNN2/W5AZW4r3LF5ogTWd+gpV80sFCmWfCriH0heSVLuZ3TAujgYHHi+0lrA+w== X-Received: by 2002:a05:6a00:182a:b0:781:556:f41 with SMTP id d2e1a72fcca58-7a220b07183mr18510234b3a.19.1761038877219; Tue, 21 Oct 2025 02:27:57 -0700 (PDT) Received: from bee.. (p5332007-ipxg23901hodogaya.kanagawa.ocn.ne.jp. [180.34.120.7]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7a25f41ad15sm493996b3a.41.2025.10.21.02.27.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Oct 2025 02:27:56 -0700 (PDT) From: FUJITA Tomonori To: dakr@kernel.org, aliceryhl@google.com, daniel.almeida@collabora.com, a.hindborg@kernel.org, alex.gaynor@gmail.com, ojeda@kernel.org Cc: anna-maria@linutronix.de, bjorn3_gh@protonmail.com, boqun.feng@gmail.com, frederic@kernel.org, gary@garyguo.net, jstultz@google.com, linux-kernel@vger.kernel.org, lossin@kernel.org, lyude@redhat.com, rust-for-linux@vger.kernel.org, sboyd@kernel.org, tglx@linutronix.de, tmgross@umich.edu Subject: [PATCH v2 1/2] rust: add udelay() function Date: Tue, 21 Oct 2025 16:11:45 +0900 Message-ID: <20251021071146.2357069-2-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251021071146.2357069-1-fujita.tomonori@gmail.com> References: <20251021071146.2357069-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add udelay() function, inserts a delay based on microseconds with busy waiting, in preparation for supporting read_poll_count_atomic(). Signed-off-by: FUJITA Tomonori --- rust/helpers/time.c | 5 +++++ rust/kernel/time/delay.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/rust/helpers/time.c b/rust/helpers/time.c index a318e9fa4408..67a36ccc3ec4 100644 --- a/rust/helpers/time.c +++ b/rust/helpers/time.c @@ -33,3 +33,8 @@ s64 rust_helper_ktime_to_ms(const ktime_t kt) { return ktime_to_ms(kt); } + +void rust_helper_udelay(unsigned long usec) +{ + udelay(usec); +} diff --git a/rust/kernel/time/delay.rs b/rust/kernel/time/delay.rs index eb8838da62bc..fb7c15dfe186 100644 --- a/rust/kernel/time/delay.rs +++ b/rust/kernel/time/delay.rs @@ -47,3 +47,40 @@ pub fn fsleep(delta: Delta) { bindings::fsleep(delta.as_micros_ceil() as c_ulong) } } + +/// Inserts a delay based on microseconds with busy waiting. +/// +/// Equivalent to the C side [`udelay()`], which delays in microseconds. +/// +/// `delta` must be within `[0, `MAX_UDELAY_MS`]` in milliseconds; +/// otherwise, it is erroneous behavior. That is, it is considered a bug to +/// call this function with an out-of-range value, in which case the funct= ion +/// will insert a delay for at least the maximum value in the range and +/// may warn in the future. +/// +/// The behavior above differs from the C side [`udelay()`] for which out-= of-range +/// values could lead to an overflow and unexpected behavior. +/// +/// [`udelay()`]: https://docs.kernel.org/timers/delay_sleep_functions.htm= l#c.udelay +pub fn udelay(delta: Delta) { + const MAX_UDELAY_DELTA: Delta =3D Delta::from_millis(bindings::MAX_UDE= LAY_MS as i64); + + let delta =3D if (Delta::ZERO..=3DMAX_UDELAY_DELTA).contains(&delta) { + delta + } else { + // TODO: Add WARN_ONCE() when it's supported. + MAX_UDELAY_DELTA + }; + + // SAFETY: It is always safe to call `udelay()` with any duration. + // Note that the kernel is compiled with `-fno-strict-overflow` + // so any out-of-range value could lead to unexpected behavior + // but won't lead to undefined behavior. + unsafe { + // Convert the duration to microseconds and round up to preserve + // the guarantee; `udelay()` inserts a delay for at least + // the provided duration, but that it may delay for longer + // under some circumstances. + bindings::udelay(delta.as_micros_ceil() as c_ulong) + } +} --=20 2.43.0 From nobody Mon Feb 9 08:33:36 2026 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 327F7329C4B for ; Tue, 21 Oct 2025 09:28:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761038884; cv=none; b=dQyLy1YPGSU1509E1BHPJ8cl6ZHYoQKlOiuJwkO++eyWhdEcmuYmhhsqd0Va1k+xZCGBxbGLKJs01hCATjTs01oHGTSE61Oq4RvPIRrGRaM8pCDEeRi1tp7G/yCBRkFFAoTtTPu0+1w1p2IVJI/kIyefL6o74Ci1RQ5Wsqpmjd0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761038884; c=relaxed/simple; bh=FFUbOLmpWiVq/OQnGPHbAXC9NvVSCCzyQElac8PiRN8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fpIDS5dzAsKvJUrr5JpMSx+J3UoNrIvzCr1d47MwmGGypWe+PGXzPlp4NjTAQSbNmc6n+QYUl8qPip0BTKIhkpnipjk5EZIB8q8H4QpJrNcy4KCWAJrqHDgsBSV5TbHuiAw/xv5cZ88BV8wW2o07qowaX7/PzqCnd/lINVNGp64= 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=R8gmg47j; arc=none smtp.client-ip=209.85.210.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="R8gmg47j" Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-791c287c10dso4125961b3a.1 for ; Tue, 21 Oct 2025 02:28:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761038881; x=1761643681; 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=DP4fvZf3pq9SY9bqxcQGVH+sQa4EzI3l8Fc79jvogJY=; b=R8gmg47j1dWzGxsUhgK9IPgq67xahELp36WghPwP/648ej/hEVRigPP43HyFlrgZqS 7vN0HJ5AKzqxvhgSAiqy67oVLECDp4qtHHTNbsnTWoeHzxwC3RwYtHxcGu7UmsXSiAbI S2nW30YOS2UZsw9+KGoybv+w4Kwb6OPuS0pdrDdOjlNMuH3Ibo2OWWW314OxmNJxp2Ez sktid0ks1BgNsp9Kto+FRm9zDeGIEXdOXn3515QlLTX4Aj7YDRJ0Ag3XtdUZLaxCYN0K 3f18YkfIlbYvEnMAKlND8Ib+Tm8no61jCm/Bi/DmxgtDksrEC19JfoJFsrUS7uaM/MpY q/TA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761038881; x=1761643681; 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=DP4fvZf3pq9SY9bqxcQGVH+sQa4EzI3l8Fc79jvogJY=; b=IUVoLZ+bKnfGO1UDSqA5FCi2cL4uyuN5bmvmGCSct46G5JUZ5fBOHKJZfDcI0Mpcuu es6Uc2eNq4yufSidsHZXEBsOftkL2rvvwvqv+5MTJYLO/6Yw3Q+mWyfm4Mt1U1NlEmc5 FKfQm1netjqn58sjKSlb/XXT/JETc5bgxXInXJZfur9+RP/GQMeI7en0T2NU18v55qlp EmLAzyPBfrUE4RqKEHX2QRPduu6daVD5ingpN6ZcnNbEC2rNi7BCli2EBh+rUpM44F8O kd7c8XcF9n2cW/eNZCQXS8fe2h9ez1tfEcAzDYJpfg1x1ISZK03hd3IbqMU2ZEKeQto6 POyQ== X-Forwarded-Encrypted: i=1; AJvYcCWRPVUOy6vggWKSUexOEGa3V0+v41iUCwL3qhHyHg37KFE0DOaLH7A9kaXVvKXN3DGRp9mLHgDMAR8KtJY=@vger.kernel.org X-Gm-Message-State: AOJu0YzqD8xnOTVRylpMcvNmNhbpFN0zuAmWmh9PncYENLLW6SPksbcL BXU5V85hapWyM4DdHI+uEiZu6qv84R5/nvYxE3auLarn1wzLGUCOEGzW X-Gm-Gg: ASbGnctub6t3C+49nT1zRw0zGWK4OcA0EUev6UWdqVMsM6DLUFmNJ2l22MhkNWluImX 28TuBdlV3ygZSUfFfHgbKEN+v5AYxevrXsF191Sm/Ltb+kuqAqiy/N7R408ZirQEJiiUffso5sI tmxJKv/O4a/FG2hqj3m4TZBlPU0fqUCV29vGj194ffoTVG79LQ85wS4Bib1CqWKd4QiuK+iegIo hezmV2CH5ooK9LpW7uPkxrdHnr7huUNoFEddj/rhR8SAAh/ZX1DyawC3oliriLY17aCJBdlI4Fb yWBtz1xTW1zqNj484SD/K4ENH2YpcxPluZaXyMSjCYBUbA8RYOX8B2L7wnRLEKXb/LEg8XaeE4O XoIt2sEcEtp21EHDYkPoLx9mgS2mGFIhWHW7zwytCU+FR+KBap2vNp4KDbXdKMkr/7ERxVswGm9 +fjarjFOsDGcJbTYBzUKUfLRl3a+57DT9Ug+CdZRSryMdkvVoy X-Google-Smtp-Source: AGHT+IF7uNQlQx60VxuY/4vkMJx5kQH2Fl9qR73hXhH6ml9SwS2D1kJVI4vEX9WnryxiBax6hS4PCg== X-Received: by 2002:a05:6a20:918d:b0:32b:7001:6601 with SMTP id adf61e73a8af0-334a8585a44mr22213945637.25.1761038881302; Tue, 21 Oct 2025 02:28:01 -0700 (PDT) Received: from bee.. (p5332007-ipxg23901hodogaya.kanagawa.ocn.ne.jp. [180.34.120.7]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7a25f41ad15sm493996b3a.41.2025.10.21.02.27.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Oct 2025 02:28:01 -0700 (PDT) From: FUJITA Tomonori To: dakr@kernel.org, aliceryhl@google.com, daniel.almeida@collabora.com, a.hindborg@kernel.org, alex.gaynor@gmail.com, ojeda@kernel.org Cc: anna-maria@linutronix.de, bjorn3_gh@protonmail.com, boqun.feng@gmail.com, frederic@kernel.org, gary@garyguo.net, jstultz@google.com, linux-kernel@vger.kernel.org, lossin@kernel.org, lyude@redhat.com, rust-for-linux@vger.kernel.org, sboyd@kernel.org, tglx@linutronix.de, tmgross@umich.edu Subject: [PATCH v2 2/2] rust: Add read_poll_count_atomic function Date: Tue, 21 Oct 2025 16:11:46 +0900 Message-ID: <20251021071146.2357069-3-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251021071146.2357069-1-fujita.tomonori@gmail.com> References: <20251021071146.2357069-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add read_poll_count_atomic function which polls periodically until a condition is met, an error occurs, or the attempt limit is reached. The C's read_poll_timeout_atomic() is used for the similar purpose. In atomic context the timekeeping infrastructure is unavailable, so reliable time-based timeouts cannot be implemented. So instead, the helper accepts a maximum number of attempts and busy-waits (udelay + cpu_relax) between tries. Signed-off-by: FUJITA Tomonori --- rust/kernel/io/poll.rs | 80 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/rust/kernel/io/poll.rs b/rust/kernel/io/poll.rs index 613eb25047ef..c7dd0816205f 100644 --- a/rust/kernel/io/poll.rs +++ b/rust/kernel/io/poll.rs @@ -8,7 +8,10 @@ error::{code::*, Result}, processor::cpu_relax, task::might_sleep, - time::{delay::fsleep, Delta, Instant, Monotonic}, + time::{ + delay::{fsleep, udelay}, + Delta, Instant, Monotonic, + }, }; =20 /// Polls periodically until a condition is met, an error occurs, @@ -102,3 +105,78 @@ pub fn read_poll_timeout( cpu_relax(); } } + +/// Polls periodically until a condition is met, an error occurs, +/// or the attempt limit is reached. +/// +/// The function repeatedly executes the given operation `op` closure and +/// checks its result using the condition closure `cond`. +/// +/// If `cond` returns `true`, the function returns successfully with the r= esult of `op`. +/// Otherwise, it performs a busy wait for a duration specified by `delay_= delta` +/// before executing `op` again. +/// +/// This process continues until either `op` returns an error, `cond` +/// returns `true`, or the attempt limit specified by `count` is reached. +/// +/// # Errors +/// +/// If `op` returns an error, then that error is returned directly. +/// +/// If the attempt limit specified by `count` is reached, then +/// `Err(ETIMEDOUT)` is returned. +/// +/// # Examples +/// +/// ```no_run +/// use kernel::io::{Io, poll::read_poll_count_atomic}; +/// use kernel::time::Delta; +/// +/// const HW_READY: u16 =3D 0x01; +/// +/// fn wait_for_hardware(io: &Io) -> Result { +/// match read_poll_count_atomic( +/// // The `op` closure reads the value of a specific status regis= ter. +/// || io.try_read16(0x1000), +/// // The `cond` closure takes a reference to the value returned = by `op` +/// // and checks whether the hardware is ready. +/// |val: &u16| *val =3D=3D HW_READY, +/// Delta::from_micros(50), +/// 1000, +/// ) { +/// Ok(_) =3D> { +/// // The hardware is ready. The returned value of the `op` c= losure +/// // isn't used. +/// Ok(()) +/// } +/// Err(e) =3D> Err(e), +/// } +/// } +/// ``` +pub fn read_poll_count_atomic( + mut op: Op, + mut cond: Cond, + delay_delta: Delta, + count: usize, +) -> Result +where + Op: FnMut() -> Result, + Cond: FnMut(&T) -> bool, +{ + for _ in 0..count { + let val =3D op()?; + if cond(&val) { + // Unlike the C version, we immediately return. + // We know the condition is met so we don't need to check agai= n. + return Ok(val); + } + + if !delay_delta.is_zero() { + udelay(delay_delta); + } + + cpu_relax(); + } + + Err(ETIMEDOUT) +} --=20 2.43.0