Skip to contentSkip to conversation

sync_file: Return consistent status in SYNC_IOC_FILE_INFO

PATCH Superseded John Einar Reitan 4 participants 7 messages Oct 3, 2017 – Oct 10, 2017

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

PATCH 1/1 sync_file: Return consistent status in SYNC_IOC_FILE_INFO

sync_file_ioctl_fence_info has a race between filling the status of the underlying fences and the overall status of the sync_file. If fence transitions in the time frame between its sync_fill_fence_info and the later dma_fence_is_signaled for the sync_file, the returned information is inconsistent showing non-signaled underlying fences but an overall signaled state.

This patch changes sync_file_ioctl_fence_info to track what has been encoded and using that as the overall sync_file status.

---
 drivers/dma-buf/sync_file.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
drivers/dma-buf/sync_file.c +12 -6
index 66fb40d0ebdb..ebf5764b868c 100644
@@ -383,7 +383,7 @@ static long sync_file_ioctl_merge(struct sync_file *sync_file,
 	return err;
 }
 
-static void sync_fill_fence_info(struct dma_fence *fence,
+static int sync_fill_fence_info(struct dma_fence *fence,
 				 struct sync_fence_info *info)
 {
 	strlcpy(info->obj_name, fence->ops->get_timeline_name(fence),
@@ -399,6 +399,8 @@ static void sync_fill_fence_info(struct dma_fence *fence,
 		test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags) ?
 		ktime_to_ns(fence->timestamp) :
 		ktime_set(0, 0);
+
+	return info->status;
 }
 
 static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
@@ -408,7 +410,7 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
 	struct sync_fence_info *fence_info = NULL;
 	struct dma_fence **fences;
 	__u32 size;
-	int num_fences, ret, i;
+	int num_fences, ret, i, fences_status = 1;
 
 	if (copy_from_user(&info, (void __user *)arg, sizeof(info)))
 		return -EFAULT;
@@ -424,8 +426,10 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
 	 * sync_fence_info and return the actual number of fences on
 	 * info->num_fences.
 	 */
-	if (!info.num_fences)
+	if (!info.num_fences) {
+		fences_status = dma_fence_is_signaled(sync_file->fence);
 		goto no_fences;
+	}
 
 	if (info.num_fences < num_fences)
 		return -EINVAL;
@@ -435,8 +439,10 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
 	if (!fence_info)
 		return -ENOMEM;
 
-	for (i = 0; i < num_fences; i++)
-		sync_fill_fence_info(fences[i], &fence_info[i]);
+	for (i = 0; i < num_fences; i++) {
+		int status = sync_fill_fence_info(fences[i], &fence_info[i]);
+		fences_status = fences_status <= 0 ? fences_status : status;
+	}
 
 	if (copy_to_user(u64_to_user_ptr(info.sync_fence_info), fence_info,
 			 size)) {
@@ -446,7 +452,7 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
 
 no_fences:
 	sync_file_get_name(sync_file, info.name, sizeof(info.name));
-	info.status = dma_fence_is_signaled(sync_file->fence);
+	info.status = fences_status;
 	info.num_fences = num_fences;
 
 	if (copy_to_user((void __user *)arg, &info, sizeof(info)))
Signature · 6 lines
2.13.6

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Reviewed-by on v1 1/1
#1 lore raw

Quoting John Einar Reitan (2017-10-03 14:51:00)

Quoted 33 lines

sync_file_ioctl_fence_info has a race between filling the status of the underlying fences and the overall status of the sync_file. If fence transitions in the time frame between its sync_fill_fence_info and the later dma_fence_is_signaled for the sync_file, the returned information is inconsistent showing non-signaled underlying fences but an overall signaled state.

This patch changes sync_file_ioctl_fence_info to track what has been encoded and using that as the overall sync_file status.

---
 drivers/dma-buf/sync_file.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
drivers/dma-buf/sync_file.c +1 -1
index 66fb40d0ebdb..ebf5764b868c 100644
@@ -383,7 +383,7 @@ static long sync_file_ioctl_merge(struct sync_file *sync_file,
        return err;
 }
 
-static void sync_fill_fence_info(struct dma_fence *fence,
+static int sync_fill_fence_info(struct dma_fence *fence,
                                 struct sync_fence_info *info)
 {
        strlcpy(info->obj_name, fence->ops->get_timeline_name(fence),

drivers/dma-buf/sync_file.c static void sync_fill_fence_info(struct dma_fence *fence, · line 403

Quoted 5 lines
drivers/dma-buf/sync_file.c +2 -0
@@ -399,6 +399,8 @@ static void sync_fill_fence_info(struct dma_fence *fence,
                test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags) ?
                ktime_to_ns(fence->timestamp) :
                ktime_set(0, 0);
+
+       return info->status;

In hindsight, having both the per-fence and global variable be called info was a mistake. Certainly made this diff a little harder to parse than required!

Quoted 13 lines
drivers/dma-buf/sync_file.c +1 -1
 }
 
 static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
@@ -408,7 +410,7 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
        struct sync_fence_info *fence_info = NULL;
        struct dma_fence **fences;
        __u32 size;
-       int num_fences, ret, i;
+       int num_fences, ret, i, fences_status = 1;
 
        if (copy_from_user(&info, (void __user *)arg, sizeof(info)))
                return -EFAULT;

drivers/dma-buf/sync_file.c static long sync_file_ioctl_fence_info(struct sync_file *sync_file, · line 430

Quoted 6 lines
drivers/dma-buf/sync_file.c +2 -1
@@ -424,8 +426,10 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
         * sync_fence_info and return the actual number of fences on
         * info->num_fences.
         */
-       if (!info.num_fences)
+       if (!info.num_fences) {
+               fences_status = dma_fence_is_signaled(sync_file->fence);

Personally I would have set info.status directly rather than add a new fences_status. But that's immaterial to the bug fix,

-Chris
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#2 lore raw
Quoted 3 lines · Chris Wilson

In hindsight, having both the per-fence and global variable be called info was a mistake. Certainly made this diff a little harder to parse than required!

I agree, I had to re-read the code a few times to find & fix the bug.

Quoted 2 lines

Personally I would have set info.status directly rather than add a new fences_status. But that's immaterial to the bug fix,

I can clean that up if there is a need for a v2.

Quoted 1 line

Thanks!

John

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#3 lore raw

PATCH v2 1/1 sync_file: Return consistent status in SYNC_IOC_FILE_INFO

In reply to John Einar Reitan

sync_file_ioctl_fence_info has a race between filling the status of the underlying fences and the overall status of the sync_file. If fence transitions in the time frame between its sync_fill_fence_info and the later dma_fence_is_signaled for the sync_file, the returned information is inconsistent showing non-signaled underlying fences but an overall signaled state.

This patch changes sync_file_ioctl_fence_info to track what has been encoded and using that as the overall sync_file status.

---

Changes since v1 (thanks Chris Wilson):
- Replaced an unneeded local variable by writing directly to the struct

 drivers/dma-buf/sync_file.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
drivers/dma-buf/sync_file.c +12 -5
index 66fb40d0ebdb..03830634e141 100644
@@ -383,7 +383,7 @@ static long sync_file_ioctl_merge(struct sync_file *sync_file,
 	return err;
 }
 
-static void sync_fill_fence_info(struct dma_fence *fence,
+static int sync_fill_fence_info(struct dma_fence *fence,
 				 struct sync_fence_info *info)
 {
 	strlcpy(info->obj_name, fence->ops->get_timeline_name(fence),
@@ -399,6 +399,8 @@ static void sync_fill_fence_info(struct dma_fence *fence,
 		test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags) ?
 		ktime_to_ns(fence->timestamp) :
 		ktime_set(0, 0);
+
+	return info->status;
 }
 
 static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
@@ -424,8 +426,12 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
 	 * sync_fence_info and return the actual number of fences on
 	 * info->num_fences.
 	 */
-	if (!info.num_fences)
+	if (!info.num_fences) {
+		info.status = dma_fence_is_signaled(sync_file->fence);
 		goto no_fences;
+	} else {
+		info.status = 1;
+	}
 
 	if (info.num_fences < num_fences)
 		return -EINVAL;
@@ -435,8 +441,10 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
 	if (!fence_info)
 		return -ENOMEM;
 
-	for (i = 0; i < num_fences; i++)
-		sync_fill_fence_info(fences[i], &fence_info[i]);
+	for (i = 0; i < num_fences; i++) {
+		int status = sync_fill_fence_info(fences[i], &fence_info[i]);
+		info.status = info.status <= 0 ? info.status : status;
+	}
 
 	if (copy_to_user(u64_to_user_ptr(info.sync_fence_info), fence_info,
 			 size)) {
@@ -446,7 +454,6 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
 
 no_fences:
 	sync_file_get_name(sync_file, info.name, sizeof(info.name));
-	info.status = dma_fence_is_signaled(sync_file->fence);
 	info.num_fences = num_fences;
 
 	if (copy_to_user((void __user *)arg, &info, sizeof(info)))
Signature · 6 lines
2.13.6

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Reviewed-by on v2 1/1
#4 lore raw

Quoting John Einar Reitan (2017-10-09 14:49:36)

Quoted 7 lines

sync_file_ioctl_fence_info has a race between filling the status of the underlying fences and the overall status of the sync_file. If fence transitions in the time frame between its sync_fill_fence_info and the later dma_fence_is_signaled for the sync_file, the returned information is inconsistent showing non-signaled underlying fences but an overall signaled state.

Quoted 8 lines

This patch changes sync_file_ioctl_fence_info to track what has been encoded and using that as the overall sync_file status.

-Chris
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#5 lore raw

2017-10-09 Chris Wilson <[email protected]>:

Quoted 11 lines

Quoting John Einar Reitan (2017-10-09 14:49:36)

Quoted 10 lines

sync_file_ioctl_fence_info has a race between filling the status of the underlying fences and the overall status of the sync_file. If fence transitions in the time frame between its sync_fill_fence_info and the later dma_fence_is_signaled for the sync_file, the returned information is inconsistent showing non-signaled underlying fences but an overall signaled state.

This patch changes sync_file_ioctl_fence_info to track what has been encoded and using that as the overall sync_file status.

Quoted 6 lines
Quoted 5 lines

Pushed to drm-misc-fixes.

Gustavo
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#6 lore raw
In reply to John Einar Reitan
Quoted 28 lines · John Einar Reitan

sync_file_ioctl_fence_info has a race between filling the status of the underlying fences and the overall status of the sync_file. If fence transitions in the time frame between its sync_fill_fence_info and the later dma_fence_is_signaled for the sync_file, the returned information is inconsistent showing non-signaled underlying fences but an overall signaled state.

This patch changes sync_file_ioctl_fence_info to track what has been encoded and using that as the overall sync_file status.

---

Changes since v1 (thanks Chris Wilson):
- Replaced an unneeded local variable by writing directly to the struct

  drivers/dma-buf/sync_file.c | 17 ++++++++++++-----
  1 file changed, 12 insertions(+), 5 deletions(-)
drivers/dma-buf/sync_file.c +0 -0
index 66fb40d0ebdb..03830634e141 100644

drivers/dma-buf/sync_file.c static long sync_file_ioctl_merge(struct sync_file *sync_file, · line 387

Quoted 6 lines
drivers/dma-buf/sync_file.c +1 -1
@@ -383,7 +383,7 @@ static long sync_file_ioctl_merge(struct sync_file *sync_file,
  	return err;
  }
 
-static void sync_fill_fence_info(struct dma_fence *fence,
+static int sync_fill_fence_info(struct dma_fence *fence,
  				 struct sync_fence_info *info)

Out of curious, why name to sync_fill_xxx not sync_file_xxx?

Regards,
David Zhou

Quoted 39 lines
drivers/dma-buf/sync_file.c +11 -3
  {
  	strlcpy(info->obj_name, fence->ops->get_timeline_name(fence),
@@ -399,6 +399,8 @@ static void sync_fill_fence_info(struct dma_fence *fence,
  		test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags) ?
  		ktime_to_ns(fence->timestamp) :
  		ktime_set(0, 0);
+
+	return info->status;
  }
 
  static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
@@ -424,8 +426,12 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
  	 * sync_fence_info and return the actual number of fences on
  	 * info->num_fences.
  	 */
-	if (!info.num_fences)
+	if (!info.num_fences) {
+		info.status = dma_fence_is_signaled(sync_file->fence);
  		goto no_fences;
+	} else {
+		info.status = 1;
+	}
 
  	if (info.num_fences < num_fences)
  		return -EINVAL;
@@ -435,8 +441,10 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
  	if (!fence_info)
  		return -ENOMEM;
 
-	for (i = 0; i < num_fences; i++)
-		sync_fill_fence_info(fences[i], &fence_info[i]);
+	for (i = 0; i < num_fences; i++) {
+		int status = sync_fill_fence_info(fences[i], &fence_info[i]);
+		info.status = info.status <= 0 ? info.status : status;
+	}
 
  	if (copy_to_user(u64_to_user_ptr(info.sync_fence_info), fence_info,
  			 size)) {

drivers/dma-buf/sync_file.c static long sync_file_ioctl_fence_info(struct sync_file *sync_file, · line 459

Quoted 7 lines
drivers/dma-buf/sync_file.c +0 -1
@@ -446,7 +454,6 @@ static long sync_file_ioctl_fence_info(struct sync_file *sync_file,
 
  no_fences:
  	sync_file_get_name(sync_file, info.name, sizeof(info.name));
-	info.status = dma_fence_is_signaled(sync_file->fence);
  	info.num_fences = num_fences;
 
  	if (copy_to_user((void __user *)arg, &info, sizeof(info)))

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel