drm: Add drm_timeout_rel_to_jiffies()

drm_timeout_abs_to_jiffies() covers the drivers whose wait UAPI takes an
absolute deadline, but there is no equivalent for the drivers that
express a wait as a duration. Drivers such as i915 and v3d convert the
value themselves.

Converting a nanosecond duration to jiffies needs some care.
nsecs_to_jiffies() returns unsigned long, so on 32-bit a large
userspace-supplied timeout overflows its range and is silently truncated.

i915 already handles both cases in a local helper, which v3d has a copy
of. Add the same conversion to the core, so that it is available to any
driver and both copies can be dropped.

Differently from drm_timeout_abs_to_jiffies(), the new function enforces
that a timeout in jiffies is a positive value and therefore, returns an
unsigned value.

Reviewed-by: Thomas Zimmermann <tzimmermann@suse.de>
Reviewed-by: Christian König <christian.koenig@amd.com>
Link: https://patch.msgid.link/20260915-drm-timeout-helpers-v3-2-f2ae987d861f@igalia.com
Signed-off-by: Maíra Canal <mcanal@igalia.com>
This commit is contained in:
Maíra Canal 2026-09-15 16:42:17 -03:00
parent ae72b9c39d
commit 421cf7ccfc
No known key found for this signature in database
GPG Key ID: 3FF30E8A7688FAAA
2 changed files with 46 additions and 0 deletions

View File

@ -9,6 +9,7 @@
#include <linux/export.h>
#include <linux/jiffies.h>
#include <linux/ktime.h>
#include <linux/math64.h>
#include <linux/sched.h>
#include <drm/drm_timeout.h>
@ -48,3 +49,47 @@ signed long drm_timeout_abs_to_jiffies(s64 timeout_nsec)
return timeout_jiffies64 + 1;
}
EXPORT_SYMBOL(drm_timeout_abs_to_jiffies);
/**
* drm_timeout_rel_to_jiffies - calculate jiffies timeout from relative value
* @timeout_nsec: relative timeout in ns, 0 for poll
*
* Calculate the timeout in jiffies from a relative timeout in ns, for drivers
* whose UAPI expresses a wait as a duration rather than as a deadline.
*
* The result is clamped to MAX_JIFFY_OFFSET. That keeps it positive once it is
* converted to the signed long taken by dma_fence_wait_timeout() and friends,
* which matters on 32-bit, and keeps it distinct from MAX_SCHEDULE_TIMEOUT so
* that a finite wait is never understood as an infinite one.
*
* It's strongly discouraged to use relative timeouts in uAPIs, as they do not
* survive a restarted ioctl. A signal-interrupted ioctl is re-entered with the
* same arguments, so the duration starts counting from zero again. New uAPIs
* should take an absolute deadline and use drm_timeout_abs_to_jiffies().
*
* Returns:
* 0 if @timeout_nsec is 0. Otherwise the equivalent number of jiffies and
* clamped to MAX_JIFFY_OFFSET.
*/
unsigned long drm_timeout_rel_to_jiffies(u64 timeout_nsec)
{
u64 secs;
u32 rem;
/* Make 0 timeout means poll, as for the absolute variant. */
if (timeout_nsec == 0)
return 0;
/*
* As nsecs_to_jiffies64() does not guard against overflow, split
* the timeout into whole seconds and nanoseconds. This way
* nsecs_to_jiffies64() is always handed a value below a second.
*/
secs = div_u64_rem(timeout_nsec, NSEC_PER_SEC, &rem);
if (secs >= MAX_JIFFY_OFFSET / HZ)
return MAX_JIFFY_OFFSET;
return min_t(u64, MAX_JIFFY_OFFSET,
secs_to_jiffies(secs) + nsecs_to_jiffies64(rem) + 1);
}
EXPORT_SYMBOL(drm_timeout_rel_to_jiffies);

View File

@ -12,5 +12,6 @@
#include <linux/types.h>
signed long drm_timeout_abs_to_jiffies(s64 timeout_nsec);
unsigned long drm_timeout_rel_to_jiffies(u64 timeout_nsec);
#endif