Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp2099523lqo; Sun, 19 May 2024 13:28:45 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXWDmYNSvzkuddKqqqwXXzPPsqfz62ASTgXuA8fxVAwkkkilagOliygtqldCsLbh4EgpruJA+2zWXJtI1qMPPzs0lHdZUG17yYhEPc/gA== X-Google-Smtp-Source: AGHT+IEzP06f+rT1I7UtUzsQAjRK++vbLkUZ65xcMS+3h4jQkVLU/InzmDCL1VRXj0Wcp612AnE8 X-Received: by 2002:a05:6808:384f:b0:3c9:b7b7:d2bd with SMTP id 5614622812f47-3c9b7b7d5bfmr28480813b6e.35.1716150524987; Sun, 19 May 2024 13:28:44 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716150524; cv=pass; d=google.com; s=arc-20160816; b=FubOpjbDy93gDNK3jHGKmM7PMGeZl7SFMWnnUe3WWqEXhes7yJiUIQKwPRTVfmyFSa iXWRFzxeGGGlv7A24MqVDKoQFXENbLNLOzPKfNDu7tjJDx/6IKv7pgoyaVeJn5usUPEa HUxj06npQMdGm3+iHRBLvf9C+U9f7lLRoFE+lliHVQccahW/LBM2Dswnisp9K72XZcba vgW87hf0V1CE4NQebyf8wBVwirORVPrUynMLWH6hJoqMu/loxc2EXdZevtI8hvOkeJaj aUbz0bwFdajLXOZPzpmUiB+Hti6NaFZSeAnL2deMw+kbvBo8T5Zy5+PsM068PSEq9DLS 4O0A== 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=Yx0E1bGyvZjffiUNbQX+Ji2zc/69nwwUYWh+P+iWrSM=; fh=eVY06L/C5FDTmF2RNqfudsnjwkB3tbhT/y33cGhZc0I=; b=ZlgmKa4bO6ammQa3nNCRY4y9fwrYVMg2G4u8GuwIjUH/+jk0Mt0shfP7djmNUurVy+ Ma+klCRYdf6OtO3/klGUcydxOc9PMNO0xo9sHSE712X4cpIm3d41rqP3T6y1qM9HdO+r sGVhE4CZMmp+WkjDUX7hNmCEC9x1/HioIsX/ZVPOx6g4QOEo7zCKWnEunDeH1cNdUk9j wb1aF2yZ8QDJstWPj9cGa+48VkwCEbMOFLRFmj5XFY7kjbe7fmu/JnJYwdJOCScVzlB+ MUh32tvMK3wb6GPOt9tvwX3Hk68ZOjUtQyYyeF9SldYIJWrcMAgxWEE+Hbh8LgfJs5Bt eYUg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=EUu3gCOW; 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-183245-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-183245-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id af79cd13be357-7947d3c6ba6si278998885a.222.2024.05.19.13.28.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 May 2024 13:28:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-183245-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=EUu3gCOW; 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-183245-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-183245-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 53A171C206A5 for ; Sun, 19 May 2024 20:28:44 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CD32E7E591; Sun, 19 May 2024 20:25:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="EUu3gCOW" 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 C5CBC6E61E; Sun, 19 May 2024 20:25:15 +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=1716150318; cv=none; b=Vgv9CS8xgoWeHzbf5K/H/4KY5ruqVB+RbqgRugoCv641X/SEdJljeTjlNJuUiJEg1KXSHsxVBxp0YxuZMCe9RhrSFfL7JjSwagnxWKfFU+DNqp9AfRiCo/HxOW7EqImiiG66sS2rfwQ/7kqF1DLnrrFyGum7526OQHRzzjFvz94= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716150318; c=relaxed/simple; bh=2kXwj1nDuQyD4SNlNedYooti+VEh/c2JKHTckHGmiG8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZHQtSNPqwwmpLXB2t01lstjDVeW9WZnv54aJNdpjYW7wG9BIEbL4Xgs3cNlkYLlzKmkSSWtI2EMdssN2gDA2C3Dd/8dY/CK+NHjnXTSYIW/KtwmvlZ6yPqMFDBXtx3oL44I+YGpPHvNhAd4acBTjQoBEZq3Z4fQqtQQm0uZ2ACY= 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=EUu3gCOW; 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=Yx0E1bGyvZjffiUNbQX+Ji2zc/69nwwUYWh+P+iWrSM=; b=EUu3gCOWZ4G1cQJPyvDPT8S7MF s+9uDni6qFtKgObd1o3MYvza6TLYdn36EWJIuWQ//xepu0T6g4tjnZln82LN0Z1Z/zxz03BMsJBlQ RHIv65jPYqdrZ99hF08CHTcoANX3A67TQrpjA8u85onAsFqVnziWZ649Y8iGWC/IDFl4B46+cVBf5 NBhphNh2edRPDeBmRAxrKFVC79FHWh4yj1dFJUlelgQP1/V6hAY2jjpnZeUPbG+WNll+qG2NNdFEx 1uWc7O3HcFcvaXwSRQZWcE5WqBvhM/G5o+NRgTyAJmk1xKEZJTy0NXC1eazrUuavOWMVj6ehbnltU L0vZIRpA==; 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 1s8n5d-008wIn-1Y; Sun, 19 May 2024 15:25:05 -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 v5 13/28] ntsync: Introduce alertable waits. Date: Sun, 19 May 2024 15:24:39 -0500 Message-ID: <20240519202454.1192826-14-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240519202454.1192826-1-zfigura@codeweavers.com> References: <20240519202454.1192826-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 NT waits can optionally be made "alertable". This is a special channel for thread wakeup that is mildly similar to SIGIO. A thread has an internal single bit of "alerted" state, and if a thread is alerted while an alertable wait, the wait will return a special value, consume the "alerted" state, and will not consume any of its objects. Alerts are implemented using events; the user-space NT emulator is expected to create an internal ntsync event for each thread and pass that event to wait functions. Signed-off-by: Elizabeth Figura --- drivers/misc/ntsync.c | 70 ++++++++++++++++++++++++++++++++----- include/uapi/linux/ntsync.h | 3 +- 2 files changed, 63 insertions(+), 10 deletions(-) diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c index a11fe2469841..87a24798a5c7 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -885,22 +885,29 @@ static int setup_wait(struct ntsync_device *dev, const struct ntsync_wait_args *args, bool all, struct ntsync_q **ret_q) { + int fds[NTSYNC_MAX_WAIT_COUNT + 1]; const __u32 count = args->count; - int fds[NTSYNC_MAX_WAIT_COUNT]; struct ntsync_q *q; + __u32 total_count; __u32 i, j; - if (args->pad[0] || args->pad[1] || (args->flags & ~NTSYNC_WAIT_REALTIME)) + if (args->pad || (args->flags & ~NTSYNC_WAIT_REALTIME)) return -EINVAL; if (args->count > NTSYNC_MAX_WAIT_COUNT) return -EINVAL; + total_count = count; + if (args->alert) + total_count++; + if (copy_from_user(fds, u64_to_user_ptr(args->objs), array_size(count, sizeof(*fds)))) return -EFAULT; + if (args->alert) + fds[count] = args->alert; - q = kmalloc(struct_size(q, entries, count), GFP_KERNEL); + q = kmalloc(struct_size(q, entries, total_count), GFP_KERNEL); if (!q) return -ENOMEM; q->task = current; @@ -910,7 +917,7 @@ static int setup_wait(struct ntsync_device *dev, q->ownerdead = false; q->count = count; - for (i = 0; i < count; i++) { + for (i = 0; i < total_count; i++) { struct ntsync_q_entry *entry = &q->entries[i]; struct ntsync_obj *obj = get_obj(dev, fds[i]); @@ -960,10 +967,10 @@ static void try_wake_any_obj(struct ntsync_obj *obj) static int ntsync_wait_any(struct ntsync_device *dev, void __user *argp) { struct ntsync_wait_args args; + __u32 i, total_count; struct ntsync_q *q; int signaled; bool all; - __u32 i; int ret; if (copy_from_user(&args, argp, sizeof(args))) @@ -973,9 +980,13 @@ static int ntsync_wait_any(struct ntsync_device *dev, void __user *argp) if (ret < 0) return ret; + total_count = args.count; + if (args.alert) + total_count++; + /* queue ourselves */ - for (i = 0; i < args.count; i++) { + for (i = 0; i < total_count; i++) { struct ntsync_q_entry *entry = &q->entries[i]; struct ntsync_obj *obj = entry->obj; @@ -984,9 +995,15 @@ static int ntsync_wait_any(struct ntsync_device *dev, void __user *argp) ntsync_unlock_obj(dev, obj, all); } - /* check if we are already signaled */ + /* + * Check if we are already signaled. + * + * Note that the API requires that normal objects are checked before + * the alert event. Hence we queue the alert event last, and check + * objects in order. + */ - for (i = 0; i < args.count; i++) { + for (i = 0; i < total_count; i++) { struct ntsync_obj *obj = q->entries[i].obj; if (atomic_read(&q->signaled) != -1) @@ -1003,7 +1020,7 @@ static int ntsync_wait_any(struct ntsync_device *dev, void __user *argp) /* and finally, unqueue */ - for (i = 0; i < args.count; i++) { + for (i = 0; i < total_count; i++) { struct ntsync_q_entry *entry = &q->entries[i]; struct ntsync_obj *obj = entry->obj; @@ -1063,6 +1080,14 @@ static int ntsync_wait_all(struct ntsync_device *dev, void __user *argp) */ list_add_tail(&entry->node, &obj->all_waiters); } + if (args.alert) { + struct ntsync_q_entry *entry = &q->entries[args.count]; + struct ntsync_obj *obj = entry->obj; + + dev_lock_obj(dev, obj); + list_add_tail(&entry->node, &obj->any_waiters); + dev_unlock_obj(dev, obj); + } /* check if we are already signaled */ @@ -1070,6 +1095,21 @@ static int ntsync_wait_all(struct ntsync_device *dev, void __user *argp) mutex_unlock(&dev->wait_all_lock); + /* + * Check if the alert event is signaled, making sure to do so only + * after checking if the other objects are signaled. + */ + + if (args.alert) { + struct ntsync_obj *obj = q->entries[args.count].obj; + + if (atomic_read(&q->signaled) == -1) { + bool all = ntsync_lock_obj(dev, obj); + try_wake_any_obj(obj); + ntsync_unlock_obj(dev, obj, all); + } + } + /* sleep */ ret = ntsync_schedule(q, &args); @@ -1095,6 +1135,18 @@ static int ntsync_wait_all(struct ntsync_device *dev, void __user *argp) mutex_unlock(&dev->wait_all_lock); + if (args.alert) { + struct ntsync_q_entry *entry = &q->entries[args.count]; + struct ntsync_obj *obj = entry->obj; + bool all; + + all = ntsync_lock_obj(dev, obj); + list_del(&entry->node); + ntsync_unlock_obj(dev, obj, all); + + put_obj(obj); + } + signaled = atomic_read(&q->signaled); if (signaled != -1) { struct ntsync_wait_args __user *user_args = argp; diff --git a/include/uapi/linux/ntsync.h b/include/uapi/linux/ntsync.h index 74abeba832f7..4a8095a3fc34 100644 --- a/include/uapi/linux/ntsync.h +++ b/include/uapi/linux/ntsync.h @@ -37,7 +37,8 @@ struct ntsync_wait_args { __u32 index; __u32 flags; __u32 owner; - __u32 pad[2]; + __u32 alert; + __u32 pad; }; #define NTSYNC_MAX_WAIT_COUNT 64 -- 2.43.0