]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blobdiff - drivers/staging/android/sw_sync.c
Merge tag 'freevxfs-for-4.8' of git://git.infradead.org/users/hch/freevxfs
[mirror_ubuntu-bionic-kernel.git] / drivers / staging / android / sw_sync.c
index 90e3ee5c299b3aa4c69d3f0d3dc5939027a08828..115c9174705f1b1bd2fe34dac727df54c1b9b75b 100644 (file)
 #include <linux/file.h>
 #include <linux/fs.h>
 #include <linux/uaccess.h>
+#include <linux/slab.h>
 #include <linux/sync_file.h>
 
-#include "uapi/sw_sync.h"
-#include "sync.h"
+#include "sync_debug.h"
+
+#define CREATE_TRACE_POINTS
+#include "trace/sync.h"
+
+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)
+#define SW_SYNC_IOC_INC                        _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
+
+static const struct fence_ops timeline_fence_ops;
+
+static inline struct sync_pt *fence_to_sync_pt(struct fence *fence)
+{
+       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.
+ */
+struct sync_timeline *sync_timeline_create(const char *name)
+{
+       struct sync_timeline *obj;
+
+       obj = kzalloc(sizeof(*obj), GFP_KERNEL);
+       if (!obj)
+               return NULL;
+
+       kref_init(&obj->kref);
+       obj->context = fence_context_alloc(1);
+       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) {
+               if (fence_is_signaled_locked(&pt->base))
+                       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);
+       fence_init(&pt->base, &timeline_fence_ops, &obj->child_list_lock,
+                  obj->context, value);
+       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;
+}
+
+static const char *timeline_fence_get_driver_name(struct fence *fence)
+{
+       return "sw_sync";
+}
+
+static const char *timeline_fence_get_timeline_name(struct fence *fence)
+{
+       struct sync_timeline *parent = fence_parent(fence);
+
+       return parent->name;
+}
+
+static void timeline_fence_release(struct fence *fence)
+{
+       struct sync_pt *pt = fence_to_sync_pt(fence);
+       struct sync_timeline *parent = fence_parent(fence);
+       unsigned long flags;
+
+       spin_lock_irqsave(fence->lock, flags);
+       list_del(&pt->child_list);
+       if (WARN_ON_ONCE(!list_empty(&pt->active_list)))
+               list_del(&pt->active_list);
+       spin_unlock_irqrestore(fence->lock, flags);
+
+       sync_timeline_put(parent);
+       fence_free(fence);
+}
+
+static bool timeline_fence_signaled(struct fence *fence)
+{
+       struct sync_timeline *parent = fence_parent(fence);
+
+       return (fence->seqno > parent->value) ? false : true;
+}
+
+static bool timeline_fence_enable_signaling(struct fence *fence)
+{
+       struct sync_pt *pt = fence_to_sync_pt(fence);
+       struct sync_timeline *parent = fence_parent(fence);
+
+       if (timeline_fence_signaled(fence))
+               return false;
+
+       list_add_tail(&pt->active_list, &parent->active_list_head);
+       return true;
+}
+
+static void timeline_fence_value_str(struct fence *fence,
+                                   char *str, int size)
+{
+       snprintf(str, size, "%d", fence->seqno);
+}
+
+static void timeline_fence_timeline_value_str(struct fence *fence,
+                                            char *str, int size)
+{
+       struct sync_timeline *parent = fence_parent(fence);
+
+       snprintf(str, size, "%d", parent->value);
+}
+
+static const struct fence_ops timeline_fence_ops = {
+       .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,
+       .wait = fence_default_wait,
+       .release = timeline_fence_release,
+       .fence_value_str = timeline_fence_value_str,
+       .timeline_value_str = timeline_fence_timeline_value_str,
+};
 
 /*
  * *WARNING*
@@ -36,7 +242,7 @@ static int sw_sync_debugfs_open(struct inode *inode, struct file *file)
 
        get_task_comm(task_comm, current);
 
-       obj = sync_timeline_create("sw_sync", task_comm);
+       obj = sync_timeline_create(task_comm);
        if (!obj)
                return -ENOMEM;
 
@@ -49,7 +255,9 @@ static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
 {
        struct sync_timeline *obj = file->private_data;
 
-       sync_timeline_destroy(obj);
+       smp_wmb();
+
+       sync_timeline_put(obj);
        return 0;
 }