From patchwork Wed Feb 14 23:36:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201181 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp60184dyb; Wed, 14 Feb 2024 15:53:58 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUsEgc3f3Ka+g3XmJ7Xe8zmBIFGTwKOZRJBN+PHwnb+ydWJIIaoQuuzNyykNg7MaA947/SxKc6bbnXt1dEXCLjXBdHRug== X-Google-Smtp-Source: AGHT+IGmzj1McZNVQPAYTGq5u0YKAw3HjBoW99dae9f5SSZYdxF9s5ZQzTX2VF+SltDTRLMSsO1y X-Received: by 2002:a17:906:f74a:b0:a3d:246a:a83e with SMTP id jp10-20020a170906f74a00b00a3d246aa83emr52595ejb.2.1707954837963; Wed, 14 Feb 2024 15:53:57 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707954837; cv=pass; d=google.com; s=arc-20160816; b=SekMf0zmdmH9Qo5rZZEcYc58PInP6/pm2tKQk+Ox2LToxhFHeQoJzPbw99qmmO4hF/ qvwrddISZPsZigeyQ56wMRKWC6YLFrpCGFDVsDIRsBmVEpMgPTPZ7rdI3dkl4seHulU7 SSXkvU/92qV3l77CAB8wdqgynWh6GSpix60fegdbCs9nPSH1GZllKdez1SB8ZGUqEcp5 n0JkwPELi0qOB2WTTEfMVcHWTrL2UKezTc/Z4B4a+i6fXtc5DflgIMWJpnEMkjdEWIKq V0+AXfCMiAXUFuIgLMhUbZrFKd67ubGMVo7A7O9/L9kpatVZ6y4rjwJqEk466UyDYpxT uLTA== 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=kt09pQnBXw5xt3vMakf1lDIoZzZsWjB7BCRk3DSHX20=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=tC8sF5aCFFWt4lZIkuZvokXzrImThUrCeSeyfIbSZeCL8c5yF0ca5M0WIXcYyzzLol TL2+J1SAh4XT4FI3qrCBzQrCJO5cQhEVqazs+FdQSI8gvEiE6JuiJmaf1v9BFBFS6rdp yYdOLMRu89RN7Jod+4M2hVY6kvKcPtzjIbfpuMCkzUYR525Mg3FLUObLuByFpBNnQcPg MSOgZms5DGbR388V8eHPytFOH9T7Efmvc6/DckCUjYvwgyrtxA2fJstAFbD1/xXYSiCN YNuaFYyzhYfWaH54NnEXC9zDRxNwcDuPMN94cVRaVvTiisofSkFqlv5ir1zDI+Vt9hbw LvtA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b="V9CUr/OG"; 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-66137-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66137-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id g4-20020a1709063b0400b00a37a9478c9fsi16401ejf.294.2024.02.14.15.53.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 15:53:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66137-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b="V9CUr/OG"; 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-66137-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66137-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 6481B1F21594 for ; Wed, 14 Feb 2024 23:53:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9D4DA1420D3; Wed, 14 Feb 2024 23:53:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="V9CUr/OG" 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 44D71145347; Wed, 14 Feb 2024 23:53:15 +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=1707954796; cv=none; b=aN76t5biFXgX5c8togT0urAoh5M8YZOvLDl/Khfk8CoPLG6W/Dpiv9YQFpXWmrEaWtn9lj9ZbczPyelPhXvOKWQbwDev8MsgqN97KxL7sd+ojhe+h51XiJkur+SuqIuIdJD3Wlld2WqeFEpqcOfDjOWDqgpWU3oGQ1CNZSiWyHQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954796; c=relaxed/simple; bh=msZjmLN4VPhTlizAlCaDT2J+XYnlayGvsvKJ3iOzbAg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XKOw7dMRTI173Hf+ZoWvM8lVFNd3mOSFySWfuMV3P4tTRfXVnqD26PX2iVBVkxaZ+QLHunGlgzGZsjB1EbGcC9lNlk69azsUqum4j0oum/INROh3Z/+a3KkxcM4+VihXnYv1wLE3vrNEJqVlsuP7Be/0O0QSDoik5Lq9UzVfG48= 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=V9CUr/OG; 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=kt09pQnBXw5xt3vMakf1lDIoZzZsWjB7BCRk3DSHX20=; b=V9CUr/OGga0pmF4pEnGOh/5CT8 PqcGY8HOWvtewMqsQS2zEHV0Bu8bCFZ95FLnC4tqaCPIv4kDChhp1bKRwn3PGV8aBTrUpYEDPQYqV 51Go6+f43NsfjFBHwvhA/JhyKTGk4FhIzQ0AAnN0IBLuSYcGA5YlFEUpEpv+nxOSQiIttEQgGU+fd KGqS5fcYMS+s4c8MMUMy5uKufNprRrM0VNinOMV0DWh5goZsfbcwRaD8H1S8Ql4Sh0V8XS2YVFlsK 4Wvg1aT3Gn+vMLa+tXS2Cz7HRqSKFTyI9s20JplfO6EiFUwF5S71jNt5hgeaXtVTHHy9NGJuN4tAT bGpDWa3Q==; 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 1raOoZ-00GbiL-0T; Wed, 14 Feb 2024 17:37:19 -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 06/31] ntsync: Introduce NTSYNC_IOC_CREATE_MUTEX. Date: Wed, 14 Feb 2024 17:36:42 -0600 Message-ID: <20240214233645.9273-7-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: 1790920451917984894 X-GMAIL-MSGID: 1790920451917984894 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 e173774eea52..5b7f93709587 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -24,6 +24,7 @@ enum ntsync_type { NTSYNC_TYPE_SEM, + NTSYNC_TYPE_MUTEX, }; struct ntsync_obj { @@ -40,6 +41,10 @@ struct ntsync_obj { __u32 count; __u32 max; } sem; + struct { + __u32 count; + __u32 owner; + } mutex; } u; /* @@ -119,6 +124,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); @@ -161,6 +170,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); @@ -202,6 +215,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. @@ -361,6 +396,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); @@ -480,6 +542,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; } } @@ -668,6 +733,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)