Received: from flow-b4-smtp.messagingengine.com (flow-b4-smtp.messagingengine.com [202.12.124.139]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 74BA7481255 for ; Fri, 7 Aug 2026 13:13:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=202.12.124.139 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786108440; cv=none; b=tHh8t7vQjvCTRLKzZwt/Eo130oKwIhsMjoDpEghueFgZ7hZWfQDzcRRmxVqkC1xIraQ4bZmRccurFBETv9D1f6b+ERggBjk04Imh7GAjbYqf4rs2k43hc7ZOahBr+k0uXJq9vSohEqAY62T+P4T440NQKbuMiajMDhZUx5Qh9B8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786108440; c=relaxed/simple; bh=OFX82aQsW9pFvjBCKeUDe2CMwCO0wMnibo4G3x4psBA=; h=Date:Message-Id:To:Cc:Subject:From:In-Reply-To:References: Mime-Version:Content-Type; b=sftmqniXJYMzLybn+3/lytw53LCeJjamxQSdQ8Yy2227OPJTIArRKFG+YeDkBUzMv3RAbtUXVTrccHwgsa3vPaXNrJzMz/YeNfFPq/NBP3MPhDNQDV6cdz0iMCJNv2aRD2so2+CBqQ11boOqLBO5LFpEoLN8++iZ30nihJrKW9I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=flapping.org; spf=pass smtp.mailfrom=flapping.org; dkim=pass (2048-bit key) header.d=flapping.org header.i=@flapping.org header.b=LF+MHx+F; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=KJRdRbwI; arc=none smtp.client-ip=202.12.124.139 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=flapping.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flapping.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=flapping.org header.i=@flapping.org header.b="LF+MHx+F"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="KJRdRbwI" Received: from phl-compute-01.internal (phl-compute-01.internal [10.202.2.41]) by mailflow.stl.internal (Postfix) with ESMTP id C6C5613001E4; Fri, 7 Aug 2026 09:13:38 -0400 (EDT) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-01.internal (MEProxy); Fri, 07 Aug 2026 09:13:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=flapping.org; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm1; t=1786108418; x=1786112018; bh=ySHr/SWFhrADxrFEr/NV+GjpuDj0NF7WEzVBU8qqUos=; b= LF+MHx+Fnur+5o9vPGgpl0dVbZPfaG16JUtWwxo0VKOC9hWGcl4hEOTI4jSKwlHZ me+TfCEN5aDKvrfARlxuxZsmKszMQi6C6PD39Cal9S0gg0CRPh+ld9S9kAxUfaSM Xp73zfPOtc9Ofcqn/IcLJRMqxTNU8W31mafIdx0k92Uepf6zVe6toww3feZ6dr1x fnxryH+aGvYlNsjQ48+IoakUPPwkVGNqHzQytIJQ7OXDUMh0snFBNMnpHy6Ti/er Mk0IAjcGinScpqWP8duBvJDCbrsGMEWBb01u+0eZcY9HqsB0HhxIYcOB1obPW0Ae 86bZxG13uY55NihpLC09Ig== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1786108418; x= 1786112018; bh=ySHr/SWFhrADxrFEr/NV+GjpuDj0NF7WEzVBU8qqU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y: Feedback-ID: i51fe4b43:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 7 Aug 2026 09:13:32 -0400 (EDT) Date: Fri, 07 Aug 2026 22:13:30 +0900 (JST) Message-Id: <20260807.221330.1176623855678550212.tomo@flapping.org> To: gary@garyguo.net Cc: tomo@flapping.org, a.hindborg@kernel.org, aliceryhl@google.com, arve@android.com, boqun@kernel.org, brauner@kernel.org, cmllamas@google.com, gregkh@linuxfoundation.org, ojeda@kernel.org, tkjos@android.com, acourbot@nvidia.com, anna-maria@linutronix.de, bjorn3_gh@protonmail.com, dakr@kernel.org, daniel.almeida@collabora.com, frederic@kernel.org, jstultz@google.com, lossin@kernel.org, lyude@redhat.com, sboyd@kernel.org, tamird@kernel.org, tglx@kernel.org, tmgross@umich.edu, work@onurozkan.dev, rust-for-linux@vger.kernel.org, fujita.tomonori@gmail.com Subject: Re: [PATCH v5 4/7] rust: time: add Delta::to_jiffies() for timeout conversion From: FUJITA Tomonori In-Reply-To: References: <20260807.213837.477414639891260460.tomo@flapping.org> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit On Fri, 07 Aug 2026 13:59:20 +0100 "Gary Guo" wrote: > On Fri Aug 7, 2026 at 1:38 PM BST, FUJITA Tomonori wrote: >> On Thu, 06 Aug 2026 12:50:55 +0100 >> "Gary Guo" wrote: >> >>> On Thu Aug 6, 2026 at 8:32 AM BST, FUJITA Tomonori wrote: >>>> From: FUJITA Tomonori >>>> >>>> Add Delta::to_jiffies() conversion that rounds up so the >>>> resulting timeout is never shorter than the requested span, clamps a >>>> negative span to an immediate timeout, and saturates an overlong span >>>> to the kernel's MAX_JIFFY_OFFSET "wait forever" value. >>>> >>>> Reviewed-by: Gary Guo >>>> Signed-off-by: FUJITA Tomonori >>>> --- >>>> rust/kernel/time.rs | 21 +++++++++++++++++++++ >>>> 1 file changed, 21 insertions(+) >>>> >>>> diff --git a/rust/kernel/time.rs b/rust/kernel/time.rs >>>> index 9d65a6c1992a..5844ee985a7b 100644 >>>> --- a/rust/kernel/time.rs >>>> +++ b/rust/kernel/time.rs >>>> @@ -551,6 +551,27 @@ pub fn as_millis_ceil(self) -> i64 { >>>> } >>>> } >>>> >>>> + /// Convert this span to a [`Delta`] suitable for use as a timeout. >>>> + /// >>>> + /// The value is rounded up to the next whole jiffy, so the resulting >>>> + /// timeout is never shorter than `self` (as `msecs_to_jiffies()` does). >>>> + /// A negative span clamps to zero jiffies (an immediate timeout). >>>> + #[inline] >>>> + pub fn to_jiffies(self) -> Delta { >>>> + let msecs = self.as_millis_ceil(); >>>> + >>>> + // CAST: `msecs` is clamped to `0..=c_uint::MAX`, so it is non-negative and >>>> + // fits in `c_uint`. >>>> + let msecs = msecs.clamp(0, i64::from(crate::ffi::c_uint::MAX)) as crate::ffi::c_uint; >>>> + >>>> + // SAFETY: `__msecs_to_jiffies()` is always safe to call. >>>> + let jiffies = unsafe { bindings::__msecs_to_jiffies(msecs) }; >>>> + >>>> + // CAST: `__msecs_to_jiffies()` returns a value in `0..=MAX_JIFFY_OFFSET`, i.e. >>>> + // `((LONG_MAX >> 1) - 1)`, which is non-negative and well within `isize`. >>>> + Delta::::from_jiffies(jiffies as isize) >>>> + } >>> >>> This should be using nsec_to_jiffies? >> >> nsecs_to_jiffies() is documented as unsuitable here, time.c says: >> >> Unlike {m,u}secs_to_jiffies, type of input is not unsigned int but >> u64. And this doesn't return MAX_JIFFY_OFFSET since this function >> is designed for scheduler, not for use in device drivers to >> calculate timeout value. >> >> It also truncates, so it's not suitable for a timeout. >> >> __msecs_to_jiffies() is the timeout-oriented helper: it rounds up and >> saturates to MAX_JIFFY_OFFSET, which is what a timeout wants. > > Which sounds like is what exactly is suitable here. What you want should be a > different named function that does the ceiling and clamping? Agreed, the ceiling and clamping should not hide behind a plan conversion name. I will rename it to to_jiffies_timeout() as Andreas suggested.