[PATCH 5/8] sync_file: add support for a semaphore object (v2)

Dave Airlie airlied at gmail.com
Wed Apr 12 04:57:23 UTC 2017


From: Dave Airlie <airlied at redhat.com>

This object can be used to implement the Vulkan semaphores.

The object behaviour differs from fence, in that you can
replace the underlying fence, and you cannot merge semaphores.

v2: change replace fence API to have a return value,
don't allow polling on semaphore objects,
don't worry about polling when replacing fences (since
we don't allow polling anymore).
switch APIs to one to retreive a fence from a sync_file
to make AMDGPU implementation more robust.

Signed-off-by: Dave Airlie <airlied at redhat.com>
---
 drivers/dma-buf/sync_file.c    | 73 ++++++++++++++++++++++++++++++++++++------
 include/linux/sync_file.h      |  4 +++
 include/uapi/linux/sync_file.h | 14 ++++++++
 3 files changed, 81 insertions(+), 10 deletions(-)

diff --git a/drivers/dma-buf/sync_file.c b/drivers/dma-buf/sync_file.c
index 57ea7c7..01c12e7 100644
--- a/drivers/dma-buf/sync_file.c
+++ b/drivers/dma-buf/sync_file.c
@@ -44,7 +44,7 @@ int sync_file_validate_type_flags(uint32_t type, uint32_t flags)
 {
 	if (flags)
 		return -EINVAL;
-	if (type != SYNC_FILE_TYPE_FENCE)
+	if (type != SYNC_FILE_TYPE_FENCE && type != SYNC_FILE_TYPE_SEMAPHORE)
 		return -EINVAL;
 	return 0;
 }
@@ -165,6 +165,28 @@ struct sync_file *sync_file_fdget(int fd)
 EXPORT_SYMBOL(sync_file_fdget);
 
 /**
+ * sync_file_get_fence_from_sync_file - get the fence related to the sync_file
+ * @sync_file:		sync_file fd to get the fence from
+ *
+ * Returns a fence that represents all fence in the sync_file. On error NULL is returned.
+ */
+struct dma_fence *sync_file_get_fence_from_sync_file(struct sync_file *sync_file)
+{
+	struct dma_fence *fence;
+
+	if (!rcu_access_pointer(sync_file->fence)) {
+		fput(sync_file->file);
+		return NULL;
+	}
+
+	rcu_read_lock();
+	fence = dma_fence_get_rcu_safe(&sync_file->fence);
+	rcu_read_unlock();
+	return fence;
+}
+EXPORT_SYMBOL(sync_file_get_fence_from_sync_file);
+
+/**
  * sync_file_get_fence - get the fence related to the sync_file fd
  * @fd:		sync_file fd to get the fence from
  *
@@ -180,14 +202,7 @@ struct dma_fence *sync_file_get_fence(int fd)
 	if (!sync_file)
 		return NULL;
 
-	if (!rcu_access_pointer(sync_file->fence)) {
-		fput(sync_file->file);
-		return NULL;
-	}
-
-	rcu_read_lock();
-	fence = dma_fence_get_rcu_safe(&sync_file->fence);
-	rcu_read_unlock();
+	fence = sync_file_get_fence_from_sync_file(sync_file);
 
 	fput(sync_file->file);
 
@@ -202,6 +217,39 @@ sync_file_get_fence_locked(struct sync_file *sync_file)
 					 sync_file_held(sync_file));
 }
 
+/**
+ * sync_file_replace_fence - replace the fence related to the sync_file
+ * @sync_file:	 sync file to replace fence in
+ * @fence: fence to replace with (or NULL for no fence).
+ * Returns previous fence.
+ */
+int sync_file_replace_fence(struct sync_file *sync_file,
+			    struct dma_fence *fence,
+			    struct dma_fence **old_fence)
+{
+	struct dma_fence *ret_fence = NULL;
+
+	/* don't allow replace on fence sync files */
+	if (sync_file->type != SYNC_FILE_TYPE_SEMAPHORE)
+		return -EINVAL;
+
+	if (fence == sync_file->fence)
+		return -EINVAL;
+
+	if (fence)
+		dma_fence_get(fence);
+
+	mutex_lock(&sync_file->lock);
+
+	ret_fence = sync_file_get_fence_locked(sync_file);
+	RCU_INIT_POINTER(sync_file->fence, fence);
+
+	mutex_unlock(&sync_file->lock);
+	*old_fence = ret_fence;
+	return 0;
+}
+EXPORT_SYMBOL(sync_file_replace_fence);
+
 static int sync_file_set_fence(struct sync_file *sync_file,
 			       struct dma_fence **fences, int num_fences)
 {
@@ -406,6 +454,10 @@ static unsigned int sync_file_poll(struct file *file, poll_table *wait)
 	unsigned int ret_val = 0;
 	struct dma_fence *fence;
 
+	/* ban polls on the sync file semaphores */
+	if (!sync_file || sync_file->type != SYNC_FILE_TYPE_FENCE)
+		return POLLERR | POLLNVAL;
+
 	poll_wait(file, &sync_file->wq, wait);
 
 	mutex_lock(&sync_file->lock);
@@ -453,7 +505,8 @@ static long sync_file_ioctl_merge(struct sync_file *sync_file,
 
 	data.name[sizeof(data.name) - 1] = '\0';
 
-	if (sync_file->type != fence2->type) {
+	if (sync_file->type != fence2->type ||
+	    sync_file->type != SYNC_FILE_TYPE_FENCE) {
 		err = -EINVAL;
 		goto err_put_fd;
 	}
diff --git a/include/linux/sync_file.h b/include/linux/sync_file.h
index 4bf661b..14dff25 100644
--- a/include/linux/sync_file.h
+++ b/include/linux/sync_file.h
@@ -61,5 +61,9 @@ int sync_file_validate_type_flags(uint32_t type, uint32_t flags);
 struct sync_file *sync_file_alloc(uint32_t type, uint32_t flags);
 struct sync_file *sync_file_create(struct dma_fence *fence, uint32_t type, uint32_t flags);
 struct dma_fence *sync_file_get_fence(int fd);
+struct dma_fence *sync_file_get_fence_from_sync_file(struct sync_file *sync_file);
 struct sync_file *sync_file_fdget(int fd);
+int sync_file_replace_fence(struct sync_file *sync_file,
+			    struct dma_fence *fence,
+			    struct dma_fence **old_fence);
 #endif /* _LINUX_SYNC_H */
diff --git a/include/uapi/linux/sync_file.h b/include/uapi/linux/sync_file.h
index f439cda..5f266e0 100644
--- a/include/uapi/linux/sync_file.h
+++ b/include/uapi/linux/sync_file.h
@@ -80,6 +80,20 @@ struct sync_file_info {
 #define SYNC_FILE_TYPE_FENCE 0
 
 /**
+ * DOC: SYNC_FILE_TYPE_SEMAPHORE - semaphore sync file object
+ *
+ * This is a sync file that operates like a Vulkan semaphore.
+ * The object should just be imported/exported but not use the
+ * sync file ioctls (except info).
+ * This object can have it's backing fence replaced multiple times.
+ * Each signal operation assigns a backing fence.
+ * Each wait operation waits on the current fence, and removes it.
+ * These operations should happen via driver command submission interfaces.
+ * This is useful for shared vulkan semaphores.
+ */
+#define SYNC_FILE_TYPE_SEMAPHORE 1
+
+/**
  * struct sync_file_type - data returned from sync file type ioctl
  * @type:	sync_file type
  * @flags:	sync_file creation flags
-- 
2.9.3



More information about the amd-gfx mailing list