linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/3] dma-buf/sync_file: only enable fence signalling during wait
@ 2016-06-09 15:05 Gustavo Padovan
  2016-06-09 15:05 ` [PATCH 2/3] dma-buf/sync_file: add sync_file_get_fence() Gustavo Padovan
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Gustavo Padovan @ 2016-06-09 15:05 UTC (permalink / raw)
  To: dri-devel
  Cc: linux-kernel, Daniel Stone, Daniel Vetter, Rob Clark,
	Greg Hackmann, John Harrison, laurent.pinchart, seanpaul,
	marcheu, m.chehab, Sumit Semwal, Maarten Lankhorst,
	Gustavo Padovan

From: Gustavo Padovan <gustavo.padovan@collabora.co.uk>

Signalling doesn't need to be enabled at sync_file creation, it is only
required if userspace waiting the fence to signal through poll().

Thus we delay fence_add_callback() until poll is called. It only adds the
callback the first time poll() is called. This avoid re-adding the same
callback multiple times.

Signed-off-by: Gustavo Padovan <gustavo.padovan@collabora.co.uk>
---
 drivers/dma-buf/sync_file.c | 36 +++++++++++++++++++++++-------------
 include/linux/fence.h       | 12 ++++++++++++
 2 files changed, 35 insertions(+), 13 deletions(-)

diff --git a/drivers/dma-buf/sync_file.c b/drivers/dma-buf/sync_file.c
index 00e9186..59f3445 100644
--- a/drivers/dma-buf/sync_file.c
+++ b/drivers/dma-buf/sync_file.c
@@ -91,9 +91,7 @@ struct sync_file *sync_file_create(struct fence *fence)
 
 	sync_file->cbs[0].fence = fence;
 	sync_file->cbs[0].sync_file = sync_file;
-	if (fence_add_callback(fence, &sync_file->cbs[0].cb,
-			       fence_check_cb_func))
-		atomic_dec(&sync_file->status);
+	INIT_LIST_HEAD(&sync_file->cbs[0].cb.node);
 
 	sync_file_debug_add(sync_file);
 	return sync_file;
@@ -130,8 +128,7 @@ static void sync_file_add_pt(struct sync_file *sync_file, int *i,
 	sync_file->cbs[*i].fence = fence;
 	sync_file->cbs[*i].sync_file = sync_file;
 
-	if (!fence_add_callback(fence, &sync_file->cbs[*i].cb,
-				fence_check_cb_func)) {
+	if (!fence_is_signaled(fence)) {
 		fence_get(fence);
 		(*i)++;
 	}
@@ -212,11 +209,8 @@ static void sync_file_free(struct kref *kref)
 						     kref);
 	int i;
 
-	for (i = 0; i < sync_file->num_fences; ++i) {
-		fence_remove_callback(sync_file->cbs[i].fence,
-				      &sync_file->cbs[i].cb);
+	for (i = 0; i < sync_file->num_fences; ++i)
 		fence_put(sync_file->cbs[i].fence);
-	}
 
 	sync_file_debug_remove(sync_file);
 	kfree(sync_file);
@@ -233,17 +227,33 @@ static int sync_file_release(struct inode *inode, struct file *file)
 static unsigned int sync_file_poll(struct file *file, poll_table *wait)
 {
 	struct sync_file *sync_file = file->private_data;
-	int status;
+	int status, i;
 
 	poll_wait(file, &sync_file->wq, wait);
 
+	for (i = 0 ; i < sync_file->num_fences ; i++) {
+		if (fence_is_enabled(sync_file->cbs[i].fence))
+			continue;
+
+		if (fence_add_callback(sync_file->cbs[i].fence,
+				       &sync_file->cbs[i].cb,
+				       fence_check_cb_func))
+			atomic_dec(&sync_file->status);
+	}
+
 	status = atomic_read(&sync_file->status);
 
-	if (!status)
-		return POLLIN;
+	if (status > 0)
+		return 0;
+
+	for (i = 0 ; i < sync_file->num_fences ; i++)
+		fence_remove_callback(sync_file->cbs[i].fence,
+				      &sync_file->cbs[i].cb);
+
 	if (status < 0)
 		return POLLERR;
-	return 0;
+
+	return POLLIN;
 }
 
 static long sync_file_ioctl_merge(struct sync_file *sync_file,
diff --git a/include/linux/fence.h b/include/linux/fence.h
index 523ea3f..95a70b2 100644
--- a/include/linux/fence.h
+++ b/include/linux/fence.h
@@ -283,6 +283,18 @@ fence_is_signaled(struct fence *fence)
 }
 
 /**
+ * fence_is_enabled - return an indication if signalling is enabled
+ * @fence:	[in]	the fence to check
+ *
+ * Returns true if signalling was already enabled, false if not.
+ */
+static inline bool
+fence_is_enabled(struct fence *fence)
+{
+	return test_bit(FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags);
+}
+
+/**
  * fence_is_later - return if f1 is chronologically later than f2
  * @f1:	[in]	the first fence from the same context
  * @f2:	[in]	the second fence from the same context
-- 
2.5.5

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/3] dma-buf/sync_file: add sync_file_get_fence()
  2016-06-09 15:05 [PATCH 1/3] dma-buf/sync_file: only enable fence signalling during wait Gustavo Padovan
@ 2016-06-09 15:05 ` Gustavo Padovan
  2016-06-10 12:23   ` Chris Wilson
  2016-06-09 15:05 ` [PATCH 3/3] Documentation: add doc for sync_file_get_fence() Gustavo Padovan
  2016-06-09 22:12 ` [PATCH 1/3] dma-buf/sync_file: only enable fence signalling during wait Daniel Vetter
  2 siblings, 1 reply; 6+ messages in thread
From: Gustavo Padovan @ 2016-06-09 15:05 UTC (permalink / raw)
  To: dri-devel
  Cc: linux-kernel, Daniel Stone, Daniel Vetter, Rob Clark,
	Greg Hackmann, John Harrison, laurent.pinchart, seanpaul,
	marcheu, m.chehab, Sumit Semwal, Maarten Lankhorst,
	Gustavo Padovan

From: Gustavo Padovan <gustavo.padovan@collabora.co.uk>

Creates a function that given an sync file descriptor returns a
fence_collection containing all fences in the sync_file.

If there is only one fence in the sync_file this fence itself is returned,
however if there is more than one, a fence_collection fence is returned.

v2: Comments by Daniel Vetter
	- Adapt to new version of fence_collection_init()
	- Hold a reference for the fence we return

v3:     - Adapt to use fput() directly
	- rename to sync_file_get_fence() as we always return one fence

v4: Adapt to use fence_array

Signed-off-by: Gustavo Padovan <gustavo.padovan@collabora.co.uk>
---
 drivers/dma-buf/sync_file.c | 61 +++++++++++++++++++++++++++++++++++++++++++++
 include/linux/sync_file.h   |  4 ++-
 2 files changed, 64 insertions(+), 1 deletion(-)

diff --git a/drivers/dma-buf/sync_file.c b/drivers/dma-buf/sync_file.c
index 59f3445..daaf411 100644
--- a/drivers/dma-buf/sync_file.c
+++ b/drivers/dma-buf/sync_file.c
@@ -23,6 +23,7 @@
 #include <linux/slab.h>
 #include <linux/uaccess.h>
 #include <linux/anon_inodes.h>
+#include <linux/fence-array.h>
 #include <linux/sync_file.h>
 #include <uapi/linux/sync_file.h>
 
@@ -122,6 +123,63 @@ err:
 	return NULL;
 }
 
+/**
+ * sync_file_get_fence - get the fence related to the sync_file fd
+ * @fd:		sync_file fd to get the fence from
+ *
+ * Ensures @fd references a valid sync_file and returns a fence that
+ * represents all fence in the sync_file.
+ *
+ * If there is only one fence in the sync_file, this fence is returned.
+ * If there is more than one, a fence_array containing all fences
+ * is created and its base fence object is returned.
+ * On both cases a reference to the returned fence is held. On error
+ * NULL is returned.
+ */
+struct fence *sync_file_get_fence(int fd)
+{
+	struct sync_file *sync_file;
+	struct fence_array *fence_array;
+	struct fence **fences;
+	int i;
+
+	sync_file = sync_file_fdget(fd);
+	if (!sync_file)
+		return NULL;
+
+	if (sync_file->num_fences == 1) {
+		struct fence *fence = sync_file->cbs[0].fence;
+
+		fence_get(fence);
+		fput(sync_file->file);
+		return fence;
+	}
+
+	fences = kcalloc(sync_file->num_fences, sizeof(**fences), GFP_KERNEL);
+	if (!fences) {
+		fput(sync_file->file);
+		return NULL;
+	}
+
+	for (i = 0 ; i < sync_file->num_fences ; i++)
+		fences[i] = sync_file->cbs[i].fence;
+
+	fence_array = fence_array_create(sync_file->num_fences, fences,
+					fence_context_alloc(1), 1, false);
+	if (!fence_array) {
+		kfree(fences);
+		fput(sync_file->file);
+		return NULL;
+	}
+
+	sync_file->fence_array = fence_array;
+	fence_get(&fence_array->base);
+	fput(sync_file->file);
+
+	return &fence_array->base;
+}
+EXPORT_SYMBOL(sync_file_get_fence);
+
 static void sync_file_add_pt(struct sync_file *sync_file, int *i,
 			     struct fence *fence)
 {
@@ -209,6 +267,9 @@ static void sync_file_free(struct kref *kref)
 						     kref);
 	int i;
 
+	if (sync_file->fence_array)
+		fence_put(&sync_file->fence_array->base);
+
 	for (i = 0; i < sync_file->num_fences; ++i)
 		fence_put(sync_file->cbs[i].fence);
 
diff --git a/include/linux/sync_file.h b/include/linux/sync_file.h
index c6ffe8b..33cb3d6 100644
--- a/include/linux/sync_file.h
+++ b/include/linux/sync_file.h
@@ -35,6 +35,7 @@ struct sync_file_cb {
  * @num_fences:		number of sync_pts in the fence
  * @wq:			wait queue for fence signaling
  * @status:		0: signaled, >0:active, <0: error
+ * @fence_array:	array with the fences in the sync_file
  * @cbs:		sync_pts callback information
  */
 struct sync_file {
@@ -49,9 +50,10 @@ struct sync_file {
 	wait_queue_head_t	wq;
 	atomic_t		status;
 
+	struct fence_array	*fence_array;
 	struct sync_file_cb	cbs[];
 };
 
 struct sync_file *sync_file_create(struct fence *fence);
-
+struct fence *sync_file_get_fence(int fd);
 #endif /* _LINUX_SYNC_H */
-- 
2.5.5

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/3] Documentation: add doc for sync_file_get_fence()
  2016-06-09 15:05 [PATCH 1/3] dma-buf/sync_file: only enable fence signalling during wait Gustavo Padovan
  2016-06-09 15:05 ` [PATCH 2/3] dma-buf/sync_file: add sync_file_get_fence() Gustavo Padovan
