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/file.h>
11 #include <linux/miscdevice.h>
12 #include <linux/module.h>
13 #include <linux/overflow.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16 #include <uapi/linux/ntsync.h>
18 #define NTSYNC_NAME "ntsync"
25 * Individual synchronization primitives are represented by
26 * struct ntsync_obj, and each primitive is backed by a file.
28 * The whole namespace is represented by a struct ntsync_device also
31 * Both rely on struct file for reference counting. Individual
32 * ntsync_obj objects take a reference to the device when created.
38 enum ntsync_type type;
41 struct ntsync_device *dev;
43 /* The following fields are protected by the object lock. */
52 struct ntsync_device {
57 * Actually change the semaphore state, returning -EOVERFLOW if it is made
60 static int post_sem_state(struct ntsync_obj *sem, __u32 count)
64 lockdep_assert_held(&sem->lock);
66 if (check_add_overflow(sem->u.sem.count, count, &sum) ||
70 sem->u.sem.count = sum;
74 static int ntsync_sem_post(struct ntsync_obj *sem, void __user *argp)
76 __u32 __user *user_args = argp;
81 if (copy_from_user(&args, argp, sizeof(args)))
84 if (sem->type != NTSYNC_TYPE_SEM)
87 spin_lock(&sem->lock);
89 prev_count = sem->u.sem.count;
90 ret = post_sem_state(sem, args);
92 spin_unlock(&sem->lock);
94 if (!ret && put_user(prev_count, user_args))
100 static int ntsync_obj_release(struct inode *inode, struct file *file)
102 struct ntsync_obj *obj = file->private_data;
104 fput(obj->dev->file);
110 static long ntsync_obj_ioctl(struct file *file, unsigned int cmd,
113 struct ntsync_obj *obj = file->private_data;
114 void __user *argp = (void __user *)parm;
117 case NTSYNC_IOC_SEM_POST:
118 return ntsync_sem_post(obj, argp);
124 static const struct file_operations ntsync_obj_fops = {
125 .owner = THIS_MODULE,
126 .release = ntsync_obj_release,
127 .unlocked_ioctl = ntsync_obj_ioctl,
128 .compat_ioctl = compat_ptr_ioctl,
131 static struct ntsync_obj *ntsync_alloc_obj(struct ntsync_device *dev,
132 enum ntsync_type type)
134 struct ntsync_obj *obj;
136 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
142 spin_lock_init(&obj->lock);
147 static int ntsync_obj_get_fd(struct ntsync_obj *obj)
152 fd = get_unused_fd_flags(O_CLOEXEC);
155 file = anon_inode_getfile("ntsync", &ntsync_obj_fops, obj, O_RDWR);
158 return PTR_ERR(file);
161 fd_install(fd, file);
166 static int ntsync_create_sem(struct ntsync_device *dev, void __user *argp)
168 struct ntsync_sem_args __user *user_args = argp;
169 struct ntsync_sem_args args;
170 struct ntsync_obj *sem;
173 if (copy_from_user(&args, argp, sizeof(args)))
176 if (args.count > args.max)
179 sem = ntsync_alloc_obj(dev, NTSYNC_TYPE_SEM);
182 sem->u.sem.count = args.count;
183 sem->u.sem.max = args.max;
184 fd = ntsync_obj_get_fd(sem);
190 return put_user(fd, &user_args->sem);
193 static int ntsync_char_open(struct inode *inode, struct file *file)
195 struct ntsync_device *dev;
197 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
201 file->private_data = dev;
203 return nonseekable_open(inode, file);
206 static int ntsync_char_release(struct inode *inode, struct file *file)
208 struct ntsync_device *dev = file->private_data;
215 static long ntsync_char_ioctl(struct file *file, unsigned int cmd,
218 struct ntsync_device *dev = file->private_data;
219 void __user *argp = (void __user *)parm;
222 case NTSYNC_IOC_CREATE_SEM:
223 return ntsync_create_sem(dev, argp);
229 static const struct file_operations ntsync_fops = {
230 .owner = THIS_MODULE,
231 .open = ntsync_char_open,
232 .release = ntsync_char_release,
233 .unlocked_ioctl = ntsync_char_ioctl,
234 .compat_ioctl = compat_ptr_ioctl,
237 static struct miscdevice ntsync_misc = {
238 .minor = MISC_DYNAMIC_MINOR,
240 .fops = &ntsync_fops,
243 module_misc_device(ntsync_misc);
246 MODULE_DESCRIPTION("Kernel driver for NT synchronization primitives");
247 MODULE_LICENSE("GPL");