Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp1404948rdb; Mon, 19 Feb 2024 14:49:27 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWjV7Veg4H6pzw+XBaxGDpDnJ96QoW9devCul7p//UI4jHTKWs+yCtTG0SsMvYwEmFmugtuffSOxnluJVgNrClRj7VW5tC3eKVC6xsr8Q== X-Google-Smtp-Source: AGHT+IHlr3E5UqEtVLMJfrCbGixJGCWPtElhceC8JozCca9xmtNByxiSam/loFXttz3MyDhqvWrg X-Received: by 2002:a05:6a20:2d08:b0:19c:678d:c1c with SMTP id g8-20020a056a202d0800b0019c678d0c1cmr13343958pzl.37.1708382967733; Mon, 19 Feb 2024 14:49:27 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708382967; cv=pass; d=google.com; s=arc-20160816; b=xAJdGomiXfx1rq2y8iupe6+mz0XA6vXc7q9PmU+UPb1WrObHg0cYAaaJcVu4szQIqb iFHMtIUzmR2p9QQUkxOS/Vp/oR/rXZRf3PhFYV0PkgtuOrlPvtno0IPp7fy+p1gK04nk uyNE/0sUaAOJ5fePHjdMDC7o7z6pKvB9VWPxLKeV2EjKf7xPZVCx7hrAk0Sly1q4G2tN xlGIZQV3WWdZhi9nNKfK+caGxBCXPb0HmspLGYO763/9jDVHVOls7c9/d/dg3SoKe+mt Y5kT0oZiBnK3dSRrG5kN7i71mPhUEf2XI2h1eJnNdwhxowBzOaZkmRmXBq+S0YeIBwbf 6f6w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=03V9stfksSmZXXqovL8/FnIbRc/VcLxBEgVsBfrxEb0=; fh=hR3Xqo6s6FufP5gEw1XgbtLHrluZu7hd5XGp+hLIfiM=; b=i3l8B+io5mkUvjFVg9CSheFnw4mo97ZW1rnLZ3nDGB27Z3+aDqX9X4aKjxigmj/8Tx booEMWKX8nphIak33qfVn8TzcixOI4WTgw+pVnb0+67isnC9jGuJkZU1rwLMUaa6HO4t kZK29tBMZW2zL12GCYFZCg00BAHrVE8mkj3BG2kapgiCgFZZXV7sPxJSTxdPmBUq3H0s 3byB1UWF1FfwPCxkw5r0q0ac21jNt4ph5kdTQdANRqHVHRSsa8B+h1YmOUVHrpEMlwoF l1ti+8xlVmetQGxsr4/Waacqhsk4OJbqjW3fKUtfssy2LXiEgk00QIo/k6AC1CPfo8U3 6HWQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=rAsKfTYx; arc=pass (i=1 spf=pass spfdomain=codeweavers.com dkim=pass dkdomain=codeweavers.com dmarc=pass fromdomain=codeweavers.com); spf=pass (google.com: domain of linux-kernel+bounces-72083-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72083-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id mh16-20020a17090b4ad000b0029922583a26si5177756pjb.16.2024.02.19.14.49.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 14:49:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-72083-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=rAsKfTYx; arc=pass (i=1 spf=pass spfdomain=codeweavers.com dkim=pass dkdomain=codeweavers.com dmarc=pass fromdomain=codeweavers.com); spf=pass (google.com: domain of linux-kernel+bounces-72083-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72083-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id D9BE5B25739 for ; Mon, 19 Feb 2024 22:42:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 54A4C5D491; Mon, 19 Feb 2024 22:39:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="rAsKfTYx" Received: from mail.codeweavers.com (mail.codeweavers.com [4.36.192.163]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EB7BC55E7F; Mon, 19 Feb 2024 22:39:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=4.36.192.163 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708382380; cv=none; b=lD2XRTU5l/lpAGZU+eWjFaRYbOBuKM7Rt22peesGxSBHqZd7RRbTDyeT9cq4e6gxnB2kZhmzBEpNfHZWZg3aIBVv6oWpUuIVzHSJa131oiQA2r5lI5vT+KtHyCaS9tRKoCqX/PbhiXHHPMsuJsHKSkaH61Pj8URNoLzrg+sqpto= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708382380; c=relaxed/simple; bh=35iTzxZcFLBKZ/PDaMKqsGnCECNDfJHEeMtcKH4/tsg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Lq8pq/UO8WRgnM97LR0pYTXI73h4RwtF92ugyM88PPebUYMzNGDzXaMzyXzKjO5i1XAVd4zCTHxbAL2EPOD3FcA8PCnc/rajMqfGPRojbAm/ACTWct5hgt+NhEmlYx8sZ1Z/UceKg5WvRvXJ/gWrNSKG661SMXGbY7HTvwgHJVc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=codeweavers.com; spf=pass smtp.mailfrom=codeweavers.com; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b=rAsKfTYx; arc=none smtp.client-ip=4.36.192.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=codeweavers.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=codeweavers.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=codeweavers.com; s=s1; h=Message-ID:Date:Subject:Cc:To:From:Sender; bh=03V9stfksSmZXXqovL8/FnIbRc/VcLxBEgVsBfrxEb0=; b=rAsKfTYxiQ9Ni7q/BLbevPHtE9 Tmh48hutCmC1e/1RrzhiaM4SOTblC9cmu+bp9VdX/yVz9b4yeb00HXDRzKPxOYKbGmtHqV2f8FHvr x69+KvRkYN7C54Nm32gNBStJl1uPpQwuG+h5NggEZYKkpWIP2oqlt/FAiA/zARasq/03Sc1+p3hVS lqV9zGf9+iTGjXLG/5PQPId0cd4VIPmhIOu9tdX4TVNFCCvElaWBKL0WvhQl6ng24qzMq+esChrjE znO/r7yKOWkMewesU/CYVbwjmtpfs4sAg63fhQDhbdlvKsOyfBrRdY8buWsx69XkIyFL+FQvVe0zz 9RcuHaXg==; Received: from cw137ip160.mn.codeweavers.com ([10.69.137.160] helo=camazotz.mn.codeweavers.com) by mail.codeweavers.com with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1rcCIK-0037Oz-1V; Mon, 19 Feb 2024 16:39:28 -0600 From: Elizabeth Figura To: Arnd Bergmann , Greg Kroah-Hartman , Jonathan Corbet , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, wine-devel@winehq.org, =?UTF-8?q?Andr=C3=A9=20Almeida?= , Wolfram Sang , Arkadiusz Hiler , Peter Zijlstra , Andy Lutomirski , linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Randy Dunlap , Elizabeth Figura Subject: [PATCH v2 02/31] ntsync: Introduce NTSYNC_IOC_CREATE_SEM. Date: Mon, 19 Feb 2024 16:38:04 -0600 Message-ID: <20240219223833.95710-3-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240219223833.95710-1-zfigura@codeweavers.com> References: <20240219223833.95710-1-zfigura@codeweavers.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- .../userspace-api/ioctl/ioctl-number.rst | 2 + drivers/misc/ntsync.c | 131 ++++++++++++++++++ include/uapi/linux/ntsync.h | 21 +++ 3 files changed, 154 insertions(+) create mode 100644 include/uapi/linux/ntsync.h diff --git a/Documentation/userspace-api/ioctl/ioctl-number.rst b/Documentation/userspace-api/ioctl/ioctl-number.rst index 457e16f06e04..2f5c6994f042 100644 --- a/Documentation/userspace-api/ioctl/ioctl-number.rst +++ b/Documentation/userspace-api/ioctl/ioctl-number.rst @@ -173,6 +173,8 @@ Code Seq# Include File Comments 'M' 00-0F drivers/video/fsl-diu-fb.h conflict! 'N' 00-1F drivers/usb/scanner.h 'N' 40-7F drivers/block/nvme.c +'N' 80-8F uapi/linux/ntsync.h NT synchronization primitives + 'O' 00-06 mtd/ubi-user.h UBI 'P' all linux/soundcard.h conflict! 'P' 60-6F sound/sscape_ioctl.h conflict! diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c index bd76e653d83e..20158ec148bc 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -5,26 +5,157 @@ * Copyright (C) 2024 Elizabeth Figura */ +#include +#include #include #include #include +#include +#include #define NTSYNC_NAME "ntsync" +enum ntsync_type { + NTSYNC_TYPE_SEM, +}; + +/* + * Individual synchronization primitives are represented by + * struct ntsync_obj, and each primitive is backed by a file. + * + * The whole namespace is represented by a struct ntsync_device also + * backed by a file. + * + * Both rely on struct file for reference counting. Individual + * ntsync_obj objects take a reference to the device when created. + */ + +struct ntsync_obj { + enum ntsync_type type; + + union { + struct { + __u32 count; + __u32 max; + } sem; + } u; + + struct file *file; + struct ntsync_device *dev; +}; + +struct ntsync_device { + struct file *file; +}; + +static int ntsync_obj_release(struct inode *inode, struct file *file) +{ + struct ntsync_obj *obj = file->private_data; + + fput(obj->dev->file); + kfree(obj); + + return 0; +} + +static const struct file_operations ntsync_obj_fops = { + .owner = THIS_MODULE, + .release = ntsync_obj_release, + .llseek = no_llseek, +}; + +static struct ntsync_obj *ntsync_alloc_obj(struct ntsync_device *dev, + enum ntsync_type type) +{ + struct ntsync_obj *obj; + + obj = kzalloc(sizeof(*obj), GFP_KERNEL); + if (!obj) + return NULL; + obj->type = type; + obj->dev = dev; + get_file(dev->file); + + return obj; +} + +static int ntsync_obj_get_fd(struct ntsync_obj *obj) +{ + struct file *file; + int fd; + + fd = get_unused_fd_flags(O_CLOEXEC); + if (fd < 0) + return fd; + file = anon_inode_getfile("ntsync", &ntsync_obj_fops, obj, O_RDWR); + if (IS_ERR(file)) { + put_unused_fd(fd); + return PTR_ERR(file); + } + obj->file = file; + fd_install(fd, file); + + return fd; +} + +static int ntsync_create_sem(struct ntsync_device *dev, void __user *argp) +{ + struct ntsync_sem_args __user *user_args = argp; + struct ntsync_sem_args args; + struct ntsync_obj *sem; + int fd; + + if (copy_from_user(&args, argp, sizeof(args))) + return -EFAULT; + + if (args.count > args.max) + return -EINVAL; + + sem = ntsync_alloc_obj(dev, NTSYNC_TYPE_SEM); + if (!sem) + return -ENOMEM; + sem->u.sem.count = args.count; + sem->u.sem.max = args.max; + fd = ntsync_obj_get_fd(sem); + if (fd < 0) { + kfree(sem); + return fd; + } + + return put_user(fd, &user_args->sem); +} + static int ntsync_char_open(struct inode *inode, struct file *file) { + struct ntsync_device *dev; + + dev = kzalloc(sizeof(*dev), GFP_KERNEL); + if (!dev) + return -ENOMEM; + + file->private_data = dev; + dev->file = file; return nonseekable_open(inode, file); } static int ntsync_char_release(struct inode *inode, struct file *file) { + struct ntsync_device *dev = file->private_data; + + kfree(dev); + return 0; } static long ntsync_char_ioctl(struct file *file, unsigned int cmd, unsigned long parm) { + struct ntsync_device *dev = file->private_data; + void __user *argp = (void __user *)parm; + switch (cmd) { + case NTSYNC_IOC_CREATE_SEM: + return ntsync_create_sem(dev, argp); default: return -ENOIOCTLCMD; } diff --git a/include/uapi/linux/ntsync.h b/include/uapi/linux/ntsync.h new file mode 100644 index 000000000000..6a4867a6c97b --- /dev/null +++ b/include/uapi/linux/ntsync.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* + * Kernel support for NT synchronization primitive emulation + * + * Copyright (C) 2021-2022 Elizabeth Figura + */ + +#ifndef __LINUX_NTSYNC_H +#define __LINUX_NTSYNC_H + +#include + +struct ntsync_sem_args { + __u32 sem; + __u32 count; + __u32 max; +}; + +#define NTSYNC_IOC_CREATE_SEM _IOWR('N', 0x80, struct ntsync_sem_args) + +#endif -- 2.43.0