From nobody Tue Dec 23 23:47:58 2025 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 96120525D; Wed, 31 Jan 2024 02:16:03 +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=1706667369; cv=none; b=suahCm8j92zsH8crRtb8n+dsH7+jPbfUpYTTpAAloYcmBK1gJzNsUKnU7FiQh7Nat2GFELZhFbuUtQnkJit7zkjs1BpujqxK+XDFh/BbPB1UKOz8aGLQUaQXRgwkk2a1W6CU8iTiBfOviq18knKHm+kfBlM+uSVcQqpiPa0nKD8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706667369; c=relaxed/simple; bh=Az1dcn+diz6i5pAEU1GrHGwsdplurkEQ20tIVBYPOAQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=stKIxa34YzZY6yLbaJWkCRMJAcwl3W9Twi/VRJz+eM7z8s66z+PIDP2aR0mqWKYyfrO8v8f4Inq7iIva90vhTR4pIO0X2+kZ2dOikgwes4RMBJQDdwDzTvBRWLqXccg7myT4lwOcIJ1WGqCUazLwkCfRL6AxyO9bInzmmMP9O9w= 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=FPlQAZx/; 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="FPlQAZx/" 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=64qfv1/cxBxKplo8j/wLrSClMN20SVON1cvxHlo6Xq0=; b=FPlQAZx/WSwQH+Zw4WBTfgvqq6 4jneOXFEH1FliME/wNLgu/dEDzZ4Z6rhGaZpRJrtmYWdfW53GJofQlwpXc477irjYFcEk3LE8/sfO CwDVmS8VOh+870WkARUE7gdaP2VYW6KHnfoGvzt0uXKh6bdT1hFPAZ9Jrhq0RREsvITHcDBFPoRk0 mcSVskKEPkmXnix1iRkSnB7LhDIDoCMZ0qrh4Aan8PYH9inNBND3fUaoW629i8jjAxCOtFs094YW2 oIyz2ba4RosAcGB37HYDujY2WJMSpictYmZjUBMrzM2Toj+H/DYqiKq53S06wxhsXuUGP2B0PzZSk dxiTbGSw==; 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 1rV08p-0038Kv-0Y; Tue, 30 Jan 2024 20:15:55 -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=20Almeida?= , Wolfram Sang , Arkadiusz Hiler , Peter Zijlstra , Andy Lutomirski , linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Elizabeth Figura Subject: [RFC PATCH v2 16/29] ntsync: Introduce alertable waits. Date: Tue, 30 Jan 2024 20:13:43 -0600 Message-ID: <20240131021356.10322-17-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 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 sin= gle bit of "alerted" state, and if a thread is made 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 | 68 ++++++++++++++++++++++++++++++++----- include/uapi/linux/ntsync.h | 2 +- 2 files changed, 60 insertions(+), 10 deletions(-) diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c index 5439c1c9e90f..1e619e1ce6a6 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -784,22 +784,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 =3D args->count; - int fds[NTSYNC_MAX_WAIT_COUNT]; struct ntsync_q *q; + __u32 total_count; __u32 i, j; =20 - if (!args->owner || args->pad) + if (!args->owner) return -EINVAL; =20 if (args->count > NTSYNC_MAX_WAIT_COUNT) return -EINVAL; =20 + total_count =3D 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] =3D args->alert; =20 - q =3D kmalloc(struct_size(q, entries, count), GFP_KERNEL); + q =3D kmalloc(struct_size(q, entries, total_count), GFP_KERNEL); if (!q) return -ENOMEM; q->task =3D current; @@ -809,7 +816,7 @@ static int setup_wait(struct ntsync_device *dev, q->ownerdead =3D false; q->count =3D count; =20 - for (i =3D 0; i < count; i++) { + for (i =3D 0; i < total_count; i++) { struct ntsync_q_entry *entry =3D &q->entries[i]; struct ntsync_obj *obj =3D get_obj(dev, fds[i]); =20 @@ -860,9 +867,9 @@ static int ntsync_wait_any(struct ntsync_device *dev, v= oid __user *argp) { struct ntsync_wait_args args; struct ntsync_q *q; + __u32 i, total_count; ktime_t timeout; int signaled; - __u32 i; int ret; =20 if (copy_from_user(&args, argp, sizeof(args))) @@ -872,9 +879,13 @@ static int ntsync_wait_any(struct ntsync_device *dev, = void __user *argp) if (ret < 0) return ret; =20 + total_count =3D args.count; + if (args.alert) + total_count++; + /* queue ourselves */ =20 - for (i =3D 0; i < args.count; i++) { + for (i =3D 0; i < total_count; i++) { struct ntsync_q_entry *entry =3D &q->entries[i]; struct ntsync_obj *obj =3D entry->obj; =20 @@ -883,9 +894,15 @@ static int ntsync_wait_any(struct ntsync_device *dev, = void __user *argp) spin_unlock(&obj->lock); } =20 - /* 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. + */ =20 - for (i =3D 0; i < args.count; i++) { + for (i =3D 0; i < total_count; i++) { struct ntsync_obj *obj =3D q->entries[i].obj; =20 if (atomic_read(&q->signaled) !=3D -1) @@ -903,7 +920,7 @@ static int ntsync_wait_any(struct ntsync_device *dev, v= oid __user *argp) =20 /* and finally, unqueue */ =20 - for (i =3D 0; i < args.count; i++) { + for (i =3D 0; i < total_count; i++) { struct ntsync_q_entry *entry =3D &q->entries[i]; struct ntsync_obj *obj =3D entry->obj; =20 @@ -964,6 +981,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 =3D &q->entries[args.count]; + struct ntsync_obj *obj =3D entry->obj; + + spin_lock_nest_lock(&obj->lock, &dev->wait_all_lock); + list_add_tail(&entry->node, &obj->any_waiters); + spin_unlock(&obj->lock); + } =20 /* check if we are already signaled */ =20 @@ -971,6 +996,21 @@ static int ntsync_wait_all(struct ntsync_device *dev, = void __user *argp) =20 spin_unlock(&dev->wait_all_lock); =20 + /* + * 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 =3D q->entries[args.count].obj; + + if (atomic_read(&q->signaled) =3D=3D -1) { + spin_lock(&obj->lock); + try_wake_any_obj(obj); + spin_unlock(&obj->lock); + } + } + /* sleep */ =20 timeout =3D ns_to_ktime(args.timeout); @@ -994,6 +1034,16 @@ static int ntsync_wait_all(struct ntsync_device *dev,= void __user *argp) =20 put_obj(obj); } + if (args.alert) { + struct ntsync_q_entry *entry =3D &q->entries[args.count]; + struct ntsync_obj *obj =3D entry->obj; + + spin_lock_nest_lock(&obj->lock, &dev->wait_all_lock); + list_del(&entry->node); + spin_unlock(&obj->lock); + + put_obj(obj); + } =20 spin_unlock(&dev->wait_all_lock); =20 diff --git a/include/uapi/linux/ntsync.h b/include/uapi/linux/ntsync.h index 582d33b0dcac..7c91af7011e4 100644 --- a/include/uapi/linux/ntsync.h +++ b/include/uapi/linux/ntsync.h @@ -34,7 +34,7 @@ struct ntsync_wait_args { __u32 count; __u32 owner; __u32 index; - __u32 pad; + __u32 alert; }; =20 #define NTSYNC_MAX_WAIT_COUNT 64 --=20 2.43.0