^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Sync File validation framework
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2012 Google, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/sync_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "sync_debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define CREATE_TRACE_POINTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "sync_trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * SW SYNC validation framework
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * A sync object driver that uses a 32bit counter to coordinate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * synchronization. Useful when there is no hardware primitive backing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * the synchronization.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * To start the framework just open:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * <debugfs>/sync/sw_sync
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * That will create a sync timeline, all fences created under this timeline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * file descriptor will belong to the this timeline.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * The 'sw_sync' file can be opened many times as to create different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * timelines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * Fences can be created with SW_SYNC_IOC_CREATE_FENCE ioctl with struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * sw_sync_create_fence_data as parameter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * To increment the timeline counter, SW_SYNC_IOC_INC ioctl should be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * with the increment as u32. This will update the last signaled value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * from the timeline and signal any fence that has a seqno smaller or equal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * struct sw_sync_create_fence_data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * @value: the seqno to initialise the fence with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * @name: the name of the new sync point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * @fence: return the fd of the new sync_file with the created fence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct sw_sync_create_fence_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) __u32 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) char name[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) __s32 fence; /* fd of new fence */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #define SW_SYNC_IOC_MAGIC 'W'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #define SW_SYNC_IOC_CREATE_FENCE _IOWR(SW_SYNC_IOC_MAGIC, 0,\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct sw_sync_create_fence_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #define SW_SYNC_IOC_INC _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static const struct dma_fence_ops timeline_fence_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static inline struct sync_pt *dma_fence_to_sync_pt(struct dma_fence *fence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (fence->ops != &timeline_fence_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return container_of(fence, struct sync_pt, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * sync_timeline_create() - creates a sync object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * @name: sync_timeline name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * Creates a new sync_timeline. Returns the sync_timeline object or NULL in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * case of error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static struct sync_timeline *sync_timeline_create(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct sync_timeline *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) obj = kzalloc(sizeof(*obj), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (!obj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) kref_init(&obj->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) obj->context = dma_fence_context_alloc(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) strlcpy(obj->name, name, sizeof(obj->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) obj->pt_tree = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) INIT_LIST_HEAD(&obj->pt_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) spin_lock_init(&obj->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) sync_timeline_debug_add(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static void sync_timeline_free(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct sync_timeline *obj =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) container_of(kref, struct sync_timeline, kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) sync_timeline_debug_remove(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) kfree(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) static void sync_timeline_get(struct sync_timeline *obj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) kref_get(&obj->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static void sync_timeline_put(struct sync_timeline *obj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) kref_put(&obj->kref, sync_timeline_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static const char *timeline_fence_get_driver_name(struct dma_fence *fence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return "sw_sync";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static const char *timeline_fence_get_timeline_name(struct dma_fence *fence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct sync_timeline *parent = dma_fence_parent(fence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return parent->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static void timeline_fence_release(struct dma_fence *fence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct sync_pt *pt = dma_fence_to_sync_pt(fence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct sync_timeline *parent = dma_fence_parent(fence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) spin_lock_irqsave(fence->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (!list_empty(&pt->link)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) list_del(&pt->link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) rb_erase(&pt->node, &parent->pt_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) spin_unlock_irqrestore(fence->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) sync_timeline_put(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) dma_fence_free(fence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static bool timeline_fence_signaled(struct dma_fence *fence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct sync_timeline *parent = dma_fence_parent(fence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return !__dma_fence_is_later(fence->seqno, parent->value, fence->ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static bool timeline_fence_enable_signaling(struct dma_fence *fence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static void timeline_fence_value_str(struct dma_fence *fence,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) char *str, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) snprintf(str, size, "%lld", fence->seqno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static void timeline_fence_timeline_value_str(struct dma_fence *fence,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) char *str, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) struct sync_timeline *parent = dma_fence_parent(fence);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) snprintf(str, size, "%d", parent->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static const struct dma_fence_ops timeline_fence_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) .get_driver_name = timeline_fence_get_driver_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) .get_timeline_name = timeline_fence_get_timeline_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) .enable_signaling = timeline_fence_enable_signaling,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) .signaled = timeline_fence_signaled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) .release = timeline_fence_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) .fence_value_str = timeline_fence_value_str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) .timeline_value_str = timeline_fence_timeline_value_str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * sync_timeline_signal() - signal a status change on a sync_timeline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * @obj: sync_timeline to signal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * @inc: num to increment on timeline->value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * A sync implementation should call this any time one of it's fences
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * has signaled or has an error condition.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) struct sync_pt *pt, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) trace_sync_timeline(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) spin_lock_irq(&obj->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) obj->value += inc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (!timeline_fence_signaled(&pt->base))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) list_del_init(&pt->link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) rb_erase(&pt->node, &obj->pt_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * A signal callback may release the last reference to this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * fence, causing it to be freed. That operation has to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * last to avoid a use after free inside this loop, and must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * be after we remove the fence from the timeline in order to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * prevent deadlocking on timeline->lock inside
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * timeline_fence_release().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) dma_fence_signal_locked(&pt->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) spin_unlock_irq(&obj->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * sync_pt_create() - creates a sync pt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * @obj: parent sync_timeline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * @value: value of the fence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) * Creates a new sync_pt (fence) as a child of @parent. @size bytes will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * allocated allowing for implementation specific data to be kept after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * the generic sync_timeline struct. Returns the sync_pt object or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * NULL in case of error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static struct sync_pt *sync_pt_create(struct sync_timeline *obj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) unsigned int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) struct sync_pt *pt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) pt = kzalloc(sizeof(*pt), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (!pt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) sync_timeline_get(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) dma_fence_init(&pt->base, &timeline_fence_ops, &obj->lock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) obj->context, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) INIT_LIST_HEAD(&pt->link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) spin_lock_irq(&obj->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (!dma_fence_is_signaled_locked(&pt->base)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) struct rb_node **p = &obj->pt_tree.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) struct rb_node *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) struct sync_pt *other;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) int cmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) parent = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) other = rb_entry(parent, typeof(*pt), node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) cmp = value - other->base.seqno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (cmp > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) p = &parent->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) } else if (cmp < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) p = &parent->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (dma_fence_get_rcu(&other->base)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) sync_timeline_put(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) kfree(pt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) pt = other;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) p = &parent->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) rb_link_node(&pt->node, parent, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) rb_insert_color(&pt->node, &obj->pt_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) parent = rb_next(&pt->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) list_add_tail(&pt->link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) parent ? &rb_entry(parent, typeof(*pt), node)->link : &obj->pt_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) spin_unlock_irq(&obj->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return pt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * *WARNING*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * improper use of this can result in deadlocking kernel drivers from userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) /* opening sw_sync create a new sync obj */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) static int sw_sync_debugfs_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) struct sync_timeline *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) char task_comm[TASK_COMM_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) get_task_comm(task_comm, current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) obj = sync_timeline_create(task_comm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (!obj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) file->private_data = obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) struct sync_timeline *obj = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) struct sync_pt *pt, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) spin_lock_irq(&obj->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) dma_fence_set_error(&pt->base, -ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) dma_fence_signal_locked(&pt->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) spin_unlock_irq(&obj->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) sync_timeline_put(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) int fd = get_unused_fd_flags(O_CLOEXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) struct sync_pt *pt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) struct sync_file *sync_file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) struct sw_sync_create_fence_data data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) pt = sync_pt_create(obj, data.value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (!pt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) sync_file = sync_file_create(&pt->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) dma_fence_put(&pt->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if (!sync_file) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) data.fence = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) fput(sync_file->file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) fd_install(fd, sync_file->file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) put_unused_fd(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) u32 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) while (value > INT_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) sync_timeline_signal(obj, INT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) value -= INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) sync_timeline_signal(obj, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) static long sw_sync_ioctl(struct file *file, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) struct sync_timeline *obj = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) case SW_SYNC_IOC_CREATE_FENCE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) return sw_sync_ioctl_create_fence(obj, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) case SW_SYNC_IOC_INC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) return sw_sync_ioctl_inc(obj, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) const struct file_operations sw_sync_debugfs_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) .open = sw_sync_debugfs_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) .release = sw_sync_debugfs_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) .unlocked_ioctl = sw_sync_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) .compat_ioctl = compat_ptr_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) static struct miscdevice sw_sync_dev = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) .minor = MISC_DYNAMIC_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) .name = "sw_sync",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) .fops = &sw_sync_debugfs_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) module_misc_device(sw_sync_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) MODULE_LICENSE("GPL v2");