From nobody Mon Feb 9 05:10:44 2026 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 B14502E040A for ; Sun, 26 Oct 2025 12:55:30 +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=1761483332; cv=none; b=LUECSI0ZWlPxKMP7QAiv/NzyFcNUjHFwbnNpqhuXlgDxkXXyzPe7A+oWcvFxzKMoprDDW041CB4AW6GyooauuWNQUYRAc/ONUdEv+BZ+IWc6d2mFXRSgKQAb0dRh0N5gz0YdrH9+oO7VV4eDe9Nv8pFcaZZjoZc5tKZPHLzrCsQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761483332; c=relaxed/simple; bh=BQH7hgD7+xt4W7z/S368Lm76rShNO58wKexO34QQko8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gB1qj+YNNc8nnLWg4zmLY0NL4E48TR2MThGtaDiwFATdJgPFnsLRN26bt6Hlwyt0M+QHlMhXUgh2pYSyrMTnVpIf1ZdcGVJ/81shUhbFNC1PqtaFRbxjvDTqlTqTT72Rhcfb5fgJovNI/hMy19zq8XbWbCawF8cWEYTS+SJLhrE= 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=AJkZvO94; 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="AJkZvO94" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-290cd62acc3so43606415ad.2 for ; Sun, 26 Oct 2025 05:55:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761483330; x=1762088130; 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=mpL5GRYYewBmCb3HYHj7NyzEFHr83PdgBMD+j8Wif4Y=; b=AJkZvO9483JwtKIc9EPOy0w5aYFQiuH3yJ2EqdHKKpXaTikU4mJ1S8ZX9Jm6t55WYa iPvCixebq8mm+LX5BURYFz6vbhnmhI0kHKZef8fkEPeQ5cFYLxxNrpo+8iB90QTNv8d+ 6HduCaucLIlnsxeT59Vk0YhktEDL4K7WLbz06GMhu1O60d+dz2bSltHflb5RNbQomjAT 8nh/TqXCeHtxYpaF20OkC2VOe3u5xf3xjiZrMkXYtmDvlr5L1MGdgjAQmPwHC+cJ3G// zt5hvRpjA+FbaRbkcBSza99FvIYhutYQ/QNuSmvXlW1cLaGzT0QFhqM8KPIZOS3/8eYZ Dm3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761483330; x=1762088130; 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=mpL5GRYYewBmCb3HYHj7NyzEFHr83PdgBMD+j8Wif4Y=; b=h5ZpLuw4QUQL9lNIvcBR88dnau0oW8Y0tshmZQr2zmIgU8i0QlN/Fjz5s9q/1XtFzs hZVRxvNhZFUC6GpfWsYQF+GciNoGt35gs2rnACBs/FFQAq0KK6iVnLzGu+Kl4A3CPM7s Pxm7yxWqtiLPSYYMWyq0kYw8tRUEKmZql+PynZrYnYoSWqDyhynty3/wSqXkDFWUoBHh zLgVXqCX9otHYmD77OPDgc4s27GZgpKgTurHmwyT3d17H+YSHDWX4I9Zr02WsJy81gaR bgb4O1y4gxVKc/lvFQ7DM3ksZ7CaAVvTOKn6QplLo6KbgIk+PJXTyTBwSy4hrpmdl0pW DFFw== X-Forwarded-Encrypted: i=1; AJvYcCWBpaFpZ7FXfAZtGj4QzyIkaHvXLA9CScm2cGoILUsFMWe3ixuiyqGqAwZk8+NUW4dg4k9kBn207YVbClA=@vger.kernel.org X-Gm-Message-State: AOJu0Ywfi3SWwUHLgEAXyeRwv6DFyh2Bwz16OsW4pTXRLJTPjRXYb6BF KjnTCjQpVpXXZI8mYS8+42gDGLbCWRJ9k/F5ileWxm6WU02e46q0AxhC X-Gm-Gg: ASbGncv2J+/mykOsRuvtthssebiLC1b8FzQbCKaVOfXnVwa7ZkP+s6n3gaz45lWyTbw 9wpvQxSKCKaMpr8k5S4zNxsI1SPXGMg3kVf2ExMPNeaiaeYl6eiyZOyuCOJLsgwRUXnmHZxkw3K 0bmau2RhIsaLIfP9QhH5KCKsPVFvk4EMlppoi+ENpNhvSp4dsFB6izNTD6ZTQpy8af027VYCiwi px24tueykwX3ah7E3AuHeKzoO1vfZXW/+mf8HLuwkREE+gUkbWsirlQSc7mLPpoKoR3EdWjJtbP MPRTS6PwCYo7as5c7udQqdJGFLlnxMBYYAThx/aMsSvqMcBfqfXjuSOWapmMVWs24PYCvB4DhMT 0TbAwFm7PNgmLMhoClno06RppJDUsneMTB7ltH5tE9BHcyUMBO7wL9pmtvVW9F+/mH7kpupeYqR DZL3O6FEej6G05yCsITAx9O8PVG2vRm+IRl3XcU3iV0cTl/uZI X-Google-Smtp-Source: AGHT+IGbWGIIOzAhr1d2Z5UjYERrsJ5I4YvcxAZSP1AAcn2uDneNWiOnjZDQ7TMXZAuZy/JTyzooJw== X-Received: by 2002:a17:902:dac3:b0:277:3488:787e with SMTP id d9443c01a7336-2948b95c1f5mr102003455ad.12.1761483329901; Sun, 26 Oct 2025 05:55:29 -0700 (PDT) Received: from bee.. (p5332007-ipxg23901hodogaya.kanagawa.ocn.ne.jp. [180.34.120.7]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b71268bdb2dsm4641759a12.5.2025.10.26.05.55.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Oct 2025 05:55:29 -0700 (PDT) From: FUJITA Tomonori To: a.hindborg@kernel.org, alex.gaynor@gmail.com, aliceryhl@google.com, dakr@kernel.org, daniel.almeida@collabora.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 v3 1/2] rust: add udelay() function Date: Sun, 26 Oct 2025 21:54:57 +0900 Message-ID: <20251026125458.2772103-2-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251026125458.2772103-1-fujita.tomonori@gmail.com> References: <20251026125458.2772103-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_timeout_atomic(). Signed-off-by: FUJITA Tomonori Reviewed-by: Alice Ryhl Reviewed-by: Andreas Hindborg --- rust/helpers/time.c | 5 +++++ rust/kernel/time/delay.rs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 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..0739b75fb9c3 100644 --- a/rust/kernel/time/delay.rs +++ b/rust/kernel/time/delay.rs @@ -47,3 +47,42 @@ 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); + + debug_assert!(delta.as_nanos() >=3D 0); + debug_assert!(delta <=3D MAX_UDELAY_DELTA); + + let delta =3D if (Delta::ZERO..=3DMAX_UDELAY_DELTA).contains(&delta) { + delta + } else { + 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 05:10:44 2026 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) (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 D6C342DF149 for ; Sun, 26 Oct 2025 12:55:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761483336; cv=none; b=b5rkZwm9ZnBeuJ0zCnJMLyOvs5jbNPCmNxg+kPjmX2hsUxLZ0zzhLuL3JJGbl8xCEa+0CaN/ll3w9yOM/ZX50naKFCD/RvGzK+AO3Xbq+Hwm17I8F5HvIx6IqgpWIDQXJr8jbBJVMClcoBiM2P3BADBWaYEgDnQxsjVQeztICT4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1761483336; c=relaxed/simple; bh=ga/2Ql3JpoWfmrFHfn50pQoQySYcTifPzY8FBOMDrWM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YyvGe35tbrW/pHIuIRAb9s+qF/n+o9rwcNtKzefpiUlEUTOjVbU7HHDOD8X6fPnOskbbfax+ViPJT4TJ7EcitUgbS47my9KHe3Uv2C3q+LT2bcX6F9Fc3cOym/+P3rwhocPGe/dfxUGR5bVIYKrU5C/QljZ3cdjUVhSl+61CBdc= 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=fnpQIUnB; arc=none smtp.client-ip=209.85.216.44 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="fnpQIUnB" Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-339c9bf3492so4422414a91.2 for ; Sun, 26 Oct 2025 05:55:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761483334; x=1762088134; 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=1CAzZXd4A3lF4KIIahj6WEixTTC+T0E5VAC/UV75Tc8=; b=fnpQIUnBCpFjd2zqIvrtqTrB1OkiyRzsAjMhd7HwNhrXBJ1qjstAqCumalTsXE4l0L atydmeuTQEJx27Uduf6GUcNjYrlamS6S9eVkNmiUTbjnn+MpIiiYV2pQShsBoqLOZSQZ 1IyNYjhnqMv4nuFxDBWRtrMN34o5APdkClvzm42hYlYRT4gge2fGOlNKPzFfac/uN0Th SrbqHADzNfwG83tyHWBclKvnoJZ2ivkZBNAT6VhBN2YdN6eozg/zaEIU3jkLbJOFF52G AJ9jQFadKDR1uB8iR4R6slyXV3+L1SwAgjQbWCDkOu4e0K5/6p6ElLLRUyqCjYmpSW3q 0gqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761483334; x=1762088134; 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=1CAzZXd4A3lF4KIIahj6WEixTTC+T0E5VAC/UV75Tc8=; b=g3AH9SFHYgYrtnvq78ZCv2YjpdV7MNOsogZW+yFUesBtr9OlucvE/Yo+3sfGxREMhO ZEJQvSicEJerjYUePybwBttxUyd9LZ531nYVkTfALgohD6SMQdxeeLevpjOnxP3YkNXd vgqU2JzIy+LHOIF0xEIED6J+iZHTDYCLQ7QK9bhxsvxQSz6+U10YQxoI46tNFZjOFX96 QLgoK6BAA3PfXmLY8nn3kHUzyBr5NZbvSt1uKIyCRvbnW34zsL02wWacuEqLMVepWh8h taGf1w62nATOR9QRHDQriPBQgsQBYMgsFpkU3P3c2oxNQn2/07xOqGIiiCq82aLwBUBk E8Pw== X-Forwarded-Encrypted: i=1; AJvYcCVK1OEcVprXIwU6fjCkmwXZTtCe6LxUje4OWdLGD+yQrCrf6KeWrjWvR2k4yE2UagYBr4xC96DHvRiL6ws=@vger.kernel.org X-Gm-Message-State: AOJu0YwlspIkLWQO3BCUJ62mGQzU2o1BDcnSWo9AzMHQgs05j1hJNZcR 1+hrwmNRJvvod81Wol5vjtA6T/EhacZsF4T1ac1JzCmHz7+OunsZnNuU X-Gm-Gg: ASbGncs5ZvpRe7oQn1HoJW+3sGwjBXc2q7AB1lHMr2Fycy7hjDOTAD9Tkub7680Kd4v C7Xg7UCpMKx/gEY5VwGQaR9k0Vlo6yH2smTvAE733zGJFU65uC/V2uBFndX6CIvYl8oayGgaJ98 LCY/Szmaj3n36HJBu+wCsx/rD6a0IQWtDxG9ITs+f4DgwSosvp5hD3UU+wVTie5BmT/Yqs3HEpj VasrTAXg6HUoBVJO7HFmp69spLfDTfwi+m/1LuqTqK3CkzcUZRaQPm/5sjrS+uibpj94/meXRRS 31uqQUhXp56KMkbCRVUuQLkOXS6QpFCFfEUuozhuD/17OlTwTQwTYBG+PPapeY5zrsOWDB25wBt K/QuDOV/9haExbXcEQjUZijr24dUV+UK/3CY3atYwytrCjybX4TfrMOhufN+sAh96BwMs9BY7Ee tpFNub6+SUZfzdLqrR+IL5FRY9/XfJzbhjpP5ekQ9Rb0sZCpbF X-Google-Smtp-Source: AGHT+IHZWhs/E+CtlUuWIrH5OJyrAhAoGqPWvwTTsxXtMWGAgRDVXQfA8CrHQP0ju6+3vvvAqTyMlA== X-Received: by 2002:a17:90b:5105:b0:32e:e3af:45f6 with SMTP id 98e67ed59e1d1-33bcf87b6a4mr43304692a91.10.1761483334095; Sun, 26 Oct 2025 05:55:34 -0700 (PDT) Received: from bee.. (p5332007-ipxg23901hodogaya.kanagawa.ocn.ne.jp. [180.34.120.7]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b71268bdb2dsm4641759a12.5.2025.10.26.05.55.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Oct 2025 05:55:33 -0700 (PDT) From: FUJITA Tomonori To: a.hindborg@kernel.org, alex.gaynor@gmail.com, aliceryhl@google.com, dakr@kernel.org, daniel.almeida@collabora.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 v3 2/2] rust: Add read_poll_timeout_atomic function Date: Sun, 26 Oct 2025 21:54:58 +0900 Message-ID: <20251026125458.2772103-3-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251026125458.2772103-1-fujita.tomonori@gmail.com> References: <20251026125458.2772103-1-fujita.tomonori@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add read_poll_timeout_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 Reviewed-by: Alice Ryhl --- rust/kernel/io/poll.rs | 72 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/rust/kernel/io/poll.rs b/rust/kernel/io/poll.rs index 613eb25047ef..65c8ec3fb683 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,70 @@ 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 `retry` is reached. +/// +/// # Errors +/// +/// If `op` returns an error, then that error is returned directly. +/// +/// If the attempt limit specified by `retry` is reached, then +/// `Err(ETIMEDOUT)` is returned. +/// +/// # Examples +/// +/// ```no_run +/// use kernel::io::{poll::read_poll_timeout_atomic, Io}; +/// use kernel::time::Delta; +/// +/// const HW_READY: u16 =3D 0x01; +/// +/// fn wait_for_hardware(io: &Io) -> Result { +/// read_poll_timeout_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(()) +/// } +/// ``` +pub fn read_poll_timeout_atomic( + mut op: Op, + mut cond: Cond, + delay_delta: Delta, + retry: usize, +) -> Result +where + Op: FnMut() -> Result, + Cond: FnMut(&T) -> bool, +{ + for _ in 0..retry { + let val =3D op()?; + if cond(&val) { + return Ok(val); + } + + if !delay_delta.is_zero() { + udelay(delay_delta); + } + + cpu_relax(); + } + + Err(ETIMEDOUT) +} --=20 2.43.0