1 // SPDX-License-Identifier: GPL-2.0-only
3 * ntsync.c - Kernel driver for NT synchronization primitives
8 #include <linux/anon_inodes.h>
9 #include <linux/atomic.h>
10 #include <linux/file.h>
12 #include <linux/hrtimer.h>
13 #include <linux/ktime.h>
14 #include <linux/miscdevice.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/overflow.h>
18 #include <linux/sched.h>
19 #include <linux/sched/signal.h>
20 #include <linux/slab.h>
21 #include <linux/spinlock.h>
22 #include <uapi/linux/ntsync.h>
24 #define NTSYNC_NAME "ntsync"
33 * Individual synchronization primitives are represented by
34 * struct ntsync_obj, and each primitive is backed by a file.
36 * The whole namespace is represented by a struct ntsync_device also
39 * Both rely on struct file for reference counting. Individual
40 * ntsync_obj objects take a reference to the device when created.
41 * Wait operations take a reference to each object being waited on for
42 * the duration of the wait.
49 enum ntsync_type type;
52 struct ntsync_device *dev;
54 /* The following fields are protected by the object lock. */
72 * any_waiters is protected by the object lock, but all_waiters is
73 * protected by the device wait_all_lock.
75 struct list_head any_waiters;
76 struct list_head all_waiters;
79 * Hint describing how many tasks are queued on this object in a
82 * Any time we do a wake, we may need to wake "all" waiters as well as
83 * "any" waiters. In order to atomically wake "all" waiters, we must
84 * lock all of the objects, and that means grabbing the wait_all_lock
85 * below (and, due to lock ordering rules, before locking this object).
86 * However, wait-all is a rare operation, and grabbing the wait-all
87 * lock for every wake would create unnecessary contention.
88 * Therefore we first check whether all_hint is zero, and, if it is,
89 * we skip trying to wake "all" waiters.
91 * Since wait requests must originate from user-space threads, we're
92 * limited here by PID_MAX_LIMIT, so there's no risk of overflow.
97 struct ntsync_q_entry {
98 struct list_head node;
100 struct ntsync_obj *obj;
105 struct task_struct *task;
109 * Protected via atomic_try_cmpxchg(). Only the thread that wins the
110 * compare-and-swap may actually change object states and wake this
118 struct ntsync_q_entry entries[];
121 struct ntsync_device {
123 * Wait-all operations must atomically grab all objects, and be totally
124 * ordered with respect to each other and wait-any operations.
125 * If one thread is trying to acquire several objects, another thread
126 * cannot touch the object at the same time.
128 * This device-wide lock is used to serialize wait-for-all
129 * operations, and operations on an object that is involved in a
132 struct mutex wait_all_lock;
138 * Single objects are locked using obj->lock.
140 * Multiple objects are 'locked' while holding dev->wait_all_lock.
141 * In this case however, individual objects are not locked by holding
142 * obj->lock, but by setting obj->dev_locked.
144 * This means that in order to lock a single object, the sequence is slightly
145 * more complicated than usual. Specifically it needs to check obj->dev_locked
146 * after acquiring obj->lock, if set, it needs to drop the lock and acquire
147 * dev->wait_all_lock in order to serialize against the multi-object operation.
150 static void dev_lock_obj(struct ntsync_device *dev, struct ntsync_obj *obj)
152 lockdep_assert_held(&dev->wait_all_lock);
153 lockdep_assert(obj->dev == dev);
154 spin_lock(&obj->lock);
156 * By setting obj->dev_locked inside obj->lock, it is ensured that
157 * anyone holding obj->lock must see the value.
160 spin_unlock(&obj->lock);
163 static void dev_unlock_obj(struct ntsync_device *dev, struct ntsync_obj *obj)
165 lockdep_assert_held(&dev->wait_all_lock);
166 lockdep_assert(obj->dev == dev);
167 spin_lock(&obj->lock);
169 spin_unlock(&obj->lock);
172 static void obj_lock(struct ntsync_obj *obj)
174 struct ntsync_device *dev = obj->dev;
177 spin_lock(&obj->lock);
178 if (likely(!obj->dev_locked))
181 spin_unlock(&obj->lock);
182 mutex_lock(&dev->wait_all_lock);
183 spin_lock(&obj->lock);
185 * obj->dev_locked should be set and released under the same
186 * wait_all_lock section, since we now own this lock, it should
189 lockdep_assert(!obj->dev_locked);
190 spin_unlock(&obj->lock);
191 mutex_unlock(&dev->wait_all_lock);
195 static void obj_unlock(struct ntsync_obj *obj)
197 spin_unlock(&obj->lock);
200 static bool ntsync_lock_obj(struct ntsync_device *dev, struct ntsync_obj *obj)
205 all = atomic_read(&obj->all_hint);
208 mutex_lock(&dev->wait_all_lock);
209 dev_lock_obj(dev, obj);
215 static void ntsync_unlock_obj(struct ntsync_device *dev, struct ntsync_obj *obj, bool all)
218 dev_unlock_obj(dev, obj);
219 mutex_unlock(&dev->wait_all_lock);
225 #define ntsync_assert_held(obj) \
226 lockdep_assert((lockdep_is_held(&(obj)->lock) != LOCK_STATE_NOT_HELD) || \
227 ((lockdep_is_held(&(obj)->dev->wait_all_lock) != LOCK_STATE_NOT_HELD) && \
230 static bool is_signaled(struct ntsync_obj *obj, __u32 owner)
232 ntsync_assert_held(obj);
235 case NTSYNC_TYPE_SEM:
236 return !!obj->u.sem.count;
237 case NTSYNC_TYPE_MUTEX:
238 if (obj->u.mutex.owner && obj->u.mutex.owner != owner)
240 return obj->u.mutex.count < UINT_MAX;
241 case NTSYNC_TYPE_EVENT:
242 return obj->u.event.signaled;
245 WARN(1, "bad object type %#x\n", obj->type);
250 * "locked_obj" is an optional pointer to an object which is already locked and
251 * should not be locked again. This is necessary so that changing an object's
252 * state and waking it can be a single atomic operation.
254 static void try_wake_all(struct ntsync_device *dev, struct ntsync_q *q,
255 struct ntsync_obj *locked_obj)
257 __u32 count = q->count;
258 bool can_wake = true;
262 lockdep_assert_held(&dev->wait_all_lock);
264 lockdep_assert(locked_obj->dev_locked);
266 for (i = 0; i < count; i++) {
267 if (q->entries[i].obj != locked_obj)
268 dev_lock_obj(dev, q->entries[i].obj);
271 for (i = 0; i < count; i++) {
272 if (!is_signaled(q->entries[i].obj, q->owner)) {
278 if (can_wake && atomic_try_cmpxchg(&q->signaled, &signaled, 0)) {
279 for (i = 0; i < count; i++) {
280 struct ntsync_obj *obj = q->entries[i].obj;
283 case NTSYNC_TYPE_SEM:
286 case NTSYNC_TYPE_MUTEX:
287 if (obj->u.mutex.ownerdead)
289 obj->u.mutex.ownerdead = false;
290 obj->u.mutex.count++;
291 obj->u.mutex.owner = q->owner;
293 case NTSYNC_TYPE_EVENT:
294 if (!obj->u.event.manual)
295 obj->u.event.signaled = false;
299 wake_up_process(q->task);
302 for (i = 0; i < count; i++) {
303 if (q->entries[i].obj != locked_obj)
304 dev_unlock_obj(dev, q->entries[i].obj);
308 static void try_wake_all_obj(struct ntsync_device *dev, struct ntsync_obj *obj)
310 struct ntsync_q_entry *entry;
312 lockdep_assert_held(&dev->wait_all_lock);
313 lockdep_assert(obj->dev_locked);
315 list_for_each_entry(entry, &obj->all_waiters, node)
316 try_wake_all(dev, entry->q, obj);
319 static void try_wake_any_sem(struct ntsync_obj *sem)
321 struct ntsync_q_entry *entry;
323 ntsync_assert_held(sem);
324 lockdep_assert(sem->type == NTSYNC_TYPE_SEM);
326 list_for_each_entry(entry, &sem->any_waiters, node) {
327 struct ntsync_q *q = entry->q;
330 if (!sem->u.sem.count)
333 if (atomic_try_cmpxchg(&q->signaled, &signaled, entry->index)) {
335 wake_up_process(q->task);
340 static void try_wake_any_mutex(struct ntsync_obj *mutex)
342 struct ntsync_q_entry *entry;
344 ntsync_assert_held(mutex);
345 lockdep_assert(mutex->type == NTSYNC_TYPE_MUTEX);
347 list_for_each_entry(entry, &mutex->any_waiters, node) {
348 struct ntsync_q *q = entry->q;
351 if (mutex->u.mutex.count == UINT_MAX)
353 if (mutex->u.mutex.owner && mutex->u.mutex.owner != q->owner)
356 if (atomic_try_cmpxchg(&q->signaled, &signaled, entry->index)) {
357 if (mutex->u.mutex.ownerdead)
359 mutex->u.mutex.ownerdead = false;
360 mutex->u.mutex.count++;
361 mutex->u.mutex.owner = q->owner;
362 wake_up_process(q->task);
367 static void try_wake_any_event(struct ntsync_obj *event)
369 struct ntsync_q_entry *entry;
371 ntsync_assert_held(event);
372 lockdep_assert(event->type == NTSYNC_TYPE_EVENT);
374 list_for_each_entry(entry, &event->any_waiters, node) {
375 struct ntsync_q *q = entry->q;
378 if (!event->u.event.signaled)
381 if (atomic_try_cmpxchg(&q->signaled, &signaled, entry->index)) {
382 if (!event->u.event.manual)
383 event->u.event.signaled = false;
384 wake_up_process(q->task);
390 * Actually change the semaphore state, returning -EOVERFLOW if it is made
393 static int release_sem_state(struct ntsync_obj *sem, __u32 count)
397 ntsync_assert_held(sem);
399 if (check_add_overflow(sem->u.sem.count, count, &sum) ||
400 sum > sem->u.sem.max)
403 sem->u.sem.count = sum;
407 static int ntsync_sem_release(struct ntsync_obj *sem, void __user *argp)
409 struct ntsync_device *dev = sem->dev;
410 __u32 __user *user_args = argp;
416 if (copy_from_user(&args, argp, sizeof(args)))
419 if (sem->type != NTSYNC_TYPE_SEM)
422 all = ntsync_lock_obj(dev, sem);
424 prev_count = sem->u.sem.count;
425 ret = release_sem_state(sem, args);
428 try_wake_all_obj(dev, sem);
429 try_wake_any_sem(sem);
432 ntsync_unlock_obj(dev, sem, all);
434 if (!ret && put_user(prev_count, user_args))
441 * Actually change the mutex state, returning -EPERM if not the owner.
443 static int unlock_mutex_state(struct ntsync_obj *mutex,
444 const struct ntsync_mutex_args *args)
446 ntsync_assert_held(mutex);
448 if (mutex->u.mutex.owner != args->owner)
451 if (!--mutex->u.mutex.count)
452 mutex->u.mutex.owner = 0;
456 static int ntsync_mutex_unlock(struct ntsync_obj *mutex, void __user *argp)
458 struct ntsync_mutex_args __user *user_args = argp;
459 struct ntsync_device *dev = mutex->dev;
460 struct ntsync_mutex_args args;
465 if (copy_from_user(&args, argp, sizeof(args)))
470 if (mutex->type != NTSYNC_TYPE_MUTEX)
473 all = ntsync_lock_obj(dev, mutex);
475 prev_count = mutex->u.mutex.count;
476 ret = unlock_mutex_state(mutex, &args);
479 try_wake_all_obj(dev, mutex);
480 try_wake_any_mutex(mutex);
483 ntsync_unlock_obj(dev, mutex, all);
485 if (!ret && put_user(prev_count, &user_args->count))
492 * Actually change the mutex state to mark its owner as dead,
493 * returning -EPERM if not the owner.
495 static int kill_mutex_state(struct ntsync_obj *mutex, __u32 owner)
497 ntsync_assert_held(mutex);
499 if (mutex->u.mutex.owner != owner)
502 mutex->u.mutex.ownerdead = true;
503 mutex->u.mutex.owner = 0;
504 mutex->u.mutex.count = 0;
508 static int ntsync_mutex_kill(struct ntsync_obj *mutex, void __user *argp)
510 struct ntsync_device *dev = mutex->dev;
515 if (get_user(owner, (__u32 __user *)argp))
520 if (mutex->type != NTSYNC_TYPE_MUTEX)
523 all = ntsync_lock_obj(dev, mutex);
525 ret = kill_mutex_state(mutex, owner);
528 try_wake_all_obj(dev, mutex);
529 try_wake_any_mutex(mutex);
532 ntsync_unlock_obj(dev, mutex, all);
537 static int ntsync_event_set(struct ntsync_obj *event, void __user *argp, bool pulse)
539 struct ntsync_device *dev = event->dev;
543 if (event->type != NTSYNC_TYPE_EVENT)
546 all = ntsync_lock_obj(dev, event);
548 prev_state = event->u.event.signaled;
549 event->u.event.signaled = true;
551 try_wake_all_obj(dev, event);
552 try_wake_any_event(event);
554 event->u.event.signaled = false;
556 ntsync_unlock_obj(dev, event, all);
558 if (put_user(prev_state, (__u32 __user *)argp))
564 static int ntsync_event_reset(struct ntsync_obj *event, void __user *argp)
566 struct ntsync_device *dev = event->dev;
570 if (event->type != NTSYNC_TYPE_EVENT)
573 all = ntsync_lock_obj(dev, event);
575 prev_state = event->u.event.signaled;
576 event->u.event.signaled = false;
578 ntsync_unlock_obj(dev, event, all);
580 if (put_user(prev_state, (__u32 __user *)argp))
586 static int ntsync_sem_read(struct ntsync_obj *sem, void __user *argp)
588 struct ntsync_sem_args __user *user_args = argp;
589 struct ntsync_device *dev = sem->dev;
590 struct ntsync_sem_args args;
593 if (sem->type != NTSYNC_TYPE_SEM)
596 all = ntsync_lock_obj(dev, sem);
598 args.count = sem->u.sem.count;
599 args.max = sem->u.sem.max;
601 ntsync_unlock_obj(dev, sem, all);
603 if (copy_to_user(user_args, &args, sizeof(args)))
608 static int ntsync_mutex_read(struct ntsync_obj *mutex, void __user *argp)
610 struct ntsync_mutex_args __user *user_args = argp;
611 struct ntsync_device *dev = mutex->dev;
612 struct ntsync_mutex_args args;
616 if (mutex->type != NTSYNC_TYPE_MUTEX)
619 all = ntsync_lock_obj(dev, mutex);
621 args.count = mutex->u.mutex.count;
622 args.owner = mutex->u.mutex.owner;
623 ret = mutex->u.mutex.ownerdead ? -EOWNERDEAD : 0;
625 ntsync_unlock_obj(dev, mutex, all);
627 if (copy_to_user(user_args, &args, sizeof(args)))
632 static int ntsync_event_read(struct ntsync_obj *event, void __user *argp)
634 struct ntsync_event_args __user *user_args = argp;
635 struct ntsync_device *dev = event->dev;
636 struct ntsync_event_args args;
639 if (event->type != NTSYNC_TYPE_EVENT)
642 all = ntsync_lock_obj(dev, event);
644 args.manual = event->u.event.manual;
645 args.signaled = event->u.event.signaled;
647 ntsync_unlock_obj(dev, event, all);
649 if (copy_to_user(user_args, &args, sizeof(args)))
654 static void ntsync_free_obj(struct ntsync_obj *obj)
656 fput(obj->dev->file);
660 static int ntsync_obj_release(struct inode *inode, struct file *file)
662 ntsync_free_obj(file->private_data);
666 static long ntsync_obj_ioctl(struct file *file, unsigned int cmd,
669 struct ntsync_obj *obj = file->private_data;
670 void __user *argp = (void __user *)parm;
673 case NTSYNC_IOC_SEM_RELEASE:
674 return ntsync_sem_release(obj, argp);
675 case NTSYNC_IOC_SEM_READ:
676 return ntsync_sem_read(obj, argp);
677 case NTSYNC_IOC_MUTEX_UNLOCK:
678 return ntsync_mutex_unlock(obj, argp);
679 case NTSYNC_IOC_MUTEX_KILL:
680 return ntsync_mutex_kill(obj, argp);
681 case NTSYNC_IOC_MUTEX_READ:
682 return ntsync_mutex_read(obj, argp);
683 case NTSYNC_IOC_EVENT_SET:
684 return ntsync_event_set(obj, argp, false);
685 case NTSYNC_IOC_EVENT_RESET:
686 return ntsync_event_reset(obj, argp);
687 case NTSYNC_IOC_EVENT_PULSE:
688 return ntsync_event_set(obj, argp, true);
689 case NTSYNC_IOC_EVENT_READ:
690 return ntsync_event_read(obj, argp);
696 static const struct file_operations ntsync_obj_fops = {
697 .owner = THIS_MODULE,
698 .release = ntsync_obj_release,
699 .unlocked_ioctl = ntsync_obj_ioctl,
700 .compat_ioctl = compat_ptr_ioctl,
703 static struct ntsync_obj *ntsync_alloc_obj(struct ntsync_device *dev,
704 enum ntsync_type type)
706 struct ntsync_obj *obj;
708 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
714 spin_lock_init(&obj->lock);
715 INIT_LIST_HEAD(&obj->any_waiters);
716 INIT_LIST_HEAD(&obj->all_waiters);
717 atomic_set(&obj->all_hint, 0);
722 static int ntsync_obj_get_fd(struct ntsync_obj *obj)
727 fd = get_unused_fd_flags(O_CLOEXEC);
730 file = anon_inode_getfile("ntsync", &ntsync_obj_fops, obj, O_RDWR);
733 return PTR_ERR(file);
736 fd_install(fd, file);
741 static int ntsync_create_sem(struct ntsync_device *dev, void __user *argp)
743 struct ntsync_sem_args args;
744 struct ntsync_obj *sem;
747 if (copy_from_user(&args, argp, sizeof(args)))
750 if (args.count > args.max)
753 sem = ntsync_alloc_obj(dev, NTSYNC_TYPE_SEM);
756 sem->u.sem.count = args.count;
757 sem->u.sem.max = args.max;
758 fd = ntsync_obj_get_fd(sem);
760 ntsync_free_obj(sem);
765 static int ntsync_create_mutex(struct ntsync_device *dev, void __user *argp)
767 struct ntsync_mutex_args args;
768 struct ntsync_obj *mutex;
771 if (copy_from_user(&args, argp, sizeof(args)))
774 if (!args.owner != !args.count)
777 mutex = ntsync_alloc_obj(dev, NTSYNC_TYPE_MUTEX);
780 mutex->u.mutex.count = args.count;
781 mutex->u.mutex.owner = args.owner;
782 fd = ntsync_obj_get_fd(mutex);
784 ntsync_free_obj(mutex);
789 static int ntsync_create_event(struct ntsync_device *dev, void __user *argp)
791 struct ntsync_event_args args;
792 struct ntsync_obj *event;
795 if (copy_from_user(&args, argp, sizeof(args)))
798 event = ntsync_alloc_obj(dev, NTSYNC_TYPE_EVENT);
801 event->u.event.manual = args.manual;
802 event->u.event.signaled = args.signaled;
803 fd = ntsync_obj_get_fd(event);
805 ntsync_free_obj(event);
810 static struct ntsync_obj *get_obj(struct ntsync_device *dev, int fd)
812 struct file *file = fget(fd);
813 struct ntsync_obj *obj;
818 if (file->f_op != &ntsync_obj_fops) {
823 obj = file->private_data;
824 if (obj->dev != dev) {
832 static void put_obj(struct ntsync_obj *obj)
837 static int ntsync_schedule(const struct ntsync_q *q, const struct ntsync_wait_args *args)
839 ktime_t timeout = ns_to_ktime(args->timeout);
840 clockid_t clock = CLOCK_MONOTONIC;
841 ktime_t *timeout_ptr;
844 timeout_ptr = (args->timeout == U64_MAX ? NULL : &timeout);
846 if (args->flags & NTSYNC_WAIT_REALTIME)
847 clock = CLOCK_REALTIME;
850 if (signal_pending(current)) {
855 set_current_state(TASK_INTERRUPTIBLE);
856 if (atomic_read(&q->signaled) != -1) {
860 ret = schedule_hrtimeout_range_clock(timeout_ptr, 0, HRTIMER_MODE_ABS, clock);
862 __set_current_state(TASK_RUNNING);
868 * Allocate and initialize the ntsync_q structure, but do not queue us yet.
870 static int setup_wait(struct ntsync_device *dev,
871 const struct ntsync_wait_args *args, bool all,
872 struct ntsync_q **ret_q)
874 int fds[NTSYNC_MAX_WAIT_COUNT + 1];
875 const __u32 count = args->count;
880 if (args->pad || (args->flags & ~NTSYNC_WAIT_REALTIME))
883 if (args->count > NTSYNC_MAX_WAIT_COUNT)
890 if (copy_from_user(fds, u64_to_user_ptr(args->objs),
891 array_size(count, sizeof(*fds))))
894 fds[count] = args->alert;
896 q = kmalloc(struct_size(q, entries, total_count), GFP_KERNEL);
900 q->owner = args->owner;
901 atomic_set(&q->signaled, -1);
903 q->ownerdead = false;
906 for (i = 0; i < total_count; i++) {
907 struct ntsync_q_entry *entry = &q->entries[i];
908 struct ntsync_obj *obj = get_obj(dev, fds[i]);
914 /* Check that the objects are all distinct. */
915 for (j = 0; j < i; j++) {
916 if (obj == q->entries[j].obj) {
932 for (j = 0; j < i; j++)
933 put_obj(q->entries[j].obj);
938 static void try_wake_any_obj(struct ntsync_obj *obj)
941 case NTSYNC_TYPE_SEM:
942 try_wake_any_sem(obj);
944 case NTSYNC_TYPE_MUTEX:
945 try_wake_any_mutex(obj);
947 case NTSYNC_TYPE_EVENT:
948 try_wake_any_event(obj);
953 static int ntsync_wait_any(struct ntsync_device *dev, void __user *argp)
955 struct ntsync_wait_args args;
956 __u32 i, total_count;
962 if (copy_from_user(&args, argp, sizeof(args)))
965 ret = setup_wait(dev, &args, false, &q);
969 total_count = args.count;
973 /* queue ourselves */
975 for (i = 0; i < total_count; i++) {
976 struct ntsync_q_entry *entry = &q->entries[i];
977 struct ntsync_obj *obj = entry->obj;
979 all = ntsync_lock_obj(dev, obj);
980 list_add_tail(&entry->node, &obj->any_waiters);
981 ntsync_unlock_obj(dev, obj, all);
985 * Check if we are already signaled.
987 * Note that the API requires that normal objects are checked before
988 * the alert event. Hence we queue the alert event last, and check
992 for (i = 0; i < total_count; i++) {
993 struct ntsync_obj *obj = q->entries[i].obj;
995 if (atomic_read(&q->signaled) != -1)
998 all = ntsync_lock_obj(dev, obj);
999 try_wake_any_obj(obj);
1000 ntsync_unlock_obj(dev, obj, all);
1005 ret = ntsync_schedule(q, &args);
1007 /* and finally, unqueue */
1009 for (i = 0; i < total_count; i++) {
1010 struct ntsync_q_entry *entry = &q->entries[i];
1011 struct ntsync_obj *obj = entry->obj;
1013 all = ntsync_lock_obj(dev, obj);
1014 list_del(&entry->node);
1015 ntsync_unlock_obj(dev, obj, all);
1020 signaled = atomic_read(&q->signaled);
1021 if (signaled != -1) {
1022 struct ntsync_wait_args __user *user_args = argp;
1024 /* even if we caught a signal, we need to communicate success */
1025 ret = q->ownerdead ? -EOWNERDEAD : 0;
1027 if (put_user(signaled, &user_args->index))
1037 static int ntsync_wait_all(struct ntsync_device *dev, void __user *argp)
1039 struct ntsync_wait_args args;
1045 if (copy_from_user(&args, argp, sizeof(args)))
1048 ret = setup_wait(dev, &args, true, &q);
1052 /* queue ourselves */
1054 mutex_lock(&dev->wait_all_lock);
1056 for (i = 0; i < args.count; i++) {
1057 struct ntsync_q_entry *entry = &q->entries[i];
1058 struct ntsync_obj *obj = entry->obj;
1060 atomic_inc(&obj->all_hint);
1063 * obj->all_waiters is protected by dev->wait_all_lock rather
1064 * than obj->lock, so there is no need to acquire obj->lock
1067 list_add_tail(&entry->node, &obj->all_waiters);
1070 struct ntsync_q_entry *entry = &q->entries[args.count];
1071 struct ntsync_obj *obj = entry->obj;
1073 dev_lock_obj(dev, obj);
1074 list_add_tail(&entry->node, &obj->any_waiters);
1075 dev_unlock_obj(dev, obj);
1078 /* check if we are already signaled */
1080 try_wake_all(dev, q, NULL);
1082 mutex_unlock(&dev->wait_all_lock);
1085 * Check if the alert event is signaled, making sure to do so only
1086 * after checking if the other objects are signaled.
1090 struct ntsync_obj *obj = q->entries[args.count].obj;
1092 if (atomic_read(&q->signaled) == -1) {
1093 bool all = ntsync_lock_obj(dev, obj);
1094 try_wake_any_obj(obj);
1095 ntsync_unlock_obj(dev, obj, all);
1101 ret = ntsync_schedule(q, &args);
1103 /* and finally, unqueue */
1105 mutex_lock(&dev->wait_all_lock);
1107 for (i = 0; i < args.count; i++) {
1108 struct ntsync_q_entry *entry = &q->entries[i];
1109 struct ntsync_obj *obj = entry->obj;
1112 * obj->all_waiters is protected by dev->wait_all_lock rather
1113 * than obj->lock, so there is no need to acquire it here.
1115 list_del(&entry->node);
1117 atomic_dec(&obj->all_hint);
1122 mutex_unlock(&dev->wait_all_lock);
1125 struct ntsync_q_entry *entry = &q->entries[args.count];
1126 struct ntsync_obj *obj = entry->obj;
1129 all = ntsync_lock_obj(dev, obj);
1130 list_del(&entry->node);
1131 ntsync_unlock_obj(dev, obj, all);
1136 signaled = atomic_read(&q->signaled);
1137 if (signaled != -1) {
1138 struct ntsync_wait_args __user *user_args = argp;
1140 /* even if we caught a signal, we need to communicate success */
1141 ret = q->ownerdead ? -EOWNERDEAD : 0;
1143 if (put_user(signaled, &user_args->index))
1153 static int ntsync_char_open(struct inode *inode, struct file *file)
1155 struct ntsync_device *dev;
1157 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1161 mutex_init(&dev->wait_all_lock);
1163 file->private_data = dev;
1165 return nonseekable_open(inode, file);
1168 static int ntsync_char_release(struct inode *inode, struct file *file)
1170 struct ntsync_device *dev = file->private_data;
1177 static long ntsync_char_ioctl(struct file *file, unsigned int cmd,
1180 struct ntsync_device *dev = file->private_data;
1181 void __user *argp = (void __user *)parm;
1184 case NTSYNC_IOC_CREATE_EVENT:
1185 return ntsync_create_event(dev, argp);
1186 case NTSYNC_IOC_CREATE_MUTEX:
1187 return ntsync_create_mutex(dev, argp);
1188 case NTSYNC_IOC_CREATE_SEM:
1189 return ntsync_create_sem(dev, argp);
1190 case NTSYNC_IOC_WAIT_ALL:
1191 return ntsync_wait_all(dev, argp);
1192 case NTSYNC_IOC_WAIT_ANY:
1193 return ntsync_wait_any(dev, argp);
1195 return -ENOIOCTLCMD;
1199 static const struct file_operations ntsync_fops = {
1200 .owner = THIS_MODULE,
1201 .open = ntsync_char_open,
1202 .release = ntsync_char_release,
1203 .unlocked_ioctl = ntsync_char_ioctl,
1204 .compat_ioctl = compat_ptr_ioctl,
1207 static struct miscdevice ntsync_misc = {
1208 .minor = MISC_DYNAMIC_MINOR,
1209 .name = NTSYNC_NAME,
1210 .fops = &ntsync_fops,
1213 module_misc_device(ntsync_misc);
1216 MODULE_DESCRIPTION("Kernel driver for NT synchronization primitives");
1217 MODULE_LICENSE("GPL");