Module: Mesa
Branch: main
Commit: 5e8916c0bf02661b334b2b7fb52e968b9e0aabde
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=5e8916c0bf02661b334b2b7fb52e968b9e0aabde

Author: PixelyIon <[email protected]>
Date:   Thu Aug 11 20:00:27 2022 +0530

tu: Make KGSL wait IOCTLs interrupt-safe

`tu_WaitForFences` and `tu_GetFenceStatus` ended up using `ioctl` instead of
`safe_ioctl` and as a result were responsible for handling `EAGAIN`/`EINTR`
but they weren't handled correctly in wait calls which led to dropping the
wait if the thread received a signal without automatic syscall restarting on
release builds and an assertion due to `errorno` being `EINTR` on debug
builds.

This fixes early wait returns which would lead to UB or cause crashes due to
hitting debug assertions in Skyline Emulator.

Signed-off-by: Mark Collins <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/18031>

---

 src/freedreno/vulkan/tu_kgsl.c | 58 +++++++++++++++++++++++++++++++++---------
 1 file changed, 46 insertions(+), 12 deletions(-)

diff --git a/src/freedreno/vulkan/tu_kgsl.c b/src/freedreno/vulkan/tu_kgsl.c
index 8c05da40c5d..378602ac24e 100644
--- a/src/freedreno/vulkan/tu_kgsl.c
+++ b/src/freedreno/vulkan/tu_kgsl.c
@@ -632,6 +632,43 @@ tu_DestroyFence(VkDevice _device, VkFence fence, const 
VkAllocationCallbacks *pA
    vk_object_free(&device->vk, pAllocator, sync);
 }
 
+/* safe_ioctl is not enough as restarted waits would not adjust the timeout
+ * which could lead to waiting substantially longer than requested
+ */
+static int
+wait_timestamp_safe(int fd,
+                    unsigned int context_id,
+                    unsigned int timestamp,
+                    int64_t timeout_ms)
+{
+   int64_t start_time = os_time_get_nano();
+   struct kgsl_device_waittimestamp_ctxtid wait = {
+      .context_id = context_id,
+      .timestamp = timestamp,
+      .timeout = timeout_ms,
+   };
+
+   while (true) {
+      int ret = ioctl(fd, IOCTL_KGSL_DEVICE_WAITTIMESTAMP_CTXTID, &wait);
+
+      if (ret == -1 && (errno == EINTR || errno == EAGAIN)) {
+         int64_t current_time = os_time_get_nano();
+
+         /* update timeout to consider time that has passed since the start */
+         timeout_ms -= (current_time - start_time) / 1000000;
+         if (timeout_ms <= 0) {
+            errno = ETIME;
+            return -1;
+         }
+
+         wait.timeout = (unsigned int) timeout_ms;
+         start_time = current_time;
+      } else {
+         return ret;
+      }
+   }
+}
+
 VKAPI_ATTR VkResult VKAPI_CALL
 tu_WaitForFences(VkDevice _device,
                  uint32_t count,
@@ -645,12 +682,10 @@ tu_WaitForFences(VkDevice _device,
    if (!s.timestamp_valid)
       return VK_SUCCESS;
 
-   int ret = ioctl(device->fd, IOCTL_KGSL_DEVICE_WAITTIMESTAMP_CTXTID,
-                   &(struct kgsl_device_waittimestamp_ctxtid) {
-      .context_id = device->queues[0]->msm_queue_id,
-      .timestamp = s.timestamp,
-      .timeout = timeout / 1000000,
-   });
+   int ret = wait_timestamp_safe(device->fd, 
+                                 device->queues[0]->msm_queue_id, 
+                                 s.timestamp, 
+                                 timeout / 1000000);
    if (ret) {
       assert(errno == ETIME);
       return VK_TIMEOUT;
@@ -678,12 +713,11 @@ tu_GetFenceStatus(VkDevice _device, VkFence _fence)
    if (!sync->timestamp_valid)
       return VK_NOT_READY;
 
-   int ret = ioctl(device->fd, IOCTL_KGSL_DEVICE_WAITTIMESTAMP_CTXTID,
-               &(struct kgsl_device_waittimestamp_ctxtid) {
-      .context_id = device->queues[0]->msm_queue_id,
-      .timestamp = sync->timestamp,
-      .timeout = 0,
-   });
+
+   int ret = wait_timestamp_safe(device->fd, 
+                                 device->queues[0]->msm_queue_id, 
+                                 sync->timestamp, 
+                                 0);
    if (ret) {
       assert(errno == ETIME);
       return VK_NOT_READY;

Reply via email to