@ 2016-06-09 15:05 ` Gustavo Padovan
  2016-06-09 22:12 ` [PATCH 1/3] dma-buf/sync_file: only enable fence signalling during wait Daniel Vetter
  2 siblings, 0 replies; 6+ messages in thread
From: Gustavo Padovan @ 2016-06-09 15:05 UTC (permalink / raw)
  To: dri-devel
  Cc: linux-kernel, Daniel Stone, Daniel Vetter, Rob Clark,
	Greg Hackmann, John Harrison, laurent.pinchart, seanpaul,
	marcheu, m.chehab, Sumit Semwal, Maarten Lankhorst,
	Gustavo Padovan

From: Gustavo Padovan <gustavo.padovan@collabora.co.uk>

Document the new function added to sync_file.c

v2: Adapt to fence_array

Signed-off-by: Gustavo Padovan <gustavo.padovan@collabora.co.uk>
---
 Documentation/sync_file.txt | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/Documentation/sync_file.txt b/Documentation/sync_file.txt
index eaf8297..46f5769 100644
--- a/Documentation/sync_file.txt
+++ b/Documentation/sync_file.txt
@@ -64,6 +64,25 @@ The sync_file fd now can be sent to userspace.
 If the creation process fail, or the sync_file needs to be released by any
 other reason fput(sync_file->file) should be used.
 
