sw_sync.c 9.3 KB

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