From patchwork Wed Jan 31 02:13:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 194470 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp1634496dyb; Tue, 30 Jan 2024 18:46:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IHVkWkAJ8Fcd40jbeHl7ziKkQgmo/hfp4pWTAyZoXLzYMRaVv9kG9fIomjpyxd55qgSwMfI X-Received: by 2002:a17:906:52d0:b0:a35:9d66:1edc with SMTP id w16-20020a17090652d000b00a359d661edcmr146552ejn.16.1706669207080; Tue, 30 Jan 2024 18:46:47 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706669207; cv=pass; d=google.com; s=arc-20160816; b=WM+KDDjqpvXsSY+fihq0VnovccdfmiKnz5iW7X58QfT2ViZRTpPjROMz0RjvFkba49 8TI43PQsfU211k3zGweTUALZlvVydBHWg4GbmBc6KOFuH648vxBA2Zi+uHo9a7f7LiWv /p0rpKzJA9wAmLAv6PbsA62c+JnMkb54fsZM+mFz+/t5ZHIIbVgNWRXq0EcTjQNEtQbh kuXZsaoZgETpueHROg5xDIaJSeW+8lBEAeJsGtE9m2tQ/DgC5aj4n5s4igjkrXgi/qpp M5+EJQ4fbi1UuYNVqbxw2SNOnklWS75K+IxQba3j28KZH/SCtzrdr6UXltCRAs5VPHMN Ij4w== 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=HS6PtwBEYnMbEyDHfMfm+haurykzLQrGztmH0knaKOk=; fh=np3JW8phJTiFuuv6P7xHsuvWcP/mXBVoIXC5HPxnmLc=; b=Zvser5+Yghp10uhwNyPpCV7tBUaW+BeGPQgCTCkjwSr4flk1BNQ7HY4MkXi3gdNTOF 9Kzc/Ku0wycXBzy9FLJVB522n+AdgPuee8YdjFXiwGOnA40MCR5O8CWYB1qa14NOLU5u 3KCXnh6+7812NDIHKbo7uvP65psagsPXpqCKz8tiHrqV3etwC1Fk5w3VtL8nqmg9xEcF eP3M1w8fZ1SDIYa0/Nx2yAKirQNmyRdDFcUgrKFUlwD82DVD6fMVNx6FXoRxrm06iGo1 wx8oOJ1ZxSiIlsTEHBTIxdU+tHNkYpVvGC/Wgk0T114JoEs1YYmRWDB2bhoidRVa1nX1 0Ztg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=gEa9sXxl; 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-45651-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-45651-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com X-Forwarded-Encrypted: i=1; AJvYcCVy4jeOjdGTjp1kgisS8DCBJnkyy86PGjAVcT4QnuuFWxUk3mgtx4lRZJ/vAj3VZZUSljnWSX0AW1b3men5VdmpVTRmDQ== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id zn6-20020a170906ff0600b00a349982a383si5366799ejb.751.2024.01.30.18.46.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 18:46:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-45651-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=gEa9sXxl; 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-45651-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-45651-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 2AAC01F2A35F 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 AAA973D986; 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="gEa9sXxl" 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 E6134611E; Wed, 31 Jan 2024 02:16:04 +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=1706667368; cv=none; b=UFkv/XmuqldLcTG+m6jCceAv0VBnDDdq+DsA/tHKrGuuDh8a2d5dXfwwDPQpjPdFg0O8vSuYSY+kcKHkehFhns/Nk27+tywd9ayyC2A6yppd8O9YvyR/xyntPZN9i8DAaEy4mAaph4u1TFApBvdyT+ZyHkc4NT+P7+498IzyXVA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706667368; c=relaxed/simple; bh=feJuxpVrTwo1GvHucwKxw2fqfjt5D1lZlxuvTgsLJjU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=V8GKbYaS0ldFRSnQqdtHew5dHFHvFo3nBHdx76EECwhPRO5eICAtLBnzKDCRshqJW0sdzoTdHsiEubGPq1xNPbNPtXd5183XfM6TGtWr0K2/lFV03IWWR/H+Rvfpf2uyo4sYKl7SXaqDE1rpBnpnPQdfJ2BI8F/QJfoHwhelk34= 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=gEa9sXxl; 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=HS6PtwBEYnMbEyDHfMfm+haurykzLQrGztmH0knaKOk=; b=gEa9sXxleRrt2Az8HkSJ5nrhoH HBWRy76hH5pSS8rzaX+986qDARhLdT4BZNGj80EFXtw/EsyLCjgNThmRmZ27aunkulnaqkGyQul5Y ZvV0jXVPG7fq/ROfupXxOG0YVx1xhnnamzFc5wy/QhUP2Tiv6QaA/SrZgb/b+aZOR3iybPcrtFGpd 6qKE5ZMLRfBkBc98QBuX/g4Yv8lBNBvB4NKGuENQNTSNOVTeO8n17IPnpwBpVVt5nBySFjkCYRdCw Z9yXlsl88fRGspIR98ahUw4yAJCL45OWwgxPnbn3DAjOLAzyGhox7aFH8b0nglhuGtxB0qtjD5zXC 7jgo6pXw==; 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-2K; 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 09/29] ntsync: Introduce NTSYNC_IOC_CREATE_EVENT. Date: Tue, 30 Jan 2024 20:13:36 -0600 Message-ID: <20240131021356.10322-10-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: 1789572370977514789 X-GMAIL-MSGID: 1789572370977514789 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 aadf01c65ca0..c719ddd9f6d7 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -18,6 +18,7 @@ enum ntsync_type { NTSYNC_TYPE_SEM, NTSYNC_TYPE_MUTEX, + NTSYNC_TYPE_EVENT, }; struct ntsync_obj { @@ -39,6 +40,10 @@ struct ntsync_obj { __u32 owner; bool ownerdead; } mutex; + struct { + bool manual; + bool signaled; + } event; } u; /* @@ -123,6 +128,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); @@ -172,6 +179,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); @@ -238,6 +249,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. @@ -544,6 +575,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); @@ -665,6 +720,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; } } @@ -857,6 +915,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 3861397c6c2f..b8cf503365ef 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)