From patchwork Wed Jan 31 02:13:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 194461 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp1630057dyb; Tue, 30 Jan 2024 18:32:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IF3UOGsAM+TjGDAsrS7TdxRE96hg0RzXwRL/hDoAH+ey4SsmN41lvOIToJA6X15gR9jMrae X-Received: by 2002:ac8:57d0:0:b0:42a:a9dd:3f36 with SMTP id w16-20020ac857d0000000b0042aa9dd3f36mr543302qta.3.1706668331367; Tue, 30 Jan 2024 18:32:11 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706668331; cv=pass; d=google.com; s=arc-20160816; b=X5LxWH+lJHl6MxCdaRo3L9hUJva/zO/NHZ+ypxIJnGe5EXa1a/UWV+Ig9kuL7B4HY7 KI1Gmr0KBhwC3suRFv5G6vyV6A+OBLYGdwNmV5BcNIcA36xjkG+OsHTvYPhwkJY89CrL iGolQoP1+Kc9hGR34Jjc1tsziBHohYTiyozva0sXB8Uf1ra1k4uzIuzTZbz3YvjTo8f5 f0ZLz2F8glGLipDcVWMEmQOMwViVGW41d8G5pZySP3D/oAJMWcO2QTC8miF2QVQNvTl5 CAQGpX91CRC4n0tXxmCDOF+em68WsfUcyYZo8CoMjgmiyU7YjRlJvXPUi4kar4oac8Rb FXXQ== 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=Of6XvOdno9uHHT4oAURNTaZ9M/wg5z+Nwm47YS4aRDY=; fh=TKcTsE/bC2RrE7HNpSHSRj5eqYeYEd6BSCUTFgCntdI=; b=Jgf5n+/d09LPcMfn3MSaSb6grvY11CvefJgJKsqEQNztU+kk1NdHGqmkOjQVSl+Ijb vtwMDh5jKlDYEKCFMF7NWLXcJLPNJ8yLaNLh3d9SecIatVm+sbIz6fBR7euuEwnnMy7Z vpOrfJXuQk20LKlumXmR+JXVcnGWQjzoLS5neSmQmwqRFtyD5SvYb0QRskXleeoDMB6L GDkMXRNP+Di6l6xamsiGMpeDFLJreKDLr2mP+NHWBBezO5MWAsyrtNblPRPstpst48Ff 20dkC4f2eDAiOJIj5P6BaxdQR98yzD9SazTdv7LR3j3n8cLCOCwguLKZ+NlcJFw66Fw3 uhFA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=Y1oQjZkp; 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-45655-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-45655-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com X-Forwarded-Encrypted: i=1; AJvYcCXWfkOePTy2zYmvBQvRpsXDd7k1NfCxrxO4d0SI6528lFWZXnLL03b+RJmpt3ux1Wqz5gucY4A0hVfSBr6AeyOjesn6MQ== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id v21-20020a05622a145500b0042a3d352f58si7678848qtx.522.2024.01.30.18.32.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 18:32:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-45655-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=Y1oQjZkp; 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-45655-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-45655-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 334AB1C279CF for ; Wed, 31 Jan 2024 02:23:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B04B63D992; Wed, 31 Jan 2024 02:16:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="Y1oQjZkp" 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 966E95681; Wed, 31 Jan 2024 02:16:03 +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=1706667369; cv=none; b=Te4PXsuGnkzvTPspI3fWBf82d1Mt17auMFkTMKPPl7c2Fmyx9QdAFelyhHISUHXsuQLkt5y3vU3u5VmGEzj59xvaGBnyYI42muNVUyH2FcgRew3hd3gEb3TxdbwRZn+orqo7Gjrc2JJ0oEPiB5faLdFz4Q1QwFaqVFLWADvpL6U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706667369; c=relaxed/simple; bh=7AY2HZYGR310aOiJlG/Cr8lCYxSCjMuzd+bM62Jk8lg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ta96n6eI5y86yx6NPLsGapnLPuUja4L8rm44c1E/6XFQD1RAyFYKP9mL694xssshJKWEQgfRKs9UUcV94fddgzqbH52UazinxwAE70mpJ3PDE/y7+qoqfVwtmIXzKmgEazBI7xUSDGsG96zaiyInVBDpKt1blzcenLnGGNNHLEE= 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=Y1oQjZkp; 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=Of6XvOdno9uHHT4oAURNTaZ9M/wg5z+Nwm47YS4aRDY=; b=Y1oQjZkpIVyB0olo8Cvjg7obnV /40XsM0b4bsZUxHER+nJBgp49OCRJiXwpmARuvbfcGXVpdq3y1aO4YAig+DXJ16fDu+cSa6E4CkzM lWuRxu5exTEhOzDgg4dJXwqKuKuksPqxMaTs8QxT8frZHw9E8rMBg7O0XgpdCgb3GoyxkoiRDN9GY aKLGv3JjKsihBgJeWaqR9kywP7omVbyycadb+gl+4D8k4fagPbLlPkwKjeul8qVLy/nQ8HLUgQtPf YlEEAsDlm5vHAX8Ab4whTn/4s+75aE1J0+KLj4FeN+GJdPVxM3fBpVYDNjMfvRxQZyvBLGMJsJXkb iV8IbpBw==; 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 1rV08n-0038Kv-0W; Tue, 30 Jan 2024 20:15:53 -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: [RFC PATCH v2 06/29] ntsync: Introduce NTSYNC_IOC_CREATE_MUTEX. Date: Tue, 30 Jan 2024 20:13:33 -0600 Message-ID: <20240131021356.10322-7-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240131021356.10322-1-zfigura@codeweavers.com> References: <20240131021356.10322-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: 1789571452330132272 X-GMAIL-MSGID: 1789571452330132272 This corresponds to the NT syscall NtCreateMutant(). An NT mutex is recursive, with a 32-bit recursion counter. When acquired via NtWaitForMultipleObjects(), the recursion counter is incremented by one. The OS records the thread which acquired it. However, in order to keep this driver self-contained, the owning thread ID is managed by user-space, and passed as a parameter to all relevant ioctls. The initial owner and recursion count, if any, are specified when the mutex is created. Signed-off-by: Elizabeth Figura --- drivers/misc/ntsync.c | 67 +++++++++++++++++++++++++++++++++++++ include/uapi/linux/ntsync.h | 7 ++++ 2 files changed, 74 insertions(+) diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c index b86d62094344..484219a266ae 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -17,6 +17,7 @@ enum ntsync_type { NTSYNC_TYPE_SEM, + NTSYNC_TYPE_MUTEX, }; struct ntsync_obj { @@ -33,6 +34,10 @@ struct ntsync_obj { __u32 count; __u32 max; } sem; + struct { + __u32 count; + __u32 owner; + } mutex; } u; /* @@ -112,6 +117,10 @@ static bool is_signaled(struct ntsync_obj *obj, __u32 owner) switch (obj->type) { case NTSYNC_TYPE_SEM: return !!obj->u.sem.count; + case NTSYNC_TYPE_MUTEX: + if (obj->u.mutex.owner && obj->u.mutex.owner != owner) + return false; + return obj->u.mutex.count < UINT_MAX; } WARN(1, "bad object type %#x\n", obj->type); @@ -154,6 +163,10 @@ static void try_wake_all(struct ntsync_device *dev, struct ntsync_q *q, case NTSYNC_TYPE_SEM: obj->u.sem.count--; break; + case NTSYNC_TYPE_MUTEX: + obj->u.mutex.count++; + obj->u.mutex.owner = q->owner; + break; } } wake_up_process(q->task); @@ -195,6 +208,28 @@ static void try_wake_any_sem(struct ntsync_obj *sem) } } +static void try_wake_any_mutex(struct ntsync_obj *mutex) +{ + struct ntsync_q_entry *entry; + + lockdep_assert_held(&mutex->lock); + + list_for_each_entry(entry, &mutex->any_waiters, node) { + struct ntsync_q *q = entry->q; + + if (mutex->u.mutex.count == UINT_MAX) + break; + if (mutex->u.mutex.owner && mutex->u.mutex.owner != q->owner) + continue; + + if (atomic_cmpxchg(&q->signaled, -1, entry->index) == -1) { + mutex->u.mutex.count++; + mutex->u.mutex.owner = q->owner; + wake_up_process(q->task); + } + } +} + /* * Actually change the semaphore state, returning -EOVERFLOW if it is made * invalid. @@ -352,6 +387,33 @@ static int ntsync_create_sem(struct ntsync_device *dev, void __user *argp) return put_user(fd, &user_args->sem); } +static int ntsync_create_mutex(struct ntsync_device *dev, void __user *argp) +{ + struct ntsync_mutex_args __user *user_args = argp; + struct ntsync_mutex_args args; + struct ntsync_obj *mutex; + int fd; + + if (copy_from_user(&args, argp, sizeof(args))) + return -EFAULT; + + if (!args.owner != !args.count) + return -EINVAL; + + mutex = ntsync_alloc_obj(dev, NTSYNC_TYPE_MUTEX); + if (!mutex) + return -ENOMEM; + mutex->u.mutex.count = args.count; + mutex->u.mutex.owner = args.owner; + fd = ntsync_obj_get_fd(mutex); + if (fd < 0) { + kfree(mutex); + return fd; + } + + return put_user(fd, &user_args->mutex); +} + static struct ntsync_obj *get_obj(struct ntsync_device *dev, int fd) { struct file *file = fget(fd); @@ -469,6 +531,9 @@ static void try_wake_any_obj(struct ntsync_obj *obj) case NTSYNC_TYPE_SEM: try_wake_any_sem(obj); break; + case NTSYNC_TYPE_MUTEX: + try_wake_any_mutex(obj); + break; } } @@ -661,6 +726,8 @@ static long ntsync_char_ioctl(struct file *file, unsigned int cmd, void __user *argp = (void __user *)parm; switch (cmd) { + case NTSYNC_IOC_CREATE_MUTEX: + return ntsync_create_mutex(dev, argp); case NTSYNC_IOC_CREATE_SEM: return ntsync_create_sem(dev, argp); case NTSYNC_IOC_WAIT_ALL: diff --git a/include/uapi/linux/ntsync.h b/include/uapi/linux/ntsync.h index 524404f6aceb..d68f24fd75a2 100644 --- a/include/uapi/linux/ntsync.h +++ b/include/uapi/linux/ntsync.h @@ -16,6 +16,12 @@ struct ntsync_sem_args { __u32 max; }; +struct ntsync_mutex_args { + __u32 mutex; + __u32 owner; + __u32 count; +}; + struct ntsync_wait_args { __u64 timeout; __u64 objs; @@ -30,6 +36,7 @@ struct ntsync_wait_args { #define NTSYNC_IOC_CREATE_SEM _IOWR('N', 0x80, struct ntsync_sem_args) #define NTSYNC_IOC_WAIT_ANY _IOWR('N', 0x82, struct ntsync_wait_args) #define NTSYNC_IOC_WAIT_ALL _IOWR('N', 0x83, struct ntsync_wait_args) +#define NTSYNC_IOC_CREATE_MUTEX _IOWR('N', 0x84, struct ntsync_sem_args) #define NTSYNC_IOC_SEM_POST _IOWR('N', 0x81, __u32)