+Receiving Sync Files from Userspace
+-----------------------------------
+
+When userspace needs to send an in-fence to the driver it pass file descriptor
+of the Sync File to the kernel. The kernel then can retrieve the sync_file
+from it.
+
+Interface:
+	struct fence *sync_file_get_fence(int fd);
+
+
+The function return a struct fence pointer referencing the fence(s) in the Sync
+File. If the Sync File contains only one fence, this fence is returned. But if
+has more than one fence a fence_array[3] is returned. However the callers only
+see the arrays' base class which is struct fence. In both cases a reference to
+the returned fence is held.
+
+
 References:
 [1] struct sync_file in include/linux/sync_file.h
 [2] All interfaces mentioned above defined in include/linux/sync_file.h
+[1] struct fence_array in include/linux/fence-array.h
-- 
2.5.5

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/3] dma-buf/sync_file: only enable fence signalling during wait
  2016-06-09 15:05 [PATCH 1/3] dma-buf/sync_file: only enable fence signalling during wait Gustavo Padovan
  2016-06-09 15:05 ` [PATCH 2/3] dma-buf/sync_file: add sync_file_get_fence() Gustavo Padovan
  2016-06-09 15:05 ` [PATCH 3/3] Documentation: add doc for sync_file_get_fence() Gustavo Padovan
