Received: by 2002:ab2:69cc:0:b0:1f4:be93:e15a with SMTP id n12csp1780989lqp; Mon, 15 Apr 2024 18:16:35 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX4/mZiV7oRT1EU0lmyfWFNfbTNhmGmimhztf9PiToL0I5CDzW3+1WZ9zEwsaJzP55q0bBNuWJ9XUtD2yipi5qmm8dAmIehmy6wmCExLg== X-Google-Smtp-Source: AGHT+IE+fUqubcyVTxA2A+ioxn6AcDZBTh+47XNsQF/VXSVjXMWveVDjAKZpz4+FIrb6cdLctYfP X-Received: by 2002:a05:6a00:9394:b0:6ea:d61b:edf4 with SMTP id ka20-20020a056a00939400b006ead61bedf4mr16782715pfb.7.1713230194714; Mon, 15 Apr 2024 18:16:34 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713230194; cv=pass; d=google.com; s=arc-20160816; b=kCxDoy8n3tU8XFKY7U+0zU/vw7FGWo0qnthU8oHg4rDvMqkwJVCUddKGNDydpTMoin fYLUi5JSJtO+icxLWEG/7sdUTBfR7/y309BMtSkZ3e2YsM+OzYrYs4Hyz6x7/n9Zti3b tXw5k8fGgGIZePlXzGqyFugMVTjdzv3mOuy/gPKX2fYatQpAJ7GmKY3bFwhfX7559r16 KU6XucUQVxlraBJSdg/J1kN4Wh2zd+TSDP80YiqTd75+GlVhOEZCztbgQewoZiF7g4gt m2gjEVAF9WR7gDVFwtXQkpWyDPUe/xZkTWZGhnpyptfUwoBthHFXzMgex4PznkKwgXrY 3tEA== 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=cKdpBzNnrH572ZCQ0KkLAIrp1rHYDFK3Qi77ARszWL4=; fh=eVY06L/C5FDTmF2RNqfudsnjwkB3tbhT/y33cGhZc0I=; b=EfE+dpdSJ9XfTVldlbXCnH/qjt3Eux77FasMkk72JN6d6KEw/PGM5w0qa/zFx9/WqR EdG7za/K8buAtcXwVbmqLJtb9ekLvFuI0x/Inpou1vgsSkXdVLhjzttT2o3JPdUrJUNO biNHcfnvIOHZA35qw8fVL2bSh6NQT+R1WhxgQvbVgjBTt51+Fa6IIMeI22IVEF1CVc+H /ce1TSIH1kTX4KKjpUgPDhnMxloL9OPnkRY8df4QtP1PGcPbdIgzbbhQHAT3BsFWNgcV IgSOvflhK3y3Supz1adNMwcU74eBT3C503oyy/1jaMCIxo5FnaZ0BdqDb6L4Qw83Iqb8 rb2A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=hleiHDZs; 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-146067-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-146067-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id z12-20020aa79e4c000000b006ecfd9eb45bsi8709379pfq.312.2024.04.15.18.16.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Apr 2024 18:16:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-146067-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=hleiHDZs; 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-146067-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-146067-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 8A8BBB21AB1 for ; Tue, 16 Apr 2024 01:13:30 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 339DB6EB45; Tue, 16 Apr 2024 01:10:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="hleiHDZs" 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 BD83B6FC7; Tue, 16 Apr 2024 01:10:28 +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=1713229832; cv=none; b=NhbpqTdqW0HJUsABxFpfhrOKV20dQsfIi2B4wXUHiIKnmYg6XsLZORi34qyI3YK3+xmOL202EKaaPoVwswOowkf6giHXHDPjcxxdyp1IQbsyvgT5a07wCY1bjw16RC6WDeZohu0wwGYVFJocVeFsPkmI56hXMF+b40+M/wZ34go= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713229832; c=relaxed/simple; bh=qk0lOShQNh46Ivwy26TSgyRhbSnLQO0T3i9GzNtHLDY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=D1tBWjyT0F7BATC/0w9kcBaMeohaxDsyXxkkFtgB+HWJ+oO4oeyqr/NdVAA7hhftJzissXMA19tIA938wgKdhPQ3Z+JW5f8JzFTCQLqe16jW/G/q5rAxXu5UV/HEr0lKiisPmFrLbvsc8dbQf+ChyqgaHXyri4VsYJZRUOa58dc= 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=hleiHDZs; 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=cKdpBzNnrH572ZCQ0KkLAIrp1rHYDFK3Qi77ARszWL4=; b=hleiHDZsQN2v77aojocKSsIqEg TcW0IB2Pm617Gl8kk7CCOCbe6lwJKjaZyGywEjsU639IZeJ/Q8ewkujc9AXClu3Bg6Zz08Eg4qgkP gbXcD9CQTCwyiI8Eb4a/eHEyzjvqbZUgyliH+g1PBRYgLmSCM+wx1Q/Tk+WK+fWBNH9H6uqwQHXJJ c0eF+PbmeLTma16OPuY8mOe3AG0df6qMJQBALURuPu35J4dq87F5Z+KZ4f3CMUfh9nG1GUF3X6mPz ipB68Qa0auamvSgIv/D3I+cM567X+tTEgUsthnrd03H+2D/pbGazpamYLHITa1J9vfHYGie2L9qYY Cg3QIxsw==; 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 1rwXKz-00FbQv-29; Mon, 15 Apr 2024 20:10:17 -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 , Ingo Molnar , Will Deacon , Waiman Long , Boqun Feng , Elizabeth Figura Subject: [PATCH v4 06/27] ntsync: Introduce NTSYNC_IOC_CREATE_EVENT. Date: Mon, 15 Apr 2024 20:08:16 -0500 Message-ID: <20240416010837.333694-7-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240416010837.333694-1-zfigura@codeweavers.com> References: <20240416010837.333694-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 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 | 61 +++++++++++++++++++++++++++++++++++++ include/uapi/linux/ntsync.h | 7 +++++ 2 files changed, 68 insertions(+) diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c index 1e68f96bc2a6..3e125c805c00 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); @@ -193,6 +200,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); @@ -261,6 +272,27 @@ 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; + int signaled = -1; + + if (!event->u.event.signaled) + break; + + if (atomic_try_cmpxchg(&q->signaled, &signaled, entry->index)) { + 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. @@ -569,6 +601,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); @@ -702,6 +758,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; } } @@ -890,6 +949,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 1bff8f19d6d9..0d133f2eaf0b 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; +}; + #define NTSYNC_WAIT_REALTIME 0x1 struct ntsync_wait_args { @@ -41,6 +47,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) -- 2.43.0