Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp792605lqh; Thu, 28 Mar 2024 17:28:42 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX611mAEPeYzHkBv2Z7xja1+2Ch/DeZvM6qYgT8BAmE9xAKVcMh72nY3gGX7AijJAseZhoBQ5/r9QoXV08G2CBaS81fHedQ5kw34UMa+w== X-Google-Smtp-Source: AGHT+IEuE09/pq6fJz+xFIH8WjJ5WalTZbhuK+17KAAO7u/VvKJOuh+4RWmxY12u7OCW5WZd++ot X-Received: by 2002:a17:902:db04:b0:1e2:2587:d43f with SMTP id m4-20020a170902db0400b001e22587d43fmr1278403plx.36.1711672122276; Thu, 28 Mar 2024 17:28:42 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711672122; cv=pass; d=google.com; s=arc-20160816; b=z0ScZKL1wmtMfN6DnxFa64AYbT2Fl+jtUKM85kMeosGTor2JQd9mKBd27skNoiMgWo pIhKkb94nTq+3fywHWtC7bCR9gPFhyGMBpHtiwzNMwRRDVgtnqhU8veUDI6Vmn69IDDd ulv5qP8maArZXYCEO0rm70zEzJ9zhKeiXVgAKXtM0oKz831SdNz9iNUoe/FwQ9gva+kH 2KX2mLgClxcxOP8bBAWnGpO5s3kjl8mZ3s0+a5+2nSkzoDJEKg6OHnEwlYcqecZlrDCa +UN22y/izH+xdZJGAIuAqLD6bqwy9/zbcidnzy7OQhOI7Y/rXFdA5OgERNS13kNesn5K vKUQ== 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=6vzyn21Yvsf91uCotryrtUtNtD1ulA2lMPCtNFDbJYU=; fh=hR3Xqo6s6FufP5gEw1XgbtLHrluZu7hd5XGp+hLIfiM=; b=MmjYG0IOJHCFC/D5e3NRRP+DVYFOEgD1nYRUvuwx8/BhYHKLGeg4Gzt+SKg9jIT4T9 MJcnCmw91D1TQJmqh3xw1m3TZ1O5sRyl9dV3nSx/GIU00DkuWjGn4aZFaNs9tkT0F4bo cCxD89l0CynwbSkW66oIjPp/szaq5FbUQxjJYFf52ftE+9itMy4rypXBiWt++sdwdsWI vEmURccxTI80RwCMxYFnHAWvJrESGQb9r+3vWEEX7LlMKebZ7OkCNAs3NSkKIalIFoOU A3lfsh3drWr++T3tH2Zvxm8hwHDXgRMKT8b0/QbZVvArVPXbzPOsIDsEoTzFiscoR9l+ Q2qQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=I2WlmheF; 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-123899-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-123899-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id k16-20020a170902d59000b001e0afe33809si2631616plh.96.2024.03.28.17.28.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 17:28:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-123899-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=I2WlmheF; 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-123899-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-123899-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 79362289668 for ; Fri, 29 Mar 2024 00:28:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 566BE3FBA1; Fri, 29 Mar 2024 00:24:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="I2WlmheF" 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 5EABA3D993; Fri, 29 Mar 2024 00:24:13 +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=1711671856; cv=none; b=Pc/haQ3aRUEZ69qdBqd7hpuyi6/3RjiuhBamPYD1sohfv6p39b8sAn4TaKRCJfWyclDMIyhC7TPUJv7Nz6Faw6bXM3Z2ZpRN+QqFSef7BKstZOglen0MKPVHIL4m9khcz7U5i2PrYMOYg4RQehzvRYJen5xN9upSziq4tiYonD0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711671856; c=relaxed/simple; bh=eaBYY3vipGlHits3xyZ/UNe7G4SCN98gS9e1fNOpl54=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qQG7PuAmkAeflJwaqnosbW1S9LA/2qZPWYOLIA9LfDofuj6K03xsgvbdbEnxhMp4cgNnhu8DQF/VFQdDivKxM6WXYNWnSnVwi16dgXkOEP0sYwmLYY1cre3bamZ2CVrl/qlGqtVsLhBT7kPXXDXXh5Be+Nrwv+sMxnIQYLJyFEo= 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=I2WlmheF; 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=6vzyn21Yvsf91uCotryrtUtNtD1ulA2lMPCtNFDbJYU=; b=I2WlmheFDl+us7ONBp1sLwjFvV KcAyoRf/WKeo1WJeaUcA6rshVnCoEguIeqbSnZrYeDVZiRwuWStMTR/hsRvoGBS0WaxXsV2DH/lbv fiG6lkuoa9Ii0r8OudQ4rwMeP4ZhKybaJwZV/9l/K0POecZp5sfOeIgil8AAXnQlIeZSaSkP544o5 6eHmmmfY1uSSp7i153SkZTisftmG07p+aJ/rbSwKBALbaZCEAd1EHsVUqN6xEzdbcUC74iFyNjqQE TM429cgXfP+U6VWLjj6dVuh0aa2IVf9P51GzSupkaWfJrtlSjUP/W58gQPeWh+jEG8zFZHP6tBkHi 9PhZIhxA==; 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 1rpzlM-000iik-38; Thu, 28 Mar 2024 19:06:29 -0500 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 v3 06/30] ntsync: Introduce NTSYNC_IOC_CREATE_MUTEX. Date: Thu, 28 Mar 2024 19:05:57 -0500 Message-ID: <20240329000621.148791-7-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240329000621.148791-1-zfigura@codeweavers.com> References: <20240329000621.148791-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 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 | 68 +++++++++++++++++++++++++++++++++++++ include/uapi/linux/ntsync.h | 7 ++++ 2 files changed, 75 insertions(+) diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c index e914d626465a..173513aeeacc 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -24,6 +24,7 @@ enum ntsync_type { NTSYNC_TYPE_SEM, + NTSYNC_TYPE_MUTEX, }; /* @@ -53,6 +54,10 @@ struct ntsync_obj { __u32 count; __u32 max; } sem; + struct { + __u32 count; + __u32 owner; + } mutex; } u; /* @@ -132,6 +137,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); @@ -175,6 +184,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); @@ -217,6 +230,29 @@ 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; + int signaled = -1; + + if (mutex->u.mutex.count == UINT_MAX) + break; + if (mutex->u.mutex.owner && mutex->u.mutex.owner != q->owner) + continue; + + if (atomic_try_cmpxchg(&q->signaled, &signaled, entry->index)) { + 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. @@ -376,6 +412,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); @@ -505,6 +568,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; } } @@ -693,6 +759,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 83784d4438a1..cd7841cdba49 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; +}; + #define NTSYNC_WAIT_REALTIME 0x1 struct ntsync_wait_args { @@ -34,6 +40,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) -- 2.43.0