sw_sync.c 10 KB

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