From nobody Wed Dec 17 11:05:32 2025 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 4A4BA3064B3 for ; Mon, 3 Nov 2025 11:30:41 +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=1762169442; cv=none; b=c1Yrh8nSqnkeBnP9B2vmqUDeZVcobKEuiBZApVq7nIpM1D65uAgMwaZEEn5WlK45i7VAp83GJ1zp4rlP8TL4JHhey2cSmEWonYsOb/5p1u+SmTvE7qL0keQtsdupS+upQwWqew+1YpWFuIsxnozQBNrdhn+2eIgS2tM9dpXU71w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762169442; c=relaxed/simple; bh=c4fre0DPVQedQ1omSaBy8kI0uNFs2j2AoanFTIizUb0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=i6QSitg7Z1lsaVjWw77ZAoVWJZhcrHKImBc1wwwyNLF6JLudQFsTQXZxKdnJWiRMczuRJLT1fLDt1O2xitpEe5MJw+P1raPz/jqmLAbQufQ2WFgNZ0USuJrR9BuhQqQCg8nF0ciksPtvl18drhcBapW9pN+I04xdYcB8pxC9ngA= 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=Xr/aPDju; 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="Xr/aPDju" Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-7aad4823079so823725b3a.0 for ; Mon, 03 Nov 2025 03:30:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762169440; x=1762774240; 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=UTYI1ijY3wk0C6HSPCcVsF+f0DiQ6/0qsG0vbcCU7Hs=; b=Xr/aPDjuFeKdHnPCy++p2cTqizP3xQbbBxj24o0ARGe7tpgcRUcSRYhk9zX3L+HJNm 2mSXUrCesLjd/3aIl3V4uh1WptkZX4KlJtHRQ89M8FessdIME9p1Q8ldFGdEmkU0V1tG FM9HIxDUUgNajIOZLd9imZZFdsn0jS+Pp0FafGO7ArwOnKTBvLYwJCtAgBiaglU6dT92 smbTBw8bn5g8lYuIr1l+K127thMDdMMKbLTz0Xutl7Kn1TLaD851L3OKyHd4uigKndm5 UIQ9AEDtZ52CL36+u2yUsRFMrJdFyfu4f272t4WKpNGiY+2wDUPnu17yFmDgd8ZwYYbH Iv3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762169440; x=1762774240; 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=UTYI1ijY3wk0C6HSPCcVsF+f0DiQ6/0qsG0vbcCU7Hs=; b=Yg7eavcTDt8h78d2UjP+g6noPnspVb0keTbXG2WwMxw23IeBMaA4eQOe6U947LcojZ MF+eM0+v9f0EmtrxXWZDq4K5DCOWd1Mfjo66R4HjXWylvxDxx9E+6hvKHIiPbf0FsYIB o+yrJsCzxtEC6WSWaDAJLQnRJBr1qB6NYhnRSdPv/WGtBuMGjU0knusP2xWwno9jTgff l0UF56Qfqk5744vpuQ2hkz7OneBSmHO0pxgPnKO7cDkv0Ul7GpzMcGwUVWlLSOb0Jmrv kQw/E7eHmlqc8h08mCbQa23igPVAbrDh7sGaZAUV4pw76BfNjW96r+O8vZD+dKGfDsLI KvRg== X-Forwarded-Encrypted: i=1; AJvYcCUuu6O5MOMvexexOmqsqCQwl/+/xSpTaQfPRCiiDTe/OPV3tDnO12fkIpPMY3tn1Q6wyxpKO0UqMUDSJ/M=@vger.kernel.org X-Gm-Message-State: AOJu0YxjYRMDaSBRpO1aYNfwArldZL4QYrr/on7KtBvUXH+JfP8htXWA ynzwthXhxy7gkj1lYpnTVtIIWwDBv6XxtdnElM16X8tVRsL6Eo4o1/wx X-Gm-Gg: ASbGncs/CVgJiaJo4zuUXgmm2Vv3RG/MgdpxtEAvJN+NJxK9yLACZ+lOP+2nO/w844B 7XZe5oodwlxojTjZRkyBE/YizQY7CjyhDcPiqKVR5gdW4kBkZ2KDDkR1IutmcycVESlHx4rM6VF /dSeqoFINvGNfdod07AfMrEKa9Pxy98nsQd9nvnRW1C5y/vJjbno28aJpR6qUVyoDB/Vp318gDM tt3C5LBYKfELSdptIqM6g5YLY3SgYodiZ61sgFIWdyknSA+ODSSxsnwLahYmnMyKJP9Fi4S/YoL G3BJPOy9dAN3mMg+6UqErlzz8lY8oqYvk8DCTeeOo9DrNp7AYjk+GYN1q7FBwWYnR12QqhD/nsg mrfUobqCLUfQEctZx/Rm4xdgs+n/IwrD40Mwameb61JbrrAZNlIW0UHQeSYr4cqQKo9EJSGHbXK LCRmjOUcM0z+/cF4fBl1vP/2X/+tUFieFf9Cr1LOHRcDJY10Zv X-Google-Smtp-Source: AGHT+IFD63eASF0/ho7IY2zD1kT93ld6xW+VcralerQCOlJHoW/IJwkqJ4M2KL2Vbvl399L7WhCpPg== X-Received: by 2002:a05:6a20:4322:b0:33e:5d06:92d2 with SMTP id adf61e73a8af0-348c9f67250mr16915800637.5.1762169440438; Mon, 03 Nov 2025 03:30:40 -0800 (PST) Received: from bee.. (p5332007-ipxg23901hodogaya.kanagawa.ocn.ne.jp. [180.34.120.7]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7a7d897c632sm11011033b3a.10.2025.11.03.03.30.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Nov 2025 03:30:40 -0800 (PST) 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 v4 1/2] rust: add udelay() function Date: Mon, 3 Nov 2025 20:29:57 +0900 Message-ID: <20251103112958.2961517-2-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251103112958.2961517-1-fujita.tomonori@gmail.com> References: <20251103112958.2961517-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(). Reviewed-by: Andreas Hindborg Reviewed-by: Alice Ryhl Signed-off-by: FUJITA Tomonori Acked-by: Andreas Hindborg --- 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..b5b1b42797a0 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. +/// +/// 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 Wed Dec 17 11:05:32 2025 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 6E838306B2D for ; Mon, 3 Nov 2025 11:30:45 +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=1762169446; cv=none; b=b8doJDA+KoNTk+JRMM1YdaALZPk9ZHZZX1GDfjjXZ6mbzLnglt/eswfGnS5fQ27tbGCzIWR2i53G4rEfJG5HRy/emywYlrpwaXRMOXxXC43SYFEOBGxymz5kL+XasGWEiAway+HpmKdSNVcwAyixHRAi2JtQLfnqywch1KqIYaM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762169446; c=relaxed/simple; bh=Qs7F9UIu6YqaEfJe3oGUDLwZfwqHWL3NgU5G+AF7xMA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qBo0qHPSqlpkbUSRgwu2qkKs36HXoTxM01SrJGKsxGAXNuS/ToIGEtJ7/yn9szw9El1EELIb1afd5+SRuhG7jQ2o91xrsfla6NOWgR6MK1dwOUocQ/DNmEKS65B4Z61U8Vn/TpDEuAejIXz9doOgaN0YkVfeLuCKFEXHneocyUs= 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=XNwtr868; 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="XNwtr868" Received: by mail-pj1-f50.google.com with SMTP id 98e67ed59e1d1-33db8fde85cso4191371a91.0 for ; Mon, 03 Nov 2025 03:30:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762169445; x=1762774245; 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=jBtU3a0ysV2uBxdh/EjwzVIfbYaw1pOF5fN9hWc8SI0=; b=XNwtr868WMbh8gKTflU8Xv5ITDFXkiQTZr0Fe284+XtPqpoeTWnHFZ7lQ9eY0caTi/ 9QVd9DZ1ZLgFiO9eHN4gtDmz2jknOUgPn9+TrJUvo1JggD9Et22iaTlx8+F2oQflvva2 z+izOuR5S7gMZQKQVsvxxqoQy76ix6/klCpkteeU8uOOJ7T6rb9YrRnrTSXMSabMawTT FKF85aSiNpnymGU+1HNj5rqJNmDoiwZpZX75Ok3UbCzoHmm5dy5hCOEfXJzFJU0xJ+qw 2/fbHKkYNm2PzKM5oBo9Pv6mUmOTJT5M4+rNPZ/q9Dp9lr+GBfyz4XWE6TkQrsaubJwq aX+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762169445; x=1762774245; 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=jBtU3a0ysV2uBxdh/EjwzVIfbYaw1pOF5fN9hWc8SI0=; b=h8cDGI1ewwVtmpM1L/BNrZvXdWB0fygGa7dovFKZS8btHD5nKkleoTmAXwInopzHzi KABl7Bk09n5mJnw1CzHGi/5QJWg/+MGfgy6I0vRptHvWQHP6eLJCMrGPWLe9NtqdEA48 r4dMGMHeS2yHxHx+ndNAqRr/G4PW/EFcsqMs5bciBQ/Oxp6Ci1KCIgqtoP1eftsdH7iL A1ux1+T8yQHRWKUkXDN1YA7wcjd58+A0bQQngidvJ3196B6VM1bDRF30d+pBulpQx/e+ Uw/M73hmb9BwknvM5jqcissc4iepNDdD6JwRuAvT9Fdnxj1rPCzFmxaHO2MVmJrGmWxe V/Zg== X-Forwarded-Encrypted: i=1; AJvYcCV8lPAlMspEPbBCZSkK9XHWX1hEdi8VB3Q7zkMYGRlkax9o+79XuPh76Y7+5S9Prbk/6afLUNP7ZHVWjvA=@vger.kernel.org X-Gm-Message-State: AOJu0YxZttOBVr2v4uU9wKPJFueJVkVlJzkc7bxJx5XsbZjxYjyqEvf6 V8oCzJhveeDSWkS44hvlHajmxXlkTbx+XzeYtiDNTWYogv3OsP+DfY+f X-Gm-Gg: ASbGncsJ1q8PkWmnUjjUG2raAvL8wIfEY85a+B6QCJaILn59W1/VdnO9pVoeNulWSS0 27qaFE0FlAxncjhKKHMi7u4lrFOXwKwx7m02KR3OfrZryHdns94poqMjz30AU81G0elGI2iQZOb /B0V0sy0llqG9yjf0HFth2WEyAnqM8HjS5DyhqImS6qbLcaumq3zUi6zWaW+xebO8b+jaetHh7a nPJ3RUaE+A9vvZAJcRh1Wo8hsAyVKBoSRBGRoPDtkTxgm3pxJwjrRpWS62RelIA7zVsm1JN1j+m PL7qbFpNZWl/4qvQKPElX7Mue2LBKvbe6Bxq5qQfKq8QwH9mWn4cCWdJYvhK+8Ddfr6V+Q8sC5b oIgYaVuJOTkPOJ1XpgaOdz0QeQI+PVXy+OzIY5LFoKnN4FZzmol42hu6fA36K9aFVetHoKX2M8i L0juMWjhntKyh4sxsYbv9+3g3H48X2c9VjhcWzh9JfWq0mY50W X-Google-Smtp-Source: AGHT+IHmOwyUcmyvsEy5tPHeBGjIMAhnC0ExrOtR/nmM8pCo5eJUoNeq+D7oFCJVNElcXl4sgNqsYw== X-Received: by 2002:a17:90b:3fc6:b0:340:ca32:bcad with SMTP id 98e67ed59e1d1-340ca32bdadmr7979089a91.19.1762169444530; Mon, 03 Nov 2025 03:30:44 -0800 (PST) Received: from bee.. (p5332007-ipxg23901hodogaya.kanagawa.ocn.ne.jp. [180.34.120.7]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7a7d897c632sm11011033b3a.10.2025.11.03.03.30.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Nov 2025 03:30:44 -0800 (PST) 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 v4 2/2] rust: Add read_poll_timeout_atomic function Date: Mon, 3 Nov 2025 20:29:58 +0900 Message-ID: <20251103112958.2961517-3-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251103112958.2961517-1-fujita.tomonori@gmail.com> References: <20251103112958.2961517-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. Reviewed-by: Alice Ryhl Signed-off-by: FUJITA Tomonori Acked-by: Andreas Hindborg --- 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 8f8886543f34..18bed3e78ef7 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, @@ -96,3 +99,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