From patchwork Wed Feb 14 23:36:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201179 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp59903dyb; Wed, 14 Feb 2024 15:53:26 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWtT9rbf5vra1X2454H4rmUx9IKLHIKvbmBTgHwP8+sqm+tTYfSHc4bQBseaE203LSeIwlsTAIFD7XEbaH6WtGoCdbKdA== X-Google-Smtp-Source: AGHT+IFnzV3qEc2AXeenfk/Ch1lw5TmjnOtUKCQ/T337li0MuZVj4BXZlEDvorCju41kf1ge7eMc X-Received: by 2002:a17:902:ce8e:b0:1d9:7121:170e with SMTP id f14-20020a170902ce8e00b001d97121170emr263468plg.35.1707954806582; Wed, 14 Feb 2024 15:53:26 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707954806; cv=pass; d=google.com; s=arc-20160816; b=pytLA6ZkcoulgAvX37Fyerpkzsddd8vwlu9HB/1QMrSWzo6g3858uS7m0zzmjpQjAz nLxbwnyJ5CS1KowP7y6V2LwlgV/xSiQmD7BQ/vHY1gB54BvldJiKpJkOW9IG24Su543g HnH51MQ/v/Q0wjBv7lpkx7wk3dAx+7guNK9rXT2QlfOZtpSVH4Ii59eAPYoGxmVe5BCf 1xCqmYEj13XHKX0EVINNNHnlyAEWW9vKHWkfmbJUTrFwjWo0tSXok9Drglap66ptuNMl 4E5aa7knsdu832/FwiUnSF3uzO18tK1p9d/fimjZvTHCuqEARdmpX9vLRdfiysMUeNRu 7nAA== 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=kCe4tmwPVNV+GsMq6QYMed33sYkMNNuhATDpMCBOUxY=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=SCnlQGRkFf8HpvzOzjN4IyndkPtg02ByGwCWZN6uIbunBUcekpUPapRnh+mn28RhRx fwz4iUNum5c/zL5orbbz7bdcQsNtbv3CwdzvHaunw1kUQpnMqMCer62wYt8iFVqH9o4f x5XoZ+mHL7vjikxKMvZr+WaP/WwuT2vigtvPbPYGdghTSZHyhFs5y283NIGjk8IJN7qO V3aPlKRLl0TzX5X/ylDpr3h8Cz6E6hTpV2UKjyp4n1iXxA/C9yr40M3euDnc9+OqHXhS LnIr6X0MmfZ+Mh9p9FVW12KvFM2wv3QfQ+4ujOvO6APYM4u+nccAPfdXQjgLMbxKSygh bO6g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b="qI/zQAPn"; 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-66135-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66135-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id im9-20020a170902bb0900b001db80f8fbdfsi19000plb.491.2024.02.14.15.53.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 15:53:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66135-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b="qI/zQAPn"; 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-66135-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66135-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 53948287F28 for ; Wed, 14 Feb 2024 23:53:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 58BD0145330; Wed, 14 Feb 2024 23:53:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="qI/zQAPn" 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 C2CF11420B6; Wed, 14 Feb 2024 23:53:09 +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=1707954791; cv=none; b=m1wsIJutpk0hhYogRofd0c0XzmRnLotjfMmtE6Hlx8fc6qoJU0lIpWIl+xS9t5ypIVKUQtqEXRSP4Zj04JvwME7E/XQ75esZFnrLPNM2TncK88U0DviDVxW/sfILh5zzfFmKg45rwu88k6hTMwd4yBRN+KvXVfLD4q4WN4zHZDA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954791; c=relaxed/simple; bh=l13BgqVKFJovtJfczy/tTQhCjfR6OdlM4okW+pv3D0Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aIT/R3CrtaztI7eyjI+6H0QQwUVa/3UILPPIVeekBeyXUMQvSGwUz6NX5CmKO2NfcI6csLeCKNy9WW4W47E16IWqALZVZRFExguYp1IEHVDdfogqU6dNDVPqmSIKDdDd8wbY9DrKTQRtCVFDJNAm2omeODoC/TBLL/XloVu8qkY= 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=qI/zQAPn; 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=kCe4tmwPVNV+GsMq6QYMed33sYkMNNuhATDpMCBOUxY=; b=qI/zQAPnmOYY5yx1FrZPOmnRni 8Qqu8fDDFp8QP0NfLFDkc6fx6KlhwN72bbFqIsT+yy0Ote1xi3K8Ap8IV/yHBwccFyN/ZFQdY/MwR NXLwwRCdiTH/sEW/5iB792wwg1j65xp71nHcVHFy97KyxzQHSDniLD0Olf+jDNd0X7WBDusSMsaH/ FHY9ePjuX6tyxoQ+NvBxdpS0yzWwJ0V+iNmFL48TF2pII6vC7/+6z+TTNxMaUzt329w30qIZiLjwI 0rGNwSVbpWUItynfnefWGLyZwMEo6RSuEbDSQJ/i1rpDJFXKxAw4cx7Clg1VHeaQL2HO/3NH6N/gd PPTHLoyw==; 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 1raOob-00GbiL-2M; Wed, 14 Feb 2024 17:37:22 -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 09/31] ntsync: Introduce NTSYNC_IOC_CREATE_EVENT. Date: Wed, 14 Feb 2024 17:36:45 -0600 Message-ID: <20240214233645.9273-10-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: 1790920419472701231 X-GMAIL-MSGID: 1790920419472701231 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 8cb951c76aca..2a21e7b0982a 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, }; struct ntsync_obj { @@ -46,6 +47,10 @@ struct ntsync_obj { __u32 owner; bool ownerdead; } mutex; + struct { + bool manual; + bool signaled; + } event; } u; /* @@ -130,6 +135,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); @@ -179,6 +186,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); @@ -245,6 +256,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. @@ -553,6 +584,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); @@ -676,6 +731,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; } } @@ -864,6 +922,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)