Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

ntsync: Introduce NTSYNC_IOC_SEM_POST.

This corresponds to the NT syscall NtReleaseSemaphore().

This increases the semaphore's internal counter by the given value, and returns
the previous value. If the counter would overflow the defined maximum, the
function instead fails and returns -EOVERFLOW.

Signed-off-by: Elizabeth Figura <zfigura@codeweavers.com>
Link: https://lore.kernel.org/r/20240329000621.148791-4-zfigura@codeweavers.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Elizabeth Figura and committed by
Greg Kroah-Hartman
dc806bd4 b46271ec

+71 -3
+69 -3
drivers/misc/ntsync.c
··· 10 10 #include <linux/fs.h> 11 11 #include <linux/miscdevice.h> 12 12 #include <linux/module.h> 13 + #include <linux/overflow.h> 13 14 #include <linux/slab.h> 15 + #include <linux/spinlock.h> 14 16 #include <uapi/linux/ntsync.h> 15 17 16 18 #define NTSYNC_NAME "ntsync" ··· 33 31 */ 34 32 35 33 struct ntsync_obj { 34 + spinlock_t lock; 35 + 36 36 enum ntsync_type type; 37 37 38 + struct file *file; 39 + struct ntsync_device *dev; 40 + 41 + /* The following fields are protected by the object lock. */ 38 42 union { 39 43 struct { 40 44 __u32 count; 41 45 __u32 max; 42 46 } sem; 43 47 } u; 44 - 45 - struct file *file; 46 - struct ntsync_device *dev; 47 48 }; 48 49 49 50 struct ntsync_device { 50 51 struct file *file; 51 52 }; 53 + 54 + /* 55 + * Actually change the semaphore state, returning -EOVERFLOW if it is made 56 + * invalid. 57 + */ 58 + static int post_sem_state(struct ntsync_obj *sem, __u32 count) 59 + { 60 + __u32 sum; 61 + 62 + lockdep_assert_held(&sem->lock); 63 + 64 + if (check_add_overflow(sem->u.sem.count, count, &sum) || 65 + sum > sem->u.sem.max) 66 + return -EOVERFLOW; 67 + 68 + sem->u.sem.count = sum; 69 + return 0; 70 + } 71 + 72 + static int ntsync_sem_post(struct ntsync_obj *sem, void __user *argp) 73 + { 74 + __u32 __user *user_args = argp; 75 + __u32 prev_count; 76 + __u32 args; 77 + int ret; 78 + 79 + if (copy_from_user(&args, argp, sizeof(args))) 80 + return -EFAULT; 81 + 82 + if (sem->type != NTSYNC_TYPE_SEM) 83 + return -EINVAL; 84 + 85 + spin_lock(&sem->lock); 86 + 87 + prev_count = sem->u.sem.count; 88 + ret = post_sem_state(sem, args); 89 + 90 + spin_unlock(&sem->lock); 91 + 92 + if (!ret && put_user(prev_count, user_args)) 93 + ret = -EFAULT; 94 + 95 + return ret; 96 + } 52 97 53 98 static int ntsync_obj_release(struct inode *inode, struct file *file) 54 99 { ··· 107 58 return 0; 108 59 } 109 60 61 + static long ntsync_obj_ioctl(struct file *file, unsigned int cmd, 62 + unsigned long parm) 63 + { 64 + struct ntsync_obj *obj = file->private_data; 65 + void __user *argp = (void __user *)parm; 66 + 67 + switch (cmd) { 68 + case NTSYNC_IOC_SEM_POST: 69 + return ntsync_sem_post(obj, argp); 70 + default: 71 + return -ENOIOCTLCMD; 72 + } 73 + } 74 + 110 75 static const struct file_operations ntsync_obj_fops = { 111 76 .owner = THIS_MODULE, 112 77 .release = ntsync_obj_release, 78 + .unlocked_ioctl = ntsync_obj_ioctl, 79 + .compat_ioctl = compat_ptr_ioctl, 113 80 .llseek = no_llseek, 114 81 }; 115 82 ··· 140 75 obj->type = type; 141 76 obj->dev = dev; 142 77 get_file(dev->file); 78 + spin_lock_init(&obj->lock); 143 79 144 80 return obj; 145 81 }
+2
include/uapi/linux/ntsync.h
··· 18 18 19 19 #define NTSYNC_IOC_CREATE_SEM _IOWR('N', 0x80, struct ntsync_sem_args) 20 20 21 + #define NTSYNC_IOC_SEM_POST _IOWR('N', 0x81, __u32) 22 + 21 23 #endif