sw_sync.c 11 KB

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