From patchwork Mon Feb 19 22:38:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 203334 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp78407dyc; Mon, 19 Feb 2024 14:51:37 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCW1z2qJsLx50QDDMcqV477pmSHgyGail1Ul4J8sjtLUB9hrcHU50F+VesMrDl0xqlrJqNi9QEDmhkrMIRsX0iYAvEsiLA== X-Google-Smtp-Source: AGHT+IFWKTsfcUzA3S5SDra9JthqPqeD8X9jzmEFhGBEzn9BgioDvbkup0YDBcCr3TyCQ2wIh5RM X-Received: by 2002:a17:902:a381:b0:1db:2d8d:914 with SMTP id x1-20020a170902a38100b001db2d8d0914mr11295580pla.53.1708383097532; Mon, 19 Feb 2024 14:51:37 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708383097; cv=pass; d=google.com; s=arc-20160816; b=fChF8zUH5MD4irMkeEUV3O9ZJUnrlYkFrQVSgxrisYDlf5+NUdpJmHIFE6Aho7A0hK hljeR7fr/v2iayXjJuK0wfTgwdBwADrGlcn4ugriM/y4bwp49yLCJVHDTkJSTKcT3jLp HyMcO/GW9e0aV40vtP8w+llaBtoz5aD03pNCCKU8PZesoLYfXZ04PjrGOnZyXYZyqnCI m8SIh4LcXWEbxQALv2Rrsm2HshINuYv8v8pooLU3D7rvzMvXCZExMS6Z+mGzpZwLo7h3 dW/XhChqxT034MStFdpBsdjZZ+hZz63jrmC2NjNdQHxxKHZIT2OB8gq0lEerarQral01 ItqQ== 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=Et3o9k0nFOX8ov+Qt9gKeJTZmLzXmMV1XAJPSQtN7VM=; fh=hR3Xqo6s6FufP5gEw1XgbtLHrluZu7hd5XGp+hLIfiM=; b=UzSNeXMumVzREF4xbLwvI05v9XGVlGH2MI+UUcq+Fo3lUxiyHBwwkwYztE3Vb6oPLi 6tVR39RfXN1yRnkDA5z49BaLZQXT42k2focoBkVvdg3pcU0AIMCBApBWd3ybXHr8AzcT oPjn9I20umiL+cnfrh5Qfov9QM1Su26qAuAmz5wnSNt7o70EbQIjctYncLq3rpQcOY5u GNgG4J5WbUCH3f8uy0xS5yc/yzZ2UzMym3KzuxjxF+rDCZYU8c9u0tkSl6RwLgsZh+dd Tm0pT/7SG3/rGSEZlPZqUE2ke3xuD0fIavd4LDpaYjicd7pLkS23sujsqmIHX4fD0jni 3PlA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=JHSKKv4V; 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-72086-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72086-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. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id f16-20020a170902ce9000b001dbcf653020si4322061plg.193.2024.02.19.14.51.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 14:51:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-72086-ouuuleilei=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=JHSKKv4V; 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-72086-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72086-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 CA1AFB2630A for ; Mon, 19 Feb 2024 22:44:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DA70360DF5; Mon, 19 Feb 2024 22:39:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="JHSKKv4V" 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 0067656754; 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=ugeF5SHRvz4GTXjxNQVlJWqq6zJx+trbYteDgQltKi5rQHvWc+Cwk0Ih6revFGlmVyZQ6u6g1/jkVuIPlYQXXwN57l8i4B3axiXk4xyawTbNQjeGNDKv9bAP3w4QCr2aQPFrehVxIXOcNkNYndsErmifFn4NznB01F6Bn0RLkx8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708382380; c=relaxed/simple; bh=SkM+Po9CiARMjKueDw7X9NLS2+iuSp9SOqoCk41jnV8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hhG+PBet8RnLLoKYI+y/zQmijPcyQA8kBpbCsHg6J0lPAPeziHpZGPxzRG2FSXSw+u2o04EpxuiaglHVS7QtXXC4xWCaYNNafVjqCId9FEmmdG5sNNmeOwXgyTTL4JwzQtvIFDlK4dwNleu7iMwffZeK7iOlCsyEbPFOuMiygII= 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=JHSKKv4V; 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=Et3o9k0nFOX8ov+Qt9gKeJTZmLzXmMV1XAJPSQtN7VM=; b=JHSKKv4V4IkVgv8Nx8TniofYVB mycgXbCopzV0nLkq77n04NKn00pXa0Iq9ac1mfa4HU9IsPW+pYV/+O/GznsFoEthcXapVAkbW6F6/ yb6v9I27N2Z2v+/4BnIUjNFMau3KqmzkbrW6zMbZpWwE41XxgsbA7ekHLGwqFwUjhShBKG8sAfaza jqKlVrKSJp1WcBG0AU0iTlDYX+MBLwAZTDWOkCj9L2dS3CIh22uEy8CTuat93FWykykDF0996mTkL 0AkwAn/Y1L/4hSpvG1+YG2A0yJlSbwfzfT4juA735Jr4Z6lOrX9BctMORgFdiaBX4Bc52DnjFacPj p87TAduw==; 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 1rcCIM-0037Oz-17; Mon, 19 Feb 2024 16:39:30 -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, Randy Dunlap , Elizabeth Figura Subject: [PATCH v2 09/31] ntsync: Introduce NTSYNC_IOC_CREATE_EVENT. Date: Mon, 19 Feb 2024 16:38:11 -0600 Message-ID: <20240219223833.95710-10-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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791369514814200960 X-GMAIL-MSGID: 1791369514814200960 This correspond to the NT syscall NtCreateEvent(). An NT event holds a single bit of state denoting whether it is signaled or unsignaled. There are two types of events: manual-reset and automatic-reset. When an automatic-reset event is acquired via a wait function, its state is reset to unsignaled. Manual-reset events are not affected by wait functions. Whether the event is manual-reset, and its initial state, are specified at creation time. Signed-off-by: Elizabeth Figura --- drivers/misc/ntsync.c | 60 +++++++++++++++++++++++++++++++++++++ include/uapi/linux/ntsync.h | 7 +++++ 2 files changed, 67 insertions(+) diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c index a3466be50c45..17dd47d06e0a 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -25,6 +25,7 @@ enum ntsync_type { NTSYNC_TYPE_SEM, NTSYNC_TYPE_MUTEX, + NTSYNC_TYPE_EVENT, }; /* @@ -59,6 +60,10 @@ struct ntsync_obj { __u32 owner; bool ownerdead; } mutex; + struct { + bool manual; + bool signaled; + } event; } u; /* @@ -143,6 +148,8 @@ static bool is_signaled(struct ntsync_obj *obj, __u32 owner) if (obj->u.mutex.owner && obj->u.mutex.owner != owner) return false; return obj->u.mutex.count < UINT_MAX; + case NTSYNC_TYPE_EVENT: + return obj->u.event.signaled; } WARN(1, "bad object type %#x\n", obj->type); @@ -192,6 +199,10 @@ static void try_wake_all(struct ntsync_device *dev, struct ntsync_q *q, obj->u.mutex.count++; obj->u.mutex.owner = q->owner; break; + case NTSYNC_TYPE_EVENT: + if (!obj->u.event.manual) + obj->u.event.signaled = false; + break; } } wake_up_process(q->task); @@ -258,6 +269,26 @@ static void try_wake_any_mutex(struct ntsync_obj *mutex) } } +static void try_wake_any_event(struct ntsync_obj *event) +{ + struct ntsync_q_entry *entry; + + lockdep_assert_held(&event->lock); + + list_for_each_entry(entry, &event->any_waiters, node) { + struct ntsync_q *q = entry->q; + + if (!event->u.event.signaled) + break; + + if (atomic_cmpxchg(&q->signaled, -1, entry->index) == -1) { + if (!event->u.event.manual) + event->u.event.signaled = false; + wake_up_process(q->task); + } + } +} + /* * Actually change the semaphore state, returning -EOVERFLOW if it is made * invalid. @@ -566,6 +597,30 @@ static int ntsync_create_mutex(struct ntsync_device *dev, void __user *argp) return put_user(fd, &user_args->mutex); } +static int ntsync_create_event(struct ntsync_device *dev, void __user *argp) +{ + struct ntsync_event_args __user *user_args = argp; + struct ntsync_event_args args; + struct ntsync_obj *event; + int fd; + + if (copy_from_user(&args, argp, sizeof(args))) + return -EFAULT; + + event = ntsync_alloc_obj(dev, NTSYNC_TYPE_EVENT); + if (!event) + return -ENOMEM; + event->u.event.manual = args.manual; + event->u.event.signaled = args.signaled; + fd = ntsync_obj_get_fd(event); + if (fd < 0) { + kfree(event); + return fd; + } + + return put_user(fd, &user_args->event); +} + static struct ntsync_obj *get_obj(struct ntsync_device *dev, int fd) { struct file *file = fget(fd); @@ -689,6 +744,9 @@ static void try_wake_any_obj(struct ntsync_obj *obj) case NTSYNC_TYPE_MUTEX: try_wake_any_mutex(obj); break; + case NTSYNC_TYPE_EVENT: + try_wake_any_event(obj); + break; } } @@ -877,6 +935,8 @@ static long ntsync_char_ioctl(struct file *file, unsigned int cmd, void __user *argp = (void __user *)parm; switch (cmd) { + case NTSYNC_IOC_CREATE_EVENT: + return ntsync_create_event(dev, argp); case NTSYNC_IOC_CREATE_MUTEX: return ntsync_create_mutex(dev, argp); case NTSYNC_IOC_CREATE_SEM: diff --git a/include/uapi/linux/ntsync.h b/include/uapi/linux/ntsync.h index 4800941fcbda..040cbdb39033 100644 --- a/include/uapi/linux/ntsync.h +++ b/include/uapi/linux/ntsync.h @@ -22,6 +22,12 @@ struct ntsync_mutex_args { __u32 count; }; +struct ntsync_event_args { + __u32 event; + __u32 manual; + __u32 signaled; +}; + struct ntsync_wait_args { __u64 timeout; __u64 objs; @@ -37,6 +43,7 @@ struct ntsync_wait_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_CREATE_EVENT _IOWR('N', 0x87, struct ntsync_event_args) #define NTSYNC_IOC_SEM_POST _IOWR('N', 0x81, __u32) #define NTSYNC_IOC_MUTEX_UNLOCK _IOWR('N', 0x85, struct ntsync_mutex_args)