@ 2016-06-09 22:12 ` Daniel Vetter
  2 siblings, 0 replies; 6+ messages in thread
From: Daniel Vetter @ 2016-06-09 22:12 UTC (permalink / raw)
  To: Gustavo Padovan
  Cc: dri-devel, linux-kernel, Daniel Stone, Daniel Vetter, Rob Clark,
	Greg Hackmann, John Harrison, laurent.pinchart, seanpaul,
	marcheu, m.chehab, Sumit Semwal, Maarten Lankhorst,
	Gustavo Padovan

On Thu, Jun 09, 2016 at 12:05:28PM -0300, Gustavo Padovan wrote:
> From: Gustavo Padovan <gustavo.padovan@collabora.co.uk>
> 
> Signalling doesn't need to be enabled at sync_file creation, it is only
> required if userspace waiting the fence to signal through poll().
> 
> Thus we delay fence_add_callback() until poll is called. It only adds the
> callback the first time poll() is called. This avoid re-adding the same
> callback multiple times.
> 
> Signed-off-by: Gustavo Padovan <gustavo.padovan@collabora.co.uk>
> ---
>  drivers/dma-buf/sync_file.c | 36 +++++++++++++++++++++++-------------
>  include/linux/fence.h       | 12 ++++++++++++
>  2 files changed, 35 insertions(+), 13 deletions(-)
> 
> diff --git a/drivers/dma-buf/sync_file.c b/drivers/dma-buf/sync_file.c
> index 00e9186..59f3445 100644
> --- a/drivers/dma-buf/sync_file.c
> +++ b/drivers/dma-buf/sync_file.c
> @@ -91,9 +91,7 @@ struct sync_file *sync_file_create(struct fence *fence)
>  
>  	sync_file->cbs[0].fence = fence;
>  	sync_file->cbs[0].sync_file = sync_file;
> -	if (fence_add_callback(fence, &sync_file->cbs[0].cb,
> -			       fence_check_cb_func))
> -		atomic_dec(&sync_file->status);
> +	INIT_LIST_HEAD(&sync_file->cbs[0].cb.node);
>  
>  	sync_file_debug_add(sync_file);
>  	return sync_file;
> @@ -130,8 +128,7 @@ static void sync_file_add_pt(struct sync_file *sync_file, int *i,
>  	sync_file->cbs[*i].fence = fence;
>  	sync_file->cbs[*i].sync_file = sync_file;
>  
> -	if (!fence_add_callback(fence, &sync_file->cbs[*i].cb,
> -				fence_check_cb_func)) {
> +	if (!fence_is_signaled(fence)) {
>  		fence_get(fence);
>  		(*i)++;
>  	}
> @@ -212,11 +209,8 @@ static void sync_file_free(struct kref *kref)
>  						     kref);
>  	int i;
>  
> -	for (i = 0; i < sync_file->num_fences; ++i) {
> -		fence_remove_callback(sync_file->cbs[i].fence,
> -				      &sync_file->cbs[i].cb);
> +	for (i = 0; i < sync_file->num_fences; ++i)
>  		fence_put(sync_file->cbs[i].fence);
> -	}
>  
>  	sync_file_debug_remove(sync_file);
>  	kfree(sync_file);
> @@ -233,17 +227,33 @@ static int sync_file_release(struct inode *inode, struct file *file)
>  static unsigned int sync_file_poll(struct file *file, poll_table *wait)
>  {
>  	struct sync_file *sync_file = file->private_data;
> -	int status;
> +	int status, i;
>  
>  	poll_wait(file, &sync_file->wq, wait);
>  
> +	for (i = 0 ; i < sync_file->num_fences ; i++) {
> +		if (fence_is_enabled(sync_file->cbs[i].fence))

If some other sync_file (or anyone else really) added enabling, then
you'll miss adding a callback. I don't think this check here works. You
probably also need more testcases in the debugfs-based tests if this falls
through the cracks ;-)
-Daniel

> +			continue;
> +
> +		if (fence_add_callback(sync_file->cbs[i].fence,
> +				       &sync_file->cbs[i].cb,
> +				       fence_check_cb_func))
> +			atomic_dec(&sync_file->status);
> +	}
> +
>  	status = atomic_read(&sync_file->status);
>  
> -	if (!status)
> -		return POLLIN;
> +	if (status > 0)
> +		return 0;
> +
> +	for (i = 0 ; i < sync_file->num_fences ; i++)
> +		fence_remove_callback(sync_file->cbs[i].fence,
> +				      &sync_file->cbs[i].cb);
> +
>  	if (status < 0)
>  		return POLLERR;
> -	return 0;
> +
> +	return POLLIN;
>  }
>  
>  static long sync_file_ioctl_merge(struct sync_file *sync_file,
> diff --git a/include/linux/fence.h b/include/linux/fence.h
> index 523ea3f..95a70b2 100644
> --- a/include/linux/fence.h
> +++ b/include/linux/fence.h
> @@ -283,6 +283,18 @@ fence_is_signaled(struct fence *fence)
>  }
>  
>  /**
> + * fence_is_enabled - return an indication if signalling is enabled
> + * @fence:	[in]	the fence to check
> + *
> + * Returns true if signalling was already enabled, false if not.
> + */
> +static inline bool
> +fence_is_enabled(struct fence *fence)
> +{
> +	return test_bit(FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags);
> +}
> +
> +/**
>   * fence_is_later - return if f1 is chronologically later than f2
>   * @f1:	[in]	the first fence from the same context
>   * @f2:	[in]	the second fence from the same context
> -- 
> 2.5.5
> 

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/3] dma-buf/sync_file: add sync_file_get_fence()
  2016-06-09 15:05 ` [PATCH 2/3] dma-buf/sync_file: add sync_file_get_fence() Gustavo Padovan
