Skip to contentSkip to conversation

drm/xe: Use milliseconds instead of jiffies in uapi for user fence

PATCH In review Zbigniew Kempczyński 3 participants 5 messages May 19, 2023 – May 28, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/xe: Use milliseconds instead of jiffies in uapi for user fence

Using jiffies as a timeout from userspace is weird even it theoretically exists possiblity of acquiring jiffies via getconf. Unfortunately this method is unreliable and returned value may vary from configured in the kernel config.

Due to kernel constraints regarding milliseconds type (unsigned int) conversion to jiffies and uapi timeout defined as u64 return an error if timeout exceeds unsigned int size.

---
 drivers/gpu/drm/xe/xe_wait_user_fence.c | 13 +++++++++++--
 include/uapi/drm/xe_drm.h               |  2 +-
 2 files changed, 12 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_wait_user_fence.c +11 -2
index 15c2e5aa08d2..585681b5fa9a 100644
@@ -79,6 +79,13 @@ static int check_hw_engines(struct xe_device *xe,
 	return 0;
 }
 
+static unsigned long msecs_to_jiffies_timeout(const unsigned int m)
+{
+	unsigned long j = msecs_to_jiffies(m);
+
+	return min_t(unsigned long, MAX_JIFFY_OFFSET, j + 1);
+}
+
 #define VALID_FLAGS	(DRM_XE_UFENCE_WAIT_SOFT_OP | \
 			 DRM_XE_UFENCE_WAIT_ABSTIME | \
 			 DRM_XE_UFENCE_WAIT_VM_ERROR)
