From patchwork Wed Feb 14 23:36:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201208 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp71974dyb; Wed, 14 Feb 2024 16:17:00 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX+bE0IDdiBSJ9hGjFif011lBUAUo0PF6OU7/EHj7j6DEKopvAQyhRPEvEq3JO5hrJGg+LuQ09qI8tOxrN2XKuyKeiM1A== X-Google-Smtp-Source: AGHT+IGABY/JccWv+6xuiZVNmUSjW6YevFgMY5AWRII/oCe7beInt9ymPPFd+Are+2wfaPHUL4tJ X-Received: by 2002:a92:c641:0:b0:363:d9fb:6147 with SMTP id 1-20020a92c641000000b00363d9fb6147mr68610ill.29.1707956220186; Wed, 14 Feb 2024 16:17:00 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707956220; cv=pass; d=google.com; s=arc-20160816; b=E6L5Fjszyvm9d4Mq44LbnZtVWJBC0S16Hu5P4dG4RiUMlaZ5y8m9EPAEi3mFyYyjHZ mAJ3ZqGMWOOkYuQFvigMv+Q9EPSJzd9CKvpI64o+6XzPHmd2QMllPjex8asOxF8DJRI4 Px/yYZPyPYs5V67Fx0ocVgteUP5E/FAjarZ/CTfYdkZH0aTxIg475lHJafTwNgA4Uuda yGwFnBNwCjIDQvwsiSN/+vwPqOqAUYouql6q+EWcuiugajn/ThfAetQPbvigSx0PKTbK C48ffvwkGU4M+cjFh5v0HaIuFcoTvzeahahVJrPeboTg1T9CWEzSghB3xBFF9MReYwpe a/NQ== 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=YtvcNT1nJgwYe+S6BR4d8dQs6sl/vLgKx6v9T6c9bhY=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=qIt/8IDq5UfSK5JPHefbmHEiTNETSOlB2IPtIEV3VR7GXWpqJ/rYiAfjm9cal6adlL XX//QNF/RckjwdqTeRtXNDkjDa3Az80P/iNjaV3WG1OwL8LhHN8V40ImNulfjkh+kLuV aeH+7sArcH9xMUKXpeMZto7oqmm/vB2RIERHMY3LPVaY6jBHZaS7OQ9igRVkw631r3xk RZ7g1VOGOkWwIAt4Vo/IBcshTfOUqdOb0j67U+wYwrSxMQDNBBkG2XCpdu+foQ9i2K4J d/+SDGPHSW3ERy25KIYeGdmAdgAWcuFIxR5MLQ/w2Q6+Ee7qqtZmttKrRmRpREfv4ZWt aP3g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=dW0ckDBB; 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-66138-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66138-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id l23-20020a637017000000b005dc4328dc23si33340pgc.780.2024.02.14.16.16.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 16:17:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66138-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=dW0ckDBB; 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-66138-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66138-ouuuleilei=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 405AAB29DC2 for ; Wed, 14 Feb 2024 23:54:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6F7A3145B37; Wed, 14 Feb 2024 23:53:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="dW0ckDBB" 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 41633145B0E; Wed, 14 Feb 2024 23:53:18 +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=1707954799; cv=none; b=LE2Z8RiXI+T28PzBI0oM7A3CHovvbgQdIv4fFDjFQXY1Pj3QYEF6/Zi0yZ8XEva2+Q4h0yMSimUPmwqHKrvNRMqakYq1aUKgO/fn+IblMVFfPHfKCrv/j0W21qCRolRaIPnoCYbDJzgdQeHzZNaOuk1v78j7jy/88hnsR15apeo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954799; c=relaxed/simple; bh=Ny4Ci7/Bf+MThJse/6wJNKGtKOLPatN9/l94Q+XkAJ4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DmzYnc29pMUrnn4rHckghbDmVdvABpnpkCb0ZsAgmWj0uIA8MeCy2ruisWGzoMYhDQqlBgDA3uBTP2U0APTjiLyx0cbYVJwXjcAmhgUmX2Xsmpfk2g79+gcWmS65Eumtlc09I/QMV2mspJCk/mKctR72XgGmElqCmdY98cdoeCA= 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=dW0ckDBB; 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=YtvcNT1nJgwYe+S6BR4d8dQs6sl/vLgKx6v9T6c9bhY=; b=dW0ckDBBbcBtc8f2rK1xkogp7Q Q86RM2jvUDOP1zqngVfG2qkqx9K+c6bn87KKXHYqFxW3ul3pBAlFIv+wsBnz9uUXAdHi5YSRgyo8E Wg7p/FMFxTVjlup3z4IeN5tWm1KXQ4tLfSropWlea7tx2lv//tMqABxVjGP73kwc8oR9BmjUqSCK4 t/xbwncLLCbJoh3sa2vM8XqPZBd6yvTCBsGbZMnwZRkg+TVm5hYBVgt5bDSAtZDO8pgr7btBT5pFT ACNqpOLj4+fgzoNDmDMLOLjFZ9SUkgptY0F9CpLw5OTq5sjePoQBb85MTyYimpwZIMDxreYVAvo0V 89aXrtvQ==; 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 1raOoW-00GbiL-2e; Wed, 14 Feb 2024 17:37:16 -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_Almeida?= , Wolfram Sang , Arkadiusz Hiler , Peter Zijlstra , Andy Lutomirski , linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Elizabeth Figura Subject: [PATCH 03/31] ntsync: Introduce NTSYNC_IOC_SEM_POST. Date: Wed, 14 Feb 2024 17:36:39 -0600 Message-ID: <20240214233645.9273-4-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240214233645.9273-1-zfigura@codeweavers.com> References: <20240214233645.9273-1-zfigura@codeweavers.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790921901310469519 X-GMAIL-MSGID: 1790921901310469519 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 --- drivers/misc/ntsync.c | 72 +++++++++++++++++++++++++++++++++++-- include/uapi/linux/ntsync.h | 2 ++ 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c index 3ad86d98b82d..01556475f5cc 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -10,7 +10,9 @@ #include #include #include +#include #include +#include #include #define NTSYNC_NAME "ntsync" @@ -20,23 +22,70 @@ enum ntsync_type { }; struct ntsync_obj { + spinlock_t lock; + enum ntsync_type type; + struct file *file; + struct ntsync_device *dev; + + /* The following fields are protected by the object lock. */ union { struct { __u32 count; __u32 max; } sem; } u; - - struct file *file; - struct ntsync_device *dev; }; struct ntsync_device { struct file *file; }; +/* + * Actually change the semaphore state, returning -EOVERFLOW if it is made + * invalid. + */ +static int post_sem_state(struct ntsync_obj *sem, __u32 count) +{ + __u32 sum; + + lockdep_assert_held(&sem->lock); + + if (check_add_overflow(sem->u.sem.count, count, &sum) || + sum > sem->u.sem.max) + return -EOVERFLOW; + + sem->u.sem.count = sum; + return 0; +} + +static int ntsync_sem_post(struct ntsync_obj *sem, void __user *argp) +{ + __u32 __user *user_args = argp; + __u32 prev_count; + __u32 args; + int ret; + + if (copy_from_user(&args, argp, sizeof(args))) + return -EFAULT; + + if (sem->type != NTSYNC_TYPE_SEM) + return -EINVAL; + + spin_lock(&sem->lock); + + prev_count = sem->u.sem.count; + ret = post_sem_state(sem, args); + + spin_unlock(&sem->lock); + + if (!ret && put_user(prev_count, user_args)) + ret = -EFAULT; + + return ret; +} + static int ntsync_obj_release(struct inode *inode, struct file *file) { struct ntsync_obj *obj = file->private_data; @@ -47,9 +96,25 @@ static int ntsync_obj_release(struct inode *inode, struct file *file) return 0; } +static long ntsync_obj_ioctl(struct file *file, unsigned int cmd, + unsigned long parm) +{ + struct ntsync_obj *obj = file->private_data; + void __user *argp = (void __user *)parm; + + switch (cmd) { + case NTSYNC_IOC_SEM_POST: + return ntsync_sem_post(obj, argp); + default: + return -ENOIOCTLCMD; + } +} + static const struct file_operations ntsync_obj_fops = { .owner = THIS_MODULE, .release = ntsync_obj_release, + .unlocked_ioctl = ntsync_obj_ioctl, + .compat_ioctl = compat_ptr_ioctl, .llseek = no_llseek, }; @@ -64,6 +129,7 @@ static struct ntsync_obj *ntsync_alloc_obj(struct ntsync_device *dev, obj->type = type; obj->dev = dev; get_file(dev->file); + spin_lock_init(&obj->lock); return obj; } diff --git a/include/uapi/linux/ntsync.h b/include/uapi/linux/ntsync.h index f38818e7759d..878ec4f0f2e8 100644 --- a/include/uapi/linux/ntsync.h +++ b/include/uapi/linux/ntsync.h @@ -18,4 +18,6 @@ struct ntsync_sem_args { #define NTSYNC_IOC_CREATE_SEM _IOWR('N', 0x80, struct ntsync_sem_args) +#define NTSYNC_IOC_SEM_POST _IOWR('N', 0x81, __u32) + #endif