@ 2016-06-10 12:23   ` Chris Wilson
  2016-06-13 21:21     ` Gustavo Padovan
  0 siblings, 1 reply; 6+ messages in thread
From: Chris Wilson @ 2016-06-10 12:23 UTC (permalink / raw)
  To: Gustavo Padovan
  Cc: dri-devel, marcheu, Daniel Stone, seanpaul, Daniel Vetter,
	linux-kernel, laurent.pinchart, Gustavo Padovan, John Harrison,
	m.chehab

On Thu, Jun 09, 2016 at 12:05:29PM -0300, Gustavo Padovan wrote:
> From: Gustavo Padovan <gustavo.padovan@collabora.co.uk>
> 
> Creates a function that given an sync file descriptor returns a
> fence_collection containing all fences in the sync_file.
> 
> If there is only one fence in the sync_file this fence itself is returned,
> however if there is more than one, a fence_collection fence is returned.
> 
> v2: Comments by Daniel Vetter
> 	- Adapt to new version of fence_collection_init()
> 	- Hold a reference for the fence we return
> 
> v3:     - Adapt to use fput() directly
> 	- rename to sync_file_get_fence() as we always return one fence
> 
> v4: Adapt to use fence_array

The sync_file looks just like a fence_array plus struct file integration.
Is there a good reason why we cannot just use fence_array here?
-Chris

