From patchwork Wed Jan 31 02:13:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 194455 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp1628884dyb; Tue, 30 Jan 2024 18:28:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IE4E2nubFazmYaU3wq6FIy9RNc2XGQ7BZbgX86WGgjRsskAV9ubg4lhnez9cPT52Lb8sA1Z X-Received: by 2002:a25:ef45:0:b0:dc6:9c4f:9e79 with SMTP id w5-20020a25ef45000000b00dc69c4f9e79mr425435ybm.38.1706668130117; Tue, 30 Jan 2024 18:28:50 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706668130; cv=pass; d=google.com; s=arc-20160816; b=fz50+j6sYQOQmK/QV2HbW5+RzkFP4Is21Xf3NFdYXGPtVdH9c7fy0HcV5nJxTQxdGU o5J94oGEQAGh8E2VEDnR4cvWBePULKBqDyHY1ZT6ur1/gUgV0FwBY92ND+wTu90vC4f0 0oV9/+4ggPiIm3+KaIPHUe24Jo6cp1oRt9F2SyRLO+DJS7rjsSVt78yX96bsVf031OFn PkDu8sMlZmBD5bkWbQb7Ug+Da/fnX7X6yxMqzNwcPi/KVEq60VQ5BX2MZzRjHYDDgxZ6 K/LhJ1vU3pYy/dOtsN6buP2a5KBQb/9/s311MH9Nt3qZd7dlX/pEU3P7p4nklUCoxboY h8OQ== 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=64qfv1/cxBxKplo8j/wLrSClMN20SVON1cvxHlo6Xq0=; fh=uqXMSk9ovOFG7iKjSpuFhjxY2QwwLRU/tX61bulX/CU=; b=svTlG9UvQQQAcbpKWBRPd+v30ZSXrXDES1m8BPtv3yYcoAQ5/Dgcmg8Expje9eL67c sGlFGHJE8PIW2MUcWftF4ELm4JbQzYa8Tl0YjzBxSevOHObBaTgDpFHvTLOYXnrMvAIZ Q1YnhXcF55lH/FjYRVFz86jjbx8pOSNc21ED1wljdJ/N6oE3nnRT/tHTsu26Aw7gVOT2 3j7rbamYXzqznhCjoarUz/+mblFZUAVfJ8+oIs8ysZncV98s08JrdwnWLbo1OtxNFKrd K4Opk1O0i3FOoi7gDWv3oBps6fIG3BMZUMt3jyL0OYcGMfGS+1SpymxKNhGSM8oa8I+8 M4pw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b="FPlQAZx/"; 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-45657-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-45657-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com X-Forwarded-Encrypted: i=1; AJvYcCXuwIrd22kOZiA/6yzSsjtHi70mkmFpgiSdYfxIpXYcIw79ro/W/DObvNPgeq0gsrliVxjYM/AxMuUDYhBVcNpDADKmPQ== Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id b185-20020a6334c2000000b005d3a532f031si8504022pga.257.2024.01.30.18.28.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 18:28:50 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-45657-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b="FPlQAZx/"; 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-45657-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-45657-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id C643CB26F06 for ; Wed, 31 Jan 2024 02:23:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 146233DBB3; Wed, 31 Jan 2024 02:16:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="FPlQAZx/" 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 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_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 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789571241133585715 X-GMAIL-MSGID: 1789571241133585715 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 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 = args->count; - int fds[NTSYNC_MAX_WAIT_COUNT]; struct ntsync_q *q; + __u32 total_count; __u32 i, j; - if (!args->owner || args->pad) + if (!args->owner) 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; @@ -809,7 +816,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]); @@ -860,9 +867,9 @@ static int ntsync_wait_any(struct ntsync_device *dev, void __user *argp) { struct ntsync_wait_args args; struct ntsync_q *q; + __u32 i, total_count; ktime_t timeout; int signaled; - __u32 i; int ret; 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; + 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; @@ -883,9 +894,15 @@ static int ntsync_wait_any(struct ntsync_device *dev, void __user *argp) spin_unlock(&obj->lock); } - /* 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) @@ -903,7 +920,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; @@ -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 = &q->entries[args.count]; + struct ntsync_obj *obj = entry->obj; + + spin_lock_nest_lock(&obj->lock, &dev->wait_all_lock); + list_add_tail(&entry->node, &obj->any_waiters); + spin_unlock(&obj->lock); + } /* check if we are already signaled */ @@ -971,6 +996,21 @@ static int ntsync_wait_all(struct ntsync_device *dev, void __user *argp) spin_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) { + spin_lock(&obj->lock); + try_wake_any_obj(obj); + spin_unlock(&obj->lock); + } + } + /* sleep */ timeout = ns_to_ktime(args.timeout); @@ -994,6 +1034,16 @@ static int ntsync_wait_all(struct ntsync_device *dev, void __user *argp) put_obj(obj); } + if (args.alert) { + struct ntsync_q_entry *entry = &q->entries[args.count]; + struct ntsync_obj *obj = entry->obj; + + spin_lock_nest_lock(&obj->lock, &dev->wait_all_lock); + list_del(&entry->node); + spin_unlock(&obj->lock); + + put_obj(obj); + } spin_unlock(&dev->wait_all_lock); 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; }; #define NTSYNC_MAX_WAIT_COUNT 64