sw_sync.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. /**
  51. * struct sw_sync_get_deadline - get the deadline hint of a sw_sync fence
  52. * @deadline_ns: absolute time of the deadline
  53. * @pad: must be zero
  54. * @fence_fd: the sw_sync fence fd (in)
  55. *
  56. * Return the earliest deadline set on the fence. The timebase for the
  57. * deadline is CLOCK_MONOTONIC (same as vblank). If there is no deadline
  58. * set on the fence, this ioctl will return -ENOENT.
  59. */
  60. struct sw_sync_get_deadline {
  61. __u64 deadline_ns;
  62. __u32 pad;
  63. __s32 fence_fd;
  64. };
  65. #define SW_SYNC_IOC_MAGIC 'W'
  66. #define SW_SYNC_IOC_CREATE_FENCE _IOWR(SW_SYNC_IOC_MAGIC, 0,\
  67. struct sw_sync_create_fence_data)
  68. #define SW_SYNC_IOC_INC _IOW(SW_SYNC_IOC_MAGIC, 1, __u32)
  69. #define SW_SYNC_GET_DEADLINE _IOWR(SW_SYNC_IOC_MAGIC, 2, \
  70. struct sw_sync_get_deadline)
  71. #define SW_SYNC_HAS_DEADLINE_BIT DMA_FENCE_FLAG_USER_BITS
  72. static const struct dma_fence_ops timeline_fence_ops;
  73. static inline struct sync_pt *dma_fence_to_sync_pt(struct dma_fence *fence)
  74. {
  75. if (fence->ops != &timeline_fence_ops)
  76. return NULL;
  77. return container_of(fence, struct sync_pt, base);
  78. }
  79. /**
  80. * sync_timeline_create() - creates a sync object
  81. * @name: sync_timeline name
  82. *
  83. * Creates a new sync_timeline. Returns the sync_timeline object or NULL in
  84. * case of error.
  85. */
  86. static struct sync_timeline *sync_timeline_create(const char *name)
  87. {
  88. struct sync_timeline *obj;
  89. obj = kzalloc(sizeof(*obj), GFP_KERNEL);
  90. if (!obj)
  91. return NULL;
  92. kref_init(&obj->kref);
  93. obj->context = dma_fence_context_alloc(1);
  94. strscpy(obj->name, name, sizeof(obj->name));
  95. obj->pt_tree = RB_ROOT;
  96. INIT_LIST_HEAD(&obj->pt_list);
  97. spin_lock_init(&obj->lock);
  98. sync_timeline_debug_add(obj);
  99. return obj;
  100. }
  101. static void sync_timeline_free(struct kref *kref)
  102. {
  103. struct sync_timeline *obj =
  104. container_of(kref, struct sync_timeline, kref);
  105. sync_timeline_debug_remove(obj);
  106. kfree(obj);
  107. }
  108. static void sync_timeline_get(struct sync_timeline *obj)
  109. {
  110. kref_get(&obj->kref);
  111. }
  112. static void sync_timeline_put(struct sync_timeline *obj)
  113. {
  114. kref_put(&obj->kref, sync_timeline_free);
  115. }
  116. static const char *timeline_fence_get_driver_name(struct dma_fence *fence)
  117. {
  118. return "sw_sync";
  119. }
  120. static const char *timeline_fence_get_timeline_name(struct dma_fence *fence)
  121. {
  122. struct sync_timeline *parent = dma_fence_parent(fence);
  123. return parent->name;
  124. }
  125. static void timeline_fence_release(struct dma_fence *fence)
  126. {
  127. struct sync_pt *pt = dma_fence_to_sync_pt(fence);
  128. struct sync_timeline *parent = dma_fence_parent(fence);
  129. unsigned long flags;
  130. spin_lock_irqsave(fence->lock, flags);
  131. if (!list_empty(&pt->link)) {
  132. list_del(&pt->link);
  133. rb_erase(&pt->node, &parent->pt_tree);
  134. }
  135. spin_unlock_irqrestore(fence->lock, flags);
  136. sync_timeline_put(parent);
  137. dma_fence_free(fence);
  138. }
  139. static bool timeline_fence_signaled(struct dma_fence *fence)
  140. {
  141. struct sync_timeline *parent = dma_fence_parent(fence);
  142. return !__dma_fence_is_later(fence->seqno, parent->value, fence->ops);
  143. }
  144. static bool timeline_fence_enable_signaling(struct dma_fence *fence)
  145. {
  146. return true;
  147. }
  148. static void timeline_fence_value_str(struct dma_fence *fence,
  149. char *str, int size)
  150. {
  151. snprintf(str, size, "%lld", fence->seqno);
  152. }
  153. static void timeline_fence_timeline_value_str(struct dma_fence *fence,
  154. char *str, int size)
  155. {
  156. struct sync_timeline *parent = dma_fence_parent(fence);
  157. snprintf(str, size, "%d", parent->value);
  158. }
  159. static void timeline_fence_set_deadline(struct dma_fence *fence, ktime_t deadline)
  160. {
  161. struct sync_pt *pt = dma_fence_to_sync_pt(fence);
  162. unsigned long flags;
  163. spin_lock_irqsave(fence->lock, flags);
  164. if (test_bit(SW_SYNC_HAS_DEADLINE_BIT, &fence->flags)) {
  165. if (ktime_before(deadline, pt->deadline))
  166. pt->deadline = deadline;
  167. } else {
  168. pt->deadline = deadline;
  169. __set_bit(SW_SYNC_HAS_DEADLINE_BIT, &fence->flags);
  170. }
  171. spin_unlock_irqrestore(fence->lock, flags);
  172. }
  173. static const struct dma_fence_ops timeline_fence_ops = {
  174. .get_driver_name = timeline_fence_get_driver_name,
  175. .get_timeline_name = timeline_fence_get_timeline_name,
  176. .enable_signaling = timeline_fence_enable_signaling,
  177. .signaled = timeline_fence_signaled,
  178. .release = timeline_fence_release,
  179. .fence_value_str = timeline_fence_value_str,
  180. .timeline_value_str = timeline_fence_timeline_value_str,
  181. .set_deadline = timeline_fence_set_deadline,
  182. };
  183. /**
  184. * sync_timeline_signal() - signal a status change on a sync_timeline
  185. * @obj: sync_timeline to signal
  186. * @inc: num to increment on timeline->value
  187. *
  188. * A sync implementation should call this any time one of it's fences
  189. * has signaled or has an error condition.
  190. */
  191. static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
  192. {
  193. LIST_HEAD(signalled);
  194. struct sync_pt *pt, *next;
  195. trace_sync_timeline(obj);
  196. spin_lock_irq(&obj->lock);
  197. obj->value += inc;
  198. list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
  199. if (!timeline_fence_signaled(&pt->base))
  200. break;
  201. dma_fence_get(&pt->base);
  202. list_move_tail(&pt->link, &signalled);
  203. rb_erase(&pt->node, &obj->pt_tree);
  204. dma_fence_signal_locked(&pt->base);
  205. }
  206. spin_unlock_irq(&obj->lock);
  207. list_for_each_entry_safe(pt, next, &signalled, link) {
  208. list_del_init(&pt->link);
  209. dma_fence_put(&pt->base);
  210. }
  211. }
  212. /**
  213. * sync_pt_create() - creates a sync pt
  214. * @obj: parent sync_timeline
  215. * @value: value of the fence
  216. *
  217. * Creates a new sync_pt (fence) as a child of @parent. @size bytes will be
  218. * allocated allowing for implementation specific data to be kept after
  219. * the generic sync_timeline struct. Returns the sync_pt object or
  220. * NULL in case of error.
  221. */
  222. static struct sync_pt *sync_pt_create(struct sync_timeline *obj,
  223. unsigned int value)
  224. {
  225. struct sync_pt *pt;
  226. pt = kzalloc(sizeof(*pt), GFP_KERNEL);
  227. if (!pt)
  228. return NULL;
  229. sync_timeline_get(obj);
  230. dma_fence_init(&pt->base, &timeline_fence_ops, &obj->lock,
  231. obj->context, value);
  232. INIT_LIST_HEAD(&pt->link);
  233. spin_lock_irq(&obj->lock);
  234. if (!dma_fence_is_signaled_locked(&pt->base)) {
  235. struct rb_node **p = &obj->pt_tree.rb_node;
  236. struct rb_node *parent = NULL;
  237. while (*p) {
  238. struct sync_pt *other;
  239. int cmp;
  240. parent = *p;
  241. other = rb_entry(parent, typeof(*pt), node);
  242. cmp = value - other->base.seqno;
  243. if (cmp > 0) {
  244. p = &parent->rb_right;
  245. } else if (cmp < 0) {
  246. p = &parent->rb_left;
  247. } else {
  248. if (dma_fence_get_rcu(&other->base)) {
  249. sync_timeline_put(obj);
  250. kfree(pt);
  251. pt = other;
  252. goto unlock;
  253. }
  254. p = &parent->rb_left;
  255. }
  256. }
  257. rb_link_node(&pt->node, parent, p);
  258. rb_insert_color(&pt->node, &obj->pt_tree);
  259. parent = rb_next(&pt->node);
  260. list_add_tail(&pt->link,
  261. parent ? &rb_entry(parent, typeof(*pt), node)->link : &obj->pt_list);
  262. }
  263. unlock:
  264. spin_unlock_irq(&obj->lock);
  265. return pt;
  266. }
  267. /*
  268. * *WARNING*
  269. *
  270. * improper use of this can result in deadlocking kernel drivers from userspace.
  271. */
  272. /* opening sw_sync create a new sync obj */
  273. static int sw_sync_debugfs_open(struct inode *inode, struct file *file)
  274. {
  275. struct sync_timeline *obj;
  276. char task_comm[TASK_COMM_LEN];
  277. get_task_comm(task_comm, current);
  278. obj = sync_timeline_create(task_comm);
  279. if (!obj)
  280. return -ENOMEM;
  281. file->private_data = obj;
  282. return 0;
  283. }
  284. static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
  285. {
  286. struct sync_timeline *obj = file->private_data;
  287. struct sync_pt *pt, *next;
  288. spin_lock_irq(&obj->lock);
  289. list_for_each_entry_safe(pt, next, &obj->pt_list, link) {
  290. dma_fence_set_error(&pt->base, -ENOENT);
  291. dma_fence_signal_locked(&pt->base);
  292. }
  293. spin_unlock_irq(&obj->lock);
  294. sync_timeline_put(obj);
  295. return 0;
  296. }
  297. static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
  298. unsigned long arg)
  299. {
  300. int fd = get_unused_fd_flags(O_CLOEXEC);
  301. int err;
  302. struct sync_pt *pt;
  303. struct sync_file *sync_file;
  304. struct sw_sync_create_fence_data data;
  305. if (fd < 0)
  306. return fd;
  307. if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
  308. err = -EFAULT;
  309. goto err;
  310. }
  311. pt = sync_pt_create(obj, data.value);
  312. if (!pt) {
  313. err = -ENOMEM;
  314. goto err;
  315. }
  316. sync_file = sync_file_create(&pt->base);
  317. dma_fence_put(&pt->base);
  318. if (!sync_file) {
  319. err = -ENOMEM;
  320. goto err;
  321. }
  322. data.fence = fd;
  323. if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
  324. fput(sync_file->file);
  325. err = -EFAULT;
  326. goto err;
  327. }
  328. fd_install(fd, sync_file->file);
  329. return 0;
  330. err:
  331. put_unused_fd(fd);
  332. return err;
  333. }
  334. static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
  335. {
  336. u32 value;
  337. if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
  338. return -EFAULT;
  339. while (value > INT_MAX) {
  340. sync_timeline_signal(obj, INT_MAX);
  341. value -= INT_MAX;
  342. }
  343. sync_timeline_signal(obj, value);
  344. return 0;
  345. }
  346. static int sw_sync_ioctl_get_deadline(struct sync_timeline *obj, unsigned long arg)
  347. {
  348. struct sw_sync_get_deadline data;
  349. struct dma_fence *fence;
  350. unsigned long flags;
  351. struct sync_pt *pt;
  352. int ret = 0;
  353. if (copy_from_user(&data, (void __user *)arg, sizeof(data)))
  354. return -EFAULT;
  355. if (data.deadline_ns || data.pad)
  356. return -EINVAL;
  357. fence = sync_file_get_fence(data.fence_fd);
  358. if (!fence)
  359. return -EINVAL;
  360. pt = dma_fence_to_sync_pt(fence);
  361. if (!pt) {
  362. ret = -EINVAL;
  363. goto put_fence;
  364. }
  365. spin_lock_irqsave(fence->lock, flags);
  366. if (!test_bit(SW_SYNC_HAS_DEADLINE_BIT, &fence->flags)) {
  367. ret = -ENOENT;
  368. goto unlock;
  369. }
  370. data.deadline_ns = ktime_to_ns(pt->deadline);
  371. spin_unlock_irqrestore(fence->lock, flags);
  372. dma_fence_put(fence);
  373. if (ret)
  374. return ret;
  375. if (copy_to_user((void __user *)arg, &data, sizeof(data)))
  376. return -EFAULT;
  377. return 0;
  378. unlock:
  379. spin_unlock_irqrestore(fence->lock, flags);
  380. put_fence:
  381. dma_fence_put(fence);
  382. return ret;
  383. }
  384. static long sw_sync_ioctl(struct file *file, unsigned int cmd,
  385. unsigned long arg)
  386. {
  387. struct sync_timeline *obj = file->private_data;
  388. switch (cmd) {
  389. case SW_SYNC_IOC_CREATE_FENCE:
  390. return sw_sync_ioctl_create_fence(obj, arg);
  391. case SW_SYNC_IOC_INC:
  392. return sw_sync_ioctl_inc(obj, arg);
  393. case SW_SYNC_GET_DEADLINE:
  394. return sw_sync_ioctl_get_deadline(obj, arg);
  395. default:
  396. return -ENOTTY;
  397. }
  398. }
  399. const struct file_operations sw_sync_debugfs_fops = {
  400. .open = sw_sync_debugfs_open,
  401. .release = sw_sync_debugfs_release,
  402. .unlocked_ioctl = sw_sync_ioctl,
  403. .compat_ioctl = compat_ptr_ioctl,
  404. };