-- 
Chris Wilson, Intel Open Source Technology Centre

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 2/3] dma-buf/sync_file: add sync_file_get_fence()
  2016-06-10 12:23   ` Chris Wilson
@ 2016-06-13 21:21     ` Gustavo Padovan
  0 siblings, 0 replies; 6+ messages in thread
From: Gustavo Padovan @ 2016-06-13 21:21 UTC (permalink / raw)
  To: Chris Wilson, dri-devel, marcheu, Daniel Stone, seanpaul,
	Daniel Vetter, linux-kernel, laurent.pinchart, Gustavo Padovan,
	John Harrison, m.chehab

2016-06-10 Chris Wilson <chris@chris-wilson.co.uk>:

> On Thu, Jun 09, 2016 at 12:05:29PM -0300, Gustavo Padovan wrote:
> > From: Gustavo Padovan <gustavo.padovan@collabora.co.uk>
> > 
> > Creates a function that given an sync file descriptor returns a
> > fence_collection containing all fences in the sync_file.
> > 
> > If there is only one fence in the sync_file this fence itself is returned,
> > however if there is more than one, a fence_collection fence is returned.
> > 
> > v2: Comments by Daniel Vetter
> > 	- Adapt to new version of fence_collection_init()
> > 	- Hold a reference for the fence we return
> > 
> > v3:     - Adapt to use fput() directly
> > 	- rename to sync_file_get_fence() as we always return one fence
> > 
> > v4: Adapt to use fence_array
> 
> The sync_file looks just like a fence_array plus struct file integration.
> Is there a good reason why we cannot just use fence_array here?

I think the only reason is because I haven't thought about this before.
If we add proper support to fence_array we can make sync_files even
simpler.

	Gustavo

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2016-06-13 21:21 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-09 15:05 [PATCH 1/3] dma-buf/sync_file: only enable fence signalling during wait Gustavo Padovan
2016-06-09 15:05 ` [PATCH 2/3] dma-buf/sync_file: add sync_file_get_fence() Gustavo Padovan
2016-06-10 12:23   ` Chris Wilson
2016-06-13 21:21     ` Gustavo Padovan
2016-06-09 15:05 ` [PATCH 3/3] Documentation: add doc for sync_file_get_fence() Gustavo Padovan
2016-06-09 22:12 ` [PATCH 1/3] dma-buf/sync_file: only enable fence signalling during wait Daniel Vetter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).