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_CREATE_SEM.

This corresponds to the NT syscall NtCreateSemaphore().

Semaphores are one of three types of object to be implemented in this driver,
the others being mutexes and events.

An NT semaphore contains a 32-bit counter, and is signaled and can be acquired
when the counter is nonzero. The counter has a maximum value which is specified
at creation time. The initial value of the semaphore is also specified at
creation time. There are no restrictions on the maximum and initial value.

Each object is exposed as an file, to which any number of fds may be opened.
When all fds are closed, the object is deleted.

Objects hold a pointer to the ntsync_device that created them. The device's
reference count is driven by struct file.

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

authored by

Elizabeth Figura and committed by
Greg Kroah-Hartman
b46271ec 25b9cadb

+154
+2
Documentation/userspace-api/ioctl/ioctl-number.rst
··· 174 174 'M' 00-0F drivers/video/fsl-diu-fb.h conflict! 175 175 'N' 00-1F drivers/usb/scanner.h 176 176 'N' 40-7F drivers/block/nvme.c 177 + 'N' 80-8F uapi/linux/ntsync.h NT synchronization primitives 178 + <mailto:wine-devel@winehq.org> 177 179 'O' 00-06 mtd/ubi-user.h UBI 178 180 'P' all linux/soundcard.h conflict! 179 181 'P' 60-6F sound/sscape_ioctl.h conflict!
+131
drivers/misc/ntsync.c
··· 5 5 * Copyright (C) 2024 Elizabeth Figura <zfigura@codeweavers.com> 6 6 */ 7 7 8 + #include <linux/anon_inodes.h> 9 + #include <linux/file.h> 8 10 #include <linux/fs.h> 9 11 #include <linux/miscdevice.h> 10 12 #include <linux/module.h> 13 + #include <linux/slab.h> 14 + #include <uapi/linux/ntsync.h> 11 15 12 16 #define NTSYNC_NAME "ntsync" 13 17 18 + enum ntsync_type { 19 + NTSYNC_TYPE_SEM, 20 + }; 21 + 22 + /* 23 + * Individual synchronization primitives are represented by 24 + * struct ntsync_obj, and each primitive is backed by a file. 25 + * 26 + * The whole namespace is represented by a struct ntsync_device also 27 + * backed by a file. 28 + * 29 + * Both rely on struct file for reference counting. Individual 30 + * ntsync_obj objects take a reference to the device when created. 31 + */ 32 + 33 + struct ntsync_obj { 34 + enum ntsync_type type; 35 + 36 + union { 37 + struct { 38 + __u32 count; 39 + __u32 max; 40 + } sem; 41 + } u; 42 + 43 + struct file *file; 44 + struct ntsync_device *dev; 45 + }; 46 + 47 + struct ntsync_device { 48 + struct file *file; 49 + }; 50 + 51 + static int ntsync_obj_release(struct inode *inode, struct file *file) 52 + { 53 + struct ntsync_obj *obj = file->private_data; 54 + 55 + fput(obj->dev->file); 56 + kfree(obj); 57 + 58 + return 0; 59 + } 60 + 61 + static const struct file_operations ntsync_obj_fops = { 62 + .owner = THIS_MODULE, 63 + .release = ntsync_obj_release, 64 + .llseek = no_llseek, 65 + }; 66 + 67 + static struct ntsync_obj *ntsync_alloc_obj(struct ntsync_device *dev, 68 + enum ntsync_type type) 69 + { 70 + struct ntsync_obj *obj; 71 + 72 + obj = kzalloc(sizeof(*obj), GFP_KERNEL); 73 + if (!obj) 74 + return NULL; 75 + obj->type = type; 76 + obj->dev = dev; 77 + get_file(dev->file); 78 + 79 + return obj; 80 + } 81 + 82 + static int ntsync_obj_get_fd(struct ntsync_obj *obj) 83 + { 84 + struct file *file; 85 + int fd; 86 + 87 + fd = get_unused_fd_flags(O_CLOEXEC); 88 + if (fd < 0) 89 + return fd; 90 + file = anon_inode_getfile("ntsync", &ntsync_obj_fops, obj, O_RDWR); 91 + if (IS_ERR(file)) { 92 + put_unused_fd(fd); 93 + return PTR_ERR(file); 94 + } 95 + obj->file = file; 96 + fd_install(fd, file); 97 + 98 + return fd; 99 + } 100 + 101 + static int ntsync_create_sem(struct ntsync_device *dev, void __user *argp) 102 + { 103 + struct ntsync_sem_args __user *user_args = argp; 104 + struct ntsync_sem_args args; 105 + struct ntsync_obj *sem; 106 + int fd; 107 + 108 + if (copy_from_user(&args, argp, sizeof(args))) 109 + return -EFAULT; 110 + 111 + if (args.count > args.max) 112 + return -EINVAL; 113 + 114 + sem = ntsync_alloc_obj(dev, NTSYNC_TYPE_SEM); 115 + if (!sem) 116 + return -ENOMEM; 117 + sem->u.sem.count = args.count; 118 + sem->u.sem.max = args.max; 119 + fd = ntsync_obj_get_fd(sem); 120 + if (fd < 0) { 121 + kfree(sem); 122 + return fd; 123 + } 124 + 125 + return put_user(fd, &user_args->sem); 126 + } 127 + 14 128 static int ntsync_char_open(struct inode *inode, struct file *file) 15 129 { 130 + struct ntsync_device *dev; 131 + 132 + dev = kzalloc(sizeof(*dev), GFP_KERNEL); 133 + if (!dev) 134 + return -ENOMEM; 135 + 136 + file->private_data = dev; 137 + dev->file = file; 16 138 return nonseekable_open(inode, file); 17 139 } 18 140 19 141 static int ntsync_char_release(struct inode *inode, struct file *file) 20 142 { 143 + struct ntsync_device *dev = file->private_data; 144 + 145 + kfree(dev); 146 + 21 147 return 0; 22 148 } 23 149 24 150 static long ntsync_char_ioctl(struct file *file, unsigned int cmd, 25 151 unsigned long parm) 26 152 { 153 + struct ntsync_device *dev = file->private_data; 154 + void __user *argp = (void __user *)parm; 155 + 27 156 switch (cmd) { 157 + case NTSYNC_IOC_CREATE_SEM: 158 + return ntsync_create_sem(dev, argp); 28 159 default: 29 160 return -ENOIOCTLCMD; 30 161 }
+21
include/uapi/linux/ntsync.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ 2 + /* 3 + * Kernel support for NT synchronization primitive emulation 4 + * 5 + * Copyright (C) 2021-2022 Elizabeth Figura <zfigura@codeweavers.com> 6 + */ 7 + 8 + #ifndef __LINUX_NTSYNC_H 9 + #define __LINUX_NTSYNC_H 10 + 11 + #include <linux/types.h> 12 + 13 + struct ntsync_sem_args { 14 + __u32 sem; 15 + __u32 count; 16 + __u32 max; 17 + }; 18 + 19 + #define NTSYNC_IOC_CREATE_SEM _IOWR('N', 0x80, struct ntsync_sem_args) 20 + 21 + #endif