2016-06-01 03:59:05 +08:00
|
|
|
/*
|
2016-08-11 23:26:42 +08:00
|
|
|
* Sync File validation framework
|
2016-06-01 03:59:05 +08:00
|
|
|
*
|
|
|
|
* Copyright (C) 2012 Google, Inc.
|
|
|
|
*
|
|
|
|
* This software is licensed under the terms of the GNU General Public
|
|
|
|
* License version 2, as published by the Free Software Foundation, and
|
|
|
|
* may be copied, distributed, and modified under those terms.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/file.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/uaccess.h>
|
2016-06-01 03:59:07 +08:00
|
|
|
#include <linux/slab.h>
|
2016-06-01 03:59:05 +08:00
|
|
|
#include <linux/sync_file.h>
|
|
|
|
|
2016-06-01 03:59:12 +08:00
|
|
|
#include "sync_debug.h"
|
2016-06-01 03:59:05 +08:00
|
|
|
|
2016-06-01 03:59:07 +08:00
|
|
|
#define CREATE_TRACE_POINTS
|
2016-08-11 23:26:41 +08:00
|
|
|
#include "sync_trace.h"
|
2016-06-01 03:59:07 +08:00
|
|
|
|
2016-08-12 00:45:53 +08:00
|
|
|
/*
|
|
|
|
* SW SYNC validation framework
|
|
|
|
*
|
|
|
|
* A sync object driver that uses a 32bit counter to coordinate
|
|
|
|
* synchronization. Useful when there is no hardware primitive backing
|
|
|
|
* the synchronization.
|
|
|
|
*
|
|
|
|
* To start the framework just open:
|
|
|
|
*
|
|
|
|
* <debugfs>/sync/sw_sync
|
|
|
|
*
|
|
|
|
* That will create a sync timeline, all fences created under this timeline
|
|
|
|
* file descriptor will belong to the this timeline.
|
|
|
|
*
|
|
|
|
* The 'sw_sync' file can be opened many times as to create different
|
|
|
|
* timelines.
|
|
|
|
*
|
|
|
|
* Fences can be created with SW_SYNC_IOC_CREATE_FENCE ioctl with struct
|
|
|
|
* sw_sync_ioctl_create_fence as parameter.
|
|
|
|
*
|
|
|
|
* To increment the timeline counter, SW_SYNC_IOC_INC ioctl should be used
|
|
|
|
* with the increment as u32. This will update the last signaled value
|
|
|
|
* from the timeline and signal any fence that has a seqno smaller or equal
|
|
|
|
* to it.
|
|
|
|
*
|
|
|
|
* struct sw_sync_ioctl_create_fence
|
|
|
|
* @value: the seqno to initialise the fence with
|
|
|
|
* @name: the name of the new sync point
|
|
|
|
* @fence: return the fd of the new sync_file with the created fence
|
|
|
|
*/
|
2016-06-01 03:59:08 +08:00
|
|
|
struct sw_sync_create_fence_data {
|
|
|
|
__u32 value;
|
|
|
|
char name[32];
|
|
|
|
__s32 fence; /* fd of new fence */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define SW_SYNC_IOC_MAGIC 'W'
|
|
|
|
|
|
|
|
#define SW_SYNC_IOC_CREATE_FENCE _IOWR(SW_SYNC_IOC_MAGIC, 0,\
|
|
|
|
struct sw_sync_create_fence_data)
|
2016-08-12 00:45:53 +08:00
|
|
|
|
2016-06-01 03:59:08 +08:00
|
|
|
#define SW_SYNC_IOC_INC _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
|
|
|
|
|
2016-10-25 20:00:45 +08:00
|
|
|
static const struct dma_fence_ops timeline_fence_ops;
|
2016-06-01 03:59:07 +08:00
|
|
|
|
2016-10-25 20:00:45 +08:00
|
|
|
static inline struct sync_pt *dma_fence_to_sync_pt(struct dma_fence *fence)
|
2016-06-01 03:59:07 +08:00
|
|
|
{
|
|
|
|
if (fence->ops != &timeline_fence_ops)
|
|
|
|
return NULL;
|
|
|
|
return container_of(fence, struct sync_pt, base);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sync_timeline_create() - creates a sync object
|
|
|
|
* @name: sync_timeline name
|
|
|
|
*
|
|
|
|
* Creates a new sync_timeline. Returns the sync_timeline object or NULL in
|
|
|
|
* case of error.
|
|
|
|
*/
|
2016-09-18 20:49:21 +08:00
|
|
|
static struct sync_timeline *sync_timeline_create(const char *name)
|
2016-06-01 03:59:07 +08:00
|
|
|
{
|
|
|
|
struct sync_timeline *obj;
|
|
|
|
|
|
|
|
obj = kzalloc(sizeof(*obj), GFP_KERNEL);
|
|
|
|
if (!obj)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
kref_init(&obj->kref);
|
2016-10-25 20:00:45 +08:00
|
|
|
obj->context = dma_fence_context_alloc(1);
|
2016-06-01 03:59:07 +08:00
|
|
|
strlcpy(obj->name, name, sizeof(obj->name));
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&obj->child_list_head);
|
|
|
|
INIT_LIST_HEAD(&obj->active_list_head);
|
|
|
|
spin_lock_init(&obj->child_list_lock);
|
|
|
|
|
|
|
|
sync_timeline_debug_add(obj);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sync_timeline_free(struct kref *kref)
|
|
|
|
{
|
|
|
|
struct sync_timeline *obj =
|
|
|
|
container_of(kref, struct sync_timeline, kref);
|
|
|
|
|
|
|
|
sync_timeline_debug_remove(obj);
|
|
|
|
|
|
|
|
kfree(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sync_timeline_get(struct sync_timeline *obj)
|
|
|
|
{
|
|
|
|
kref_get(&obj->kref);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sync_timeline_put(struct sync_timeline *obj)
|
|
|
|
{
|
|
|
|
kref_put(&obj->kref, sync_timeline_free);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sync_timeline_signal() - signal a status change on a sync_timeline
|
|
|
|
* @obj: sync_timeline to signal
|
|
|
|
* @inc: num to increment on timeline->value
|
|
|
|
*
|
|
|
|
* A sync implementation should call this any time one of it's fences
|
|
|
|
* has signaled or has an error condition.
|
|
|
|
*/
|
|
|
|
static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
struct sync_pt *pt, *next;
|
|
|
|
|
|
|
|
trace_sync_timeline(obj);
|
|
|
|
|
|
|
|
spin_lock_irqsave(&obj->child_list_lock, flags);
|
|
|
|
|
|
|
|
obj->value += inc;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(pt, next, &obj->active_list_head,
|
|
|
|
active_list) {
|
2016-10-25 20:00:45 +08:00
|
|
|
if (dma_fence_is_signaled_locked(&pt->base))
|
2016-06-01 03:59:07 +08:00
|
|
|
list_del_init(&pt->active_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&obj->child_list_lock, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sync_pt_create() - creates a sync pt
|
|
|
|
* @parent: fence's parent sync_timeline
|
|
|
|
* @size: size to allocate for this pt
|
|
|
|
* @inc: value of the fence
|
|
|
|
*
|
|
|
|
* Creates a new sync_pt as a child of @parent. @size bytes will be
|
|
|
|
* allocated allowing for implementation specific data to be kept after
|
|
|
|
* the generic sync_timeline struct. Returns the sync_pt object or
|
|
|
|
* NULL in case of error.
|
|
|
|
*/
|
|
|
|
static struct sync_pt *sync_pt_create(struct sync_timeline *obj, int size,
|
|
|
|
unsigned int value)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
struct sync_pt *pt;
|
|
|
|
|
|
|
|
if (size < sizeof(*pt))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
pt = kzalloc(size, GFP_KERNEL);
|
|
|
|
if (!pt)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&obj->child_list_lock, flags);
|
|
|
|
sync_timeline_get(obj);
|
2016-10-25 20:00:45 +08:00
|
|
|
dma_fence_init(&pt->base, &timeline_fence_ops, &obj->child_list_lock,
|
|
|
|
obj->context, value);
|
2016-06-01 03:59:07 +08:00
|
|
|
list_add_tail(&pt->child_list, &obj->child_list_head);
|
|
|
|
INIT_LIST_HEAD(&pt->active_list);
|
|
|
|
spin_unlock_irqrestore(&obj->child_list_lock, flags);
|
|
|
|
return pt;
|
|
|
|
}
|
|
|
|
|
2016-10-25 20:00:45 +08:00
|
|
|
static const char *timeline_fence_get_driver_name(struct dma_fence *fence)
|
2016-06-01 03:59:07 +08:00
|
|
|
{
|
2016-06-01 03:59:11 +08:00
|
|
|
return "sw_sync";
|
2016-06-01 03:59:07 +08:00
|
|
|
}
|
|
|
|
|
2016-10-25 20:00:45 +08:00
|
|
|
static const char *timeline_fence_get_timeline_name(struct dma_fence *fence)
|
2016-06-01 03:59:07 +08:00
|
|
|
{
|
2016-10-25 20:00:45 +08:00
|
|
|
struct sync_timeline *parent = dma_fence_parent(fence);
|
2016-06-01 03:59:07 +08:00
|
|
|
|
|
|
|
return parent->name;
|
|
|
|
}
|
|
|
|
|
2016-10-25 20:00:45 +08:00
|
|
|
static void timeline_fence_release(struct dma_fence *fence)
|
2016-06-01 03:59:07 +08:00
|
|
|
{
|
2016-10-25 20:00:45 +08:00
|
|
|
struct sync_pt *pt = dma_fence_to_sync_pt(fence);
|
|
|
|
struct sync_timeline *parent = dma_fence_parent(fence);
|
2016-06-01 03:59:07 +08:00
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(fence->lock, flags);
|
|
|
|
list_del(&pt->child_list);
|
2016-08-11 23:26:40 +08:00
|
|
|
if (!list_empty(&pt->active_list))
|
2016-06-01 03:59:07 +08:00
|
|
|
list_del(&pt->active_list);
|
|
|
|
spin_unlock_irqrestore(fence->lock, flags);
|
|
|
|
|
|
|
|
sync_timeline_put(parent);
|
2016-10-25 20:00:45 +08:00
|
|
|
dma_fence_free(fence);
|
2016-06-01 03:59:07 +08:00
|
|
|
}
|
|
|
|
|
2016-10-25 20:00:45 +08:00
|
|
|
static bool timeline_fence_signaled(struct dma_fence *fence)
|
2016-06-01 03:59:07 +08:00
|
|
|
{
|
2016-10-25 20:00:45 +08:00
|
|
|
struct sync_timeline *parent = dma_fence_parent(fence);
|
2016-06-01 03:59:07 +08:00
|
|
|
|
|
|
|
return (fence->seqno > parent->value) ? false : true;
|
|
|
|
}
|
|
|
|
|
2016-10-25 20:00:45 +08:00
|
|
|
static bool timeline_fence_enable_signaling(struct dma_fence *fence)
|
2016-06-01 03:59:07 +08:00
|
|
|
{
|
2016-10-25 20:00:45 +08:00
|
|
|
struct sync_pt *pt = dma_fence_to_sync_pt(fence);
|
|
|
|
struct sync_timeline *parent = dma_fence_parent(fence);
|
2016-06-01 03:59:07 +08:00
|
|
|
|
|
|
|
if (timeline_fence_signaled(fence))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
list_add_tail(&pt->active_list, &parent->active_list_head);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-10-25 20:00:45 +08:00
|
|
|
static void timeline_fence_value_str(struct dma_fence *fence,
|
2016-06-01 03:59:07 +08:00
|
|
|
char *str, int size)
|
|
|
|
{
|
|
|
|
snprintf(str, size, "%d", fence->seqno);
|
|
|
|
}
|
|
|
|
|
2016-10-25 20:00:45 +08:00
|
|
|
static void timeline_fence_timeline_value_str(struct dma_fence *fence,
|
2016-06-01 03:59:07 +08:00
|
|
|
char *str, int size)
|
|
|
|
{
|
2016-10-25 20:00:45 +08:00
|
|
|
struct sync_timeline *parent = dma_fence_parent(fence);
|
2016-06-01 03:59:07 +08:00
|
|
|
|
|
|
|
snprintf(str, size, "%d", parent->value);
|
|
|
|
}
|
|
|
|
|
2016-10-25 20:00:45 +08:00
|
|
|
static const struct dma_fence_ops timeline_fence_ops = {
|
2016-06-01 03:59:07 +08:00
|
|
|
.get_driver_name = timeline_fence_get_driver_name,
|
|
|
|
.get_timeline_name = timeline_fence_get_timeline_name,
|
|
|
|
.enable_signaling = timeline_fence_enable_signaling,
|
|
|
|
.signaled = timeline_fence_signaled,
|
2016-10-25 20:00:45 +08:00
|
|
|
.wait = dma_fence_default_wait,
|
2016-06-01 03:59:07 +08:00
|
|
|
.release = timeline_fence_release,
|
|
|
|
.fence_value_str = timeline_fence_value_str,
|
|
|
|
.timeline_value_str = timeline_fence_timeline_value_str,
|
|
|
|
};
|
|
|
|
|
2016-06-01 03:59:05 +08:00
|
|
|
/*
|
|
|
|
* *WARNING*
|
|
|
|
*
|
|
|
|
* improper use of this can result in deadlocking kernel drivers from userspace.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* opening sw_sync create a new sync obj */
|
|
|
|
static int sw_sync_debugfs_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
struct sync_timeline *obj;
|
|
|
|
char task_comm[TASK_COMM_LEN];
|
|
|
|
|
|
|
|
get_task_comm(task_comm, current);
|
|
|
|
|
2016-06-01 03:59:11 +08:00
|
|
|
obj = sync_timeline_create(task_comm);
|
2016-06-01 03:59:05 +08:00
|
|
|
if (!obj)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
file->private_data = obj;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
struct sync_timeline *obj = file->private_data;
|
|
|
|
|
2016-06-01 03:59:10 +08:00
|
|
|
smp_wmb();
|
|
|
|
|
|
|
|
sync_timeline_put(obj);
|
2016-06-01 03:59:05 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
|
|
|
|
unsigned long arg)
|
|
|
|
{
|
|
|
|
int fd = get_unused_fd_flags(O_CLOEXEC);
|
|
|
|
int err;
|
|
|
|
struct sync_pt *pt;
|
|
|
|
struct sync_file *sync_file;
|
|
|
|
struct sw_sync_create_fence_data data;
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
|
|
|
|
err = -EFAULT;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
pt = sync_pt_create(obj, sizeof(*pt), data.value);
|
|
|
|
if (!pt) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
sync_file = sync_file_create(&pt->base);
|
2016-10-27 04:59:59 +08:00
|
|
|
dma_fence_put(&pt->base);
|
2016-06-01 03:59:05 +08:00
|
|
|
if (!sync_file) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
data.fence = fd;
|
|
|
|
if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
|
|
|
|
fput(sync_file->file);
|
|
|
|
err = -EFAULT;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd_install(fd, sync_file->file);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
put_unused_fd(fd);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
|
|
|
|
{
|
|
|
|
u32 value;
|
|
|
|
|
|
|
|
if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
sync_timeline_signal(obj, value);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long sw_sync_ioctl(struct file *file, unsigned int cmd,
|
|
|
|
unsigned long arg)
|
|
|
|
{
|
|
|
|
struct sync_timeline *obj = file->private_data;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case SW_SYNC_IOC_CREATE_FENCE:
|
|
|
|
return sw_sync_ioctl_create_fence(obj, arg);
|
|
|
|
|
|
|
|
case SW_SYNC_IOC_INC:
|
|
|
|
return sw_sync_ioctl_inc(obj, arg);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -ENOTTY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct file_operations sw_sync_debugfs_fops = {
|
|
|
|
.open = sw_sync_debugfs_open,
|
|
|
|
.release = sw_sync_debugfs_release,
|
|
|
|
.unlocked_ioctl = sw_sync_ioctl,
|
|
|
|
.compat_ioctl = sw_sync_ioctl,
|
|
|
|
};
|