sw_sync.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 fence_ops timeline_fence_ops;
  64. static inline struct sync_pt *fence_to_sync_pt(struct 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 = fence_context_alloc(1);
  85. strlcpy(obj->name, name, sizeof(obj->name));
  86. INIT_LIST_HEAD(&obj->child_list_head);
  87. INIT_LIST_HEAD(&obj->active_list_head);
  88. spin_lock_init(&obj->child_list_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. /**
  108. * sync_timeline_signal() - signal a status change on a sync_timeline
  109. * @obj: sync_timeline to signal
  110. * @inc: num to increment on timeline->value
  111. *
  112. * A sync implementation should call this any time one of it's fences
  113. * has signaled or has an error condition.
  114. */
  115. static void sync_timeline_signal(struct sync_timeline *obj, unsigned int inc)
  116. {
  117. unsigned long flags;
  118. struct sync_pt *pt, *next;
  119. trace_sync_timeline(obj);
  120. spin_lock_irqsave(&obj->child_list_lock, flags);
  121. obj->value += inc;
  122. list_for_each_entry_safe(pt, next, &obj->active_list_head,
  123. active_list) {
  124. if (fence_is_signaled_locked(&pt->base))
  125. list_del_init(&pt->active_list);
  126. }
  127. spin_unlock_irqrestore(&obj->child_list_lock, flags);
  128. }
  129. /**
  130. * sync_pt_create() - creates a sync pt
  131. * @parent: fence's parent sync_timeline
  132. * @size: size to allocate for this pt
  133. * @inc: value of the fence
  134. *
  135. * Creates a new sync_pt as a child of @parent. @size bytes will be
  136. * allocated allowing for implementation specific data to be kept after
  137. * the generic sync_timeline struct. Returns the sync_pt object or
  138. * NULL in case of error.
  139. */
  140. static struct sync_pt *sync_pt_create(struct sync_timeline *obj, int size,
  141. unsigned int value)
  142. {
  143. unsigned long flags;
  144. struct sync_pt *pt;
  145. if (size < sizeof(*pt))
  146. return NULL;
  147. pt = kzalloc(size, GFP_KERNEL);
  148. if (!pt)
  149. return NULL;
  150. spin_lock_irqsave(&obj->child_list_lock, flags);
  151. sync_timeline_get(obj);
  152. fence_init(&pt->base, &timeline_fence_ops, &obj->child_list_lock,
  153. obj->context, value);
  154. list_add_tail(&pt->child_list, &obj->child_list_head);
  155. INIT_LIST_HEAD(&pt->active_list);
  156. spin_unlock_irqrestore(&obj->child_list_lock, flags);
  157. return pt;
  158. }
  159. static const char *timeline_fence_get_driver_name(struct fence *fence)
  160. {
  161. return "sw_sync";
  162. }
  163. static const char *timeline_fence_get_timeline_name(struct fence *fence)
  164. {
  165. struct sync_timeline *parent = fence_parent(fence);
  166. return parent->name;
  167. }
  168. static void timeline_fence_release(struct fence *fence)
  169. {
  170. struct sync_pt *pt = fence_to_sync_pt(fence);
  171. struct sync_timeline *parent = fence_parent(fence);
  172. unsigned long flags;
  173. spin_lock_irqsave(fence->lock, flags);
  174. list_del(&pt->child_list);
  175. if (!list_empty(&pt->active_list))
  176. list_del(&pt->active_list);
  177. spin_unlock_irqrestore(fence->lock, flags);
  178. sync_timeline_put(parent);
  179. fence_free(fence);
  180. }
  181. static bool timeline_fence_signaled(struct fence *fence)
  182. {
  183. struct sync_timeline *parent = fence_parent(fence);
  184. return (fence->seqno > parent->value) ? false : true;
  185. }
  186. static bool timeline_fence_enable_signaling(struct fence *fence)
  187. {
  188. struct sync_pt *pt = fence_to_sync_pt(fence);
  189. struct sync_timeline *parent = fence_parent(fence);
  190. if (timeline_fence_signaled(fence))
  191. return false;
  192. list_add_tail(&pt->active_list, &parent->active_list_head);
  193. return true;
  194. }
  195. static void timeline_fence_value_str(struct fence *fence,
  196. char *str, int size)
  197. {
  198. snprintf(str, size, "%d", fence->seqno);
  199. }
  200. static void timeline_fence_timeline_value_str(struct fence *fence,
  201. char *str, int size)
  202. {
  203. struct sync_timeline *parent = fence_parent(fence);
  204. snprintf(str, size, "%d", parent->value);
  205. }
  206. static const struct fence_ops timeline_fence_ops = {
  207. .get_driver_name = timeline_fence_get_driver_name,
  208. .get_timeline_name = timeline_fence_get_timeline_name,
  209. .enable_signaling = timeline_fence_enable_signaling,
  210. .signaled = timeline_fence_signaled,
  211. .wait = fence_default_wait,
  212. .release = timeline_fence_release,
  213. .fence_value_str = timeline_fence_value_str,
  214. .timeline_value_str = timeline_fence_timeline_value_str,
  215. };
  216. /*
  217. * *WARNING*
  218. *
  219. * improper use of this can result in deadlocking kernel drivers from userspace.
  220. */
  221. /* opening sw_sync create a new sync obj */
  222. static int sw_sync_debugfs_open(struct inode *inode, struct file *file)
  223. {
  224. struct sync_timeline *obj;
  225. char task_comm[TASK_COMM_LEN];
  226. get_task_comm(task_comm, current);
  227. obj = sync_timeline_create(task_comm);
  228. if (!obj)
  229. return -ENOMEM;
  230. file->private_data = obj;
  231. return 0;
  232. }
  233. static int sw_sync_debugfs_release(struct inode *inode, struct file *file)
  234. {
  235. struct sync_timeline *obj = file->private_data;
  236. smp_wmb();
  237. sync_timeline_put(obj);
  238. return 0;
  239. }
  240. static long sw_sync_ioctl_create_fence(struct sync_timeline *obj,
  241. unsigned long arg)
  242. {
  243. int fd = get_unused_fd_flags(O_CLOEXEC);
  244. int err;
  245. struct sync_pt *pt;
  246. struct sync_file *sync_file;
  247. struct sw_sync_create_fence_data data;
  248. if (fd < 0)
  249. return fd;
  250. if (copy_from_user(&data, (void __user *)arg, sizeof(data))) {
  251. err = -EFAULT;
  252. goto err;
  253. }
  254. pt = sync_pt_create(obj, sizeof(*pt), data.value);
  255. if (!pt) {
  256. err = -ENOMEM;
  257. goto err;
  258. }
  259. sync_file = sync_file_create(&pt->base);
  260. if (!sync_file) {
  261. fence_put(&pt->base);
  262. err = -ENOMEM;
  263. goto err;
  264. }
  265. data.fence = fd;
  266. if (copy_to_user((void __user *)arg, &data, sizeof(data))) {
  267. fput(sync_file->file);
  268. err = -EFAULT;
  269. goto err;
  270. }
  271. fd_install(fd, sync_file->file);
  272. return 0;
  273. err:
  274. put_unused_fd(fd);
  275. return err;
  276. }
  277. static long sw_sync_ioctl_inc(struct sync_timeline *obj, unsigned long arg)
  278. {
  279. u32 value;
  280. if (copy_from_user(&value, (void __user *)arg, sizeof(value)))
  281. return -EFAULT;
  282. sync_timeline_signal(obj, value);
  283. return 0;
  284. }
  285. static long sw_sync_ioctl(struct file *file, unsigned int cmd,
  286. unsigned long arg)
  287. {
  288. struct sync_timeline *obj = file->private_data;
  289. switch (cmd) {
  290. case SW_SYNC_IOC_CREATE_FENCE:
  291. return sw_sync_ioctl_create_fence(obj, arg);
  292. case SW_SYNC_IOC_INC:
  293. return sw_sync_ioctl_inc(obj, arg);
  294. default:
  295. return -ENOTTY;
  296. }
  297. }
  298. const struct file_operations sw_sync_debugfs_fops = {
  299. .open = sw_sync_debugfs_open,
  300. .release = sw_sync_debugfs_release,
  301. .unlocked_ioctl = sw_sync_ioctl,
  302. .compat_ioctl = sw_sync_ioctl,
  303. };