@@ -98,7 +105,7 @@ int xe_wait_user_fence_ioctl(struct drm_device *dev, void *data,
 	int err;
 	bool no_engines = args->flags & DRM_XE_UFENCE_WAIT_SOFT_OP ||
 		args->flags & DRM_XE_UFENCE_WAIT_VM_ERROR;
-	unsigned long timeout = args->timeout;
+	unsigned long timeout;
 
 	if (XE_IOCTL_ERR(xe, args->extensions))
 		return -EINVAL;
@@ -148,9 +155,11 @@ int xe_wait_user_fence_ioctl(struct drm_device *dev, void *data,
 		addr = vm->async_ops.error_capture.addr;
 	}
 
-	if (XE_IOCTL_ERR(xe, timeout > MAX_SCHEDULE_TIMEOUT))
+	if (XE_IOCTL_ERR(xe, args->timeout > UINT_MAX)
 		return -EINVAL;
 
+	timeout = msecs_to_jiffies_timeout(args->timeout);
+
 	/*
 	 * FIXME: Very simple implementation at the moment, single wait queue
 	 * for everything. Could be optimized to have a wait queue for every
include/uapi/drm/xe_drm.h +1 -1
index b0b80aae3ee8..3e6f679ab534 100644
@@ -726,7 +726,7 @@ struct drm_xe_wait_user_fence {
 #define DRM_XE_UFENCE_WAIT_U32		0xffffffffu
 #define DRM_XE_UFENCE_WAIT_U64		0xffffffffffffffffu
 	__u64 mask;
-	/** @timeout: how long to wait before bailing, value in jiffies */
+	/** @timeout: how long to wait before bailing, value in milliseconds */
 	__s64 timeout;
 	/**
 	 * @num_engines: number of engine instances to wait on, must be zero
Signature · 1 line
2.40.0
#1 lore raw

[Intel-xe] ✓ CI.Patch_applied: success for drm/xe: Use milliseconds instead of jiffies in uapi for user fence

== Series Details ==

== Series Details ==

Series: drm/xe: Use milliseconds instead of jiffies in uapi for user fence
URL : https://patchwork.freedesktop.org/series/118011/
State : success

== Summary ==

=== Applying kernel patches on branch 'drm-xe-next' with base: === Base commit: 69989b35a drm/xe: Use GT oriented log messages in xe_gt.c === git am output follows ===
Applying: drm/xe: Use milliseconds instead of jiffies in uapi for user fence

#2 lore raw

[Intel-xe] ✗ CI.KUnit: failure for drm/xe: Use milliseconds instead of jiffies in uapi for user fence

In reply to Zbigniew Kempczyński
== Series Details ==

== Series Details ==

Series: drm/xe: Use milliseconds instead of jiffies in uapi for user fence
URL : https://patchwork.freedesktop.org/series/118011/
State : failure

== Summary ==

+ trap cleanup EXIT
+ /kernel/tools/testing/kunit/kunit.py run --kunitconfig /kernel/drivers/gpu/drm/xe/.kunitconfig ERROR:root:../drivers/gpu/drm/xe/xe_wait_user_fence.c: In function ‘xe_wait_user_fence_ioctl’:
../drivers/gpu/drm/xe/xe_wait_user_fence.c:159:3: error: expected ‘)’ before ‘return’

  159 |   return -EINVAL;
      |   ^~~~~~
../drivers/gpu/drm/xe/xe_wait_user_fence.c:158:5: note: to match this ‘(’
  158 |  if (XE_IOCTL_ERR(xe, args->timeout > UINT_MAX)
      |     ^
../drivers/gpu/drm/xe/xe_wait_user_fence.c:213:1: error: expected expression before ‘}’ token
  213 | }
      | ^
../drivers/gpu/drm/xe/xe_wait_user_fence.c:108:16: warning: unused variable ‘timeout’ [-Wunused-variable]
  108 |  unsigned long timeout;
      |                ^~~~~~~
../drivers/gpu/drm/xe/xe_wait_user_fence.c:213:1: error: control reaches end of non-void function [-Werror=return-type]
  213 | }
      | ^
At top level:
../drivers/gpu/drm/xe/xe_wait_user_fence.c:82:22: warning: ‘msecs_to_jiffies_timeout’ defined but not used [-Wunused-function]
   82 | static unsigned long msecs_to_jiffies_timeout(const unsigned int m)
      |                      ^~~~~~~~~~~~~~~~~~~~~~~~
../drivers/gpu/drm/xe/xe_wait_user_fence.c:17:12: warning: ‘do_compare’ defined but not used [-Wunused-function]
   17 | static int do_compare(u64 addr, u64 value, u64 mask, u16 op)
      |            ^~~~~~~~~~

cc1: some warnings being treated as errors
make[6]: *** [../scripts/Makefile.build:252: drivers/gpu/drm/xe/xe_wait_user_fence.o] Error 1 make[6]: *** Waiting for unfinished jobs....
make[5]: *** [../scripts/Makefile.build:494: drivers/gpu/drm/xe] Error 2 make[4]: *** [../scripts/Makefile.build:494: drivers/gpu/drm] Error 2
make[3]: *** [../scripts/Makefile.build:494: drivers/gpu] Error 2
make[2]: *** [../scripts/Makefile.build:494: drivers] Error 2
make[1]: *** [/kernel/Makefile:2025: .] Error 2
make: *** [Makefile:226: __sub-make] Error 2

[11:00:06] Configuring KUnit Kernel ...
Generating .config ...
Populating config with:

$ make ARCH=um O=.kunit olddefconfig
[11:00:10] Building KUnit Kernel ...
Populating config with:
$ make ARCH=um O=.kunit olddefconfig
Building with:
$ make ARCH=um O=.kunit --jobs=48
+ cleanup
++ stat -c %u:%g /kernel
+ chown -R 1003:1003 /kernel
#3 lore raw
In reply to Zbigniew Kempczyński

Hi Zbigniew,

Quoted 8 lines · Zbigniew Kempczyński

Using jiffies as a timeout from userspace is weird even it theoretically exists possiblity of acquiring jiffies via getconf. Unfortunately this method is unreliable and returned value may vary from configured in the kernel config.

Due to kernel constraints regarding milliseconds type (unsigned int) conversion to jiffies and uapi timeout defined as u64 return an error if timeout exceeds unsigned int size.

Thus... this should go on a different patch.

Quoted 11 lines
---
 drivers/gpu/drm/xe/xe_wait_user_fence.c | 13 +++++++++++--
 include/uapi/drm/xe_drm.h               |  2 +-
 2 files changed, 12 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_wait_user_fence.c +0 -0
index 15c2e5aa08d2..585681b5fa9a 100644

drivers/gpu/drm/xe/xe_wait_user_fence.c static int check_hw_engines(struct xe_device *xe, · line 88

Quoted 10 lines
drivers/gpu/drm/xe/xe_wait_user_fence.c +7 -0
@@ -79,6 +79,13 @@ static int check_hw_engines(struct xe_device *xe,
 	return 0;
 }
 
+static unsigned long msecs_to_jiffies_timeout(const unsigned int m)
+{
+	unsigned long j = msecs_to_jiffies(m);
+
+	return min_t(unsigned long, MAX_JIFFY_OFFSET, j + 1);
+}
+

mmhhh... this is a copy paste from i915 and given that there are already two drivers using it, I think this should go in the library in jiffies.h.

Besides, do we really need this? already msecs_to_jiffies(). performs this check.

Andi

Quoted 40 lines
drivers/gpu/drm/xe/xe_wait_user_fence.c +4 -2
 #define VALID_FLAGS	(DRM_XE_UFENCE_WAIT_SOFT_OP | \
 			 DRM_XE_UFENCE_WAIT_ABSTIME | \
 			 DRM_XE_UFENCE_WAIT_VM_ERROR)
@@ -98,7 +105,7 @@ int xe_wait_user_fence_ioctl(struct drm_device *dev, void *data,
 	int err;
 	bool no_engines = args->flags & DRM_XE_UFENCE_WAIT_SOFT_OP ||
 		args->flags & DRM_XE_UFENCE_WAIT_VM_ERROR;
-	unsigned long timeout = args->timeout;
+	unsigned long timeout;
 
 	if (XE_IOCTL_ERR(xe, args->extensions))
 		return -EINVAL;
@@ -148,9 +155,11 @@ int xe_wait_user_fence_ioctl(struct drm_device *dev, void *data,
 		addr = vm->async_ops.error_capture.addr;
 	}
 
-	if (XE_IOCTL_ERR(xe, timeout > MAX_SCHEDULE_TIMEOUT))
+	if (XE_IOCTL_ERR(xe, args->timeout > UINT_MAX)
 		return -EINVAL;
 
+	timeout = msecs_to_jiffies_timeout(args->timeout);
+
 	/*
 	 * FIXME: Very simple implementation at the moment, single wait queue
 	 * for everything. Could be optimized to have a wait queue for every
include/uapi/drm/xe_drm.h +1 -1
index b0b80aae3ee8..3e6f679ab534 100644
@@ -726,7 +726,7 @@ struct drm_xe_wait_user_fence {
 #define DRM_XE_UFENCE_WAIT_U32		0xffffffffu
 #define DRM_XE_UFENCE_WAIT_U64		0xffffffffffffffffu
 	__u64 mask;
-	/** @timeout: how long to wait before bailing, value in jiffies */
+	/** @timeout: how long to wait before bailing, value in milliseconds */
 	__s64 timeout;
 	/**
 	 * @num_engines: number of engine instances to wait on, must be zero
Signature · 1 line
2.40.0
#4 lore raw
Quoted 40 lines · Andi Shyti

Hi Zbigniew,

Quoted 8 lines · Zbigniew Kempczyński

Using jiffies as a timeout from userspace is weird even it theoretically exists possiblity of acquiring jiffies via getconf. Unfortunately this method is unreliable and returned value may vary from configured in the kernel config.

Due to kernel constraints regarding milliseconds type (unsigned int) conversion to jiffies and uapi timeout defined as u64 return an error if timeout exceeds unsigned int size.

Thus... this should go on a different patch.

Quoted 11 lines
---
 drivers/gpu/drm/xe/xe_wait_user_fence.c | 13 +++++++++++--
 include/uapi/drm/xe_drm.h               |  2 +-
 2 files changed, 12 insertions(+), 3 deletions(-)
drivers/gpu/drm/xe/xe_wait_user_fence.c +0 -0
index 15c2e5aa08d2..585681b5fa9a 100644

drivers/gpu/drm/xe/xe_wait_user_fence.c static int check_hw_engines(struct xe_device *xe, · line 88

Quoted 10 lines
drivers/gpu/drm/xe/xe_wait_user_fence.c +7 -0
@@ -79,6 +79,13 @@ static int check_hw_engines(struct xe_device *xe,
 	return 0;
 }
 
+static unsigned long msecs_to_jiffies_timeout(const unsigned int m)
+{
+	unsigned long j = msecs_to_jiffies(m);
+
+	return min_t(unsigned long, MAX_JIFFY_OFFSET, j + 1);
+}
+

mmhhh... this is a copy paste from i915 and given that there are already two drivers using it, I think this should go in the library in jiffies.h.

Quoted 2 lines

Besides, do we really need this? already msecs_to_jiffies(). performs this check.

Thank you for the review.

Previously I wanted to treat 0 as at least 1 jiffy to avoid getting -ETIME for the call. But that's the user responsibility to properly configure required timeout as positive integer so msecs_to_jiffies_timeout() isn't really needed here. Negative means neverending sleep so user has wide spectrum of timeout to choose.

Real problem with this patch is uapi change. Normally msecs passed via ioctl() should be integer (s32) but I would break current umd which uses s64 right now (too big). I wondered to split to s32 + s32 (reserved) but I should handle little/big endianess and I'm not sure such constructs should be introduced.

Anyway I tested this patch without _timeout() and values:
* 0: -ETIME
* positive <= UINT_MAX -> real timeout, works fine
* positive > UINT_MAX -> -EINVAL
* negative -> MAX_JIFFY_OFFSET, unlimited timeout

The best would be change to s32 to avoid third case (-EINVAL) but this requires umd recompilation.

--
Zbigniew

Quoted 43 lines

Andi

Quoted 40 lines
drivers/gpu/drm/xe/xe_wait_user_fence.c +4 -2
 #define VALID_FLAGS	(DRM_XE_UFENCE_WAIT_SOFT_OP | \
 			 DRM_XE_UFENCE_WAIT_ABSTIME | \
 			 DRM_XE_UFENCE_WAIT_VM_ERROR)
@@ -98,7 +105,7 @@ int xe_wait_user_fence_ioctl(struct drm_device *dev, void *data,
 	int err;
 	bool no_engines = args->flags & DRM_XE_UFENCE_WAIT_SOFT_OP ||
 		args->flags & DRM_XE_UFENCE_WAIT_VM_ERROR;
-	unsigned long timeout = args->timeout;
+	unsigned long timeout;
 
 	if (XE_IOCTL_ERR(xe, args->extensions))
 		return -EINVAL;
@@ -148,9 +155,11 @@ int xe_wait_user_fence_ioctl(struct drm_device *dev, void *data,
 		addr = vm->async_ops.error_capture.addr;
 	}
 
-	if (XE_IOCTL_ERR(xe, timeout > MAX_SCHEDULE_TIMEOUT))
+	if (XE_IOCTL_ERR(xe, args->timeout > UINT_MAX)
 		return -EINVAL;
 
+	timeout = msecs_to_jiffies_timeout(args->timeout);
+
 	/*
 	 * FIXME: Very simple implementation at the moment, single wait queue
 	 * for everything. Could be optimized to have a wait queue for every
include/uapi/drm/xe_drm.h +1 -1
index b0b80aae3ee8..3e6f679ab534 100644
@@ -726,7 +726,7 @@ struct drm_xe_wait_user_fence {
 #define DRM_XE_UFENCE_WAIT_U32		0xffffffffu
 #define DRM_XE_UFENCE_WAIT_U64		0xffffffffffffffffu
 	__u64 mask;
-	/** @timeout: how long to wait before bailing, value in jiffies */
+	/** @timeout: how long to wait before bailing, value in milliseconds */
 	__s64 timeout;
 	/**
 	 * @num_engines: number of engine instances to wait on, must be zero
Signature · 1 line
2.40.0