All of lore.kernel.org
 help / color / mirror / Atom feed
From: Chris Wilson <chris-Y6uKTt2uX1cEflXRtASbqLVCufUGDwFn@public.gmane.org>
To: Dave Airlie <airlied-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
Cc: dri-devel-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org,
	amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org
Subject: Re: [PATCH 4/7] sync_file: add support for sem_file
Date: Thu, 13 Apr 2017 03:52:14 +0100	[thread overview]
Message-ID: <20170413025214.GA27081@nuc-i3427.alporthouse.com> (raw)
In-Reply-To: <20170413014144.637-5-airlied-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>

On Thu, Apr 13, 2017 at 11:41:41AM +1000, Dave Airlie wrote:
> From: Dave Airlie <airlied@redhat.com>
> 
> This adds support for a file that has semaphore semantics
> (for Vulkan shared semaphores).
> 
> These objects are persistent objects that can have a
> fence that changes. When the object is signaled, a fence
> is attached to it, and when an object is waited on, the
> fence is removed. All interactions with these objects
> should be via command submission routines in the drm
> drivers. The sem_file is just for passing the sems between
> processes.
> 
> Signed-off-by: Dave Airlie <airlied@redhat.com>
> ---
>  drivers/dma-buf/sync_file.c | 101 ++++++++++++++++++++++++++++++++++++++++++++
>  include/linux/sync_file.h   |  16 +++++++
>  2 files changed, 117 insertions(+)
> 
> diff --git a/drivers/dma-buf/sync_file.c b/drivers/dma-buf/sync_file.c
> index 2342d8b..a88d786 100644
> --- a/drivers/dma-buf/sync_file.c
> +++ b/drivers/dma-buf/sync_file.c
> @@ -468,3 +468,104 @@ static const struct file_operations sync_file_fops = {
>  	.unlocked_ioctl = sync_file_ioctl,
>  	.compat_ioctl = sync_file_ioctl,
>  };
> +
> +static int sem_file_release(struct inode *inode, struct file *file)
> +{
> +	struct sem_file *sem_file = file->private_data;
> +	struct dma_fence *fence;
> +
> +	fence = rcu_dereference_protected(sem_file->base.fence, 1);
> +	dma_fence_put(fence);
> +	kfree(sem_file);
> +
> +	return 0;
> +}
> +
> +static const struct file_operations sem_file_fops = {
> +	.release = sem_file_release,
> +};
> +
> +struct sem_file *sem_file_alloc(void)
> +{
> +	struct sem_file *sem_file;
> +	int ret;
> +
> +	sem_file = kzalloc(sizeof(*sem_file), GFP_KERNEL);
> +	if (!sem_file)
> +		return NULL;
> +
> +	ret = fence_file_init(&sem_file->base,
> +			      &sem_file_fops);
> +	if (ret)
> +		goto err;
> +
> +	RCU_INIT_POINTER(sem_file->base.fence, NULL);
> +	mutex_init(&sem_file->lock);
> +
> +	return sem_file;
> +
> +err:
> +	kfree(sem_file);
> +	return NULL;
> +}
> +EXPORT_SYMBOL(sem_file_alloc);
> +
> +struct sem_file *sem_file_fdget(int fd)
> +{
> +	struct file *file = fget(fd);
> +
> +	if (!file)
> +		return NULL;
> +
> +	if (file->f_op != &sem_file_fops)
> +		goto err;
> +
> +	return file->private_data;
> +
> +err:
> +	fput(file);
> +	return NULL;
> +}
> +EXPORT_SYMBOL(sem_file_fdget);
> +
> +#define sem_file_held(obj) lockdep_is_held(&(obj)->lock)
> +
> +struct dma_fence *sem_file_get_fence(struct sem_file *sem_file)
> +{
> +	struct dma_fence *fence;
> +
> +	if (!rcu_access_pointer(sem_file->base.fence)) {
> +		return NULL;
> +	}
> +
> +	rcu_read_lock();
> +	fence = dma_fence_get_rcu_safe(&sem_file->base.fence);
> +	rcu_read_unlock();
> +	return fence;
> +}
> +EXPORT_SYMBOL(sem_file_get_fence);
> +
> +static inline struct dma_fence *
> +sem_file_get_fence_locked(struct sem_file *sem_file)
> +{
> +	return rcu_dereference_protected(sem_file->base.fence,
> +					 sem_file_held(sem_file));
> +}
> +
> +int sem_file_replace_fence(struct sem_file *sem_file,
> +			   struct dma_fence *fence,
> +			   struct dma_fence **old_fence)
> +{
> +	struct dma_fence *ret_fence = NULL;
> +
> +	if (fence)
> +		dma_fence_get(fence);
> +
> +	mutex_lock(&sem_file->lock);
> +	ret_fence = sem_file_get_fence_locked(sem_file);
> +	RCU_INIT_POINTER(sem_file->base.fence, fence);
> +	mutex_unlock(&sem_file->lock);

Is xchg() universal?

struct dma_fence *sem_file_replace_fence(struct sem_file *sem_file,
					 struct dma_fence *fence)
{
	return xchg(&sem_file->base.fence, dma_fence_get(fence));
}

safe against the rcu read and kills off the mutex.

I think this is the cleaner approach, precisely because it stops me
having the delusion that the semaphores and sync_file are
interchangeable, and I won't ask if I can merge semaphores together, or
if I can inspect the state with the CPU.
-Chris

-- 
Chris Wilson, Intel Open Source Technology Centre
_______________________________________________
amd-gfx mailing list
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

  parent reply	other threads:[~2017-04-13  2:52 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-13  1:41 [rfc repost] drm sync objects - a new beginning (make ickle happier?) Dave Airlie
2017-04-13  1:41 ` [PATCH 2/7] sync_file: mark the fence pointer as rcu Dave Airlie
2017-04-13  1:41 ` [PATCH 5/7] drm: introduce sync objects as wrappers for sem files Dave Airlie
2017-04-13  1:41 ` [PATCH 6/7] amdgpu/cs: split out fence dependency checking Dave Airlie
2017-04-13  1:41 ` [PATCH 7/7] amdgpu: use sync file for shared semaphores (v3) Dave Airlie
     [not found] ` <20170413014144.637-1-airlied-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2017-04-13  1:41   ` [PATCH 1/7] sync_file: get rid of internal reference count Dave Airlie
     [not found]     ` <20170413014144.637-2-airlied-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2017-04-17 13:13       ` Gustavo Padovan
2017-04-17 15:14         ` Sumit Semwal
     [not found]           ` <CAO_48GHYX7oDv3MtD5XhoQOSyYZOF_byP+niV0YC7f1ZhHvL1A-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2017-04-18 14:38             ` Gustavo Padovan
2017-04-13  1:41   ` [PATCH 3/7] sync_file: split out fence_file base class from sync_file Dave Airlie
2017-04-19 12:02     ` Christian König
2017-04-13  1:41   ` [PATCH 4/7] sync_file: add support for sem_file Dave Airlie
     [not found]     ` <20170413014144.637-5-airlied-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2017-04-13  2:52       ` Chris Wilson [this message]
2017-04-19 12:07   ` [rfc repost] drm sync objects - a new beginning (make ickle happier?) Christian König
2017-04-19 17:14     ` James Jones
     [not found]     ` <0734833f-e6f2-b5f3-9687-f21f82fd8599-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org>
2017-04-19 18:42       ` Dave Airlie
2017-04-19 19:14         ` Dave Airlie
2017-04-20  8:36           ` Christian König

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170413025214.GA27081@nuc-i3427.alporthouse.com \
    --to=chris-y6uktt2ux1ceflxrtasbqlvcufugdwfn@public.gmane.org \
    --cc=airlied-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
    --cc=amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org \
    --cc=dri-devel-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.