From patchwork Wed Feb 14 23:36:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201188 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp61661dyb; Wed, 14 Feb 2024 15:57:00 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUM/LX6aKMHOzf/B9Si8eeJ2FnAw0MqLGMvY9iOS0j0B90A7Q+8LKbiR+GCCRI7bT+FQNRoLUKGIs7BYdtg4WT5pDxjjw== X-Google-Smtp-Source: AGHT+IFL7b37WlSmuaJdCmG/omO1E+fuFBY7+K4AGankUPaG+V8HiddwaMWLaP4rWsE9RDNaheyG X-Received: by 2002:ae9:f70c:0:b0:785:cd81:447e with SMTP id s12-20020ae9f70c000000b00785cd81447emr325672qkg.47.1707955019848; Wed, 14 Feb 2024 15:56:59 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707955019; cv=pass; d=google.com; s=arc-20160816; b=KaUgsKS1IRz1NwahxPjFi5JB9C5YmTS8M8Gpxq1aNDS/aTruZ1THgdbfiMo5zLlYS0 DVfh1EfmXWMtnsXTN813Kw/Or7pfVQWJ3sFGvU2udf8yPp0KKzTSNao2MO3sVRlet9A/ LKUEfHtvxq9gEtPg9KXlw0YFRdbKbFQA9I9TOdgUVwTy54RAFllNtsrF6OF04oyifV/s 2p5KYDIs6zzAUiD3R494AD7iMVN28n1lCW9xhd5LdYBgtsUwhnW3XelsgiXM1Y2Y5in0 Bt1u5sSPVuW/3BevuCns/hGUSCzAi+gM+UY49G30elfDQGAGWE7auUp7hUP1Rlaf19jP 1vPA== 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=X/IZwfYWP9znQfddabnSoi+xBA9zWsqNxvzC3bQDVaM=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=Ky9gfwuOtG9IKZ7fUV29GG0valzBlynb4aQdVXqhRAF6oDGIH5hUtX4KbiuLI2iON2 4bWfHOwLinGL4DiKARCUsH29qHLKq5dMbmtqR9ccfqYNDRHlk+zsI7UEZBv1HL+hzQis 8g3yQnYCojFJzRVQKWNN6yorKB8wVsZDF867nQg7ko48i09s2+d+gsNqfEi/LXbPZQre p8az2HFCvphSVRaCecSgasxWehjkSNZvtfL4f6+AU0TJa8Zvx3aKtD93jrgQsrhJyo62 Q2omwIuA8HEsx/9kOJ5gZ4yoWDWuaCUSGDl18nN0lsegluR1LkNuZRGukzYe8yVws/83 OECA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=Tx3FeU36; 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-66148-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66148-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id bk30-20020a05620a1a1e00b007871d88581fsi277574qkb.169.2024.02.14.15.56.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 15:56:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66148-ouuuleilei=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=Tx3FeU36; 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-66148-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66148-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id A113C1C20F81 for ; Wed, 14 Feb 2024 23:56:59 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 517B014900A; Wed, 14 Feb 2024 23:53:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="Tx3FeU36" 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 C522D1474DB; Wed, 14 Feb 2024 23:53:34 +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=1707954816; cv=none; b=iOkRGCq0oGoq9elvMXMsvdpJQUbC7PxQ0R8awRvvuaC1Me98V2ANrkQrWL9bLnSfCFe0UX2jsT19f8WIqVycstxGuVORz99/NmYW9JQYlLNJiGIptz2YL0FtKaFakiEiHIBnodhx4Sj2rJtHRbSYlfqVjicTNs9qGUsikW151Ys= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954816; c=relaxed/simple; bh=I4trA2JfrK9wBwFoc8bTAUMVwA5tJ+kKV0vPGN6dfHk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=e5U50vVFdqQsw20QQhmeYbXOH1sR3e4SjpK23To1yNMJ4T7ir/UtGSEz6vL3kGi21YYIZS4eEWUQDBx/4DBdldfHZ31CYEjxlQL3JAQf8gDN6VwIKiCA2P2qyP0TrlI42ON0MiciWSg0re9tRbxLOCM/+5g++3v6x7Y5DNi+t/8= 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=Tx3FeU36; 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=X/IZwfYWP9znQfddabnSoi+xBA9zWsqNxvzC3bQDVaM=; b=Tx3FeU36Xq2ZUqbwOw5gKia8RC I+KdCplVS2o4/O6CjoEVbGmOmvXv1cd8SoGO5MPi4hbWJh4WOyxaQssM8x8BTNgCKITP5zdHC9ZKJ WkecUFN9f9Ph9S0NDVyl63Jgns+5yWJ855ZOXKSMfuYW2FDIKlixl4HD/8eyIk1cXNIqqvYVqvAqb g00v7uT2y+RrMm9auQ6uH0hwivRq9McI7Ny8oB1juCnPfq8WHXJMxrnytr+XUFMO+mn3Grzg7TNb/ H47/nAUyuKcFUR3ETIH/rjgO5rKUUHTUlMr1jmRfrju7IXy5P1pc0ydj7JYFCNbCv1t1iJEth2fYH DyO0ofDw==; 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 1raOoV-00GbiL-2O; Wed, 14 Feb 2024 17:37:15 -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 01/31] ntsync: Introduce the ntsync driver and character device. Date: Wed, 14 Feb 2024 17:36:37 -0600 Message-ID: <20240214233645.9273-2-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: 1790920643202110456 X-GMAIL-MSGID: 1790920643202110456 ntsync uses a misc device as the simplest and least intrusive uAPI interface. Each file description on the device represents an isolated NT instance, intended to correspond to a single NT virtual machine. Signed-off-by: Elizabeth Figura --- drivers/misc/Kconfig | 9 ++++++++ drivers/misc/Makefile | 1 + drivers/misc/ntsync.c | 52 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 drivers/misc/ntsync.c diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig index 4fb291f0bf7c..bdd8a71bd853 100644 --- a/drivers/misc/Kconfig +++ b/drivers/misc/Kconfig @@ -504,6 +504,15 @@ config OPEN_DICE measured boot flow. Userspace can use CDIs for remote attestation and sealing. +config NTSYNC + tristate "NT synchronization primitive emulation" + help + This module provides kernel support for emulation of Windows NT + synchronization primitives. It is not a hardware driver. + + To compile this driver as a module, choose M here: the + module will be called ntsync. + If unsure, say N. config VCPU_STALL_DETECTOR diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile index ea6ea5bbbc9c..153a3f4837e8 100644 --- a/drivers/misc/Makefile +++ b/drivers/misc/Makefile @@ -59,6 +59,7 @@ obj-$(CONFIG_PVPANIC) += pvpanic/ obj-$(CONFIG_UACCE) += uacce/ obj-$(CONFIG_XILINX_SDFEC) += xilinx_sdfec.o obj-$(CONFIG_HISI_HIKEY_USB) += hisi_hikey_usb.o +obj-$(CONFIG_NTSYNC) += ntsync.o obj-$(CONFIG_HI6421V600_IRQ) += hi6421v600-irq.o obj-$(CONFIG_OPEN_DICE) += open-dice.o obj-$(CONFIG_GP_PCI1XXXX) += mchp_pci1xxxx/ diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c new file mode 100644 index 000000000000..e4969ef90722 --- /dev/null +++ b/drivers/misc/ntsync.c @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * ntsync.c - Kernel driver for NT synchronization primitives + * + * Copyright (C) 2024 Elizabeth Figura + */ + +#include +#include +#include + +#define NTSYNC_NAME "ntsync" + +static int ntsync_char_open(struct inode *inode, struct file *file) +{ + return nonseekable_open(inode, file); +} + +static int ntsync_char_release(struct inode *inode, struct file *file) +{ + return 0; +} + +static long ntsync_char_ioctl(struct file *file, unsigned int cmd, + unsigned long parm) +{ + switch (cmd) { + default: + return -ENOIOCTLCMD; + } +} + +static const struct file_operations ntsync_fops = { + .owner = THIS_MODULE, + .open = ntsync_char_open, + .release = ntsync_char_release, + .unlocked_ioctl = ntsync_char_ioctl, + .compat_ioctl = compat_ptr_ioctl, + .llseek = no_llseek, +}; + +static struct miscdevice ntsync_misc = { + .minor = MISC_DYNAMIC_MINOR, + .name = NTSYNC_NAME, + .fops = &ntsync_fops, +}; + +module_misc_device(ntsync_misc); + +MODULE_AUTHOR("Elizabeth Figura"); +MODULE_DESCRIPTION("Kernel driver for NT synchronization primitives"); +MODULE_LICENSE("GPL"); From patchwork Wed Feb 14 23:36:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201182 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp60413dyb; Wed, 14 Feb 2024 15:54:28 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXRigJnWVSmm7P28HPWwwxCubg1n0wgPLUUZZcq+BtvYwCoPVTpcTlqyZXumSZirpmPC+GvKrsvFweNwMCw/3knfwX1iw== X-Google-Smtp-Source: AGHT+IGE1DhNN138g45H6EDqfmEgWza94U7a1XD2aY8nmdebKf3CwDAC8Ty51B5814pakxWFOZSR X-Received: by 2002:a05:6808:140e:b0:3c0:371d:3504 with SMTP id w14-20020a056808140e00b003c0371d3504mr419313oiv.5.1707954868626; Wed, 14 Feb 2024 15:54:28 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707954868; cv=pass; d=google.com; s=arc-20160816; b=Mwcg3nyRvmwZeli7w6G0XCPokzyRkhOjgaDmgNvI967ElQo3fM0sUaVoidYvAFDt3w tPnYhu3+CEq+b0nKmwwLzpvXT08DWScU2oRhe0I18H2/iVaZ9u7MJC8Q68ULbckmKqH0 ZehE13yu5LU8t62+BpgPzPeCkd02pIAiRuKzBT0kPKJHW77y5+nFank+4mJrfjmy0P+S +vpL6ZVMa2QqJJtN5d9Frnc95r14QXX1myCW5Rc/8OI36FsP7OKOi8l8Isv2wcvjZyQU B8rSkVsiCGsFWovtbDcHJ7ah4Cs9cNBMOqnAWBePdbESej5C/8M3C2EXSP5LHWLL4maG K8Rg== 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=lHwW4zT2kOoH+YNEOKMeIBkf5DIZZR6wMENN39LT3Xo=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=u+8OWDdXfVqdvBPsyFQRW/kxwnRhO8kI3KYjKXH5MsZyx8zBe87DLzZrAjiaKQ6jHf YWkAzGhD6897t0HdwuCMWgCECem6CUS51UQ0o99RfqPsS/cR8Xmgcbf9gbWbEeXh57a/ WopFG+C+ug4+13gd9CBPaliQdi7di+xl96q49M+sNq9t5uFBlzgGpZKLyy7HDQshR5xt YAN9Zt0Q7bl1R5EfQ/g3gSzAgpQjx9wZ+lJXeb6t1NQYquDjiuQ9CsG7Pp2fdZBwb5r4 jH977giSLabjuuMvKQlUp4dAWrGKrKXzuWbAReKInw/z1d2+fiHw3L9Nd8ERk/MxsatX G0GQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=SFcIg0pv; 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-66139-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66139-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. [139.178.88.99]) by mx.google.com with ESMTPS id n23-20020a638f17000000b005dc55690b2bsi7876pgd.324.2024.02.14.15.54.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 15:54:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66139-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=SFcIg0pv; 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-66139-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66139-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 5AB8A287DE2 for ; Wed, 14 Feb 2024 23:54:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A6E3B145FFD; Wed, 14 Feb 2024 23:53:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="SFcIg0pv" 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 1181D145B2C; Wed, 14 Feb 2024 23:53:21 +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=1707954802; cv=none; b=UzCYQrXgAokpi1zexWRp+zEf4s5gynIOh5lN7DVRqx1v+siQCy8xg56hwVxSi43BhKYKtwES0i3qje5lM4pMHwHlxToibL52Qs+JgQqfSzkBryqgTNm7BQWnal24mnP0Wb7hgoGCRyIoUqydYU2NHkhfN2flz6jWm/NOpZR7zbY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954802; c=relaxed/simple; bh=5/Mn5ps7n4SQKWZX1KTG6D74zm3D8qkkGwwpR0oR9YY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=InLV5DNpvHg7KfXquAmIiILGaydfUfYQaF+oayQfYK4oReRewugQlHL4R4W7R/i8vqD13jCZw/mp90/bSKCuqkqVDCF5Mjnxl1w4NQgR6RfBtYiECWeIF7x1CiMYYpPT9LV+PK1xhqfpqSA3hgMulrnusCqtdcyvSyiXXbdd7qk= 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=SFcIg0pv; 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=lHwW4zT2kOoH+YNEOKMeIBkf5DIZZR6wMENN39LT3Xo=; b=SFcIg0pvgcPYO26bsLRnZN1/BY 56/VIa6wwRjQtaSO1R2Svk7IupKtdmSjW46ZvScESkC7VS+S09steAgZYRalXzidwFTZmx3X5OJo1 58s/F7h47GWDMQ7h936/ut2Gc4v54D9Q9fYlL0YIJ08i09gWUTOtEd+Vt0cL30zCo954oFaiieWA2 gB2evYh6AlckC5BwzpH21+NSZOLSejBhVBUPDXSKMfb5/fUs627GTGbbfIB3TtgPkjZqeaN5tDA7c zWYYU0wliKpp5z2OLiNDEklnYyCYS8hSsIp7Ads7a5E2vw78iXsrmImYe9LUokZ0O2pxr/XfYmYYJ cSPL9Zkg==; 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 1raOoW-00GbiL-0b; Wed, 14 Feb 2024 17:37:16 -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 02/31] ntsync: Introduce NTSYNC_IOC_CREATE_SEM. Date: Wed, 14 Feb 2024 17:36:38 -0600 Message-ID: <20240214233645.9273-3-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: 1790920484250848974 X-GMAIL-MSGID: 1790920484250848974 This corresponds to the NT syscall NtCreateSemaphore(). Semaphores are one of three types of object to be implemented in this driver, the others being mutexes and events. An NT semaphore contains a 32-bit counter, and is signaled and can be acquired when the counter is nonzero. The counter has a maximum value which is specified at creation time. The initial value of the semaphore is also specified at creation time. There are no restrictions on the maximum and initial value. Each object is exposed as an file, to which any number of fds may be opened. When all fds are closed, the object is deleted. Signed-off-by: Elizabeth Figura --- .../userspace-api/ioctl/ioctl-number.rst | 2 + drivers/misc/ntsync.c | 120 ++++++++++++++++++ include/uapi/linux/ntsync.h | 21 +++ 3 files changed, 143 insertions(+) create mode 100644 include/uapi/linux/ntsync.h diff --git a/Documentation/userspace-api/ioctl/ioctl-number.rst b/Documentation/userspace-api/ioctl/ioctl-number.rst index 457e16f06e04..2f5c6994f042 100644 --- a/Documentation/userspace-api/ioctl/ioctl-number.rst +++ b/Documentation/userspace-api/ioctl/ioctl-number.rst @@ -173,6 +173,8 @@ Code Seq# Include File Comments 'M' 00-0F drivers/video/fsl-diu-fb.h conflict! 'N' 00-1F drivers/usb/scanner.h 'N' 40-7F drivers/block/nvme.c +'N' 80-8F uapi/linux/ntsync.h NT synchronization primitives + 'O' 00-06 mtd/ubi-user.h UBI 'P' all linux/soundcard.h conflict! 'P' 60-6F sound/sscape_ioctl.h conflict! diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c index e4969ef90722..3ad86d98b82d 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -5,26 +5,146 @@ * Copyright (C) 2024 Elizabeth Figura */ +#include +#include #include #include #include +#include +#include #define NTSYNC_NAME "ntsync" +enum ntsync_type { + NTSYNC_TYPE_SEM, +}; + +struct ntsync_obj { + enum ntsync_type type; + + union { + struct { + __u32 count; + __u32 max; + } sem; + } u; + + struct file *file; + struct ntsync_device *dev; +}; + +struct ntsync_device { + struct file *file; +}; + +static int ntsync_obj_release(struct inode *inode, struct file *file) +{ + struct ntsync_obj *obj = file->private_data; + + fput(obj->dev->file); + kfree(obj); + + return 0; +} + +static const struct file_operations ntsync_obj_fops = { + .owner = THIS_MODULE, + .release = ntsync_obj_release, + .llseek = no_llseek, +}; + +static struct ntsync_obj *ntsync_alloc_obj(struct ntsync_device *dev, + enum ntsync_type type) +{ + struct ntsync_obj *obj; + + obj = kzalloc(sizeof(*obj), GFP_KERNEL); + if (!obj) + return NULL; + obj->type = type; + obj->dev = dev; + get_file(dev->file); + + return obj; +} + +static int ntsync_obj_get_fd(struct ntsync_obj *obj) +{ + struct file *file; + int fd; + + fd = get_unused_fd_flags(O_CLOEXEC); + if (fd < 0) + return fd; + file = anon_inode_getfile("ntsync", &ntsync_obj_fops, obj, O_RDWR); + if (IS_ERR(file)) { + put_unused_fd(fd); + return PTR_ERR(file); + } + obj->file = file; + fd_install(fd, file); + + return fd; +} + +static int ntsync_create_sem(struct ntsync_device *dev, void __user *argp) +{ + struct ntsync_sem_args __user *user_args = argp; + struct ntsync_sem_args args; + struct ntsync_obj *sem; + int fd; + + if (copy_from_user(&args, argp, sizeof(args))) + return -EFAULT; + + if (args.count > args.max) + return -EINVAL; + + sem = ntsync_alloc_obj(dev, NTSYNC_TYPE_SEM); + if (!sem) + return -ENOMEM; + sem->u.sem.count = args.count; + sem->u.sem.max = args.max; + fd = ntsync_obj_get_fd(sem); + if (fd < 0) { + kfree(sem); + return fd; + } + + return put_user(fd, &user_args->sem); +} + static int ntsync_char_open(struct inode *inode, struct file *file) { + struct ntsync_device *dev; + + dev = kzalloc(sizeof(*dev), GFP_KERNEL); + if (!dev) + return -ENOMEM; + + file->private_data = dev; + dev->file = file; return nonseekable_open(inode, file); } static int ntsync_char_release(struct inode *inode, struct file *file) { + struct ntsync_device *dev = file->private_data; + + kfree(dev); + return 0; } static long ntsync_char_ioctl(struct file *file, unsigned int cmd, unsigned long parm) { + struct ntsync_device *dev = file->private_data; + void __user *argp = (void __user *)parm; + switch (cmd) { + case NTSYNC_IOC_CREATE_SEM: + return ntsync_create_sem(dev, argp); default: return -ENOIOCTLCMD; } diff --git a/include/uapi/linux/ntsync.h b/include/uapi/linux/ntsync.h new file mode 100644 index 000000000000..f38818e7759d --- /dev/null +++ b/include/uapi/linux/ntsync.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* + * Kernel support for NT synchronization primitive emulation + * + * Copyright (C) 2021-2022 Elizabeth Figura + */ + +#ifndef __LINUX_NTSYNC_H +#define __LINUX_NTSYNC_H + +#include + +struct ntsync_sem_args { + __u32 sem; + __u32 count; + __u32 max; +}; + +#define NTSYNC_IOC_CREATE_SEM _IOWR('N', 0x80, struct ntsync_sem_args) + +#endif From patchwork Wed Feb 14 23:36:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201208 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp71974dyb; Wed, 14 Feb 2024 16:17:00 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX+bE0IDdiBSJ9hGjFif011lBUAUo0PF6OU7/EHj7j6DEKopvAQyhRPEvEq3JO5hrJGg+LuQ09qI8tOxrN2XKuyKeiM1A== X-Google-Smtp-Source: AGHT+IGABY/JccWv+6xuiZVNmUSjW6YevFgMY5AWRII/oCe7beInt9ymPPFd+Are+2wfaPHUL4tJ X-Received: by 2002:a92:c641:0:b0:363:d9fb:6147 with SMTP id 1-20020a92c641000000b00363d9fb6147mr68610ill.29.1707956220186; Wed, 14 Feb 2024 16:17:00 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707956220; cv=pass; d=google.com; s=arc-20160816; b=E6L5Fjszyvm9d4Mq44LbnZtVWJBC0S16Hu5P4dG4RiUMlaZ5y8m9EPAEi3mFyYyjHZ mAJ3ZqGMWOOkYuQFvigMv+Q9EPSJzd9CKvpI64o+6XzPHmd2QMllPjex8asOxF8DJRI4 Px/yYZPyPYs5V67Fx0ocVgteUP5E/FAjarZ/CTfYdkZH0aTxIg475lHJafTwNgA4Uuda yGwFnBNwCjIDQvwsiSN/+vwPqOqAUYouql6q+EWcuiugajn/ThfAetQPbvigSx0PKTbK C48ffvwkGU4M+cjFh5v0HaIuFcoTvzeahahVJrPeboTg1T9CWEzSghB3xBFF9MReYwpe a/NQ== 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=YtvcNT1nJgwYe+S6BR4d8dQs6sl/vLgKx6v9T6c9bhY=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=qIt/8IDq5UfSK5JPHefbmHEiTNETSOlB2IPtIEV3VR7GXWpqJ/rYiAfjm9cal6adlL XX//QNF/RckjwdqTeRtXNDkjDa3Az80P/iNjaV3WG1OwL8LhHN8V40ImNulfjkh+kLuV aeH+7sArcH9xMUKXpeMZto7oqmm/vB2RIERHMY3LPVaY6jBHZaS7OQ9igRVkw631r3xk RZ7g1VOGOkWwIAt4Vo/IBcshTfOUqdOb0j67U+wYwrSxMQDNBBkG2XCpdu+foQ9i2K4J d/+SDGPHSW3ERy25KIYeGdmAdgAWcuFIxR5MLQ/w2Q6+Ee7qqtZmttKrRmRpREfv4ZWt aP3g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=dW0ckDBB; 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-66138-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66138-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id l23-20020a637017000000b005dc4328dc23si33340pgc.780.2024.02.14.16.16.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 16:17:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66138-ouuuleilei=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=dW0ckDBB; 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-66138-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66138-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 405AAB29DC2 for ; Wed, 14 Feb 2024 23:54:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6F7A3145B37; Wed, 14 Feb 2024 23:53:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="dW0ckDBB" 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 41633145B0E; Wed, 14 Feb 2024 23:53:18 +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=1707954799; cv=none; b=LE2Z8RiXI+T28PzBI0oM7A3CHovvbgQdIv4fFDjFQXY1Pj3QYEF6/Zi0yZ8XEva2+Q4h0yMSimUPmwqHKrvNRMqakYq1aUKgO/fn+IblMVFfPHfKCrv/j0W21qCRolRaIPnoCYbDJzgdQeHzZNaOuk1v78j7jy/88hnsR15apeo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954799; c=relaxed/simple; bh=Ny4Ci7/Bf+MThJse/6wJNKGtKOLPatN9/l94Q+XkAJ4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DmzYnc29pMUrnn4rHckghbDmVdvABpnpkCb0ZsAgmWj0uIA8MeCy2ruisWGzoMYhDQqlBgDA3uBTP2U0APTjiLyx0cbYVJwXjcAmhgUmX2Xsmpfk2g79+gcWmS65Eumtlc09I/QMV2mspJCk/mKctR72XgGmElqCmdY98cdoeCA= 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=dW0ckDBB; 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=YtvcNT1nJgwYe+S6BR4d8dQs6sl/vLgKx6v9T6c9bhY=; b=dW0ckDBBbcBtc8f2rK1xkogp7Q Q86RM2jvUDOP1zqngVfG2qkqx9K+c6bn87KKXHYqFxW3ul3pBAlFIv+wsBnz9uUXAdHi5YSRgyo8E Wg7p/FMFxTVjlup3z4IeN5tWm1KXQ4tLfSropWlea7tx2lv//tMqABxVjGP73kwc8oR9BmjUqSCK4 t/xbwncLLCbJoh3sa2vM8XqPZBd6yvTCBsGbZMnwZRkg+TVm5hYBVgt5bDSAtZDO8pgr7btBT5pFT ACNqpOLj4+fgzoNDmDMLOLjFZ9SUkgptY0F9CpLw5OTq5sjePoQBb85MTyYimpwZIMDxreYVAvo0V 89aXrtvQ==; 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 1raOoW-00GbiL-2e; Wed, 14 Feb 2024 17:37:16 -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 03/31] ntsync: Introduce NTSYNC_IOC_SEM_POST. Date: Wed, 14 Feb 2024 17:36:39 -0600 Message-ID: <20240214233645.9273-4-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: 1790921901310469519 X-GMAIL-MSGID: 1790921901310469519 This corresponds to the NT syscall NtReleaseSemaphore(). This increases the semaphore's internal counter by the given value, and returns the previous value. If the counter would overflow the defined maximum, the function instead fails and returns -EOVERFLOW. Signed-off-by: Elizabeth Figura --- drivers/misc/ntsync.c | 72 +++++++++++++++++++++++++++++++++++-- include/uapi/linux/ntsync.h | 2 ++ 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c index 3ad86d98b82d..01556475f5cc 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -10,7 +10,9 @@ #include #include #include +#include #include +#include #include #define NTSYNC_NAME "ntsync" @@ -20,23 +22,70 @@ enum ntsync_type { }; struct ntsync_obj { + spinlock_t lock; + enum ntsync_type type; + struct file *file; + struct ntsync_device *dev; + + /* The following fields are protected by the object lock. */ union { struct { __u32 count; __u32 max; } sem; } u; - - struct file *file; - struct ntsync_device *dev; }; struct ntsync_device { struct file *file; }; +/* + * Actually change the semaphore state, returning -EOVERFLOW if it is made + * invalid. + */ +static int post_sem_state(struct ntsync_obj *sem, __u32 count) +{ + __u32 sum; + + lockdep_assert_held(&sem->lock); + + if (check_add_overflow(sem->u.sem.count, count, &sum) || + sum > sem->u.sem.max) + return -EOVERFLOW; + + sem->u.sem.count = sum; + return 0; +} + +static int ntsync_sem_post(struct ntsync_obj *sem, void __user *argp) +{ + __u32 __user *user_args = argp; + __u32 prev_count; + __u32 args; + int ret; + + if (copy_from_user(&args, argp, sizeof(args))) + return -EFAULT; + + if (sem->type != NTSYNC_TYPE_SEM) + return -EINVAL; + + spin_lock(&sem->lock); + + prev_count = sem->u.sem.count; + ret = post_sem_state(sem, args); + + spin_unlock(&sem->lock); + + if (!ret && put_user(prev_count, user_args)) + ret = -EFAULT; + + return ret; +} + static int ntsync_obj_release(struct inode *inode, struct file *file) { struct ntsync_obj *obj = file->private_data; @@ -47,9 +96,25 @@ static int ntsync_obj_release(struct inode *inode, struct file *file) return 0; } +static long ntsync_obj_ioctl(struct file *file, unsigned int cmd, + unsigned long parm) +{ + struct ntsync_obj *obj = file->private_data; + void __user *argp = (void __user *)parm; + + switch (cmd) { + case NTSYNC_IOC_SEM_POST: + return ntsync_sem_post(obj, argp); + default: + return -ENOIOCTLCMD; + } +} + static const struct file_operations ntsync_obj_fops = { .owner = THIS_MODULE, .release = ntsync_obj_release, + .unlocked_ioctl = ntsync_obj_ioctl, + .compat_ioctl = compat_ptr_ioctl, .llseek = no_llseek, }; @@ -64,6 +129,7 @@ static struct ntsync_obj *ntsync_alloc_obj(struct ntsync_device *dev, obj->type = type; obj->dev = dev; get_file(dev->file); + spin_lock_init(&obj->lock); return obj; } diff --git a/include/uapi/linux/ntsync.h b/include/uapi/linux/ntsync.h index f38818e7759d..878ec4f0f2e8 100644 --- a/include/uapi/linux/ntsync.h +++ b/include/uapi/linux/ntsync.h @@ -18,4 +18,6 @@ struct ntsync_sem_args { #define NTSYNC_IOC_CREATE_SEM _IOWR('N', 0x80, struct ntsync_sem_args) +#define NTSYNC_IOC_SEM_POST _IOWR('N', 0x81, __u32) + #endif From patchwork Wed Feb 14 23:36:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201186 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp61330dyb; Wed, 14 Feb 2024 15:56:16 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWus2C/VW4ATKjFDpI+hnP5Bo2ZTzNn1/3eDz8X6r88PYDSWE0Svpav6f8eN5jjZr9hT6LlL9jsz8Mixs9ThObob9Dang== X-Google-Smtp-Source: AGHT+IHiSq8sPZMCCXLyBoHhfJFjHzB1hwJOAiAgPZ2Y03JctK4qBVWS7HxOFE0QYM3Jh3hRp+i4 X-Received: by 2002:a0c:ae91:0:b0:68c:6f8c:6741 with SMTP id j17-20020a0cae91000000b0068c6f8c6741mr170383qvd.11.1707954975808; Wed, 14 Feb 2024 15:56:15 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707954975; cv=pass; d=google.com; s=arc-20160816; b=Nub4VNqHZzDfqt4CQKgLCE/qz8PK6CMVG5iXq0or8WWfQWZJbc3zf7u3cuqLUUeAoZ 3SOGFSwNl9n5ufB9uw2MtMQXDAXqbgnSMbOLL1m8oNXRBM8bjTCzNgqsbYArmqtAbGyi VjfU8xpjdsNafVdhevTm50bazRFSQM5BIhW9fs7YNdbBE0A/+SFbIVfQvIYUeAq9lnrt rZB4IlhpvCgdAz3rNCL3JxWJxE9OQyLWD9uEJqeYtfXXSwxXJFgNhnQoDMTTFqsG0gPD dFN4CMlQX60vN2oAzBdPWIVg0Z3y/P7JF69rqq4cVUpa4tHs06ttK4can7L/wvs6mg4O m9Pg== 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=swZMnGhvO/1HH3tk0ma3xvTm3heYbVsiMB5nX61+XbM=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=c5PTXKC4EHMt4pqUA6xqb0hQsU1dRPgOTSjIeCj7nwjQ+qVlpFK5TpmTnhpUaGV8Gk LcBkvgCMjciQPidx/dtZBIFyzEsyZTArGEiZ4h3ULZ/wX0xRZk2LR8yBAwxlQa+RtRPc KyWw3eqixDKGfsqSr8wgO/OBq0VPt9f6DcfwpJgbxYKWUuqyUiaM1dS6unURtjJkaqhy /vX5s2y8rGnc8kcpaoVPPsVCpPdx+7+FMcB+/wJUVEHMzEhzUbmOid3Semk6wtnx40rx uUZ4woZpfq2vff9Wjcr6BKUrB09UemZJtIAUHfVNwwZvrflCdZTYkLkmkdksbEcRqGk1 HjKw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=qRJM8kLa; 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-66141-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66141-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id 2-20020a0562140dc200b0068c4f264e8asi203012qvt.175.2024.02.14.15.56.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 15:56:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66141-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=qRJM8kLa; 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-66141-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66141-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 5E23A1C2881D for ; Wed, 14 Feb 2024 23:55:37 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BACC11474D8; Wed, 14 Feb 2024 23:53:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="qRJM8kLa" 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 395D0146014; Wed, 14 Feb 2024 23:53: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=1707954810; cv=none; b=OfJi8VVGClYF9cvCX/NuHQUYDWoWc9R7mFnUCX5YQ9iG2HjyRAYQuXd+Kb1yTccKCQq/45BmiQ724pGGleIVaiNgYeMEm87L9b+zjyb3jNHnk+W99UEHbTEBXt59rmwl+cKwTCEvxqAItT7aa2Z7TLwF0kbDroOr18VP3A9q724= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954810; c=relaxed/simple; bh=1yN9eC0/UnvZjEFI1nohqxO7chrSrAzybvm5pljn4sA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lDUxZBH44QeIFCeXiumgd47kqUHAVubQWbvDQah0oYzMVd+sd9J+/MpL9TRulVfqX+polKmy1XU5nz0JOakdbR6sDEVMhQbs0BsDl0xoc1N70tj+r0zHX8tPrRSDTrnJlyaaZJLir/93d+6j7juySTg+2k20I1ORw1j1osQeBIk= 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=qRJM8kLa; 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=swZMnGhvO/1HH3tk0ma3xvTm3heYbVsiMB5nX61+XbM=; b=qRJM8kLa2WKN6uaLzH5777EwdZ nrTJyv8B1vGlQ5zAQ2e/zbbPHYv2F+4Bwj20JJ2mdddcEYh5SZLUdQpX5U6S7MaR1+88ceIRHeCPu hDPCVG5tKeXkTsTOTC2p05TopsQLatjIbO95x3LqL2ELjUPiicORzzOeKmb/UbJV28MwofnFycIGb +IWfr8J066CC4YPbdvn4jIIoeeRZO1o0/DB9CWwt/rOGyyTIJhlQekVPvsyBXB4Jr0kuyHqsSlHUN 38uy2y/RJ7j/EaZ9iK9g6c/21RGtKq6pvpLGOUR04kYxbmxRJ6fEYuVRth6FKzkaJ5vrtOQsLceZA QbapUMfw==; 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 1raOoY-00GbiL-0V; Wed, 14 Feb 2024 17:37:18 -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 04/31] ntsync: Introduce NTSYNC_IOC_WAIT_ANY. Date: Wed, 14 Feb 2024 17:36:40 -0600 Message-ID: <20240214233645.9273-5-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: 1790920597117666072 X-GMAIL-MSGID: 1790920597117666072 This corresponds to part of the functionality of the NT syscall NtWaitForMultipleObjects(). Specifically, it implements the behaviour where the third argument (wait_any) is TRUE, and it does not handle alertable waits. Those features have been split out into separate patches to ease review. NTSYNC_IOC_WAIT_ANY is a vectored wait function similar to poll(). Unlike poll(), it "consumes" objects when they are signaled. For semaphores, this means decreasing one from the internal counter. At most one object can be consumed by this function. Up to 64 objects can be waited on at once. As soon as one is signaled, the object with the lowest index is consumed, and that index is returned via the "index" field. A timeout is supported. The timeout is passed as a u64 nanosecond value, which represents absolute time measured against the MONOTONIC clock. If U64_MAX is passed, the ioctl waits indefinitely. This ioctl validates that all objects belong to the relevant device. This is not necessary for any technical reason related to NTSYNC_IOC_WAIT_ANY, but will be necessary for NTSYNC_IOC_WAIT_ALL introduced in the following patch. Signed-off-by: Elizabeth Figura --- drivers/misc/ntsync.c | 237 ++++++++++++++++++++++++++++++++++++ include/uapi/linux/ntsync.h | 12 ++ 2 files changed, 249 insertions(+) diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c index 01556475f5cc..123af239f52e 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -6,11 +6,16 @@ */ #include +#include #include #include +#include +#include #include #include #include +#include +#include #include #include #include @@ -36,12 +41,55 @@ struct ntsync_obj { __u32 max; } sem; } u; + + struct list_head any_waiters; +}; + +struct ntsync_q_entry { + struct list_head node; + struct ntsync_q *q; + struct ntsync_obj *obj; + __u32 index; +}; + +struct ntsync_q { + struct task_struct *task; + __u32 owner; + + /* + * Protected via atomic_cmpxchg(). Only the thread that wins the + * compare-and-swap may actually change object states and wake this + * task. + */ + atomic_t signaled; + + __u32 count; + struct ntsync_q_entry entries[]; }; struct ntsync_device { struct file *file; }; +static void try_wake_any_sem(struct ntsync_obj *sem) +{ + struct ntsync_q_entry *entry; + + lockdep_assert_held(&sem->lock); + + list_for_each_entry(entry, &sem->any_waiters, node) { + struct ntsync_q *q = entry->q; + + if (!sem->u.sem.count) + break; + + if (atomic_cmpxchg(&q->signaled, -1, entry->index) == -1) { + sem->u.sem.count--; + wake_up_process(q->task); + } + } +} + /* * Actually change the semaphore state, returning -EOVERFLOW if it is made * invalid. @@ -77,6 +125,8 @@ static int ntsync_sem_post(struct ntsync_obj *sem, void __user *argp) prev_count = sem->u.sem.count; ret = post_sem_state(sem, args); + if (!ret) + try_wake_any_sem(sem); spin_unlock(&sem->lock); @@ -130,6 +180,7 @@ static struct ntsync_obj *ntsync_alloc_obj(struct ntsync_device *dev, obj->dev = dev; get_file(dev->file); spin_lock_init(&obj->lock); + INIT_LIST_HEAD(&obj->any_waiters); return obj; } @@ -180,6 +231,190 @@ static int ntsync_create_sem(struct ntsync_device *dev, void __user *argp) return put_user(fd, &user_args->sem); } +static struct ntsync_obj *get_obj(struct ntsync_device *dev, int fd) +{ + struct file *file = fget(fd); + struct ntsync_obj *obj; + + if (file->f_op != &ntsync_obj_fops) { + fput(file); + return NULL; + } + + obj = file->private_data; + if (obj->dev != dev) { + fput(file); + return NULL; + } + + return obj; +} + +static void put_obj(struct ntsync_obj *obj) +{ + fput(obj->file); +} + +static int ntsync_schedule(const struct ntsync_q *q, const struct ntsync_wait_args *args) +{ + ktime_t timeout = ns_to_ktime(args->timeout); + ktime_t *timeout_ptr; + int ret = 0; + + timeout_ptr = (args->timeout == U64_MAX ? NULL : &timeout); + + do { + if (signal_pending(current)) { + ret = -ERESTARTSYS; + break; + } + + set_current_state(TASK_INTERRUPTIBLE); + if (atomic_read(&q->signaled) != -1) { + ret = 0; + break; + } + ret = schedule_hrtimeout(timeout_ptr, HRTIMER_MODE_ABS); + } while (ret < 0); + __set_current_state(TASK_RUNNING); + + return ret; +} + +/* + * Allocate and initialize the ntsync_q structure, but do not queue us yet. + */ +static int setup_wait(struct ntsync_device *dev, + const struct ntsync_wait_args *args, + struct ntsync_q **ret_q) +{ + const __u32 count = args->count; + int fds[NTSYNC_MAX_WAIT_COUNT]; + struct ntsync_q *q; + __u32 i, j; + + if (!args->owner || args->pad) + return -EINVAL; + + if (args->count > NTSYNC_MAX_WAIT_COUNT) + return -EINVAL; + + if (copy_from_user(fds, u64_to_user_ptr(args->objs), + array_size(count, sizeof(*fds)))) + return -EFAULT; + + q = kmalloc(struct_size(q, entries, count), GFP_KERNEL); + if (!q) + return -ENOMEM; + q->task = current; + q->owner = args->owner; + atomic_set(&q->signaled, -1); + q->count = count; + + for (i = 0; i < count; i++) { + struct ntsync_q_entry *entry = &q->entries[i]; + struct ntsync_obj *obj = get_obj(dev, fds[i]); + + if (!obj) + goto err; + + entry->obj = obj; + entry->q = q; + entry->index = i; + } + + *ret_q = q; + return 0; + +err: + for (j = 0; j < i; j++) + put_obj(q->entries[j].obj); + kfree(q); + return -EINVAL; +} + +static void try_wake_any_obj(struct ntsync_obj *obj) +{ + switch (obj->type) { + case NTSYNC_TYPE_SEM: + try_wake_any_sem(obj); + break; + } +} + +static int ntsync_wait_any(struct ntsync_device *dev, void __user *argp) +{ + struct ntsync_wait_args args; + struct ntsync_q *q; + int signaled; + __u32 i; + int ret; + + if (copy_from_user(&args, argp, sizeof(args))) + return -EFAULT; + + ret = setup_wait(dev, &args, &q); + if (ret < 0) + return ret; + + /* queue ourselves */ + + for (i = 0; i < args.count; i++) { + struct ntsync_q_entry *entry = &q->entries[i]; + struct ntsync_obj *obj = entry->obj; + + spin_lock(&obj->lock); + list_add_tail(&entry->node, &obj->any_waiters); + spin_unlock(&obj->lock); + } + + /* check if we are already signaled */ + + for (i = 0; i < args.count; i++) { + struct ntsync_obj *obj = q->entries[i].obj; + + if (atomic_read(&q->signaled) != -1) + break; + + spin_lock(&obj->lock); + try_wake_any_obj(obj); + spin_unlock(&obj->lock); + } + + /* sleep */ + + ret = ntsync_schedule(q, &args); + + /* and finally, unqueue */ + + for (i = 0; i < args.count; i++) { + struct ntsync_q_entry *entry = &q->entries[i]; + struct ntsync_obj *obj = entry->obj; + + spin_lock(&obj->lock); + list_del(&entry->node); + spin_unlock(&obj->lock); + + put_obj(obj); + } + + signaled = atomic_read(&q->signaled); + if (signaled != -1) { + struct ntsync_wait_args __user *user_args = argp; + + /* even if we caught a signal, we need to communicate success */ + ret = 0; + + if (put_user(signaled, &user_args->index)) + ret = -EFAULT; + } else if (!ret) { + ret = -ETIMEDOUT; + } + + kfree(q); + return ret; +} + static int ntsync_char_open(struct inode *inode, struct file *file) { struct ntsync_device *dev; @@ -211,6 +446,8 @@ static long ntsync_char_ioctl(struct file *file, unsigned int cmd, switch (cmd) { case NTSYNC_IOC_CREATE_SEM: return ntsync_create_sem(dev, argp); + case NTSYNC_IOC_WAIT_ANY: + return ntsync_wait_any(dev, argp); default: return -ENOIOCTLCMD; } diff --git a/include/uapi/linux/ntsync.h b/include/uapi/linux/ntsync.h index 878ec4f0f2e8..9cd1dd05d971 100644 --- a/include/uapi/linux/ntsync.h +++ b/include/uapi/linux/ntsync.h @@ -16,7 +16,19 @@ struct ntsync_sem_args { __u32 max; }; +struct ntsync_wait_args { + __u64 timeout; + __u64 objs; + __u32 count; + __u32 owner; + __u32 index; + __u32 pad; +}; + +#define NTSYNC_MAX_WAIT_COUNT 64 + #define NTSYNC_IOC_CREATE_SEM _IOWR('N', 0x80, struct ntsync_sem_args) +#define NTSYNC_IOC_WAIT_ANY _IOWR('N', 0x82, struct ntsync_wait_args) #define NTSYNC_IOC_SEM_POST _IOWR('N', 0x81, __u32) From patchwork Wed Feb 14 23:36:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201183 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp60527dyb; Wed, 14 Feb 2024 15:54:42 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU5zfaJ3v1YfNhVoW91TbgTxnzBcQOXVKkZqmSBZ0Fi3BvApw8rVCGBi1gPwNGHkpcbv6slNWlPFk94vbyVIfTigiZjxA== X-Google-Smtp-Source: AGHT+IE0Fq6hAgqfuPvu/VYfEWoLyWSXMFZwhzbsFVW5j/plgnHu3JbH443oW5Ipps+gfRGOCak1 X-Received: by 2002:a25:bed1:0:b0:dc6:c32f:6126 with SMTP id k17-20020a25bed1000000b00dc6c32f6126mr80925ybm.22.1707954881714; Wed, 14 Feb 2024 15:54:41 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707954881; cv=pass; d=google.com; s=arc-20160816; b=Tqx2RgsWMSZgeboXTo0xFBAZOlvvNug9aMr/6/yvv2V9JoEet952sQsfikQHPIdT7Q TgtafwPkscs/bSboMHZgE+4dvNZSEHnwSgi5/9TGJ1hZ5rTg6bSzIdJ8rZONDJIq8LG0 F00FSuwjjTMZJNhk7Jbm0Miug9K2tTLPpeqGSsKO2RZv9dc9gfIUcFJ7oL4W/2IbVOEz PlydJ+dzGA2nVUQ1tlaD2xw9kWbSveGgPy2yC2cWupbWaYGlNmfYC8mY9TaI2RA0cBL6 rSqjJKxPNrCDbDE4x8ReT0++IimHHkxgI0wmp/64vlDM+pKJ4FGYvxUudTQtn+7eKx0Y IPZw== 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=nPxEOocdQ6kTDNgZV3Ar518DRwgNsQZLvjzChVikMYc=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=bZFCkDoya8R/fhPpDtQFszSzrbwmv5kHBwCRPm7Y9gRPjwkIQuFdQqXR48GmyzHc/Y daw0Z11p5GjhUwkKQqQwPsozrS+ewXQo0eoPAfjvFD9xXWqfh8nMU7PtjUQ5fPJH6pqJ XF0cbnwhMyUDRZ7AI62YDtSfSVWv1/VtI7ya2efis9RRRw63x+LDoKem14kgxiDp3xMs yTVe4Pg5CQF7sVuCyUgtyhOkqHZ5YkUJceb6lHhy3qqk6XGEy6hPe6VtiMme+Q+kDtdS srWs0g1UudjkVVZoAT98vFaMqGMcbJOBuKA18h5IapVPX+FgOrY80+EDZeax1CMrgTPv +t0A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=pwGtBRJJ; 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-66140-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66140-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id vw3-20020a05620a564300b0078726dd3ecbsi215103qkn.761.2024.02.14.15.54.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 15:54:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66140-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=pwGtBRJJ; 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-66140-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66140-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 71BEE1C21B83 for ; Wed, 14 Feb 2024 23:54:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 639EE145337; Wed, 14 Feb 2024 23:53:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="pwGtBRJJ" 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 B08B5145FEC; Wed, 14 Feb 2024 23:53:23 +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=1707954805; cv=none; b=IxawJo8Knyo5YKvDDxh3GnJeihrpWveuz+p4rjvXH4LYATo6RSOH8YhDIrrdXOCQ/uGXUevoVijZCQMzo6XSkoIh54rGvyt2ffblTq4loeU6G7qi9dftSO6XM5PyO3EG/8UFzsXOxWIaYuSp7nqiVb6o128a4hdte6jAMrvDXNA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954805; c=relaxed/simple; bh=QEOmYAFBcyflsW+7yCOpfSmQPkEKpjFiurXMV8s3T4o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iGFXkcW5gc80x87d2+2quOjWNcnyeOKDtYb0tH4+0UukejkwXeKxCzbuenrurr506FU5Op2Qfsobviec5Rz0uJEtIBBv0v3rh8hVtcqvLFKlNLH3YaK5QRHb+SDSLXAlYQU67IGNo6zJzDzutqI/hRP1DQ52YGwAFi9bV72aK2A= 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=pwGtBRJJ; 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=nPxEOocdQ6kTDNgZV3Ar518DRwgNsQZLvjzChVikMYc=; b=pwGtBRJJwM+Ry8ODobeLuuKXm2 ++UaWPPzsNwJHtFFYGnrkXVFlwuftf6ZyiK99PKLHp+gYXnyC3Fj6vaM9YrLxv216UNvCScUc4WGv f9fMIsOikAxEMNzTy3uHzAEPpljvwBpM/AZW4rFG9+qbGNE7QB7JBUiD5Pw5JoTLQAs6hxL1zp3YO xk5w2iotF4xJifdPaTLyq8hK1M6A3wWaWAHHQpofNfLSnbJi3YdCaAGxtF15nQuDyIuo14EepANjA IYOzDca6Hd+RGC6kLDYlK1pMJj9EsG7Qbr/skEQgt48Wi1eNltx2p3nt373E/4OEDzuI0uecT8SHW ZrdTkQNw==; 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 1raOoY-00GbiL-20; Wed, 14 Feb 2024 17:37:18 -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 05/31] ntsync: Introduce NTSYNC_IOC_WAIT_ALL. Date: Wed, 14 Feb 2024 17:36:41 -0600 Message-ID: <20240214233645.9273-6-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: 1790920498174234425 X-GMAIL-MSGID: 1790920498174234425 This is similar to NTSYNC_IOC_WAIT_ANY, but waits until all of the objects are simultaneously signaled, and then acquires all of them as a single atomic operation. Signed-off-by: Elizabeth Figura --- drivers/misc/ntsync.c | 242 ++++++++++++++++++++++++++++++++++-- include/uapi/linux/ntsync.h | 1 + 2 files changed, 235 insertions(+), 8 deletions(-) diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c index 123af239f52e..e173774eea52 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -42,7 +42,34 @@ struct ntsync_obj { } sem; } u; + /* + * any_waiters is protected by the object lock, but all_waiters is + * protected by the device wait_all_lock. + */ struct list_head any_waiters; + struct list_head all_waiters; + + /* + * Hint describing how many tasks are queued on this object in a + * wait-all operation. + * + * Any time we do a wake, we may need to wake "all" waiters as well as + * "any" waiters. In order to atomically wake "all" waiters, we must + * lock all of the objects, and that means grabbing the wait_all_lock + * below (and, due to lock ordering rules, before locking this object). + * However, wait-all is a rare operation, and grabbing the wait-all + * lock for every wake would create unnecessary contention. + * Therefore we first check whether all_hint is zero, and, if it is, + * we skip trying to wake "all" waiters. + * + * This hint isn't protected by any lock. It might change during the + * course of a wake, but there's no meaningful race there; it's only a + * hint. + * + * Since wait requests must originate from user-space threads, we're + * limited here by PID_MAX_LIMIT, so there's no risk of overflow. + */ + atomic_t all_hint; }; struct ntsync_q_entry { @@ -63,14 +90,99 @@ struct ntsync_q { */ atomic_t signaled; + bool all; __u32 count; struct ntsync_q_entry entries[]; }; struct ntsync_device { + /* + * Wait-all operations must atomically grab all objects, and be totally + * ordered with respect to each other and wait-any operations. + * If one thread is trying to acquire several objects, another thread + * cannot touch the object at the same time. + * + * We achieve this by grabbing multiple object locks at the same time. + * However, this creates a lock ordering problem. To solve that problem, + * wait_all_lock is taken first whenever multiple objects must be locked + * at the same time. + */ + spinlock_t wait_all_lock; + struct file *file; }; +static bool is_signaled(struct ntsync_obj *obj, __u32 owner) +{ + lockdep_assert_held(&obj->lock); + + switch (obj->type) { + case NTSYNC_TYPE_SEM: + return !!obj->u.sem.count; + } + + WARN(1, "bad object type %#x\n", obj->type); + return false; +} + +/* + * "locked_obj" is an optional pointer to an object which is already locked and + * should not be locked again. This is necessary so that changing an object's + * state and waking it can be a single atomic operation. + */ +static void try_wake_all(struct ntsync_device *dev, struct ntsync_q *q, + struct ntsync_obj *locked_obj) +{ + __u32 count = q->count; + bool can_wake = true; + __u32 i; + + lockdep_assert_held(&dev->wait_all_lock); + if (locked_obj) + lockdep_assert_held(&locked_obj->lock); + + for (i = 0; i < count; i++) { + if (q->entries[i].obj != locked_obj) + spin_lock_nest_lock(&q->entries[i].obj->lock, &dev->wait_all_lock); + } + + for (i = 0; i < count; i++) { + if (!is_signaled(q->entries[i].obj, q->owner)) { + can_wake = false; + break; + } + } + + if (can_wake && atomic_cmpxchg(&q->signaled, -1, 0) == -1) { + for (i = 0; i < count; i++) { + struct ntsync_obj *obj = q->entries[i].obj; + + switch (obj->type) { + case NTSYNC_TYPE_SEM: + obj->u.sem.count--; + break; + } + } + wake_up_process(q->task); + } + + for (i = 0; i < count; i++) { + if (q->entries[i].obj != locked_obj) + spin_unlock(&q->entries[i].obj->lock); + } +} + +static void try_wake_all_obj(struct ntsync_device *dev, struct ntsync_obj *obj) +{ + struct ntsync_q_entry *entry; + + lockdep_assert_held(&dev->wait_all_lock); + lockdep_assert_held(&obj->lock); + + list_for_each_entry(entry, &obj->all_waiters, node) + try_wake_all(dev, entry->q, obj); +} + static void try_wake_any_sem(struct ntsync_obj *sem) { struct ntsync_q_entry *entry; @@ -110,6 +222,7 @@ static int post_sem_state(struct ntsync_obj *sem, __u32 count) static int ntsync_sem_post(struct ntsync_obj *sem, void __user *argp) { + struct ntsync_device *dev = sem->dev; __u32 __user *user_args = argp; __u32 prev_count; __u32 args; @@ -121,14 +234,29 @@ static int ntsync_sem_post(struct ntsync_obj *sem, void __user *argp) if (sem->type != NTSYNC_TYPE_SEM) return -EINVAL; - spin_lock(&sem->lock); + if (atomic_read(&sem->all_hint) > 0) { + spin_lock(&dev->wait_all_lock); + spin_lock_nest_lock(&sem->lock, &dev->wait_all_lock); - prev_count = sem->u.sem.count; - ret = post_sem_state(sem, args); - if (!ret) - try_wake_any_sem(sem); + prev_count = sem->u.sem.count; + ret = post_sem_state(sem, args); + if (!ret) { + try_wake_all_obj(dev, sem); + try_wake_any_sem(sem); + } - spin_unlock(&sem->lock); + spin_unlock(&sem->lock); + spin_unlock(&dev->wait_all_lock); + } else { + spin_lock(&sem->lock); + + prev_count = sem->u.sem.count; + ret = post_sem_state(sem, args); + if (!ret) + try_wake_any_sem(sem); + + spin_unlock(&sem->lock); + } if (!ret && put_user(prev_count, user_args)) ret = -EFAULT; @@ -181,6 +309,8 @@ static struct ntsync_obj *ntsync_alloc_obj(struct ntsync_device *dev, get_file(dev->file); spin_lock_init(&obj->lock); INIT_LIST_HEAD(&obj->any_waiters); + INIT_LIST_HEAD(&obj->all_waiters); + atomic_set(&obj->all_hint, 0); return obj; } @@ -285,7 +415,7 @@ static int ntsync_schedule(const struct ntsync_q *q, const struct ntsync_wait_ar * Allocate and initialize the ntsync_q structure, but do not queue us yet. */ static int setup_wait(struct ntsync_device *dev, - const struct ntsync_wait_args *args, + const struct ntsync_wait_args *args, bool all, struct ntsync_q **ret_q) { const __u32 count = args->count; @@ -309,6 +439,7 @@ static int setup_wait(struct ntsync_device *dev, q->task = current; q->owner = args->owner; atomic_set(&q->signaled, -1); + q->all = all; q->count = count; for (i = 0; i < count; i++) { @@ -318,6 +449,16 @@ static int setup_wait(struct ntsync_device *dev, if (!obj) goto err; + if (all) { + /* Check that the objects are all distinct. */ + for (j = 0; j < i; j++) { + if (obj == q->entries[j].obj) { + put_obj(obj); + goto err; + } + } + } + entry->obj = obj; entry->q = q; entry->index = i; @@ -353,7 +494,7 @@ static int ntsync_wait_any(struct ntsync_device *dev, void __user *argp) if (copy_from_user(&args, argp, sizeof(args))) return -EFAULT; - ret = setup_wait(dev, &args, &q); + ret = setup_wait(dev, &args, false, &q); if (ret < 0) return ret; @@ -415,6 +556,87 @@ static int ntsync_wait_any(struct ntsync_device *dev, void __user *argp) return ret; } +static int ntsync_wait_all(struct ntsync_device *dev, void __user *argp) +{ + struct ntsync_wait_args args; + struct ntsync_q *q; + int signaled; + __u32 i; + int ret; + + if (copy_from_user(&args, argp, sizeof(args))) + return -EFAULT; + + ret = setup_wait(dev, &args, true, &q); + if (ret < 0) + return ret; + + /* queue ourselves */ + + spin_lock(&dev->wait_all_lock); + + for (i = 0; i < args.count; i++) { + struct ntsync_q_entry *entry = &q->entries[i]; + struct ntsync_obj *obj = entry->obj; + + atomic_inc(&obj->all_hint); + + /* + * obj->all_waiters is protected by dev->wait_all_lock rather + * than obj->lock, so there is no need to acquire obj->lock + * here. + */ + list_add_tail(&entry->node, &obj->all_waiters); + } + + /* check if we are already signaled */ + + try_wake_all(dev, q, NULL); + + spin_unlock(&dev->wait_all_lock); + + /* sleep */ + + ret = ntsync_schedule(q, &args); + + /* and finally, unqueue */ + + spin_lock(&dev->wait_all_lock); + + for (i = 0; i < args.count; i++) { + struct ntsync_q_entry *entry = &q->entries[i]; + struct ntsync_obj *obj = entry->obj; + + /* + * obj->all_waiters is protected by dev->wait_all_lock rather + * than obj->lock, so there is no need to acquire it here. + */ + list_del(&entry->node); + + atomic_dec(&obj->all_hint); + + put_obj(obj); + } + + spin_unlock(&dev->wait_all_lock); + + signaled = atomic_read(&q->signaled); + if (signaled != -1) { + struct ntsync_wait_args __user *user_args = argp; + + /* even if we caught a signal, we need to communicate success */ + ret = 0; + + if (put_user(signaled, &user_args->index)) + ret = -EFAULT; + } else if (!ret) { + ret = -ETIMEDOUT; + } + + kfree(q); + return ret; +} + static int ntsync_char_open(struct inode *inode, struct file *file) { struct ntsync_device *dev; @@ -423,6 +645,8 @@ static int ntsync_char_open(struct inode *inode, struct file *file) if (!dev) return -ENOMEM; + spin_lock_init(&dev->wait_all_lock); + file->private_data = dev; dev->file = file; return nonseekable_open(inode, file); @@ -446,6 +670,8 @@ static long ntsync_char_ioctl(struct file *file, unsigned int cmd, switch (cmd) { case NTSYNC_IOC_CREATE_SEM: return ntsync_create_sem(dev, argp); + case NTSYNC_IOC_WAIT_ALL: + return ntsync_wait_all(dev, argp); case NTSYNC_IOC_WAIT_ANY: return ntsync_wait_any(dev, argp); default: diff --git a/include/uapi/linux/ntsync.h b/include/uapi/linux/ntsync.h index 9cd1dd05d971..524404f6aceb 100644 --- a/include/uapi/linux/ntsync.h +++ b/include/uapi/linux/ntsync.h @@ -29,6 +29,7 @@ struct ntsync_wait_args { #define NTSYNC_IOC_CREATE_SEM _IOWR('N', 0x80, struct ntsync_sem_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_SEM_POST _IOWR('N', 0x81, __u32) From patchwork Wed Feb 14 23:36:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201181 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp60184dyb; Wed, 14 Feb 2024 15:53:58 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUsEgc3f3Ka+g3XmJ7Xe8zmBIFGTwKOZRJBN+PHwnb+ydWJIIaoQuuzNyykNg7MaA947/SxKc6bbnXt1dEXCLjXBdHRug== X-Google-Smtp-Source: AGHT+IGmzj1McZNVQPAYTGq5u0YKAw3HjBoW99dae9f5SSZYdxF9s5ZQzTX2VF+SltDTRLMSsO1y X-Received: by 2002:a17:906:f74a:b0:a3d:246a:a83e with SMTP id jp10-20020a170906f74a00b00a3d246aa83emr52595ejb.2.1707954837963; Wed, 14 Feb 2024 15:53:57 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707954837; cv=pass; d=google.com; s=arc-20160816; b=SekMf0zmdmH9Qo5rZZEcYc58PInP6/pm2tKQk+Ox2LToxhFHeQoJzPbw99qmmO4hF/ qvwrddISZPsZigeyQ56wMRKWC6YLFrpCGFDVsDIRsBmVEpMgPTPZ7rdI3dkl4seHulU7 SSXkvU/92qV3l77CAB8wdqgynWh6GSpix60fegdbCs9nPSH1GZllKdez1SB8ZGUqEcp5 n0JkwPELi0qOB2WTTEfMVcHWTrL2UKezTc/Z4B4a+i6fXtc5DflgIMWJpnEMkjdEWIKq V0+AXfCMiAXUFuIgLMhUbZrFKd67ubGMVo7A7O9/L9kpatVZ6y4rjwJqEk466UyDYpxT uLTA== 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=kt09pQnBXw5xt3vMakf1lDIoZzZsWjB7BCRk3DSHX20=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=tC8sF5aCFFWt4lZIkuZvokXzrImThUrCeSeyfIbSZeCL8c5yF0ca5M0WIXcYyzzLol TL2+J1SAh4XT4FI3qrCBzQrCJO5cQhEVqazs+FdQSI8gvEiE6JuiJmaf1v9BFBFS6rdp yYdOLMRu89RN7Jod+4M2hVY6kvKcPtzjIbfpuMCkzUYR525Mg3FLUObLuByFpBNnQcPg MSOgZms5DGbR388V8eHPytFOH9T7Efmvc6/DckCUjYvwgyrtxA2fJstAFbD1/xXYSiCN YNuaFYyzhYfWaH54NnEXC9zDRxNwcDuPMN94cVRaVvTiisofSkFqlv5ir1zDI+Vt9hbw LvtA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b="V9CUr/OG"; 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-66137-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66137-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id g4-20020a1709063b0400b00a37a9478c9fsi16401ejf.294.2024.02.14.15.53.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 15:53:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66137-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b="V9CUr/OG"; 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-66137-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66137-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 6481B1F21594 for ; Wed, 14 Feb 2024 23:53:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9D4DA1420D3; Wed, 14 Feb 2024 23:53:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="V9CUr/OG" 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 44D71145347; Wed, 14 Feb 2024 23:53: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=1707954796; cv=none; b=aN76t5biFXgX5c8togT0urAoh5M8YZOvLDl/Khfk8CoPLG6W/Dpiv9YQFpXWmrEaWtn9lj9ZbczPyelPhXvOKWQbwDev8MsgqN97KxL7sd+ojhe+h51XiJkur+SuqIuIdJD3Wlld2WqeFEpqcOfDjOWDqgpWU3oGQ1CNZSiWyHQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954796; c=relaxed/simple; bh=msZjmLN4VPhTlizAlCaDT2J+XYnlayGvsvKJ3iOzbAg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XKOw7dMRTI173Hf+ZoWvM8lVFNd3mOSFySWfuMV3P4tTRfXVnqD26PX2iVBVkxaZ+QLHunGlgzGZsjB1EbGcC9lNlk69azsUqum4j0oum/INROh3Z/+a3KkxcM4+VihXnYv1wLE3vrNEJqVlsuP7Be/0O0QSDoik5Lq9UzVfG48= 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=V9CUr/OG; 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=kt09pQnBXw5xt3vMakf1lDIoZzZsWjB7BCRk3DSHX20=; b=V9CUr/OGga0pmF4pEnGOh/5CT8 PqcGY8HOWvtewMqsQS2zEHV0Bu8bCFZ95FLnC4tqaCPIv4kDChhp1bKRwn3PGV8aBTrUpYEDPQYqV 51Go6+f43NsfjFBHwvhA/JhyKTGk4FhIzQ0AAnN0IBLuSYcGA5YlFEUpEpv+nxOSQiIttEQgGU+fd KGqS5fcYMS+s4c8MMUMy5uKufNprRrM0VNinOMV0DWh5goZsfbcwRaD8H1S8Ql4Sh0V8XS2YVFlsK 4Wvg1aT3Gn+vMLa+tXS2Cz7HRqSKFTyI9s20JplfO6EiFUwF5S71jNt5hgeaXtVTHHy9NGJuN4tAT bGpDWa3Q==; 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 1raOoZ-00GbiL-0T; Wed, 14 Feb 2024 17:37:19 -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 06/31] ntsync: Introduce NTSYNC_IOC_CREATE_MUTEX. Date: Wed, 14 Feb 2024 17:36:42 -0600 Message-ID: <20240214233645.9273-7-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: 1790920451917984894 X-GMAIL-MSGID: 1790920451917984894 This corresponds to the NT syscall NtCreateMutant(). An NT mutex is recursive, with a 32-bit recursion counter. When acquired via NtWaitForMultipleObjects(), the recursion counter is incremented by one. The OS records the thread which acquired it. However, in order to keep this driver self-contained, the owning thread ID is managed by user-space, and passed as a parameter to all relevant ioctls. The initial owner and recursion count, if any, are specified when the mutex is created. Signed-off-by: Elizabeth Figura --- drivers/misc/ntsync.c | 67 +++++++++++++++++++++++++++++++++++++ include/uapi/linux/ntsync.h | 7 ++++ 2 files changed, 74 insertions(+) diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c index e173774eea52..5b7f93709587 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -24,6 +24,7 @@ enum ntsync_type { NTSYNC_TYPE_SEM, + NTSYNC_TYPE_MUTEX, }; struct ntsync_obj { @@ -40,6 +41,10 @@ struct ntsync_obj { __u32 count; __u32 max; } sem; + struct { + __u32 count; + __u32 owner; + } mutex; } u; /* @@ -119,6 +124,10 @@ static bool is_signaled(struct ntsync_obj *obj, __u32 owner) switch (obj->type) { case NTSYNC_TYPE_SEM: return !!obj->u.sem.count; + case NTSYNC_TYPE_MUTEX: + if (obj->u.mutex.owner && obj->u.mutex.owner != owner) + return false; + return obj->u.mutex.count < UINT_MAX; } WARN(1, "bad object type %#x\n", obj->type); @@ -161,6 +170,10 @@ static void try_wake_all(struct ntsync_device *dev, struct ntsync_q *q, case NTSYNC_TYPE_SEM: obj->u.sem.count--; break; + case NTSYNC_TYPE_MUTEX: + obj->u.mutex.count++; + obj->u.mutex.owner = q->owner; + break; } } wake_up_process(q->task); @@ -202,6 +215,28 @@ static void try_wake_any_sem(struct ntsync_obj *sem) } } +static void try_wake_any_mutex(struct ntsync_obj *mutex) +{ + struct ntsync_q_entry *entry; + + lockdep_assert_held(&mutex->lock); + + list_for_each_entry(entry, &mutex->any_waiters, node) { + struct ntsync_q *q = entry->q; + + if (mutex->u.mutex.count == UINT_MAX) + break; + if (mutex->u.mutex.owner && mutex->u.mutex.owner != q->owner) + continue; + + if (atomic_cmpxchg(&q->signaled, -1, entry->index) == -1) { + mutex->u.mutex.count++; + mutex->u.mutex.owner = q->owner; + wake_up_process(q->task); + } + } +} + /* * Actually change the semaphore state, returning -EOVERFLOW if it is made * invalid. @@ -361,6 +396,33 @@ static int ntsync_create_sem(struct ntsync_device *dev, void __user *argp) return put_user(fd, &user_args->sem); } +static int ntsync_create_mutex(struct ntsync_device *dev, void __user *argp) +{ + struct ntsync_mutex_args __user *user_args = argp; + struct ntsync_mutex_args args; + struct ntsync_obj *mutex; + int fd; + + if (copy_from_user(&args, argp, sizeof(args))) + return -EFAULT; + + if (!args.owner != !args.count) + return -EINVAL; + + mutex = ntsync_alloc_obj(dev, NTSYNC_TYPE_MUTEX); + if (!mutex) + return -ENOMEM; + mutex->u.mutex.count = args.count; + mutex->u.mutex.owner = args.owner; + fd = ntsync_obj_get_fd(mutex); + if (fd < 0) { + kfree(mutex); + return fd; + } + + return put_user(fd, &user_args->mutex); +} + static struct ntsync_obj *get_obj(struct ntsync_device *dev, int fd) { struct file *file = fget(fd); @@ -480,6 +542,9 @@ static void try_wake_any_obj(struct ntsync_obj *obj) case NTSYNC_TYPE_SEM: try_wake_any_sem(obj); break; + case NTSYNC_TYPE_MUTEX: + try_wake_any_mutex(obj); + break; } } @@ -668,6 +733,8 @@ static long ntsync_char_ioctl(struct file *file, unsigned int cmd, void __user *argp = (void __user *)parm; switch (cmd) { + case NTSYNC_IOC_CREATE_MUTEX: + return ntsync_create_mutex(dev, argp); case NTSYNC_IOC_CREATE_SEM: return ntsync_create_sem(dev, argp); case NTSYNC_IOC_WAIT_ALL: diff --git a/include/uapi/linux/ntsync.h b/include/uapi/linux/ntsync.h index 524404f6aceb..d68f24fd75a2 100644 --- a/include/uapi/linux/ntsync.h +++ b/include/uapi/linux/ntsync.h @@ -16,6 +16,12 @@ struct ntsync_sem_args { __u32 max; }; +struct ntsync_mutex_args { + __u32 mutex; + __u32 owner; + __u32 count; +}; + struct ntsync_wait_args { __u64 timeout; __u64 objs; @@ -30,6 +36,7 @@ struct ntsync_wait_args { #define NTSYNC_IOC_CREATE_SEM _IOWR('N', 0x80, struct ntsync_sem_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_SEM_POST _IOWR('N', 0x81, __u32) From patchwork Wed Feb 14 23:36: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: 201180 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp60052dyb; Wed, 14 Feb 2024 15:53:43 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUbl0v9z/eC05xG9+9Sn6liZNF4vruhb3OgvUFCvhrnHTmGHwWuUklzqxCy0kNLO7qH4XEaraz+6ZGiDhe1fzXeDi79bQ== X-Google-Smtp-Source: AGHT+IFfwGZVoSLyGa5d1Iu1c6hvegkUuQ0gH+O0LQZ6O+Pv4c18cZXHEo8nCLRDnHzwRQ2PJ1SQ X-Received: by 2002:a05:6214:3a8a:b0:68f:1102:680c with SMTP id nz10-20020a0562143a8a00b0068f1102680cmr2462995qvb.5.1707954823361; Wed, 14 Feb 2024 15:53:43 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707954823; cv=pass; d=google.com; s=arc-20160816; b=WDdpj4mw+esKXRNR1gdRDRFl5GQgz6mW06whvhvZCb/DkRIZm34DZYBW0heJJVtjks qkSk7L/FqdIadvacBqfQNsiN/HgEAnpzN4JC2FDU8udmfRPJgkznK6UKeOTp1PlvPgNG 6FjgiRAbjH/4c4PcBTqYxm0KKV5lhu+fBv0mkp0nl6BjwGmIGFSIZZ/DJAASNzJjncZ+ XZMXgDbWuV01mf3GlTrnLMpwXTA6lhkcd3yqASIHoWcZl370HXITdXVOj9zOWKqQmFoz HfkYxfga+qBM2pwvVTzMTj56s8Ok/Amh7/eo4Njjhp3D0iVlnRmyvRhY3qN2Q6ZFBOvt iu5g== 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=L7wuV3Wr5OefMrgO+45a5m75xF80Z46lGZQ4kmg1BtE=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=PmpiEUAgrVt94Zl2v+U4tbRZq63aRFxHSSBiVeEHvTJPi8jvz1fzmUFQT7k9ts+q9a fUc0hjc/qZtagYeIGsuAKJ2rdkDOTDDbJxwdI3OZ+ktN1UCRiNXQ6iOAubkaDLthrIBf XeMlCBlpquhSO7m2CZXnEjlfpYgguhLJZGsoTzsRt7drHfc/8hbjLkfhyF5HTkxbhFpN TqtFvu+R0CvnhhraymkpDfMWuj6cF522HbjeszsxumWN60v7lkmt/BQ40eWGcdgVrjZk LokKoSq+iGwoEIwdHW541N+FMh5ruMRp24KiL5CksI4xkgLr8Ii9SBZ8Dt5BzSwF4OUD PVHA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=bpGNDtTb; 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-66136-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66136-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id vw3-20020a05620a564300b0078726dd3ecbsi215103qkn.761.2024.02.14.15.53.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 15:53:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66136-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=bpGNDtTb; 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-66136-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66136-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 2A6DD1C21CBA for ; Wed, 14 Feb 2024 23:53:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EBAC014535A; Wed, 14 Feb 2024 23:53:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="bpGNDtTb" 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 D4A08145322; Wed, 14 Feb 2024 23:53:12 +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=1707954794; cv=none; b=ctNu42FcOgumZZWhN7241FFJX28U+/Xp6DiB+YwR4SKwGfLTpbG3rV/dipAFopqlMNFoYU4hka8tXGB1HllpSF3A3fzr/2wy604WWmu9vJG5iHhVUhfecMVe/Z5CC5Ue/fZnvViICjqtfzQ2BTNaTsNBNbTGL21yw/XP7rvqCV8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954794; c=relaxed/simple; bh=K6zdf91Wo6sPO0UUq27EuXcrBoHBiVRejjL6HL++Jxw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RIiFJnRqneHpA1e/kNCoiUYX3KSbpjXSPE00ZH2m0mxI/C3CEvo07UchPMnpIJ6qO8GKta69s3s/BMYUm5shvoURgDCyKlxZ19TSCYnRddM8Vf310KcY5NqsWOl3Tg6ZKAKFtlbkDoohzkWx2bJ7zp0vOjbSsTDxgSmvdEMGl88= 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=bpGNDtTb; 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=L7wuV3Wr5OefMrgO+45a5m75xF80Z46lGZQ4kmg1BtE=; b=bpGNDtTbcTJJ0J/whxj6D0L2A1 klRDVFO8F1coV+KysGWJarp5tbAPsV23AbimMUIoJMETeYkr//5m1SJkkh/ylc9ts8zicfBagc1Th ZGlYXLAXK/aWey9R7vNErJ95iqwtrkHDwGbjNNzHHzTOXczTD43QCprtp1S9uBmm7z7+1yUGoDIDz 1MfDZy+AmWlNIiuSlx4RrbodsajKBADsHT9xn0HvHJhmxeaDpbrEpNpjblmqsc2rf+iuyQSOx6EEH vSKkIFkwWi6kyiFdIO3BoFxWSLeUQoe6QxVDpy7ARayBoBc2sCmjpLQRQVww+5ZRUyqM/P7a3ZbRz ILeHMDVQ==; 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 1raOoZ-00GbiL-2A; Wed, 14 Feb 2024 17:37:19 -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 07/31] ntsync: Introduce NTSYNC_IOC_MUTEX_UNLOCK. Date: Wed, 14 Feb 2024 17:36:43 -0600 Message-ID: <20240214233645.9273-8-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: 1790920436961127968 X-GMAIL-MSGID: 1790920436961127968 This corresponds to the NT syscall NtReleaseMutant(). This syscall decrements the mutex's recursion count by one, and returns the previous value. If the mutex is not owned by the given owner ID, the function instead fails and returns -EPERM. Signed-off-by: Elizabeth Figura --- drivers/misc/ntsync.c | 64 +++++++++++++++++++++++++++++++++++++ include/uapi/linux/ntsync.h | 1 + 2 files changed, 65 insertions(+) diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c index 5b7f93709587..42041afd8d15 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -299,6 +299,68 @@ static int ntsync_sem_post(struct ntsync_obj *sem, void __user *argp) return ret; } +/* + * Actually change the mutex state, returning -EPERM if not the owner. + */ +static int unlock_mutex_state(struct ntsync_obj *mutex, + const struct ntsync_mutex_args *args) +{ + lockdep_assert_held(&mutex->lock); + + if (mutex->u.mutex.owner != args->owner) + return -EPERM; + + if (!--mutex->u.mutex.count) + mutex->u.mutex.owner = 0; + return 0; +} + +static int ntsync_mutex_unlock(struct ntsync_obj *mutex, void __user *argp) +{ + struct ntsync_mutex_args __user *user_args = argp; + struct ntsync_device *dev = mutex->dev; + struct ntsync_mutex_args args; + __u32 prev_count; + int ret; + + if (copy_from_user(&args, argp, sizeof(args))) + return -EFAULT; + if (!args.owner) + return -EINVAL; + + if (mutex->type != NTSYNC_TYPE_MUTEX) + return -EINVAL; + + if (atomic_read(&mutex->all_hint) > 0) { + spin_lock(&dev->wait_all_lock); + spin_lock_nest_lock(&mutex->lock, &dev->wait_all_lock); + + prev_count = mutex->u.mutex.count; + ret = unlock_mutex_state(mutex, &args); + if (!ret) { + try_wake_all_obj(dev, mutex); + try_wake_any_mutex(mutex); + } + + spin_unlock(&mutex->lock); + spin_unlock(&dev->wait_all_lock); + } else { + spin_lock(&mutex->lock); + + prev_count = mutex->u.mutex.count; + ret = unlock_mutex_state(mutex, &args); + if (!ret) + try_wake_any_mutex(mutex); + + spin_unlock(&mutex->lock); + } + + if (!ret && put_user(prev_count, &user_args->count)) + ret = -EFAULT; + + return ret; +} + static int ntsync_obj_release(struct inode *inode, struct file *file) { struct ntsync_obj *obj = file->private_data; @@ -318,6 +380,8 @@ static long ntsync_obj_ioctl(struct file *file, unsigned int cmd, switch (cmd) { case NTSYNC_IOC_SEM_POST: return ntsync_sem_post(obj, argp); + case NTSYNC_IOC_MUTEX_UNLOCK: + return ntsync_mutex_unlock(obj, argp); default: return -ENOIOCTLCMD; } diff --git a/include/uapi/linux/ntsync.h b/include/uapi/linux/ntsync.h index d68f24fd75a2..a3f5f4f13798 100644 --- a/include/uapi/linux/ntsync.h +++ b/include/uapi/linux/ntsync.h @@ -39,5 +39,6 @@ struct ntsync_wait_args { #define NTSYNC_IOC_CREATE_MUTEX _IOWR('N', 0x84, struct ntsync_sem_args) #define NTSYNC_IOC_SEM_POST _IOWR('N', 0x81, __u32) +#define NTSYNC_IOC_MUTEX_UNLOCK _IOWR('N', 0x85, struct ntsync_mutex_args) #endif From patchwork Wed Feb 14 23:36:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201191 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp62219dyb; Wed, 14 Feb 2024 15:58:33 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVCqg0jgUtqKPQcxpuXWOd0KjKZDiQdTBuyDJ+gfSADJL3jGjsaC7rEmCcb3r3S8dOUD8+0RCWHurZHKQXLnF6vmWR5lg== X-Google-Smtp-Source: AGHT+IHvIcAfpCARZoMM/YsRjZ9hFulu1SvtGVFtL+PCDceiU55LKO2S1DbMtBYZowYtpsKgYeLC X-Received: by 2002:a17:90a:a014:b0:296:1e9:2c91 with SMTP id q20-20020a17090aa01400b0029601e92c91mr287958pjp.31.1707955113756; Wed, 14 Feb 2024 15:58:33 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707955113; cv=pass; d=google.com; s=arc-20160816; b=ccO/j4AQVDTr/ZgXMzlLs3oI9XAl4cQvHxDqteCHEPWvV5kmCGMz+Vimc82VG6jCMk 2OPSKYdgSrZE/EHqC91mgRSViytSdWVJ6yZpRLEuKRdS7wqO4dWmuwE9XfHM9VctK/cG FXaFpRXUGgExVOB2+yqkgNNjXFMTJuqluowNjHOy6/OurQspL1vmFYtOdYNAzOxlQpZ0 V1WxAnzheommb1RUB1kIyQcUvbzPPiuWbFft4fPmKnwOab+bqA3KGC6k3gBtOzHxJSoS P6V+sY0tPjbu+/ov0HBmcZM8EBXenFRHYIcdZuCIcT3LOCWSCmf7OorasbIlX5+b02Jg sAsQ== 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=L81PuhjSIRjAjeyJBw9VXn44xHf+EZrYzbujwA6VKQQ=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=Fi1OArGJTN3dKMLBfEgvZs1Q1eONqsSfLTQVj6bNBsxrGBTtYe7FEwOm8UXV+mwGKt nXOAQ0kalfauGlGeo9Lovk29AHzBMUZaNiVaxngNLCOBb6BCyjspgvYf3H0G/p48mcmO +M7MyyQXWY9Q578TOcBpOPDo/MCv14TSvFKK9o2yLlY3Zt6rNFqXoYhpcI6zVMkmcIkh 3A4TXBg/8dJ/R9ymhYnZG0DeOSmp05ri+Ld0MgvUXtXSON4EtChNJoHLBmiTVqG/F4ok wPASxAFeBzeJsCaAJJ4iVORQaPTI0Ls/PWIjbzI7k6AWbJv38MPk+hkzy4TpmoIs+C9V 4qlQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=OjkZwJXh; 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-66145-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66145-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 pc16-20020a17090b3b9000b00298641bc176si127718pjb.43.2024.02.14.15.58.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 15:58:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66145-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=OjkZwJXh; 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-66145-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66145-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 D591328F9B6 for ; Wed, 14 Feb 2024 23:56:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F169E14534B; Wed, 14 Feb 2024 23:53:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="OjkZwJXh" 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 EB3321468FF; Wed, 14 Feb 2024 23:53:31 +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=1707954813; cv=none; b=j2dv+EV2bMIuEacAEd8VFPttLzXW0qOA1KNK7XEr1TGJ7meZtlWBKOTU0bqa+IJan2oZLycHMEL80y2P+wA9Zuj9ZT9ECPklbyfibaMCn5mFNeHZn0PqORs0vVbRCaHW0Frna1AU9QHmV7QdjnkBv//Fn+vhFeq6c5mVKVp6TRA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954813; c=relaxed/simple; bh=g2t5359dB4FigZaXrTNh4sSOe3d4Mc/XHu4ktisxSSw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=u5YkUpg3ntRm3+SbnnoOebCLWFPRuwa6CBWAD57MhctIMp9SX55XxnqJ4Rdu/96laW9fc6U4lvwo5F62S3NEtXrcwJ6qNieeY+vMuVQy7KH45tjHbXgXRTE8U+k14DxgxURorC881UivfpDOTGKWYchRROaUMp2d3SuSCI91AVs= 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=OjkZwJXh; 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=L81PuhjSIRjAjeyJBw9VXn44xHf+EZrYzbujwA6VKQQ=; b=OjkZwJXhvLjM4LJJ2/Z7pkKK3+ VUAD7carifJqWwtzDHOMkMVYJO0UX2IqqrcStW5Or1+unkvpslrwGilvo54TS/zWtuV35dblLdPXA zmHN98pYipcVfQp2c8hNRikNAeH8tUDdqNeR4tK16AWNC0lAChMllprGv/pVERRilpVoeIzM4I0eb D1iZBgdi5qzL3bn4SH/Eejzh3YSo7L1LRo6gYa7Ju4K3bwnFlExGE6QcEFZfpkkbilvsFMJNlMcXh G+hc2I6xiQG8+w1Uy3TPo+14VLt+/1tjYdzyQ3g9HIzhT92Sk7YeEhtSZHTQOmjbdq4ARcSCv4x42 nH05oQxg==; 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 1raOoa-00GbiL-0d; Wed, 14 Feb 2024 17:37:20 -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 08/31] ntsync: Introduce NTSYNC_IOC_MUTEX_KILL. Date: Wed, 14 Feb 2024 17:36:44 -0600 Message-ID: <20240214233645.9273-9-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: 1790920741525942512 X-GMAIL-MSGID: 1790920741525942512 This does not correspond to any NT syscall. Rather, when a thread dies, it should be called by the NT emulator for each mutex. NT mutexes are robust (in the pthread sense). When an NT thread dies, any mutexes it owned are immediately released. Acquisition of those mutexes by other threads will return a special value indicating that the mutex was abandoned, like EOWNERDEAD returned from pthread_mutex_lock(), and EOWNERDEAD is indeed used here for that purpose. Signed-off-by: Elizabeth Figura --- drivers/misc/ntsync.c | 71 +++++++++++++++++++++++++++++++++++-- include/uapi/linux/ntsync.h | 1 + 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c index 42041afd8d15..8cb951c76aca 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -44,6 +44,7 @@ struct ntsync_obj { struct { __u32 count; __u32 owner; + bool ownerdead; } mutex; } u; @@ -96,6 +97,7 @@ struct ntsync_q { atomic_t signaled; bool all; + bool ownerdead; __u32 count; struct ntsync_q_entry entries[]; }; @@ -171,6 +173,9 @@ static void try_wake_all(struct ntsync_device *dev, struct ntsync_q *q, obj->u.sem.count--; break; case NTSYNC_TYPE_MUTEX: + if (obj->u.mutex.ownerdead) + q->ownerdead = true; + obj->u.mutex.ownerdead = false; obj->u.mutex.count++; obj->u.mutex.owner = q->owner; break; @@ -230,6 +235,9 @@ static void try_wake_any_mutex(struct ntsync_obj *mutex) continue; if (atomic_cmpxchg(&q->signaled, -1, entry->index) == -1) { + if (mutex->u.mutex.ownerdead) + q->ownerdead = true; + mutex->u.mutex.ownerdead = false; mutex->u.mutex.count++; mutex->u.mutex.owner = q->owner; wake_up_process(q->task); @@ -361,6 +369,62 @@ static int ntsync_mutex_unlock(struct ntsync_obj *mutex, void __user *argp) return ret; } +/* + * Actually change the mutex state to mark its owner as dead, + * returning -EPERM if not the owner. + */ +static int kill_mutex_state(struct ntsync_obj *mutex, __u32 owner) +{ + lockdep_assert_held(&mutex->lock); + + if (mutex->u.mutex.owner != owner) + return -EPERM; + + mutex->u.mutex.ownerdead = true; + mutex->u.mutex.owner = 0; + mutex->u.mutex.count = 0; + return 0; +} + +static int ntsync_mutex_kill(struct ntsync_obj *mutex, void __user *argp) +{ + struct ntsync_device *dev = mutex->dev; + __u32 owner; + int ret; + + if (get_user(owner, (__u32 __user *)argp)) + return -EFAULT; + if (!owner) + return -EINVAL; + + if (mutex->type != NTSYNC_TYPE_MUTEX) + return -EINVAL; + + if (atomic_read(&mutex->all_hint) > 0) { + spin_lock(&dev->wait_all_lock); + spin_lock_nest_lock(&mutex->lock, &dev->wait_all_lock); + + ret = kill_mutex_state(mutex, owner); + if (!ret) { + try_wake_all_obj(dev, mutex); + try_wake_any_mutex(mutex); + } + + spin_unlock(&mutex->lock); + spin_unlock(&dev->wait_all_lock); + } else { + spin_lock(&mutex->lock); + + ret = kill_mutex_state(mutex, owner); + if (!ret) + try_wake_any_mutex(mutex); + + spin_unlock(&mutex->lock); + } + + return ret; +} + static int ntsync_obj_release(struct inode *inode, struct file *file) { struct ntsync_obj *obj = file->private_data; @@ -382,6 +446,8 @@ static long ntsync_obj_ioctl(struct file *file, unsigned int cmd, return ntsync_sem_post(obj, argp); case NTSYNC_IOC_MUTEX_UNLOCK: return ntsync_mutex_unlock(obj, argp); + case NTSYNC_IOC_MUTEX_KILL: + return ntsync_mutex_kill(obj, argp); default: return -ENOIOCTLCMD; } @@ -566,6 +632,7 @@ static int setup_wait(struct ntsync_device *dev, q->owner = args->owner; atomic_set(&q->signaled, -1); q->all = all; + q->ownerdead = false; q->count = count; for (i = 0; i < count; i++) { @@ -673,7 +740,7 @@ static int ntsync_wait_any(struct ntsync_device *dev, void __user *argp) struct ntsync_wait_args __user *user_args = argp; /* even if we caught a signal, we need to communicate success */ - ret = 0; + ret = q->ownerdead ? -EOWNERDEAD : 0; if (put_user(signaled, &user_args->index)) ret = -EFAULT; @@ -754,7 +821,7 @@ static int ntsync_wait_all(struct ntsync_device *dev, void __user *argp) struct ntsync_wait_args __user *user_args = argp; /* even if we caught a signal, we need to communicate success */ - ret = 0; + ret = q->ownerdead ? -EOWNERDEAD : 0; if (put_user(signaled, &user_args->index)) ret = -EFAULT; diff --git a/include/uapi/linux/ntsync.h b/include/uapi/linux/ntsync.h index a3f5f4f13798..3861397c6c2f 100644 --- a/include/uapi/linux/ntsync.h +++ b/include/uapi/linux/ntsync.h @@ -40,5 +40,6 @@ struct ntsync_wait_args { #define NTSYNC_IOC_SEM_POST _IOWR('N', 0x81, __u32) #define NTSYNC_IOC_MUTEX_UNLOCK _IOWR('N', 0x85, struct ntsync_mutex_args) +#define NTSYNC_IOC_MUTEX_KILL _IOW ('N', 0x86, __u32) #endif 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) From patchwork Wed Feb 14 23:52: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: 201185 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp61294dyb; Wed, 14 Feb 2024 15:56:10 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCW0ZPbbYgHV+r6Py1KIV5y+e6Le/G0rT6ipD7WU8rK5445mbK37920IajSvSI7Uzbi0ea+B8KIR3+2OyScxApUjl2nKJg== X-Google-Smtp-Source: AGHT+IFK3fkUEL6X9QFC+6PeW/H+k5ELn+sq0zz4xL4QHvOLWXGC5zc1jnKnz+nixmWY82Pa2QZq X-Received: by 2002:a1f:6283:0:b0:4c0:1c20:f827 with SMTP id w125-20020a1f6283000000b004c01c20f827mr9686vkb.8.1707954970273; Wed, 14 Feb 2024 15:56:10 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707954970; cv=pass; d=google.com; s=arc-20160816; b=waJ0Cbmi15PZj8iq85cxY2YWkDgN74eCrztesfbk+TgaV1DP5SuDwq7SEQ7V6SqAnW Ey2hYwpP+iOcIOtzFj7gICAVdcQDOSiDtynZAQKuE2AQdDzIjUsKJ5R+gB29VA3Gib1k uNGX855hL/Q4n6UgpbS0OeaGAn/2ByZn8LHSZhLw02zNVtRxMiC2w0gcuNDxwB+gm8de tNlgIGmVKLPGKU/v6MBb+btHUa+PQLt9JW9NaLHR5Xz3u8CR5rqRzzcc3yxkX3YOHI2T Ob/4ET3+6vUiij8pdAV3MQMsD9nyj5T7c9PjO47DIDaig95MXtaDrWJzMDIZZnfMZIEj Ei+g== 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=MxsYi41f9tZrdqx7LdjDj8QNEYN8tm/gWSuMV4y4n5k=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=jt3+IMNuzNr/gPzPDhakiwZZysAWiwPNZNL2v4w+jQqo+fqrjdUFsPzDliFoMWoMcd KTMlopDTurKp4ZXWNM6loXa7S0k/Pmj96e3b7+r6tz3WBz58hwuAyAWD3oZpW06k0b6O e8nfj0LpkDFVcFMpnn4Jk4zg0m0RmXDIoixGLynjrxU0czvLwWR+6WNb9v80e3cyg6ke ENvyCKyFTd0AYZHXxqPeF6AhHYqRZPgMJjoWuWoPBHQYETFb4PnWJUyAzBDNYAVkRgNy UK/tdeesI1osxxVhi504vgZ+r8LfK9sEtuQeZ2oWnu2VXQ17g9/XEGCFrbtQZLdlPsho mOSg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=CFdUGGkj; 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-66143-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66143-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id ey2-20020a0562140b6200b0068ccdb86eb6si218095qvb.4.2024.02.14.15.56.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 15:56:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66143-ouuuleilei=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=CFdUGGkj; 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-66143-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66143-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id DF8C21C2753A for ; Wed, 14 Feb 2024 23:55:26 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F3CE71474C5; Wed, 14 Feb 2024 23:53:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="CFdUGGkj" 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 DD9AA145339; Wed, 14 Feb 2024 23:53:29 +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=1707954811; cv=none; b=e5GFkaDO2OFHtt8ht048vdgy2OfrXmkgNow3rg7QHaZwGMEB8qAplU2fq1ytyvc2DZDuFP/YaMdBAiQcF+7RT0K7trsC09UlorByvPfgORBtJLp5/bGq9rgq3kDwEozvP0pJO/RHb7EFcuEKbpWv4T92CND02KkpzvDkiHrL2O4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954811; c=relaxed/simple; bh=ZiDY1OWbO8A0kyxcTCRHN/56efSCVGBgAzSPdarUcgI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gt9zdVd22Jb3P3D/vGZW/aATriHxr9wqxTgDUPiEUTfWnKyTSOMrTHUQ1Pw19Y0iiYUl3p0z6cGUzn7qzdh201ptUrndAxgsqgHglfWaFf3BI/c8t0fJ0Vhew9fdeBdHo4FXLU+nSp51ccOopyOCctFW+A2GBPYZ50v+YM18kuM= 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=CFdUGGkj; 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=MxsYi41f9tZrdqx7LdjDj8QNEYN8tm/gWSuMV4y4n5k=; b=CFdUGGkjQyLXTB63BQqeAVx/nF OgHHDlxUvcWp7eyR0LFIOiW9KbDJpkZOmSWVx2nVdMmYXajdxxw9kWteTYttNqmhcDAKurOsmNoso I8T+8wXHVReHmp1ybHGZ6O2dpAai0UOhwik6mfxccoimLGc+SJy6fKym7I3sD4wnJojK1Zl3O3M1G 7qX49a0WaEbm4Is2jzreGuZjdTcsZ19zxKS87PUiGsTWEtuJL8GEYnrV4zcHSNwP3mGvA4+LNCerr ZBnul6auuecHximlbnC66y/bUYuA14yrKgXnVTh9qFS0YivKHODLM00AmcmAgPs5uvoGJjLSN1MO0 nmMhsyGg==; 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 1raP44-00GcSK-1d; Wed, 14 Feb 2024 17:53:20 -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 10/31] ntsync: Introduce NTSYNC_IOC_EVENT_SET. Date: Wed, 14 Feb 2024 17:52:45 -0600 Message-ID: <20240214235307.10494-1-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: 1790920590917890512 X-GMAIL-MSGID: 1790920590917890512 This corresponds to the NT syscall NtSetEvent(). This sets the event to the signaled state, and returns its previous state. Signed-off-by: Elizabeth Figura --- drivers/misc/ntsync.c | 37 +++++++++++++++++++++++++++++++++++++ include/uapi/linux/ntsync.h | 1 + 2 files changed, 38 insertions(+) diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c index 2a21e7b0982a..bc24de02fd1c 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -456,6 +456,41 @@ static int ntsync_mutex_kill(struct ntsync_obj *mutex, void __user *argp) return ret; } +static int ntsync_event_set(struct ntsync_obj *event, void __user *argp) +{ + struct ntsync_device *dev = event->dev; + __u32 prev_state; + + if (event->type != NTSYNC_TYPE_EVENT) + return -EINVAL; + + if (atomic_read(&event->all_hint) > 0) { + spin_lock(&dev->wait_all_lock); + spin_lock_nest_lock(&event->lock, &dev->wait_all_lock); + + prev_state = event->u.event.signaled; + event->u.event.signaled = true; + try_wake_all_obj(dev, event); + try_wake_any_event(event); + + spin_unlock(&event->lock); + spin_unlock(&dev->wait_all_lock); + } else { + spin_lock(&event->lock); + + prev_state = event->u.event.signaled; + event->u.event.signaled = true; + try_wake_any_event(event); + + spin_unlock(&event->lock); + } + + if (put_user(prev_state, (__u32 __user *)argp)) + return -EFAULT; + + return 0; +} + static int ntsync_obj_release(struct inode *inode, struct file *file) { struct ntsync_obj *obj = file->private_data; @@ -479,6 +514,8 @@ static long ntsync_obj_ioctl(struct file *file, unsigned int cmd, return ntsync_mutex_unlock(obj, argp); case NTSYNC_IOC_MUTEX_KILL: return ntsync_mutex_kill(obj, argp); + case NTSYNC_IOC_EVENT_SET: + return ntsync_event_set(obj, argp); default: return -ENOIOCTLCMD; } diff --git a/include/uapi/linux/ntsync.h b/include/uapi/linux/ntsync.h index b8cf503365ef..782057552483 100644 --- a/include/uapi/linux/ntsync.h +++ b/include/uapi/linux/ntsync.h @@ -48,5 +48,6 @@ struct ntsync_wait_args { #define NTSYNC_IOC_SEM_POST _IOWR('N', 0x81, __u32) #define NTSYNC_IOC_MUTEX_UNLOCK _IOWR('N', 0x85, struct ntsync_mutex_args) #define NTSYNC_IOC_MUTEX_KILL _IOW ('N', 0x86, __u32) +#define NTSYNC_IOC_EVENT_SET _IOR ('N', 0x88, __u32) #endif From patchwork Wed Feb 14 23:52:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201184 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp61271dyb; Wed, 14 Feb 2024 15:56:08 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUqwV/EQQY5+5tD8LNcmmDKkCWy3GLdnibxVlSXuhCSXWi1KZiKs97bY376XwOpjOYlie/E5v7vq9IA7C53gXu8zp43sA== X-Google-Smtp-Source: AGHT+IEuVwqMII8jztlVb3ThDegqtPZ31NVtKWVy9g3CAuv5JHElEI0r+0XOOr4p1Q4crYAU0U42 X-Received: by 2002:ac8:5c43:0:b0:42c:6416:b4f4 with SMTP id j3-20020ac85c43000000b0042c6416b4f4mr372478qtj.27.1707954968396; Wed, 14 Feb 2024 15:56:08 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707954968; cv=pass; d=google.com; s=arc-20160816; b=J27jqgLY9xtpCAUBzqwVNeeBJN3R9JQFRz3IEM3L4kMqleIoJbhngZJJvEKqXc7ByU yWynieCicXXhFu6miyrltJRIA4JhwPomw9bTl1B2LasGjxW1lQOzqLXrip8lcgv/wocL G4bo+cpZxhhMyAd86jGJx/gsbfpjaRRudNpF6lUy+MgM1jw0Cs/pPkOllWRzGaveb63M rclybd4vSCPMHC49vO1OrTNdZLfygPxDCpqhuVpauDVTbmDz4CXbMTxzwLS+mjLR2g3n 24lcjrMezEkCdQFBQiDt6lLBJ54URobaFGi57GlmGfg+FyjRiqBVzbHcSaJhqRazGGbd JgMg== 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=ysylq2RMZIH24LbG0jmFl4YpEtJVTp+RFmS8r+eXNAU=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=jelLT9sr2S/zC4ApCGI5XjfzP3yd1JnNrhguY+eIVWjOtUPViPLmqXPYyQKepORmFp aGN63CWRSIJwdBsYgwOr8bA1TBuTKygbClrqFfYtRMCXaik8x6BPZoLqOwxOdGaVEV6z dxgCvoNk1nQRf8iNQvSHYks6MT+yvcP814gayUOXUcX/GO6z4AvL+pQ8hbEsGYKS0dRi 9SUR/1XvHMfyq4BS+lJeLl7R0bHW/Zsd5HdNyprKjt3MHz+BREC6Tzkr/yi02fLgZUAP ndsbgCTE+hMiCgdDFKHkyurMxn0O4BPr8QFTea95S4qfcwt6BzAB323RQpVbye8fw7ik v4jw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b="Wip/RTMI"; 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-66142-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66142-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id 3-20020ac85743000000b0042db439fc96si117947qtx.514.2024.02.14.15.56.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 15:56:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66142-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b="Wip/RTMI"; 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-66142-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66142-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 8A6531C26BE4 for ; Wed, 14 Feb 2024 23:55:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CF2641474BB; Wed, 14 Feb 2024 23:53:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="Wip/RTMI" 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 BA585146011; Wed, 14 Feb 2024 23:53:29 +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=1707954811; cv=none; b=uZPcYtwCRFEXSCWvvLCZDgLVZhcAgToXJOMCFUMqVvu+1NZLkYmVHPBq9Y7SroHoE0DR2ZvoRkb5lY3FARfSAp5K+VLCd+57Kqt3qygK3bWMPVTct2VEIcl5RxjZH9SINrG280Mvn2egcp8eqM+s2+ROZJl/t/t0hKSUeLDJxRw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954811; c=relaxed/simple; bh=iNoJnS47rRHG70TRY/f6nIJyk4KHDH1hiPc+CQPBb8E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=l599iRbnfszWVd3phbdummvC3SpW2pz29dGe6QPV65kTgxnSS0iG4m6y+yv2yytvFACn68neTqTnkMPP8UJfwfJw/Jxm6t0fbAy1i9AgKea9qgVNi8WmiJswm6FPaylcMZlpF4CZINQI5up/hj3Tler6dCBNWdiyNvdtPoGGAUs= 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=Wip/RTMI; 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=ysylq2RMZIH24LbG0jmFl4YpEtJVTp+RFmS8r+eXNAU=; b=Wip/RTMI5/kTkwfD+ng/5Q49mi JEWcMzTNSykQPwp3F4yRheF7LDNVqT2t6kCPa7BnQYfrz+aV/Vz8TD4cIa5BQv/LDxcLtOpN2FVnw 0091iXLF8LiN8TBBHqTJlHILupureelN8827AagDZshNTWLnJGfXYOP/+XvXLVQTTmip4k7yxablt J2nNW/PMfqYGHdAb0vz0gTkf8N255mOxkPMJnqRtvhjiw3huOoyc+JYvzkRM03E1ZiNgf/MNsHcpb E7hZQIVlKB6QR+NCSv6to58oRC67XG1trwQneqBSpS88Cdatsqw62H4463nub2UxW7GBClw+LZo8N tdtWeS8Q==; 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 1raP45-00GcSK-12; Wed, 14 Feb 2024 17:53:21 -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 11/31] ntsync: Introduce NTSYNC_IOC_EVENT_RESET. Date: Wed, 14 Feb 2024 17:52:46 -0600 Message-ID: <20240214235307.10494-2-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240214235307.10494-1-zfigura@codeweavers.com> References: <20240214233645.9273-1-zfigura@codeweavers.com> <20240214235307.10494-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: 1790920588773734997 X-GMAIL-MSGID: 1790920588773734997 This corresponds to the NT syscall NtResetEvent(). This sets the event to the unsignaled state, and returns its previous state. Signed-off-by: Elizabeth Figura --- drivers/misc/ntsync.c | 22 ++++++++++++++++++++++ include/uapi/linux/ntsync.h | 1 + 2 files changed, 23 insertions(+) diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c index bc24de02fd1c..57d78b086241 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -491,6 +491,26 @@ static int ntsync_event_set(struct ntsync_obj *event, void __user *argp) return 0; } +static int ntsync_event_reset(struct ntsync_obj *event, void __user *argp) +{ + __u32 prev_state; + + if (event->type != NTSYNC_TYPE_EVENT) + return -EINVAL; + + spin_lock(&event->lock); + + prev_state = event->u.event.signaled; + event->u.event.signaled = false; + + spin_unlock(&event->lock); + + if (put_user(prev_state, (__u32 __user *)argp)) + return -EFAULT; + + return 0; +} + static int ntsync_obj_release(struct inode *inode, struct file *file) { struct ntsync_obj *obj = file->private_data; @@ -516,6 +536,8 @@ static long ntsync_obj_ioctl(struct file *file, unsigned int cmd, return ntsync_mutex_kill(obj, argp); case NTSYNC_IOC_EVENT_SET: return ntsync_event_set(obj, argp); + case NTSYNC_IOC_EVENT_RESET: + return ntsync_event_reset(obj, argp); default: return -ENOIOCTLCMD; } diff --git a/include/uapi/linux/ntsync.h b/include/uapi/linux/ntsync.h index 782057552483..f2d7507d8438 100644 --- a/include/uapi/linux/ntsync.h +++ b/include/uapi/linux/ntsync.h @@ -49,5 +49,6 @@ struct ntsync_wait_args { #define NTSYNC_IOC_MUTEX_UNLOCK _IOWR('N', 0x85, struct ntsync_mutex_args) #define NTSYNC_IOC_MUTEX_KILL _IOW ('N', 0x86, __u32) #define NTSYNC_IOC_EVENT_SET _IOR ('N', 0x88, __u32) +#define NTSYNC_IOC_EVENT_RESET _IOR ('N', 0x89, __u32) #endif From patchwork Wed Feb 14 23:52:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201190 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp62176dyb; Wed, 14 Feb 2024 15:58:26 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWR4z8gzBm1K4oZOgV2XD72S2wGww/W2w2OvbxOldp8Tipyei3dyxOKrbhnEAChfwO9N9rjEiRz7z9avoL+aFiOPOnWqw== X-Google-Smtp-Source: AGHT+IGhdbLahR5vNYAHKVNDFn2IT+Wc2UESbwFXlCiy4/JrKkTwoX13poe8DJPqJy65ZDd+tFFS X-Received: by 2002:a17:902:c1c4:b0:1d9:ec19:2c95 with SMTP id c4-20020a170902c1c400b001d9ec192c95mr128062plc.58.1707955105851; Wed, 14 Feb 2024 15:58:25 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707955105; cv=pass; d=google.com; s=arc-20160816; b=XZpo//rSFemlEamWmHYu/7jDB+YNduvt8SN2+5INOTbUfTmw47m/mIpJ9JTngr4MzU y5+Oc1ZMuzN7S+uy3nDLRPPpcab5y10ylo3WwQfJfNOh7Nb0J97HnaxGQs1fWF+aLfvL 3o9dHmIfYxH6++PcjMNbhATVUJF4XanPIOsP7aA9CXbS/ufkFVP05QNJMyLpEXS76JEY OgYz+9vDr3avG67uJvdQzbxmCrFJawRZcOlD3LLM29ZUASjeJspCjePIeSKS2TChpNZ6 DCsY/wonLIPjAUSu9vv5liDKVyXPlDq3LXpDSE9o7xNxgCyBEV4xZj+1UYZO0PuwOZ7x nHcw== 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=Uw+YvaVU15ffM3A3JgczPQnnrN3QpOwwpO53Okg4P4E=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=mL0X+GdZNsUMKoxhH/nqrYmhTPKQ1MwSFD7P3hk5wbDH1rXU/ixkEjh+QJjGMv0Y3u lUJ6Pw/K2hvUThsxC/dfftrzl1l8JWHdMxVXuU7TPA6tCnGA3pzJocKWodwGH/T6vGWB wEnyH5s4xb9GdrcyA804Ub0T/5ARGA4vJy35T0O4tF3JI643wpisGHmvLY3k/wccK7fV Ecx4lfjJX0wk8xhb+OUkL4rHpegL33F/ueu+/E8vQGhIZgS964RhdR56X2FTFhNZHf3E Iw6RsI+pDoJam5bNmhEhfmcvKgeoi8WO70r++g8IQ9eJsSqsFdX1Zfkgyl/aZpn8X0RZ 2cqg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=rZGFSYLg; 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-66144-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66144-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. [139.178.88.99]) by mx.google.com with ESMTPS id b5-20020a170902650500b001db675b3a9asi28534plk.377.2024.02.14.15.58.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 15:58:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66144-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=rZGFSYLg; 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-66144-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66144-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 34C1D28E960 for ; Wed, 14 Feb 2024 23:56:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 727941474D1; Wed, 14 Feb 2024 23:53:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="rZGFSYLg" 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 EB26C1468FD; Wed, 14 Feb 2024 23:53:31 +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=1707954813; cv=none; b=jZthh6JyxuGC+L6fGwPyhRZOaSD3yQdpOde6wKFcfkUW0pb4dSG6ExbdonzMyiMaig2NKPIeQIpCoNwcsj2BT2HG8AupdcyO1RJul4f+uK99Rlnj+pJPJ9LNGHSuqTB9h5h161g/2MIwlwXEnxaQs5ctgTIPgNxpmEgC1VVz+QU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954813; c=relaxed/simple; bh=BWsVN5Nd9zC4qkmCZzbH4sC7w+cs5Ze1LqiDpK6tZ0A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IdXvBi048mKZNHK/5xeQo82BJ6XgPJ4fwX9UN9Ykgqdy2K9dxPNTwMzC97CKYvsFZ9br+OX2bnzecHQmtLwyjMGI5KATbNaPlXgbUvRSnJ+bu6zUKeqoEQE08keHlE0iVQsjKOwolJlxif98WikcTPEsLrW9DnX7NVC/OOr8T8w= 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=rZGFSYLg; 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=Uw+YvaVU15ffM3A3JgczPQnnrN3QpOwwpO53Okg4P4E=; b=rZGFSYLg4jw4IbY3qq5tAfHU0M I8lUpi9v35Z1C0x6h1K/qIfE83vQHpzglujzeV/J9BXQfALYoOkN9JuaVu/NH9FYYhvhgd/9E6BMy 8+KAsZP12ETBWY4diDpnKaIY0l8EPyflu6I/ODD3kNq8FEAY4cDBXDMgSBUNyaE5EaH6TLCpay1yq GW5skFY49cUDu2q72spUKxQd6OOC1U1f49R68P6y+HSAHQj8E5rjwd1AS+U0pjR/qisKN6SppgDba gFxXuD5m6w5mUW/Hgx6zy07lx6zGXnRAp7WnBTa7SfDs3+a3iQooq9VA65wG5ARSzv0ptLGJEBeMa Torntyrg==; 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 1raP46-00GcSK-1f; Wed, 14 Feb 2024 17:53: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 12/31] ntsync: Introduce NTSYNC_IOC_EVENT_PULSE. Date: Wed, 14 Feb 2024 17:52:47 -0600 Message-ID: <20240214235307.10494-3-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240214235307.10494-1-zfigura@codeweavers.com> References: <20240214233645.9273-1-zfigura@codeweavers.com> <20240214235307.10494-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: 1790920733462970668 X-GMAIL-MSGID: 1790920733462970668 This corresponds to the NT syscall NtPulseEvent(). This wakes up any waiters as if the event had been set, but does not set the event, instead resetting it if it had been signalled. Thus, for a manual-reset event, all waiters are woken, whereas for an auto-reset event, at most one waiter is woken. Signed-off-by: Elizabeth Figura --- drivers/misc/ntsync.c | 10 ++++++++-- include/uapi/linux/ntsync.h | 1 + 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c index 57d78b086241..e8e58274e4e8 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -456,7 +456,7 @@ static int ntsync_mutex_kill(struct ntsync_obj *mutex, void __user *argp) return ret; } -static int ntsync_event_set(struct ntsync_obj *event, void __user *argp) +static int ntsync_event_set(struct ntsync_obj *event, void __user *argp, bool pulse) { struct ntsync_device *dev = event->dev; __u32 prev_state; @@ -472,6 +472,8 @@ static int ntsync_event_set(struct ntsync_obj *event, void __user *argp) event->u.event.signaled = true; try_wake_all_obj(dev, event); try_wake_any_event(event); + if (pulse) + event->u.event.signaled = false; spin_unlock(&event->lock); spin_unlock(&dev->wait_all_lock); @@ -481,6 +483,8 @@ static int ntsync_event_set(struct ntsync_obj *event, void __user *argp) prev_state = event->u.event.signaled; event->u.event.signaled = true; try_wake_any_event(event); + if (pulse) + event->u.event.signaled = false; spin_unlock(&event->lock); } @@ -535,9 +539,11 @@ static long ntsync_obj_ioctl(struct file *file, unsigned int cmd, case NTSYNC_IOC_MUTEX_KILL: return ntsync_mutex_kill(obj, argp); case NTSYNC_IOC_EVENT_SET: - return ntsync_event_set(obj, argp); + return ntsync_event_set(obj, argp, false); case NTSYNC_IOC_EVENT_RESET: return ntsync_event_reset(obj, argp); + case NTSYNC_IOC_EVENT_PULSE: + return ntsync_event_set(obj, argp, true); default: return -ENOIOCTLCMD; } diff --git a/include/uapi/linux/ntsync.h b/include/uapi/linux/ntsync.h index f2d7507d8438..598f894f868d 100644 --- a/include/uapi/linux/ntsync.h +++ b/include/uapi/linux/ntsync.h @@ -50,5 +50,6 @@ struct ntsync_wait_args { #define NTSYNC_IOC_MUTEX_KILL _IOW ('N', 0x86, __u32) #define NTSYNC_IOC_EVENT_SET _IOR ('N', 0x88, __u32) #define NTSYNC_IOC_EVENT_RESET _IOR ('N', 0x89, __u32) +#define NTSYNC_IOC_EVENT_PULSE _IOR ('N', 0x8a, __u32) #endif From patchwork Wed Feb 14 23:52:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201192 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp62289dyb; Wed, 14 Feb 2024 15:58:45 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVgh7yBibv6mkjgXySvyvGutaG4FFHKFWiOLBgVT/TIRVgY119iSssfARFbbDD7CXIN9ITQ5Mos5yJ1ucZFMnivJsK5IQ== X-Google-Smtp-Source: AGHT+IFaMmsvC5vo3zvGNAftuCsH5cfAYzlmyzgG5iZAYA067bJBTvsr3wpkM3l+LX4S5q5IDO6R X-Received: by 2002:a5b:991:0:b0:dc7:32ea:c89f with SMTP id c17-20020a5b0991000000b00dc732eac89fmr110377ybq.15.1707955125401; Wed, 14 Feb 2024 15:58:45 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707955125; cv=pass; d=google.com; s=arc-20160816; b=vEh8Vs+iADIgwJgM62KBV6Qm/y2vI5BUNr5RFj5EFy6hgkQD/arkxgA8F3IxQlHpBb ZnfysxtdsL9mvG7rrA6WaBtrCiyduiHfM/i7pFvVUhH+tmgyhZ1t62TtePShSzvXF+ea vvYgTAStbKvDOp6pcH7npsHCgRBLjyg6cZqW26v9U64IPYS6VFpLsd0Yq4fScc0XSd1A smZSd2Mab+qcPSa1VOiYoPgw1tRlJBesaiT6Pb65sVFrxJwX4nr0h0yZ5m6pVjtkszQj wWWC8FwdiYwv1YFeNOLkoE43au48p+MtNY/dGySUtPBhLM8JDGW9PkkdkK0tI9sc/92M VFoA== 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=1u6I14flCka8KvOp2oxl+aQw0301epnL+50/ePKMgkQ=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=O3dWhspQRVxnnNUkwETkumabXqHsa71ZTa6oDCBZ8knthoyM8uoZ71htvnCV098W/h t/fySMtVs8EG+oFxjZ/B5Y2AFWje6akrehMOhlTgxiPy5cRp1k6QgEyFq3UuGYJ0ks+T mi84CnozXCQNp7RE9NAK4XBk0wEQhQCfdvGUiQBD/q6ZtR/Z+cWxEZfwNRFw5nQs3SSp 356CCm9imxcL7hXHZzFU+IOfoatpk3PxR+vAtvDlwmLS5cgoZRwaBJZFZmuYVCkGu14V yYBJdlLs6UwLtoGzTJwHEpqUuWafpU0r29LNpL/dUskr2aVibGehdTgrFHg26vgEBG77 ssDA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b="T/9JqNhq"; 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-66146-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66146-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 b13-20020a65668d000000b005dbe215856dsi5875pgw.765.2024.02.14.15.58.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 15:58:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66146-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="T/9JqNhq"; 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-66146-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66146-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 ECAA9291AA1 for ; Wed, 14 Feb 2024 23:56:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0895E1487F6; Wed, 14 Feb 2024 23:53:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="T/9JqNhq" 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 A86AE1474B2; Wed, 14 Feb 2024 23:53:33 +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=1707954815; cv=none; b=g/ZCYHAEJ5vV4P/hseNWVXb0GF4uXRp5i2m1Fcy2+yHqdwbBlo55pNWFtpmmnpEEfO8VHB/C48WqofsZqpAWbMP4nVRJG42sTivIKATNoePKxXvCnJeVJXNiyknVz3buC09/4zvTUFWVIEwJYucH2OalgdDhlIU2xYsu7tNSQaI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954815; c=relaxed/simple; bh=SoOGgKEby/90/0Z1NNUySJMvgGZ/kgGpsJcso4mQE8U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=c/2G8HGy1ypDpoTmITwoC66Lt2CE8wqnmUNqLpVI4IPCL5KhYJERxPfZLDFG4FXGhW/b0HZFR1OQYCv58nsRX8PIHYRtQLXxbv8LeNY1uKKZwCpgIiJeXkGp9BO/CfEqgAqDeiKKHFhEKSxu48YQ5pPJKgRr+c0jL0AhwrE/O/s= 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=T/9JqNhq; 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=1u6I14flCka8KvOp2oxl+aQw0301epnL+50/ePKMgkQ=; b=T/9JqNhqVElMAvvaEPKdNTINrp jfHVmSQB4xyzs8wH1I/u0FpJHwQkzXsS3HMu8MGGNxw6OGY+W/TvJKPApUb25tKe5pNfkw3Vd8mBw 1qww/NufbrvOXQ5BEeugzNB3lsUAl+u3/wpplRawEudLda0JSM+FTwukL3+yIy8amhP0FzVYZlmXI b7trE/fMe0yKt2tO5+tiNdu/N8dluMNQaOxKP9ZBwU7uvuVN5foWS7L/DXYxdpcOSU6vwUXbozwQJ OIjyfFLChma2eYcRv5QG2kOGPI/CappG/F65dEEORO2xpfDl//ytr4uVsUdPYeEacEpOq1T8EaNrO GUZ8ojsA==; 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 1raP48-00GcSK-0e; Wed, 14 Feb 2024 17:53:27 -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 13/31] ntsync: Introduce NTSYNC_IOC_SEM_READ. Date: Wed, 14 Feb 2024 17:52:48 -0600 Message-ID: <20240214235307.10494-4-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240214235307.10494-1-zfigura@codeweavers.com> References: <20240214233645.9273-1-zfigura@codeweavers.com> <20240214235307.10494-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: 1790920753657075428 X-GMAIL-MSGID: 1790920753657075428 This corresponds to the NT syscall NtQuerySemaphore(). This returns the current count and maximum count of the semaphore. Signed-off-by: Elizabeth Figura --- drivers/misc/ntsync.c | 21 +++++++++++++++++++++ include/uapi/linux/ntsync.h | 1 + 2 files changed, 22 insertions(+) diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c index e8e58274e4e8..e42dbd5d6d14 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -515,6 +515,25 @@ static int ntsync_event_reset(struct ntsync_obj *event, void __user *argp) return 0; } +static int ntsync_sem_read(struct ntsync_obj *sem, void __user *argp) +{ + struct ntsync_sem_args __user *user_args = argp; + struct ntsync_sem_args args; + + if (sem->type != NTSYNC_TYPE_SEM) + return -EINVAL; + + args.sem = 0; + spin_lock(&sem->lock); + args.count = sem->u.sem.count; + args.max = sem->u.sem.max; + spin_unlock(&sem->lock); + + if (copy_to_user(user_args, &args, sizeof(args))) + return -EFAULT; + return 0; +} + static int ntsync_obj_release(struct inode *inode, struct file *file) { struct ntsync_obj *obj = file->private_data; @@ -534,6 +553,8 @@ static long ntsync_obj_ioctl(struct file *file, unsigned int cmd, switch (cmd) { case NTSYNC_IOC_SEM_POST: return ntsync_sem_post(obj, argp); + case NTSYNC_IOC_SEM_READ: + return ntsync_sem_read(obj, argp); case NTSYNC_IOC_MUTEX_UNLOCK: return ntsync_mutex_unlock(obj, argp); case NTSYNC_IOC_MUTEX_KILL: diff --git a/include/uapi/linux/ntsync.h b/include/uapi/linux/ntsync.h index 598f894f868d..6017f621687e 100644 --- a/include/uapi/linux/ntsync.h +++ b/include/uapi/linux/ntsync.h @@ -51,5 +51,6 @@ struct ntsync_wait_args { #define NTSYNC_IOC_EVENT_SET _IOR ('N', 0x88, __u32) #define NTSYNC_IOC_EVENT_RESET _IOR ('N', 0x89, __u32) #define NTSYNC_IOC_EVENT_PULSE _IOR ('N', 0x8a, __u32) +#define NTSYNC_IOC_SEM_READ _IOR ('N', 0x8b, struct ntsync_sem_args) #endif From patchwork Wed Feb 14 23:52:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201187 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp61564dyb; Wed, 14 Feb 2024 15:56:48 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWWRfBz/uRDODiHea6cX9Ibd3NSmhZ3RLnXQf/3V8ok+W1pzV/UKgybKTwlVVRyBcvAraVQXAPbvnW1Wko2XjWqZ1LPjg== X-Google-Smtp-Source: AGHT+IHf/eZr2EWJ2cY1Hwf9xwjP3F+BnBDAQsl8u1BsxTK01oYsm9amDX983wlbWSE+cDmKxS4n X-Received: by 2002:a54:4513:0:b0:3c0:387e:81d8 with SMTP id l19-20020a544513000000b003c0387e81d8mr315461oil.56.1707955007835; Wed, 14 Feb 2024 15:56:47 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707955007; cv=pass; d=google.com; s=arc-20160816; b=wA4Z9vo/zOYjRhL+dq1sCnL9wnY3Ri/qm//v6t7VDg+WmkAo/0qwj4K3zLQQR3oFSU YRd9iq0BndeEUZ/mWvJLCNhumn1JvgWWBmcChEkNJMPyZgUZCjfU8w7WEmLGN0VoOmRm tmoFnZwpiJZBezoOXPb5+oJLUK5A4OeQWiJuM3ijehIW/gTFGcUhgLXh4xy9E4DgRyzJ k0R8gYnvcrvZgVATGNJgtnFqmoYpDlw43DFtdaVR/Hk4zq0ZAG3ARXWa+hM617nEssJa C+0kxljf4Fc0ex2Trzw+ce9nzEkAtBzGLFFdNaGpUO3cdSoxN6OKjdOHlDggyop9mH+h /o7w== 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=uQl4WA7220ZuCfcaIH4Qlmtix4tUAUiNvK0NQ8JN3Nw=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=r/axv6rUPyhlERd1i085EpiQft4ileaNXhVbUh2+U1P/uqQgPKBOrc6ZfrvNSRvf5f Wq24kc6UACIOl9s47Kez6cb5jx7Cw9rVrcl2nL+YvbVtIjXHMmXrAUAo5I6ECs30eeLh mLuylTadbCk72WbQatq4i7+yP9jktfVh/89Et4oCm8f+f8l+jnPey6/P2TMhnGEtu8f9 CzXkAn0uVM5QEN46iUaNtkpvFgTcgR5VCpN6VTNWTKkuJCmrdSHFL+BpYVNzAkXacSMp q9lFMlfBQ0d7ifDuZ6CO/fIQ50CEPem52V2JIRcdCDyWP1WroLWyoEOpX9ExUqxiFLdv xJDw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b="R/VHQ4jF"; 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-66147-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66147-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id i24-20020ac860d8000000b00429d470ea36si93747qtm.592.2024.02.14.15.56.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 15:56:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66147-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b="R/VHQ4jF"; 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-66147-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66147-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id BAF431C20FAF for ; Wed, 14 Feb 2024 23:56:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4EA14148FE3; Wed, 14 Feb 2024 23:53:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="R/VHQ4jF" 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 017661474C6; Wed, 14 Feb 2024 23:53:33 +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=1707954815; cv=none; b=jhnO82tGkeYjYN3nRCdEy4xSLmtkrnPbWB+frsjQYFk4JxsUO2TUxFEVo71ajqkquubFbh4vbJC0d452e7yS7aYK2MBuqPaCUoaW+iOT01SIHRmXrRTRETpoXdJLc7QlAXbh2DpgRm7Du26Sh71r4J6QtaCIDSO7xPKTIbWXkTo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954815; c=relaxed/simple; bh=MyMUfnSpsCJiRaJ8cEmQDM/KYjA/E0/cZRMs+5Iwf7A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SbxKrlUDfAVJLy+OInkdYFb/uTJImafUtAhlH+J1aiO7PbURG7BDxSJCKAyGBwUHmU8MHStskK4B0dzF1RRFG/muFfUjUqFm5M6y17u9yVkM20+fxSGYmKD8S4yTgucD2ceG4XiwmEwQHBSmYL/SIfnYrReFPt1k0Fcnpw7C5k4= 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=R/VHQ4jF; 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=uQl4WA7220ZuCfcaIH4Qlmtix4tUAUiNvK0NQ8JN3Nw=; b=R/VHQ4jF4E8d+MHXaWp0P/PnCG K1nI1M9RvNHUb8GjmAzs9skt5NhDl6hybj0z5PHcLD+/inC7BmcSa9BFItgPP7bHuaxt51WNFCLcx z+gRp4aB4koQm3blnawOjOcGVGmSAq75UScID4Gpr8P0VRsWazgemkXLYJkIAB38AfgQwYiI2guUO frSxOMqoUiNLEr6oxN/GGurFtuxGdPHpn0C4tRUJx/QhdOTuSTVgooNTqLVOo8hNeoaB8vj7WCOtw 90EVCWnRtQKYZWhn67aUGzroZ8Y2F2EbMLXocTHfA7q86Nl3T+c2QODAj7mmDyJi7lSVnr67wuF4P iAysJ2Pw==; 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 1raP4D-00GcSK-1F; Wed, 14 Feb 2024 17:53:29 -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 14/31] ntsync: Introduce NTSYNC_IOC_MUTEX_READ. Date: Wed, 14 Feb 2024 17:52:49 -0600 Message-ID: <20240214235307.10494-5-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240214235307.10494-1-zfigura@codeweavers.com> References: <20240214233645.9273-1-zfigura@codeweavers.com> <20240214235307.10494-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: 1790920630407736057 X-GMAIL-MSGID: 1790920630407736057 This corresponds to the NT syscall NtQueryMutant(). This returns the recursion count, owner, and abandoned state of the mutex. Signed-off-by: Elizabeth Figura --- drivers/misc/ntsync.c | 23 +++++++++++++++++++++++ include/uapi/linux/ntsync.h | 1 + 2 files changed, 24 insertions(+) diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c index e42dbd5d6d14..73f40ea1c8f8 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -534,6 +534,27 @@ static int ntsync_sem_read(struct ntsync_obj *sem, void __user *argp) return 0; } +static int ntsync_mutex_read(struct ntsync_obj *mutex, void __user *argp) +{ + struct ntsync_mutex_args __user *user_args = argp; + struct ntsync_mutex_args args; + int ret; + + if (mutex->type != NTSYNC_TYPE_MUTEX) + return -EINVAL; + + args.mutex = 0; + spin_lock(&mutex->lock); + args.count = mutex->u.mutex.count; + args.owner = mutex->u.mutex.owner; + ret = mutex->u.mutex.ownerdead ? -EOWNERDEAD : 0; + spin_unlock(&mutex->lock); + + if (copy_to_user(user_args, &args, sizeof(args))) + return -EFAULT; + return ret; +} + static int ntsync_obj_release(struct inode *inode, struct file *file) { struct ntsync_obj *obj = file->private_data; @@ -559,6 +580,8 @@ static long ntsync_obj_ioctl(struct file *file, unsigned int cmd, return ntsync_mutex_unlock(obj, argp); case NTSYNC_IOC_MUTEX_KILL: return ntsync_mutex_kill(obj, argp); + case NTSYNC_IOC_MUTEX_READ: + return ntsync_mutex_read(obj, argp); case NTSYNC_IOC_EVENT_SET: return ntsync_event_set(obj, argp, false); case NTSYNC_IOC_EVENT_RESET: diff --git a/include/uapi/linux/ntsync.h b/include/uapi/linux/ntsync.h index 6017f621687e..a1d0ef581212 100644 --- a/include/uapi/linux/ntsync.h +++ b/include/uapi/linux/ntsync.h @@ -52,5 +52,6 @@ struct ntsync_wait_args { #define NTSYNC_IOC_EVENT_RESET _IOR ('N', 0x89, __u32) #define NTSYNC_IOC_EVENT_PULSE _IOR ('N', 0x8a, __u32) #define NTSYNC_IOC_SEM_READ _IOR ('N', 0x8b, struct ntsync_sem_args) +#define NTSYNC_IOC_MUTEX_READ _IOR ('N', 0x8c, struct ntsync_mutex_args) #endif From patchwork Wed Feb 14 23:52:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201209 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp74255dyb; Wed, 14 Feb 2024 16:22:38 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWwrvbcU7Uy7uR2f4KDEU9LxW0RMnXPuXULb2RUF9THGhyccJ9qEvGm+luLiIUecRa1/Qh1CV6CjioFe5sKNSLZEXPA3Q== X-Google-Smtp-Source: AGHT+IG+mgEGIhYiqPfmThwDjWEYyQeEWrXcrxfXTFigOgN5F/r1MW7AO8vPw8+1L8OzkyRN8laG X-Received: by 2002:a17:902:7888:b0:1d9:5cfc:ac6a with SMTP id q8-20020a170902788800b001d95cfcac6amr215979pll.69.1707956558799; Wed, 14 Feb 2024 16:22:38 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707956558; cv=pass; d=google.com; s=arc-20160816; b=yzfJB8aY90nU8SrJ2XQsdWTkLZNA/yYMj0eczkn5sDt1JrpUaLfysbXYFXd2QdXzmY X9TrxBZFCXdTZk+a6zLxol8yI9s/2u6mUNjl4e+UrV4hyqWMvRlmn0gK9FYC1reXlLXC UDrwpw+9q1M6NaSRClITRYw9J6PbEeL68b8xYA4IjYfYKyVFs6lTGOScZGshQDmJw8K8 jdNvplHLfpZStY01WFHMw023K7E+jLp6UqYNDFPXw54ok1IWrF0Bzp/GSm2K060/TTqk syYbqq6+e3VHJHW0WoudB6rjGZjpdg2R51iqsX/IvS1TFb+DgyKRE4+S18wTRHiNRlmK S0nQ== 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=AL5zjVdXuExjwQk+undZ6ylI2OzNOm8lFsQUivBQ0kE=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=sKxonMBkQ/y0C8T8qBQKd9lWwARh2GDLCmZRZ+a36RqsvIZUjts9GA8QzcZ5I9MIIm 2l2luqfjlXTZ6953xf0ASJTtpssh9kxBFxbWabl5VGRyWv5FQDWrhvcikoTybjXuNw77 Yj5pNvl6Gr0efS/5L2TdbcMwWTTzIN36hFXO91LuNtWOohSqbhQehOfGaftVeyo4X6Hn tZmLr6HJFzl6xorW4dZ1AvGg0vekCxNxD3JJKH6PbJvriLxrdSUxw1CVTghURn3xJMi2 D6/XQ6j3QVPWB/fc56npCSSLPbsZN3Ivug1pi3zrmXwzBK/MdeSoyDAe4xylHzoRk9ly JeGA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=Ljdg3gM1; 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-66149-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66149-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id z2-20020a170902d54200b001d92e40f68asi72644plf.69.2024.02.14.16.22.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 16:22:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66149-ouuuleilei=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=Ljdg3gM1; 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-66149-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66149-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 9D6E4B2AB35 for ; Wed, 14 Feb 2024 23:57:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C272014A082; Wed, 14 Feb 2024 23:53:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="Ljdg3gM1" 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 6134C1482ED; Wed, 14 Feb 2024 23:53:35 +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=1707954817; cv=none; b=lK6SjNYvT1CtuVmg8o8XP/sUFgu+pme2UgTcH36P1qjynD/fEL8z9k/TCZ42k+zSSvv/KBDA8ur+/PEtvoClc2SWawfcFOnUwjFrch6XiBAuJmeU2CgwRJyosyNkrSa/kRwJawfSuAXxo9ADSPwBUBzCuqQN7KVavpjTNBtrFIo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954817; c=relaxed/simple; bh=hOO6pI/Zoh0v+lz+RmYsAAK/7RhFCidMqxtVLgR3VjA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QtmjpATGVxJo5sFr1UwqJsEDOYOwHs0Ln52Hywi9zHepwIbTxNevtRiy2KJyu6wu7lnS0UMZL/5Q1jkEkMrsXaRzWIzpowYwU/wAZGtgCjci6v5OlJjgA9nqzFRtVLrNG021fYpVcIBXa7ud/oxlt7uvuJp0aNVICSePb6F8qms= 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=Ljdg3gM1; 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=AL5zjVdXuExjwQk+undZ6ylI2OzNOm8lFsQUivBQ0kE=; b=Ljdg3gM16KAUQBJlZaKxtXg4YC n/A78hCG0YEzPgZNxZNq+D16bsJlnZYbeS6f17ft+EA1gkRl2zwEe0huj/cs46OV4b7Nc3fc5eRAr 3EVfdXp82cPmg+vKYThuWCzaC/1+n/9Msqy8BwG5Mdg9lMRIVian7bnLDfn4anuOrdn0n0mIsF2Jw raKdln/TeymreuERliUcb0+WQa2ny45mnOhq0gAJQLfCaXWJoEpSdzYAMIHmRdJoRXNO4+gHtEIRc O3DpdgRfg3vmRVj6TNVDxEBFrJU+ve/WC1qc370YccoR8/Fby8zh02ZcVqHnMaQ2+l5tudE/VDs8k OzkEUxzg==; 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 1raP4F-00GcSK-0d; Wed, 14 Feb 2024 17:53:31 -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 15/31] ntsync: Introduce NTSYNC_IOC_EVENT_READ. Date: Wed, 14 Feb 2024 17:52:50 -0600 Message-ID: <20240214235307.10494-6-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240214235307.10494-1-zfigura@codeweavers.com> References: <20240214233645.9273-1-zfigura@codeweavers.com> <20240214235307.10494-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: 1790922256210199429 X-GMAIL-MSGID: 1790922256210199429 This corresponds to the NT syscall NtQueryEvent(). This returns the signaled state of the event and whether it is manual-reset. Signed-off-by: Elizabeth Figura --- drivers/misc/ntsync.c | 21 +++++++++++++++++++++ include/uapi/linux/ntsync.h | 1 + 2 files changed, 22 insertions(+) diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c index 73f40ea1c8f8..5bf7af8095e6 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -555,6 +555,25 @@ static int ntsync_mutex_read(struct ntsync_obj *mutex, void __user *argp) return ret; } +static int ntsync_event_read(struct ntsync_obj *event, void __user *argp) +{ + struct ntsync_event_args __user *user_args = argp; + struct ntsync_event_args args; + + if (event->type != NTSYNC_TYPE_EVENT) + return -EINVAL; + + args.event = 0; + spin_lock(&event->lock); + args.manual = event->u.event.manual; + args.signaled = event->u.event.signaled; + spin_unlock(&event->lock); + + if (copy_to_user(user_args, &args, sizeof(args))) + return -EFAULT; + return 0; +} + static int ntsync_obj_release(struct inode *inode, struct file *file) { struct ntsync_obj *obj = file->private_data; @@ -588,6 +607,8 @@ static long ntsync_obj_ioctl(struct file *file, unsigned int cmd, return ntsync_event_reset(obj, argp); case NTSYNC_IOC_EVENT_PULSE: return ntsync_event_set(obj, argp, true); + case NTSYNC_IOC_EVENT_READ: + return ntsync_event_read(obj, argp); default: return -ENOIOCTLCMD; } diff --git a/include/uapi/linux/ntsync.h b/include/uapi/linux/ntsync.h index a1d0ef581212..582d33b0dcac 100644 --- a/include/uapi/linux/ntsync.h +++ b/include/uapi/linux/ntsync.h @@ -53,5 +53,6 @@ struct ntsync_wait_args { #define NTSYNC_IOC_EVENT_PULSE _IOR ('N', 0x8a, __u32) #define NTSYNC_IOC_SEM_READ _IOR ('N', 0x8b, struct ntsync_sem_args) #define NTSYNC_IOC_MUTEX_READ _IOR ('N', 0x8c, struct ntsync_mutex_args) +#define NTSYNC_IOC_EVENT_READ _IOR ('N', 0x8d, struct ntsync_event_args) #endif From patchwork Wed Feb 14 23:52:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201210 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp74520dyb; Wed, 14 Feb 2024 16:23:20 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU/HROf4r+GjBdXrrfe7ha6csHosYxmDNHQBNN+qBJW96VrZywNZa81kS99KkA9iefxYULRwqSAE02IsvjSwp3tT0mjKQ== X-Google-Smtp-Source: AGHT+IELd9RJP8+N5ETUQXBdHtkj14OMtW2D0WSOHKBt0ZW240KbM+7IrXDEwgsjIJEmVKBy0nL1 X-Received: by 2002:a05:6830:1409:b0:6e2:b5fd:440d with SMTP id v9-20020a056830140900b006e2b5fd440dmr181574otp.6.1707956600553; Wed, 14 Feb 2024 16:23:20 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707956600; cv=pass; d=google.com; s=arc-20160816; b=C+9B9qFFVrE6amDZIYfb0eGZOPonyI8kvPboS9sA39RfE47H3B/9Kv60rXEal20ATS Hrj21QF50Ky/yGVC1AlppWSdYjEDU07WyuY5+tdv/Iq2cKNaAy7uVKxJkyh2pNXlF3Mh MEtuJmHmQwFJkiKpRX/6JUqAbUqJ4Wuakh3hDNkQDtkhVvWmPZtlCVQObf4QnLgn0ktk G8jh7QBJoEWB4aAAeA6W9sjb6hWQOrDDCRsBMO8HxQsM6i2CZ8BvIp69asvserae3gUW psUocpz+0URiGoWUMS7FOHPYXy4oky5lKlRE1O5NniJ574A2MRLgv7L0FLD70mk1BBFB +6pg== 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=aZYNOwb29gWNQzvTlREYZIBloWSXlYgqolSN8OjEbzk=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=0Ze7h+RJEYLPTVdiOPJQsNt9rfK5bhFArbd6kDcB+UW3WZM/KmSN6bmfR8oeQ8WOWX fVLCUBC3YVmEUB8GxKlIxI1kvdBlO+cSxiYLNvVWydowMBAefdz9JP5VPFDzPpW61nMq 8/S/e8Ui0Opw9aiFNqjxFpWObUlK7D7jHAhEN5LAfxY6u8jwsR4bFbT21XS9xuSMtAFb himgvQA1im8d5irTrGY5g39C9ubctxfRY1JxH/EC5m5fLuocnovthFz6Dcnhl5l0wmGX LvKD3UZJidoymNiHecFpIL9C645YCi9mV/NFFjzqvzAIxlZDUICsuWebk1yY2yFF31r5 PzPQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=ZFm4GufW; 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-66150-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66150-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id ca17-20020a056a02069100b005dc8587419esi46332pgb.421.2024.02.14.16.23.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 16:23:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66150-ouuuleilei=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=ZFm4GufW; 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-66150-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66150-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 18082B2AF9B for ; Wed, 14 Feb 2024 23:57:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 537AF14A4CE; Wed, 14 Feb 2024 23:53:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="ZFm4GufW" 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 5936B1487E3; Wed, 14 Feb 2024 23:53:37 +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=1707954819; cv=none; b=tgQutCpiTbqM2X3MIpnCY/gO43tZJp7uWdb5Qq3Ek2yfS4MXx4dhegUny8+RaiTutYufTJqsSgaARULitQX3AoKGNiwJdAdF3tM3Csn6kCShu+us0EILcbY6MWhQ4DAby8HlxYrwV0L6s3vxpB8Zhry8JtI6mCA6ilpGcnmdafM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954819; c=relaxed/simple; bh=rtyTKQiLKDkf87TwOX0OHpNk/dIQS6klJO2zDXPJGZY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FBpfjYrzkUp8t+7o7wHKH/6FUGtO3A93IVKZMmfSq9tw1YWmf20z37dEZP/OrIYf7F5VlnB4H9BGsG1OeoRNsWcyaO2ZLhWX7HSkH3VrKqLUFcapUkMslSGRoDLtPLTjyJJ1yAR2l8FWGXrJkBsFa408QR6dnQ7tBFVZCjgEk1k= 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=ZFm4GufW; 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=aZYNOwb29gWNQzvTlREYZIBloWSXlYgqolSN8OjEbzk=; b=ZFm4GufW/ApDIHtPUhPbGEbxk9 8cMdCp0qEOcdNw10BKqT2bRoeqB1TCU5lPyXsgXfgkl3KkDXN3tEclNpaznubK3NWkMQkoDTHDYCm 49btvJrqVrzvUiqWCnFAuPmrN+QmSNMy8Y0H1GnkGFfTDNUyxrM8Ewf0Od0hBHRGvtn+ja9Ma/Okv IGYD7wnqD3HDmMSzcdyc1Ag77NZtbRVfUsRYyj65dus3d/N5bEuOWYDGBUHcJ92CNFE5wlpLY6nQ3 mlcrbu3FAc7kQEoE4NKH6LpTFVwBi64DSN1F/3Wj0X0wmQCAeyL7XK+trPgTFjBOLzYXQgEtV0YxE sEI7IGyA==; 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 1raP4G-00GcSK-1n; Wed, 14 Feb 2024 17:53:32 -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 16/31] ntsync: Introduce alertable waits. Date: Wed, 14 Feb 2024 17:52:51 -0600 Message-ID: <20240214235307.10494-7-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240214235307.10494-1-zfigura@codeweavers.com> References: <20240214233645.9273-1-zfigura@codeweavers.com> <20240214235307.10494-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: 1790922300638611673 X-GMAIL-MSGID: 1790922300638611673 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 5bf7af8095e6..0773602cc86d 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -795,22 +795,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; @@ -820,7 +827,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]); @@ -870,9 +877,9 @@ 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; - __u32 i; int ret; if (copy_from_user(&args, argp, sizeof(args))) @@ -882,9 +889,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; @@ -893,9 +904,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) @@ -912,7 +929,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; @@ -972,6 +989,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 */ @@ -979,6 +1004,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 */ ret = ntsync_schedule(q, &args); @@ -1001,6 +1041,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 From patchwork Wed Feb 14 23:52:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201189 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp62061dyb; Wed, 14 Feb 2024 15:58:02 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXrllcDIic97zcMEvaz0HnvxhpycTusA5RreKTkft05xqI0L5atnGHGIFRFWlIJfCZdQnOGDty0GDpIoyWFLe5328HgwQ== X-Google-Smtp-Source: AGHT+IGw9YWDxxBiooCPxmkjX75Y9/7XGiroE1kTfQAl9V//ctWNYjf+ZcddplmhnmyXuFbD59KA X-Received: by 2002:a2e:8e77:0:b0:2d0:a6d3:56c6 with SMTP id t23-20020a2e8e77000000b002d0a6d356c6mr105533ljk.32.1707955082292; Wed, 14 Feb 2024 15:58:02 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707955082; cv=pass; d=google.com; s=arc-20160816; b=YwAiALG3fPr2Ci4QP93uIrfnNQ0XWPEh+iFd+5itUJNyYG7wj+Y+D1QqdPT1/hwj0Q 9JRdAcMFwwCjOQDk8LYhlE6smbgLqPTjC177PygNDpeRp7t66hMs92Hwg099z0Px84ux +VbXKYA7Cc0RXcbOh8G5B9BPVhc8UZt55xvqYiTqSYTxr7mQPoYnNWIhqNP6HBMyE8Tt ek8mtPoJg2XqV6FEOtD2JbOmrxpKZMy3m0TnCSccvX2jF2p6Yq/4PFauOBxm4YktIST6 bAFTj367cndjJFjgSQ+bi9LsKXODF/9P52gnNDaHAkA2rP30Zn/RI4TKT7Ptn5/7s7AY 6DvQ== 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=38M0BRywykSfrPjqrIMMa7pA+nqCF6Vo5IqJFLs5TlI=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=r35JMcOQ8lcgYAfkIowRaqhip/FoggLHZjwQY8GN6cCVBqMcDku6NoA83FSSe7av2R QDrQiRqQL1BzxCr7DZhi/oj91P+yu8JYqQvQ2uWPx0J2/Yab+1CWXxYEY1akyAEc4GjY HGlhQj1AY46/SG7PDL4mZIvnuApzkNpipOsAQMd/xxtgjlIL0ecrWOht4F0vZfC1C9tC QTfoyMzTyfdVm2HxGmF23VRK+bcpdnZMKhpQxouixs4vT7C9jLgWM+178XEw9SLeZHbZ 39jpQXfkURzAGAEypb3QEkInHD3lHxfdNGO0SX0Fo6LkTa0etMOtKvJLx+Dnz2bV+P2k sBFw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=FkwuwuiO; 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-66152-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66152-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id r16-20020a056402035000b00560c8889cfcsi37766edw.280.2024.02.14.15.58.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 15:58:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66152-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=FkwuwuiO; 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-66152-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66152-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 842011F2252B for ; Wed, 14 Feb 2024 23:57:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 63BC014A4CD; Wed, 14 Feb 2024 23:53:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="FkwuwuiO" 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 22C741487FA; Wed, 14 Feb 2024 23:53:37 +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=1707954819; cv=none; b=igYhffPJgUKXYBbs+v/21JlRZrx4KYkqwRajUEwfdRH4hQ0V0mxIGlN7J9wbkV5XrtjvjNiSIA1D5iVZPK+O6H23QcCfPCGiAoHbE4ZrmZCRF6726TqTRhOErLcoBDiF05WIOZZT4a7My9ro70a+mQDmqcQyZSgxfH6Cm0ihP44= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954819; c=relaxed/simple; bh=aBdrEJ9OoB7TRKIOn6djUqNQcuFcE/19mZ3vUSewMWE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jyiF7WHzhiHUK1XtFHEAWq8C5Yf0N32gRmViPxIZbebK0pPGVCpVi9PC/PFnIoH0LFzRAaCPTQg0py/EwPyJswWmn5ydaPmFoRBD5rJ+7py7rhp86t+BsC10hGzixD0T7JmZTe2glKIHVyZl8j6N0rN3lYp3Yhj09uLQNKEyugc= 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=FkwuwuiO; 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=38M0BRywykSfrPjqrIMMa7pA+nqCF6Vo5IqJFLs5TlI=; b=FkwuwuiO3cvXXaJgkbKStjEbOa CPxS+dprY1NOaHTBE4HePTIl41Lf2/oSkyR0j33yTzzBAIOm400uc6aR0VYfiCNLha7j9n4+xhvjn ucFQ9SFztf8htvJFg31Wq7MvvikBSvSOwGPNcWYOzrNZxwESKxbvWAkNZR0/nvLPmmcHwLrbFPfyz HEwUza1kfrl6ivN6DkYT8vbXu/2E9bZdZ76LUQX5W7QPCZHhCpdU3UpS1bCADVHlv+sJ/BVOZH397 WOI6kYpdfo9r2PdXKyaEGvwBQTtQkVb1d3kCtQmaSCTe4QwHeqwLxnU4WxSnlsawn2gB1hNoKYJJR 9S69igyg==; 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 1raP4H-00GcSK-2B; Wed, 14 Feb 2024 17:53:33 -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 17/31] ntsync: Allow waits to use the REALTIME clock. Date: Wed, 14 Feb 2024 17:52:52 -0600 Message-ID: <20240214235307.10494-8-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240214235307.10494-1-zfigura@codeweavers.com> References: <20240214233645.9273-1-zfigura@codeweavers.com> <20240214235307.10494-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: 1790920708426647538 X-GMAIL-MSGID: 1790920708426647538 NtWaitForMultipleObjects() can receive a timeout in two forms, relative or absolute. Relative timeouts are unaffected by changes to the system time and do not count down while the system suspends; for absolute timeouts the opposite is true. In order to make the interface and implementation simpler, the ntsync driver only deals in absolute timeouts. However, we need to be able to emulate both behaviours apropos suspension and time adjustment, which is achieved by allowing either the MONOTONIC or REALTIME clock to be used. Signed-off-by: Elizabeth Figura --- drivers/misc/ntsync.c | 9 ++++++++- include/uapi/linux/ntsync.h | 4 ++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c index 0773602cc86d..496704268603 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -765,11 +765,15 @@ static void put_obj(struct ntsync_obj *obj) static int ntsync_schedule(const struct ntsync_q *q, const struct ntsync_wait_args *args) { ktime_t timeout = ns_to_ktime(args->timeout); + clockid_t clock = CLOCK_MONOTONIC; ktime_t *timeout_ptr; int ret = 0; timeout_ptr = (args->timeout == U64_MAX ? NULL : &timeout); + if (args->flags & NTSYNC_WAIT_REALTIME) + clock = CLOCK_REALTIME; + do { if (signal_pending(current)) { ret = -ERESTARTSYS; @@ -781,7 +785,7 @@ static int ntsync_schedule(const struct ntsync_q *q, const struct ntsync_wait_ar ret = 0; break; } - ret = schedule_hrtimeout(timeout_ptr, HRTIMER_MODE_ABS); + ret = schedule_hrtimeout_range_clock(timeout_ptr, 0, HRTIMER_MODE_ABS, clock); } while (ret < 0); __set_current_state(TASK_RUNNING); @@ -804,6 +808,9 @@ static int setup_wait(struct ntsync_device *dev, if (!args->owner) return -EINVAL; + if (args->pad || (args->flags & ~NTSYNC_WAIT_REALTIME)) + return -EINVAL; + if (args->count > NTSYNC_MAX_WAIT_COUNT) return -EINVAL; diff --git a/include/uapi/linux/ntsync.h b/include/uapi/linux/ntsync.h index 7c91af7011e4..074f26423426 100644 --- a/include/uapi/linux/ntsync.h +++ b/include/uapi/linux/ntsync.h @@ -28,6 +28,8 @@ struct ntsync_event_args { __u32 signaled; }; +#define NTSYNC_WAIT_REALTIME 0x1 + struct ntsync_wait_args { __u64 timeout; __u64 objs; @@ -35,6 +37,8 @@ struct ntsync_wait_args { __u32 owner; __u32 index; __u32 alert; + __u32 flags; + __u32 pad; }; #define NTSYNC_MAX_WAIT_COUNT 64 From patchwork Wed Feb 14 23:52:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201195 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp62543dyb; Wed, 14 Feb 2024 15:59:34 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVDmIx1lzvYqAg0N61t9Ec1S18u6RSyLT5h0JTeT/6LO8EGes8WR4r8poosMQWfeyJZddxitJE+rQzXDAjASSBtmf8meg== X-Google-Smtp-Source: AGHT+IFq+CJvmp361xOiipgZgYdnZkTCr5jDJKG1DEAxLQJslerK59jyCpogvWuAfu1u67MK1csT X-Received: by 2002:a17:902:ee86:b0:1d9:6895:81c3 with SMTP id a6-20020a170902ee8600b001d9689581c3mr168639pld.22.1707955174189; Wed, 14 Feb 2024 15:59:34 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707955174; cv=pass; d=google.com; s=arc-20160816; b=SsgzoHG2oXFd3gHGW3kVYYp8ANAlZouvApRtFj0bsmGDVo2ndT1HYbphJSlJNBzcQX 6O4b/9fIBWc1H8Wj2OXKhncQXnthafAK1pGb+a+c1Shm0SyqqLcAa5uv/uDV9vt4VC0U AF8IZjjlwRfKKdJpZbNeMyF4qziXjbtc/2Ju2/mbiCOXe1EDr9NtU3rb/Ufxlq9QFiRB bI1wts/kWF+aSBr5L9JAw9QbSTfvhfM4yn6aitCmicPIL03O5VWRjxPCOG+KOxy6Z3mW d0NV6mjHnLUbnwPpHyrFjRB5Xsxzr62cN0J6KUXyejPYXhGDqM6KMp0X2Nf5uVC/qP24 cA8g== 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=4od/v8Sb5ySlWmB6E/7HZUJ+/S4RiseN4NnQ8umsq34=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=eVxI3QzfGAXNWFALkHG78h9oWsYd/Tf8M+CNVqcDpt6lmbXmip7AayEN4eM09lRDzS pciRrs3ZfQyu29OGkVNsU2ie0f5ApkOPf0tUlI7OMEsHwmSh7PX9gN5TyWoJ34de5fNd ldELH1Bv5qKUn8r5fqObXQXZQiNBYYc+IodIfUCw/ER/55d2SerI0kO5aSxm2x/bO/a9 /No8xkLZoNCUAe+HWUdLuSHMbBRNmPGGNZI5tWmr+zL+1vj3g0Zmbi9w12pdlinvt8o9 3mGsWhjHxXFkOUvjXNljLK2rihS5yFqE4AbpyttWtBa4bYkge6oFHFmmSG3Sinmb6CZX ZEpg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=DDp5FE3o; 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-66153-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66153-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. [139.178.88.99]) by mx.google.com with ESMTPS id n12-20020a170903110c00b001da2b5252dasi29706plh.319.2024.02.14.15.59.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 15:59:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66153-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=DDp5FE3o; 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-66153-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66153-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 5A136287AE2 for ; Wed, 14 Feb 2024 23:58:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 640D9145357; Wed, 14 Feb 2024 23:53:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="DDp5FE3o" 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 480E6149002; Wed, 14 Feb 2024 23:53:39 +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=1707954821; cv=none; b=ocEg8Uo8aFYXsxeNfxsrPMkZRxSooRXZ8xKrvBIUF4z2aaMgroRCqPWFd6PpHPKuI+dQ3W8nT0ZDZgpuCPaTow1uD4nzDSZL0ecHZd7uVVRtAM6ajgvZDqWUHGGH7HjZ4GruNjI90Xib22DOPNJy6aUuoJuCqep/hU4dGk73tJg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954821; c=relaxed/simple; bh=IfixxKNJpM8gkNQfWVNaov+nAOCyAZDzOSFlHY+kX5o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=igIuinKHAXi66W6z/jTCHCx+RzV3bC8FcWVlrppGCjt/olLlLp+Ow1hJZ05N7vGqvc23hhinuMHrbAePEkQgNb711+sZCCjzzhk7ccaVVqVpa62aY7jrUehg+wpfg/2W5dnGAL8InkwIb/P+H9qffSHhos5IVWep4UvDfIY7LZA= 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=DDp5FE3o; 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=4od/v8Sb5ySlWmB6E/7HZUJ+/S4RiseN4NnQ8umsq34=; b=DDp5FE3o7p3y6JV4DNpxXbXhIM IbrLkgEN/ha293LYaOEMs5avpSQ+ZRLspXG85YREiP2LeCPHWeuiwWfzc4b9r0Sm9MdY4YxLDbf3Q dLIO/+ffuT2Kwp7WvDmPcO6/wlVv4d9kj3pmGA42x1NKoL+1h69QCPSbDKvS8uW6zOEvmudTSb3UM OWy2LrUNMmdKkLn29H7Y9KLfL0TldNIz5wt7nQ7rGslP1b2xOorL6OtjXNicD8sI7i3mr2nyOOLCG RWNar0lUf7u1JRM8ksWhpXtd7MwAHd9PnFBe+0fw+RTz73ftkI0UARDJkC5Y54hTjV17LUeIL8s18 FMFfq/3g==; 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 1raP4I-00GcSK-2k; Wed, 14 Feb 2024 17:53:35 -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 18/31] selftests: ntsync: Add some tests for semaphore state. Date: Wed, 14 Feb 2024 17:52:53 -0600 Message-ID: <20240214235307.10494-9-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240214235307.10494-1-zfigura@codeweavers.com> References: <20240214233645.9273-1-zfigura@codeweavers.com> <20240214235307.10494-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: 1790920804425870928 X-GMAIL-MSGID: 1790920804425870928 Wine has tests for its synchronization primitives, but these are more accessible to kernel developers, and also allow us to test some edge cases that Wine does not care about. This patch adds tests for semaphore-specific ioctls NTSYNC_IOC_SEM_POST and NTSYNC_IOC_SEM_READ, and waiting on semaphores. Signed-off-by: Elizabeth Figura --- tools/testing/selftests/Makefile | 1 + .../testing/selftests/drivers/ntsync/Makefile | 8 + tools/testing/selftests/drivers/ntsync/config | 1 + .../testing/selftests/drivers/ntsync/ntsync.c | 149 ++++++++++++++++++ 4 files changed, 159 insertions(+) create mode 100644 tools/testing/selftests/drivers/ntsync/Makefile create mode 100644 tools/testing/selftests/drivers/ntsync/config create mode 100644 tools/testing/selftests/drivers/ntsync/ntsync.c diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index 15b6a111c3be..6c714a4e6478 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile @@ -15,6 +15,7 @@ TARGETS += cpu-hotplug TARGETS += damon TARGETS += dmabuf-heaps TARGETS += drivers/dma-buf +TARGETS += drivers/ntsync TARGETS += drivers/s390x/uvdevice TARGETS += drivers/net/bonding TARGETS += drivers/net/team diff --git a/tools/testing/selftests/drivers/ntsync/Makefile b/tools/testing/selftests/drivers/ntsync/Makefile new file mode 100644 index 000000000000..a34da5ccacf0 --- /dev/null +++ b/tools/testing/selftests/drivers/ntsync/Makefile @@ -0,0 +1,8 @@ +# SPDX-LICENSE-IDENTIFIER: GPL-2.0-only +TEST_GEN_PROGS := ntsync + +top_srcdir =../../../../.. +CFLAGS += -I$(top_srcdir)/usr/include +LDLIBS += -lpthread + +include ../../lib.mk diff --git a/tools/testing/selftests/drivers/ntsync/config b/tools/testing/selftests/drivers/ntsync/config new file mode 100644 index 000000000000..60539c826d06 --- /dev/null +++ b/tools/testing/selftests/drivers/ntsync/config @@ -0,0 +1 @@ +CONFIG_WINESYNC=y diff --git a/tools/testing/selftests/drivers/ntsync/ntsync.c b/tools/testing/selftests/drivers/ntsync/ntsync.c new file mode 100644 index 000000000000..bda88233b427 --- /dev/null +++ b/tools/testing/selftests/drivers/ntsync/ntsync.c @@ -0,0 +1,149 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Various unit tests for the "ntsync" synchronization primitive driver. + * + * Copyright (C) 2021-2022 Elizabeth Figura + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include "../../kselftest_harness.h" + +static int read_sem_state(int sem, __u32 *count, __u32 *max) +{ + struct ntsync_sem_args args; + int ret; + + memset(&args, 0xcc, sizeof(args)); + ret = ioctl(sem, NTSYNC_IOC_SEM_READ, &args); + *count = args.count; + *max = args.max; + return ret; +} + +#define check_sem_state(sem, count, max) \ + ({ \ + __u32 __count, __max; \ + int ret = read_sem_state((sem), &__count, &__max); \ + EXPECT_EQ(0, ret); \ + EXPECT_EQ((count), __count); \ + EXPECT_EQ((max), __max); \ + }) + +static int post_sem(int sem, __u32 *count) +{ + return ioctl(sem, NTSYNC_IOC_SEM_POST, count); +} + +static int wait_any(int fd, __u32 count, const int *objs, __u32 owner, __u32 *index) +{ + struct ntsync_wait_args args = {0}; + struct timespec timeout; + int ret; + + clock_gettime(CLOCK_MONOTONIC, &timeout); + + args.timeout = timeout.tv_sec * 1000000000 + timeout.tv_nsec; + args.count = count; + args.objs = (uintptr_t)objs; + args.owner = owner; + args.index = 0xdeadbeef; + ret = ioctl(fd, NTSYNC_IOC_WAIT_ANY, &args); + *index = args.index; + return ret; +} + +TEST(semaphore_state) +{ + struct ntsync_sem_args sem_args; + struct timespec timeout; + __u32 count, index; + int fd, ret, sem; + + clock_gettime(CLOCK_MONOTONIC, &timeout); + + fd = open("/dev/ntsync", O_CLOEXEC | O_RDONLY); + ASSERT_LE(0, fd); + + sem_args.count = 3; + sem_args.max = 2; + sem_args.sem = 0xdeadbeef; + ret = ioctl(fd, NTSYNC_IOC_CREATE_SEM, &sem_args); + EXPECT_EQ(-1, ret); + EXPECT_EQ(EINVAL, errno); + + sem_args.count = 2; + sem_args.max = 2; + sem_args.sem = 0xdeadbeef; + ret = ioctl(fd, NTSYNC_IOC_CREATE_SEM, &sem_args); + EXPECT_EQ(0, ret); + EXPECT_NE(0xdeadbeef, sem_args.sem); + sem = sem_args.sem; + check_sem_state(sem, 2, 2); + + count = 0; + ret = post_sem(sem, &count); + EXPECT_EQ(0, ret); + EXPECT_EQ(2, count); + check_sem_state(sem, 2, 2); + + count = 1; + ret = post_sem(sem, &count); + EXPECT_EQ(-1, ret); + EXPECT_EQ(EOVERFLOW, errno); + check_sem_state(sem, 2, 2); + + ret = wait_any(fd, 1, &sem, 123, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, index); + check_sem_state(sem, 1, 2); + + ret = wait_any(fd, 1, &sem, 123, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, index); + check_sem_state(sem, 0, 2); + + ret = wait_any(fd, 1, &sem, 123, &index); + EXPECT_EQ(-1, ret); + EXPECT_EQ(ETIMEDOUT, errno); + + count = 3; + ret = post_sem(sem, &count); + EXPECT_EQ(-1, ret); + EXPECT_EQ(EOVERFLOW, errno); + check_sem_state(sem, 0, 2); + + count = 2; + ret = post_sem(sem, &count); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, count); + check_sem_state(sem, 2, 2); + + ret = wait_any(fd, 1, &sem, 123, &index); + EXPECT_EQ(0, ret); + ret = wait_any(fd, 1, &sem, 123, &index); + EXPECT_EQ(0, ret); + + count = 1; + ret = post_sem(sem, &count); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, count); + check_sem_state(sem, 1, 2); + + count = ~0u; + ret = post_sem(sem, &count); + EXPECT_EQ(-1, ret); + EXPECT_EQ(EOVERFLOW, errno); + check_sem_state(sem, 1, 2); + + close(sem); + + close(fd); +} + +TEST_HARNESS_MAIN From patchwork Wed Feb 14 23:52:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201193 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp62298dyb; Wed, 14 Feb 2024 15:58:46 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUe9XuAYJkhrhTlJBBBGzNXDwgfK78L/uLUtv8f4CgJCV39BJpDagNaWQN6qbDx8di5U4IVql8kd1lWYyg4t2qKg4+iOQ== X-Google-Smtp-Source: AGHT+IF0i4IP5/k8TeSok+jDi/cALJ2hheMWbODxgYARy1KR2s4o6L5lS/tFjfQYTbfpuGdIeUkC X-Received: by 2002:aa7:cd12:0:b0:562:4c81:8d7d with SMTP id b18-20020aa7cd12000000b005624c818d7dmr82739edw.17.1707955126122; Wed, 14 Feb 2024 15:58:46 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707955126; cv=pass; d=google.com; s=arc-20160816; b=HtOvJe1aLK9rDqYKmtbfu1elVWjUAnzARHhDeazSdCjbfgtIkvMG96+L2iXhWVXrzV ArntPDpUTutPqy6uP4Vvv0W4k0rE0pC/KlKGaqS6fKIgkDjdMvmAJzgU54KpaJEUYdLU +GT1ThhxvbPZ7ce4FcQY+pGRrKyzGT/MsdYrRXqUPdtjSv4bUJbGuJNQs/xYHCuqRSVQ wZt3OFY8ZQ7fkWOYUBRozlaX0DeU+8dHaLgJs7/TMU1T57+zzLF/ecgorD+JOx8xdk38 oM3MFsEfzc5nfngQKuiwULmqU9E8ayD8RL5ZOgoUTyfA1+QXFDzEfZb9n4oIA2+0i8ZB ilcw== 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=ZF4vcia88ZreqFYvisq7ptV14EhxENO+K11K9xG70PQ=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=Eh/CpOZ4SX4Y8a3BKBDXiOJ0ACnlenldFXvbj2jqaC5nWyLrICsmmmo4ndc0qmFqu6 1RIwsYCMHhv+KyN1lLf6PnvUWp2gYQqGmdCxf5F6CG/QuZ0GtPpT2YB8Sib0gxEcL5Mb 6JZle8rsrUC2U/2JtcTN3UDO++hitqVRhADlTzwsZ/oo12sNZuFx6x9/IdTGn/JVgVE8 CEi+yzcHTP/JZPOMPtYcsg+8BnByfNpU/oBFdwNPkfc1Smu/YDIAw04/QUq6uooqtDGQ 2LzmbaP5UOg6CSNnWdKJAI8gPT7BrkRO+tJBLBObUdf1OxvPqOvxeMRKx5Sj2eyliApb sSbg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=fcbfpW1a; 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-66154-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66154-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id c18-20020aa7df12000000b00563160cd15fsi34741edy.424.2024.02.14.15.58.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 15:58:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66154-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=fcbfpW1a; 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-66154-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66154-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 896441F21D0E for ; Wed, 14 Feb 2024 23:58:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DDC7514AD3F; Wed, 14 Feb 2024 23:53:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="fcbfpW1a" 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 00B38145352; Wed, 14 Feb 2024 23:53:41 +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=1707954823; cv=none; b=dXbqLrWqRQVlO33eWu8jZku6yDQbXICnyqjM+pGn7Kumhju39HLMwkjNsZL3e62Ihmf2l3FllOOorjts6CROiuFkZjFbub7++kfiaSHvkK6ZfAmdGC1oGqJ5BFNgEI49pr+kXLnyeUCoX+8L1OQ/+GGFVSXKvyAjabw9Qgyd2tY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954823; c=relaxed/simple; bh=vv/+JGmQGe3Qq3Euywo0D/0Z6gsPsvDvAvr3zg9+Bas=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=p2ghHxvIMQ7PKNBGvY6cS54D4n/AJIjmuimrTGrQ7TsA3nYBu0aX5yEzEOIrwG5flGYHgca3Jbn8JYlGxhOqBhbO5UbNii9tqKs+9uS+/Yt+tLcWnr+UQLm1vvnud3aMD6W6N9MIqyqJZxE4GBjTftOa6SfUwGu8BNLqpLf3ZdY= 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=fcbfpW1a; 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=ZF4vcia88ZreqFYvisq7ptV14EhxENO+K11K9xG70PQ=; b=fcbfpW1alVSUbLp45kmqjpTago Nv6pG7K/54jOx9h+dXSKXJuIvSBd1V+Y1arlqg76q0TW88iLeSD3DqHsqJCejAe3wlWwW+xRXGbi6 NJnWKr8oB53GAsZ+h6nlOajP7hcsDfcypF8XtwC59FGxupq0vaWdV0wOhqhdqhFUj8hWEdxO7N/V0 SHdgJtp3UjboqT+NDJnkDS9FzvWKAkOxVjJgpR5ZGIIBbk7oo1QanmPcKWofJnIwl/6kkwkg6kv4Q jdhoopJ8RH5oXUIsLbXjLkdwAPnN0RVBzGxin74s9HEOZ9ceJSI3NOXNjkmY1pJNxaZi3W2Iz19Nx xeBtmJ0A==; 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 1raP4K-00GcSK-0v; Wed, 14 Feb 2024 17:53:36 -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 19/31] selftests: ntsync: Add some tests for mutex state. Date: Wed, 14 Feb 2024 17:52:54 -0600 Message-ID: <20240214235307.10494-10-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240214235307.10494-1-zfigura@codeweavers.com> References: <20240214233645.9273-1-zfigura@codeweavers.com> <20240214235307.10494-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: 1790920754206324861 X-GMAIL-MSGID: 1790920754206324861 Test mutex-specific ioctls NTSYNC_IOC_MUTEX_UNLOCK and NTSYNC_IOC_MUTEX_READ, and waiting on mutexes. Signed-off-by: Elizabeth Figura --- .../testing/selftests/drivers/ntsync/ntsync.c | 196 ++++++++++++++++++ 1 file changed, 196 insertions(+) diff --git a/tools/testing/selftests/drivers/ntsync/ntsync.c b/tools/testing/selftests/drivers/ntsync/ntsync.c index bda88233b427..d2c0c97839ba 100644 --- a/tools/testing/selftests/drivers/ntsync/ntsync.c +++ b/tools/testing/selftests/drivers/ntsync/ntsync.c @@ -40,6 +40,39 @@ static int post_sem(int sem, __u32 *count) return ioctl(sem, NTSYNC_IOC_SEM_POST, count); } +static int read_mutex_state(int mutex, __u32 *count, __u32 *owner) +{ + struct ntsync_mutex_args args; + int ret; + + memset(&args, 0xcc, sizeof(args)); + ret = ioctl(mutex, NTSYNC_IOC_MUTEX_READ, &args); + *count = args.count; + *owner = args.owner; + return ret; +} + +#define check_mutex_state(mutex, count, owner) \ + ({ \ + __u32 __count, __owner; \ + int ret = read_mutex_state((mutex), &__count, &__owner); \ + EXPECT_EQ(0, ret); \ + EXPECT_EQ((count), __count); \ + EXPECT_EQ((owner), __owner); \ + }) + +static int unlock_mutex(int mutex, __u32 owner, __u32 *count) +{ + struct ntsync_mutex_args args; + int ret; + + args.owner = owner; + args.count = 0xdeadbeef; + ret = ioctl(mutex, NTSYNC_IOC_MUTEX_UNLOCK, &args); + *count = args.count; + return ret; +} + static int wait_any(int fd, __u32 count, const int *objs, __u32 owner, __u32 *index) { struct ntsync_wait_args args = {0}; @@ -146,4 +179,167 @@ TEST(semaphore_state) close(fd); } +TEST(mutex_state) +{ + struct ntsync_mutex_args mutex_args; + __u32 owner, count, index; + struct timespec timeout; + int fd, ret, mutex; + + clock_gettime(CLOCK_MONOTONIC, &timeout); + + fd = open("/dev/ntsync", O_CLOEXEC | O_RDONLY); + ASSERT_LE(0, fd); + + mutex_args.owner = 123; + mutex_args.count = 0; + ret = ioctl(fd, NTSYNC_IOC_CREATE_MUTEX, &mutex_args); + EXPECT_EQ(-1, ret); + EXPECT_EQ(EINVAL, errno); + + mutex_args.owner = 0; + mutex_args.count = 2; + ret = ioctl(fd, NTSYNC_IOC_CREATE_MUTEX, &mutex_args); + EXPECT_EQ(-1, ret); + EXPECT_EQ(EINVAL, errno); + + mutex_args.owner = 123; + mutex_args.count = 2; + mutex_args.mutex = 0xdeadbeef; + ret = ioctl(fd, NTSYNC_IOC_CREATE_MUTEX, &mutex_args); + EXPECT_EQ(0, ret); + EXPECT_NE(0xdeadbeef, mutex_args.mutex); + mutex = mutex_args.mutex; + check_mutex_state(mutex, 2, 123); + + ret = unlock_mutex(mutex, 0, &count); + EXPECT_EQ(-1, ret); + EXPECT_EQ(EINVAL, errno); + + ret = unlock_mutex(mutex, 456, &count); + EXPECT_EQ(-1, ret); + EXPECT_EQ(EPERM, errno); + check_mutex_state(mutex, 2, 123); + + ret = unlock_mutex(mutex, 123, &count); + EXPECT_EQ(0, ret); + EXPECT_EQ(2, count); + check_mutex_state(mutex, 1, 123); + + ret = unlock_mutex(mutex, 123, &count); + EXPECT_EQ(0, ret); + EXPECT_EQ(1, count); + check_mutex_state(mutex, 0, 0); + + ret = unlock_mutex(mutex, 123, &count); + EXPECT_EQ(-1, ret); + EXPECT_EQ(EPERM, errno); + + ret = wait_any(fd, 1, &mutex, 456, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, index); + check_mutex_state(mutex, 1, 456); + + ret = wait_any(fd, 1, &mutex, 456, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, index); + check_mutex_state(mutex, 2, 456); + + ret = unlock_mutex(mutex, 456, &count); + EXPECT_EQ(0, ret); + EXPECT_EQ(2, count); + check_mutex_state(mutex, 1, 456); + + ret = wait_any(fd, 1, &mutex, 123, &index); + EXPECT_EQ(-1, ret); + EXPECT_EQ(ETIMEDOUT, errno); + + owner = 0; + ret = ioctl(mutex, NTSYNC_IOC_MUTEX_KILL, &owner); + EXPECT_EQ(-1, ret); + EXPECT_EQ(EINVAL, errno); + + owner = 123; + ret = ioctl(mutex, NTSYNC_IOC_MUTEX_KILL, &owner); + EXPECT_EQ(-1, ret); + EXPECT_EQ(EPERM, errno); + check_mutex_state(mutex, 1, 456); + + owner = 456; + ret = ioctl(mutex, NTSYNC_IOC_MUTEX_KILL, &owner); + EXPECT_EQ(0, ret); + + memset(&mutex_args, 0xcc, sizeof(mutex_args)); + ret = ioctl(mutex, NTSYNC_IOC_MUTEX_READ, &mutex_args); + EXPECT_EQ(-1, ret); + EXPECT_EQ(EOWNERDEAD, errno); + EXPECT_EQ(0, mutex_args.count); + EXPECT_EQ(0, mutex_args.owner); + + memset(&mutex_args, 0xcc, sizeof(mutex_args)); + ret = ioctl(mutex, NTSYNC_IOC_MUTEX_READ, &mutex_args); + EXPECT_EQ(-1, ret); + EXPECT_EQ(EOWNERDEAD, errno); + EXPECT_EQ(0, mutex_args.count); + EXPECT_EQ(0, mutex_args.owner); + + ret = wait_any(fd, 1, &mutex, 123, &index); + EXPECT_EQ(-1, ret); + EXPECT_EQ(EOWNERDEAD, errno); + EXPECT_EQ(0, index); + check_mutex_state(mutex, 1, 123); + + owner = 123; + ret = ioctl(mutex, NTSYNC_IOC_MUTEX_KILL, &owner); + EXPECT_EQ(0, ret); + + memset(&mutex_args, 0xcc, sizeof(mutex_args)); + ret = ioctl(mutex, NTSYNC_IOC_MUTEX_READ, &mutex_args); + EXPECT_EQ(-1, ret); + EXPECT_EQ(EOWNERDEAD, errno); + EXPECT_EQ(0, mutex_args.count); + EXPECT_EQ(0, mutex_args.owner); + + ret = wait_any(fd, 1, &mutex, 123, &index); + EXPECT_EQ(-1, ret); + EXPECT_EQ(EOWNERDEAD, errno); + EXPECT_EQ(0, index); + check_mutex_state(mutex, 1, 123); + + close(mutex); + + mutex_args.owner = 0; + mutex_args.count = 0; + mutex_args.mutex = 0xdeadbeef; + ret = ioctl(fd, NTSYNC_IOC_CREATE_MUTEX, &mutex_args); + EXPECT_EQ(0, ret); + EXPECT_NE(0xdeadbeef, mutex_args.mutex); + mutex = mutex_args.mutex; + check_mutex_state(mutex, 0, 0); + + ret = wait_any(fd, 1, &mutex, 123, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, index); + check_mutex_state(mutex, 1, 123); + + close(mutex); + + mutex_args.owner = 123; + mutex_args.count = ~0u; + mutex_args.mutex = 0xdeadbeef; + ret = ioctl(fd, NTSYNC_IOC_CREATE_MUTEX, &mutex_args); + EXPECT_EQ(0, ret); + EXPECT_NE(0xdeadbeef, mutex_args.mutex); + mutex = mutex_args.mutex; + check_mutex_state(mutex, ~0u, 123); + + ret = wait_any(fd, 1, &mutex, 123, &index); + EXPECT_EQ(-1, ret); + EXPECT_EQ(ETIMEDOUT, errno); + + close(mutex); + + close(fd); +} + TEST_HARNESS_MAIN From patchwork Wed Feb 14 23:52:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201194 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp62357dyb; Wed, 14 Feb 2024 15:58:58 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXXUhF5GrxNBD0wH18GWuYke2PDaH6pZUEG468p9mG3V+h9Zzos/gPED2X3XjI5u0T/iH0Bv6L6UO41C40iK7y5t7oO7Q== X-Google-Smtp-Source: AGHT+IFGTeji62VJF0lRVINUfKD7YJeW+Eqbq2IgZ3dTXZsxRFnquiDKhtZk/DwQi/ljmhohqyUq X-Received: by 2002:a2e:bb85:0:b0:2d1:1e3c:5739 with SMTP id y5-20020a2ebb85000000b002d11e3c5739mr150412lje.31.1707955138700; Wed, 14 Feb 2024 15:58:58 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707955138; cv=pass; d=google.com; s=arc-20160816; b=Nv5oFevj4e3capQsAqr9RmV1kYUh5YurlJpLfimMc0JrnX4qS4z8m9kcWNWGxEDOKc gUdgs7OASzQYVlKZ6UznWlinWyXhmcz6C2bnKSZ17xcnrQRDAb7U9z5aLopbvpDDqrwB pruAxxcT2hBJQWd5YuL/XwN9yOtRauFV8iXxtnNiWJgFM7Ugsa/igKqoCpWm0TsKNd1T h5nOv6g8izI+YO3qDsMO3NCXc0Y8ONml2RWJpNa9v/NJGXDedoi7/DAi44acwTcHcZ+w 0PLE0YWaX/MXsuY4ghvWXheIWa33XT10RniaGRl6E6oBsnInYNvXWxFkyNBxMM8Dk93y 7NNw== 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=fPM9rJ6H5CJlPqJO/3ILx/GOK2m21LbhltJ/pbLrjBs=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=a6nvfqA+g6aCM6+3Ys+9Ya1gx7sWKzFi7ToZtuM60el5yu6Max5bHtPi2KcrVlIRyJ WwVMgkEmIbGQCAFX5CzAZeYXN79TMJbmhBoq+pCzAV2uVqiEv/EXXQgIqS2X7LpAIOku wvCYiEMcgGR5OUi1FlkXGxcklNrimO1LBUKXCO/pV5QjKlwDOaqjiM7Jy5h9LGBPxfXo rO9KIKrasni5LA9EXb/8fhZNZgaxTA1Ud1ei3NG5zGpoxhd81rmPhG2kFuoysjJhQodE eKJMhhHr6BuJEXXB9n57lEPb50ldDkXWddLe5agH18jEN7mn8yn3ZngQPm22Hc2OJA9P 6h6w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=eHkbK53b; 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-66155-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66155-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id r16-20020a056402035000b00560c8889cfcsi37766edw.280.2024.02.14.15.58.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 15:58:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66155-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=eHkbK53b; 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-66155-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66155-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 4C9431F21F8C for ; Wed, 14 Feb 2024 23:58:58 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id BC1AF14C599; Wed, 14 Feb 2024 23:53:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="eHkbK53b" 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 7228214A4E6; Wed, 14 Feb 2024 23:53:43 +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=1707954825; cv=none; b=T18iONN/bCxeEpWcWsRm7BmuKpGdrK+57PYCbCkOCKVmu9/mxHEvFGEyaWwl054oe045ZM9/A6w5ueK8pr7+Y22VrECYxw4/mszdwxzKvx9l5KAN8+6P6vGGgb7NgJW367gh+hYGkEGUhwHOfiOigQ1us/NsB88BgG8C/7U3ydc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954825; c=relaxed/simple; bh=ANwJeKeLnEe9JkXTHDo3w8k+yOqr+FnkS/Fgv9klwnE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tWTm0yjryOoATQdLI35q6OMky7QLM9HZBm4vDerIWqTupC5AdF21Tt8yp9G8PEffVH3AFM9WFzRJO1w2l6Vjnrv0+VIvCtV4ix++mQNLVGBJHE8E6p4vYy81BnpMjmiFeS/7elm0E+fz6hLg/WXeQiid9VpKkrLQ/2tuLXlSJec= 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=eHkbK53b; 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=fPM9rJ6H5CJlPqJO/3ILx/GOK2m21LbhltJ/pbLrjBs=; b=eHkbK53bvkzj69i1qZF8nMGNzb 9IsboKjr+2FZ9wiCyXFaggHIpTHgLO3slKzLhdQnMRPJeEVMn/KuhqHP/aWArcRYIekMK3C7aCoRb dpcEqWTB28dLJHS69qdgGwoyexSGEQGtDBNBqAG7+v7yUIoxRgjsrM81WfxzNTtUF/6WhqQzKOara Lh05FfMPf7Huplzh5Pd6TLYXqfppnoht5kNm7Pf2yQAtLlcNA+T78BWHqA0FyjjWQOJE/il/4w+BD 4oSlkZ7MBPLSAkZk6RaGNRS2HR5uZuFHr9bBieOI/Bg37SHBYN9NREBshyPHf7A8kAiKSyLe0QVMC od0VvggA==; 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 1raP4L-00GcSK-1T; Wed, 14 Feb 2024 17:53:37 -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 20/31] selftests: ntsync: Add some tests for NTSYNC_IOC_WAIT_ANY. Date: Wed, 14 Feb 2024 17:52:55 -0600 Message-ID: <20240214235307.10494-11-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240214235307.10494-1-zfigura@codeweavers.com> References: <20240214233645.9273-1-zfigura@codeweavers.com> <20240214235307.10494-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: 1790920767841278263 X-GMAIL-MSGID: 1790920767841278263 Test basic synchronous functionality of NTSYNC_IOC_WAIT_ANY, when objects are considered signaled or not signaled, and how they are affected by a successful wait. Signed-off-by: Elizabeth Figura --- .../testing/selftests/drivers/ntsync/ntsync.c | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git a/tools/testing/selftests/drivers/ntsync/ntsync.c b/tools/testing/selftests/drivers/ntsync/ntsync.c index d2c0c97839ba..ca61fb17439b 100644 --- a/tools/testing/selftests/drivers/ntsync/ntsync.c +++ b/tools/testing/selftests/drivers/ntsync/ntsync.c @@ -342,4 +342,123 @@ TEST(mutex_state) close(fd); } +TEST(test_wait_any) +{ + int objs[NTSYNC_MAX_WAIT_COUNT + 1], fd, ret; + struct ntsync_mutex_args mutex_args = {0}; + struct ntsync_sem_args sem_args = {0}; + __u32 owner, index, count, i; + struct timespec timeout; + + clock_gettime(CLOCK_MONOTONIC, &timeout); + + fd = open("/dev/ntsync", O_CLOEXEC | O_RDONLY); + ASSERT_LE(0, fd); + + sem_args.count = 2; + sem_args.max = 3; + sem_args.sem = 0xdeadbeef; + ret = ioctl(fd, NTSYNC_IOC_CREATE_SEM, &sem_args); + EXPECT_EQ(0, ret); + EXPECT_NE(0xdeadbeef, sem_args.sem); + + mutex_args.owner = 0; + mutex_args.count = 0; + mutex_args.mutex = 0xdeadbeef; + ret = ioctl(fd, NTSYNC_IOC_CREATE_MUTEX, &mutex_args); + EXPECT_EQ(0, ret); + EXPECT_NE(0xdeadbeef, mutex_args.mutex); + + objs[0] = sem_args.sem; + objs[1] = mutex_args.mutex; + + ret = wait_any(fd, 2, objs, 123, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, index); + check_sem_state(sem_args.sem, 1, 3); + check_mutex_state(mutex_args.mutex, 0, 0); + + ret = wait_any(fd, 2, objs, 123, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, index); + check_sem_state(sem_args.sem, 0, 3); + check_mutex_state(mutex_args.mutex, 0, 0); + + ret = wait_any(fd, 2, objs, 123, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(1, index); + check_sem_state(sem_args.sem, 0, 3); + check_mutex_state(mutex_args.mutex, 1, 123); + + count = 1; + ret = post_sem(sem_args.sem, &count); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, count); + + ret = wait_any(fd, 2, objs, 123, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, index); + check_sem_state(sem_args.sem, 0, 3); + check_mutex_state(mutex_args.mutex, 1, 123); + + ret = wait_any(fd, 2, objs, 123, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(1, index); + check_sem_state(sem_args.sem, 0, 3); + check_mutex_state(mutex_args.mutex, 2, 123); + + ret = wait_any(fd, 2, objs, 456, &index); + EXPECT_EQ(-1, ret); + EXPECT_EQ(ETIMEDOUT, errno); + + owner = 123; + ret = ioctl(mutex_args.mutex, NTSYNC_IOC_MUTEX_KILL, &owner); + EXPECT_EQ(0, ret); + + ret = wait_any(fd, 2, objs, 456, &index); + EXPECT_EQ(-1, ret); + EXPECT_EQ(EOWNERDEAD, errno); + EXPECT_EQ(1, index); + + ret = wait_any(fd, 2, objs, 456, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(1, index); + + /* test waiting on the same object twice */ + count = 2; + ret = post_sem(sem_args.sem, &count); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, count); + + objs[0] = objs[1] = sem_args.sem; + ret = wait_any(fd, 2, objs, 456, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, index); + check_sem_state(sem_args.sem, 1, 3); + + ret = wait_any(fd, 0, NULL, 456, &index); + EXPECT_EQ(-1, ret); + EXPECT_EQ(ETIMEDOUT, errno); + + for (i = 0; i < NTSYNC_MAX_WAIT_COUNT + 1; ++i) + objs[i] = sem_args.sem; + + ret = wait_any(fd, NTSYNC_MAX_WAIT_COUNT, objs, 123, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, index); + + ret = wait_any(fd, NTSYNC_MAX_WAIT_COUNT + 1, objs, 123, &index); + EXPECT_EQ(-1, ret); + EXPECT_EQ(EINVAL, errno); + + ret = wait_any(fd, -1, objs, 123, &index); + EXPECT_EQ(-1, ret); + EXPECT_EQ(EINVAL, errno); + + close(sem_args.sem); + close(mutex_args.mutex); + + close(fd); +} + TEST_HARNESS_MAIN From patchwork Wed Feb 14 23:52:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201197 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp62804dyb; Wed, 14 Feb 2024 16:00:10 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX2NdPQgzcLoEyJjNld7IThG3MhnlRvwI7rvg2RQXTjGCFymNdbrR9j9Cg9hWl4m2/fNZ2btcAUfx6MST2YGqgg1FvHuA== X-Google-Smtp-Source: AGHT+IGkCncYwLGnMwLiUZRVMVbpF683g36jkr7BVkE3tpjy2PEVJOkAa89HggE21q8M/+bwD2+1 X-Received: by 2002:a17:902:7001:b0:1da:1e83:b961 with SMTP id y1-20020a170902700100b001da1e83b961mr152497plk.63.1707955210037; Wed, 14 Feb 2024 16:00:10 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707955210; cv=pass; d=google.com; s=arc-20160816; b=p5bAP+gL3aJaX5k6GqklCX3T90BnYV7dcWNLwLuepfw8cg0vw5AJ21hzD921CzyLGu oME5RYVgmP4AGm5Ptu8PvvPw3BFTpQJSmmF/6oD+lVggLivk3srvy51NmUiljjFIpnAd lfSc5auHTxyxqY1x9JEHjgSl93M0vsLGWW5d8f14hZlXVdNEodiV6dk9a+phctz8mGPV NiuV6ejSmzrfTjdEd9+0nN7lf1P6fWwvcAEZzNiF+bDItjlAEGNv2WkPyAKyZV0/gBXb hCGCJbC5kSB3QmvpOHdA9UK+2cq16IlmJdMCtsKFjk7XK53p+GlB01uVC7GDaGUe8rJH 4dKQ== 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=1JYvPoY/ppOEUjmlJpt8VqHXNCiWzb7HrdTFPiD3zs0=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=il+c09nK68HeccSQGTgxKn010YwyMollWVk1Bo6WKdcmt4SfJbCM9R4W01ZS5XeK1H iR6IggIO4MtM3+q6tl94m8/r2VSDCI0E4FNEiQ6gZHOOvAFvit+5L22V4XZYIIp+ZRuI rIa0mK5HR8WDoTXSOEeTSvgE//bBxc5+BO40yY+ZW7beKX/qlGCkpcOCfH8ItLb/gvOt hpbDfWWdKj8M74y28rTtbx2rMALbY5SOmaoh7xu2ilH/uvOz2wmv1FhqNf9Xe6olonTA zS9Xyan/tAuYuFIKb/R/lPyPr2roSXPpbnADeYhH3x8uG1hV0wIRgtvJWjoSuXzmRMiB BcMA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b="CA/L99Iu"; 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-66156-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66156-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. [139.178.88.99]) by mx.google.com with ESMTPS id p6-20020a170902780600b001db29e3ba29si44115pll.77.2024.02.14.16.00.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 16:00:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66156-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b="CA/L99Iu"; 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-66156-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66156-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 40A71282EE9 for ; Wed, 14 Feb 2024 23:59:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 454CE14D430; Wed, 14 Feb 2024 23:53:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="CA/L99Iu" 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 6D4ED14A4FB; Wed, 14 Feb 2024 23:53:44 +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=1707954825; cv=none; b=AAEtQNq//YSOfiAaPB/R4XOtUoGHyX/T+i8kJuEGCdLGnciLyGu17Wf6f8eHL5Aqqu5S1uJtSY8dKR58RIchgy90KKKxkZ2/yL25KKNdrY0JPxvD+/3K3umJ59nqHE3P6qpC4HSa0ff5bpRtSt7s3QHa/NkndtJW+QpXP/8f1ik= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954825; c=relaxed/simple; bh=UA9Bt50olybqISzQXiEAoLdmwT0I1oA3lCfN4NtbGKU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oS582QTCsb80Jqe69DVyoWtxPbjMelPE4QtlLHVUlm+Zsz3vYaoKRGJvSw0Tknu7usNvmZRQCnj/aYNBD0Rxmnoqc1rC+5E1AJn6DhLwX9J1GPai9Q6ofX/efHLJvJILgNrLpvxRUL9xRZGaXyndvvCcHNcR9673RhGBkM/YDKk= 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=CA/L99Iu; 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=1JYvPoY/ppOEUjmlJpt8VqHXNCiWzb7HrdTFPiD3zs0=; b=CA/L99Iu7+foxDZuRDdZL0SDQz z48iryMa1yPz3CntPRVq21mKsmbl2Eq1O++kSx1Ux7UZjLhz74H5DthGMedo4crh1+LGU6BIvL6Np Nkdt0rv3QNDQshtC3LvK2/SnYuR4zMyiyPcaEUoeXnv7OyFuqHzGrqrQoWELv1PwpoCpQ36UxlusW nez0S6HhdPAoepk0J+oytn1zL7bHvBbduBO/pvC+dj+FRxJIu8hl44L3UV1df2VvqEh6T6SBBv7f5 qPBE8g8qWRdsGz0RkRGRfuujGXTv7GKG0l3ncRPVdv3Oh+0FHrB/I8GcMQRlf38+Ha91I1SFvHcoz 8mWqVIqw==; 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 1raP4M-00GcSK-29; Wed, 14 Feb 2024 17:53:39 -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 21/31] selftests: ntsync: Add some tests for NTSYNC_IOC_WAIT_ALL. Date: Wed, 14 Feb 2024 17:52:56 -0600 Message-ID: <20240214235307.10494-12-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240214235307.10494-1-zfigura@codeweavers.com> References: <20240214233645.9273-1-zfigura@codeweavers.com> <20240214235307.10494-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: 1790920842357417244 X-GMAIL-MSGID: 1790920842357417244 Test basic synchronous functionality of NTSYNC_IOC_WAIT_ALL, and when objects are considered simultaneously signaled. Signed-off-by: Elizabeth Figura --- .../testing/selftests/drivers/ntsync/ntsync.c | 99 ++++++++++++++++++- 1 file changed, 97 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/drivers/ntsync/ntsync.c b/tools/testing/selftests/drivers/ntsync/ntsync.c index ca61fb17439b..6bb3bdc2ef3c 100644 --- a/tools/testing/selftests/drivers/ntsync/ntsync.c +++ b/tools/testing/selftests/drivers/ntsync/ntsync.c @@ -73,7 +73,8 @@ static int unlock_mutex(int mutex, __u32 owner, __u32 *count) return ret; } -static int wait_any(int fd, __u32 count, const int *objs, __u32 owner, __u32 *index) +static int wait_objs(int fd, unsigned long request, __u32 count, + const int *objs, __u32 owner, __u32 *index) { struct ntsync_wait_args args = {0}; struct timespec timeout; @@ -86,11 +87,21 @@ static int wait_any(int fd, __u32 count, const int *objs, __u32 owner, __u32 *in args.objs = (uintptr_t)objs; args.owner = owner; args.index = 0xdeadbeef; - ret = ioctl(fd, NTSYNC_IOC_WAIT_ANY, &args); + ret = ioctl(fd, request, &args); *index = args.index; return ret; } +static int wait_any(int fd, __u32 count, const int *objs, __u32 owner, __u32 *index) +{ + return wait_objs(fd, NTSYNC_IOC_WAIT_ANY, count, objs, owner, index); +} + +static int wait_all(int fd, __u32 count, const int *objs, __u32 owner, __u32 *index) +{ + return wait_objs(fd, NTSYNC_IOC_WAIT_ALL, count, objs, owner, index); +} + TEST(semaphore_state) { struct ntsync_sem_args sem_args; @@ -461,4 +472,88 @@ TEST(test_wait_any) close(fd); } +TEST(test_wait_all) +{ + struct ntsync_mutex_args mutex_args = {0}; + struct ntsync_sem_args sem_args = {0}; + __u32 owner, index, count; + int objs[2], fd, ret; + + fd = open("/dev/ntsync", O_CLOEXEC | O_RDONLY); + ASSERT_LE(0, fd); + + sem_args.count = 2; + sem_args.max = 3; + sem_args.sem = 0xdeadbeef; + ret = ioctl(fd, NTSYNC_IOC_CREATE_SEM, &sem_args); + EXPECT_EQ(0, ret); + EXPECT_NE(0xdeadbeef, sem_args.sem); + + mutex_args.owner = 0; + mutex_args.count = 0; + mutex_args.mutex = 0xdeadbeef; + ret = ioctl(fd, NTSYNC_IOC_CREATE_MUTEX, &mutex_args); + EXPECT_EQ(0, ret); + EXPECT_NE(0xdeadbeef, mutex_args.mutex); + + objs[0] = sem_args.sem; + objs[1] = mutex_args.mutex; + + ret = wait_all(fd, 2, objs, 123, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, index); + check_sem_state(sem_args.sem, 1, 3); + check_mutex_state(mutex_args.mutex, 1, 123); + + ret = wait_all(fd, 2, objs, 456, &index); + EXPECT_EQ(-1, ret); + EXPECT_EQ(ETIMEDOUT, errno); + check_sem_state(sem_args.sem, 1, 3); + check_mutex_state(mutex_args.mutex, 1, 123); + + ret = wait_all(fd, 2, objs, 123, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, index); + check_sem_state(sem_args.sem, 0, 3); + check_mutex_state(mutex_args.mutex, 2, 123); + + ret = wait_all(fd, 2, objs, 123, &index); + EXPECT_EQ(-1, ret); + EXPECT_EQ(ETIMEDOUT, errno); + check_sem_state(sem_args.sem, 0, 3); + check_mutex_state(mutex_args.mutex, 2, 123); + + count = 3; + ret = post_sem(sem_args.sem, &count); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, count); + + ret = wait_all(fd, 2, objs, 123, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, index); + check_sem_state(sem_args.sem, 2, 3); + check_mutex_state(mutex_args.mutex, 3, 123); + + owner = 123; + ret = ioctl(mutex_args.mutex, NTSYNC_IOC_MUTEX_KILL, &owner); + EXPECT_EQ(0, ret); + + ret = wait_all(fd, 2, objs, 123, &index); + EXPECT_EQ(-1, ret); + EXPECT_EQ(EOWNERDEAD, errno); + check_sem_state(sem_args.sem, 1, 3); + check_mutex_state(mutex_args.mutex, 1, 123); + + /* test waiting on the same object twice */ + objs[0] = objs[1] = sem_args.sem; + ret = wait_all(fd, 2, objs, 123, &index); + EXPECT_EQ(-1, ret); + EXPECT_EQ(EINVAL, errno); + + close(sem_args.sem); + close(mutex_args.mutex); + + close(fd); +} + TEST_HARNESS_MAIN From patchwork Wed Feb 14 23:52:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201203 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp64237dyb; Wed, 14 Feb 2024 16:02:19 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUQkvopjlLZWyFO05uOiOWrupRkkvxQ/R+IqDx7abbjR8Mb6UItq2v79WyZWQbjk/WopYNPdxaeqDkTXJ8g/BVqU81rVQ== X-Google-Smtp-Source: AGHT+IGlyT1S3t0H1V9K/p0UitTt8CeikdQDxBF/e01PtU8imTAt0aBHHPVbJAaiBy2rjpuZR33m X-Received: by 2002:a17:902:aa84:b0:1d9:c17b:43f3 with SMTP id d4-20020a170902aa8400b001d9c17b43f3mr178117plr.15.1707955339510; Wed, 14 Feb 2024 16:02:19 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707955339; cv=pass; d=google.com; s=arc-20160816; b=y5zNrNoqUl59SZc0op9fJaWzLJnD1LTnxd6lqa7ugnGu7YuBs0sZdhaFhnd7MwVoBk O6j5iePEO/My93413qAVs7AqFBGYdAVvm3iKx/LlhUaC8DYQqsRlayEtUaZSFsm++VZU BJphE6U000iwx9herpZgIN8vtZFnS+GwCQW2qCssKyjIzIG+hq/uBdZZBVz0Cezds+DJ TfZ+W1BKM6pIpXH40vcdr2zseKsjTNvwByZU5mjnnDep9d5HIVh1BdPk2W5edfLnUWeq lci+E+7Fr2KObCO810FIKvCBntn++b06euu/kWLe/okhH7AEP6boJKKn0WeT0v2HB/yB RBEw== 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=YSaDBCGA1golPpmWfwYA367banBPgBVmSEy31tHzCEo=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=DO/UZYNCYvxO/3D0CP7IoYZvwvai3GA1xqwfoo/OWV2KO0goODdu9v2BbSSkjPLASt GAZ0bj990NOxN0gnd0mf6+pVfbajNDze7JAJm1mSRB4PnYx+98UTFc8boLmtHNf22Qan qOIJQ1VC8APPy4D2uQ8VgzP4ZuAh73l8M1zh4xMcLd0BseVveqWdjH1Lq78x1OWOvc4R MYxuym26tuIqOOV3ApAoRMolPLbAkwYpxx76O+qFGXz/XLkYZr7f/vx3Dq4/83qtTGuC x11dS+UuIXhl5G9ZoNI6I6L2fEluZQ0Hf7wefh7liJXYRxm4bS+VkzB9opTzF5K4hAQ8 6BaA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=aqPqoxM9; 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-66157-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66157-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 a4-20020a170902ecc400b001d720af9a66si35547plh.366.2024.02.14.16.02.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 16:02:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66157-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=aqPqoxM9; 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-66157-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66157-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 477A1282325 for ; Wed, 14 Feb 2024 23:59:36 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2978414D44F; Wed, 14 Feb 2024 23:53:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="aqPqoxM9" 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 3BC7F14AD0F; Wed, 14 Feb 2024 23:53:45 +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=1707954826; cv=none; b=aboVrI63MqeSvaMScZYhXf6WgeLYbfbqOMFp6o7STbial0JP6TzSXMyPJ58CMlsSCLOnXUXr1j2X98BwICAgyp47uMQpSlXpcx+NI7eoGTF83sV2bJBGMj5n1A4Ulv3gPFTm3uLZJp1icn+C5HpofyGjmrr8xjm0ptq+lb+3XXA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954826; c=relaxed/simple; bh=/whYOeACwtu31Fu1HZzhtAvXvBVbIbJitm1kfNTCQdc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TdsiWf8FUgeaLUwu44Bjgj8648uGeyjHcNCemQlUkbaQieRWY1haFzvdMxuY4sXa+cg1TujIck4K1Htl9isFq04EjMZyrYdyJh9Iazo+p61Lwcx1t/pAb1hrGYs7k9GtvMwKRvVD4EMKXCuKFjza/AtoRCWAsyqB9bqTMKrVgEw= 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=aqPqoxM9; 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=YSaDBCGA1golPpmWfwYA367banBPgBVmSEy31tHzCEo=; b=aqPqoxM9ZJ606x8AqtcLyW3yYU L17z4eErFppbYR5lXYblhEswyJRE9thuDNFBDaxbGUWhEtM7ncSlB03Jfj2lu36NqG+pszFhZUMuE yR2QKUDaemXWBGt0aNN8hNmB72+Qgl7nzGX0kh8YF9OwzBkJTvUgCvvwdQQR2qOkXeDcoNGzyQqLm Uglv8NmTvBeC7pNn8rqddonKhuv49xBEdA8NQflu0Xe5xs87SeR1OIBB7Nemwum0RxFvVens4TmTa LRlAJ9ixWHrDi9B2Y9n67LuFgLmY4Ms78dt/Ed+ctG98spREKeJo5bkdMV0uFOafM3awh6CeE0B1l uMob4zhw==; 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 1raP4O-00GcSK-0d; Wed, 14 Feb 2024 17:53:41 -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 22/31] selftests: ntsync: Add some tests for wakeup signaling with WINESYNC_IOC_WAIT_ANY. Date: Wed, 14 Feb 2024 17:52:57 -0600 Message-ID: <20240214235307.10494-13-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240214235307.10494-1-zfigura@codeweavers.com> References: <20240214233645.9273-1-zfigura@codeweavers.com> <20240214235307.10494-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: 1790920977950824570 X-GMAIL-MSGID: 1790920977950824570 Test contended "wait-for-any" waits, to make sure that scheduling and wakeup logic works correctly. Signed-off-by: Elizabeth Figura --- .../testing/selftests/drivers/ntsync/ntsync.c | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) diff --git a/tools/testing/selftests/drivers/ntsync/ntsync.c b/tools/testing/selftests/drivers/ntsync/ntsync.c index 6bb3bdc2ef3c..e825e115f11f 100644 --- a/tools/testing/selftests/drivers/ntsync/ntsync.c +++ b/tools/testing/selftests/drivers/ntsync/ntsync.c @@ -556,4 +556,154 @@ TEST(test_wait_all) close(fd); } +struct wake_args { + int fd; + int obj; +}; + +struct wait_args { + int fd; + unsigned long request; + struct ntsync_wait_args *args; + int ret; + int err; +}; + +static void *wait_thread(void *arg) +{ + struct wait_args *args = arg; + + args->ret = ioctl(args->fd, args->request, args->args); + args->err = errno; + return NULL; +} + +static __u64 get_abs_timeout(unsigned int ms) +{ + struct timespec timeout; + clock_gettime(CLOCK_MONOTONIC, &timeout); + return (timeout.tv_sec * 1000000000) + timeout.tv_nsec + (ms * 1000000); +} + +static int wait_for_thread(pthread_t thread, unsigned int ms) +{ + struct timespec timeout; + + clock_gettime(CLOCK_REALTIME, &timeout); + timeout.tv_nsec += ms * 1000000; + timeout.tv_sec += (timeout.tv_nsec / 1000000000); + timeout.tv_nsec %= 1000000000; + return pthread_timedjoin_np(thread, NULL, &timeout); +} + +TEST(wake_any) +{ + struct ntsync_mutex_args mutex_args = {0}; + struct ntsync_wait_args wait_args = {0}; + struct ntsync_sem_args sem_args = {0}; + struct wait_args thread_args; + int objs[2], fd, ret; + __u32 count, index; + pthread_t thread; + + fd = open("/dev/ntsync", O_CLOEXEC | O_RDONLY); + ASSERT_LE(0, fd); + + sem_args.count = 0; + sem_args.max = 3; + sem_args.sem = 0xdeadbeef; + ret = ioctl(fd, NTSYNC_IOC_CREATE_SEM, &sem_args); + EXPECT_EQ(0, ret); + EXPECT_NE(0xdeadbeef, sem_args.sem); + + mutex_args.owner = 123; + mutex_args.count = 1; + mutex_args.mutex = 0xdeadbeef; + ret = ioctl(fd, NTSYNC_IOC_CREATE_MUTEX, &mutex_args); + EXPECT_EQ(0, ret); + EXPECT_NE(0xdeadbeef, mutex_args.mutex); + + objs[0] = sem_args.sem; + objs[1] = mutex_args.mutex; + + /* test waking the semaphore */ + + wait_args.timeout = get_abs_timeout(1000); + wait_args.objs = (uintptr_t)objs; + wait_args.count = 2; + wait_args.owner = 456; + wait_args.index = 0xdeadbeef; + thread_args.fd = fd; + thread_args.args = &wait_args; + thread_args.request = NTSYNC_IOC_WAIT_ANY; + ret = pthread_create(&thread, NULL, wait_thread, &thread_args); + EXPECT_EQ(0, ret); + + ret = wait_for_thread(thread, 100); + EXPECT_EQ(ETIMEDOUT, ret); + + count = 1; + ret = post_sem(sem_args.sem, &count); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, count); + check_sem_state(sem_args.sem, 0, 3); + + ret = wait_for_thread(thread, 100); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, thread_args.ret); + EXPECT_EQ(0, wait_args.index); + + /* test waking the mutex */ + + /* first grab it again for owner 123 */ + ret = wait_any(fd, 1, &mutex_args.mutex, 123, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, index); + + wait_args.timeout = get_abs_timeout(1000); + wait_args.owner = 456; + ret = pthread_create(&thread, NULL, wait_thread, &thread_args); + EXPECT_EQ(0, ret); + + ret = wait_for_thread(thread, 100); + EXPECT_EQ(ETIMEDOUT, ret); + + ret = unlock_mutex(mutex_args.mutex, 123, &count); + EXPECT_EQ(0, ret); + EXPECT_EQ(2, count); + + ret = pthread_tryjoin_np(thread, NULL); + EXPECT_EQ(EBUSY, ret); + + ret = unlock_mutex(mutex_args.mutex, 123, &count); + EXPECT_EQ(0, ret); + EXPECT_EQ(1, mutex_args.count); + check_mutex_state(mutex_args.mutex, 1, 456); + + ret = wait_for_thread(thread, 100); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, thread_args.ret); + EXPECT_EQ(1, wait_args.index); + + /* delete an object while it's being waited on */ + + wait_args.timeout = get_abs_timeout(200); + wait_args.owner = 123; + ret = pthread_create(&thread, NULL, wait_thread, &thread_args); + EXPECT_EQ(0, ret); + + ret = wait_for_thread(thread, 100); + EXPECT_EQ(ETIMEDOUT, ret); + + close(sem_args.sem); + close(mutex_args.mutex); + + ret = wait_for_thread(thread, 200); + EXPECT_EQ(0, ret); + EXPECT_EQ(-1, thread_args.ret); + EXPECT_EQ(ETIMEDOUT, thread_args.err); + + close(fd); +} + TEST_HARNESS_MAIN From patchwork Wed Feb 14 23:52:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201196 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp62663dyb; Wed, 14 Feb 2024 15:59:53 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCULDs2ePMpmUGiNz8+H8wq225IyNUZ0wssUmlmhEHyZb5oVCE3x5cbAmqcHylnscArQ0lgblogKjU0ycUwXQ3rPE+pTFw== X-Google-Smtp-Source: AGHT+IFPm49HiNBhSSkTFxXfi8c2PXTN7/iEvu0W6cnPVF9fhkNqxZjMLMDaHWUd6OYqYFVaHdIK X-Received: by 2002:a50:ed93:0:b0:563:7f35:6a26 with SMTP id h19-20020a50ed93000000b005637f356a26mr96990edr.8.1707955193413; Wed, 14 Feb 2024 15:59:53 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707955193; cv=pass; d=google.com; s=arc-20160816; b=dfLn5t0I+d3y3kfUduoq1ukFZGftkCnNPxBtsgUaJ4/dfV7Qz8QTZ38MQp8sNGc/6M NIpt8NLEqZyU+nANp1N/NTk5eRsRV2d8KPgFeBqfsT1bOB40cgsjjUEllGNdPuu0/ENj tT8sMZMiINvqOr8c3mJEYcXI2+3JcvTKDHa+Xk9Iprk7QgTdPVgIb98tkMPux7Uy5Oc2 ZXKckekDGEXalsuTfFrx5tfJ3pm7IoNcN3WYHy4uVlKACcvIKK1syn2yISJ/B8ePVDDz sBayP3+hwiiLDkgh2VIWFK/WXiW7tRerU6+tMFBcbuzOu/VGuJkY8MuY0Nk/b7SHEUFd uKpg== 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=KwS1FfUvDDDY/WvGCa7UVhhPBJO8eSHPusvHbywdvFk=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=Ghez2W6VgFqJMJVU8Jq5ClGZRSBq0y7NcRm/j2hhS53DeQZSshBe3khMN2+6b/aSsD JyL4PwAcpXrUzn74DeG4vJkBdAS/Qfnggx5CTU4jzu2tYfIKz5+eAbFHrT0um+V3fdVW JOK6DHdQqkJWTKkjqn7ML2Grxtuc5bwAJ/sObTR6EsmrqkURycmkGILMvddcb+SFRigs +7r3ByP1m+tTDnZDQHCiq/BH+gSvlN+glifdPUrm1ybd4lYOPWBzDyIQB+aKhxXHxPUr gH8CCkpZnt9pyewst9Y/yZKbwo5PkhNaYt3jkIaDySZ6PLVb+nczdctYT9jhDSY9GTHG UJWg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=jaumXm3F; 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-66158-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66158-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id o16-20020aa7c510000000b00563a2e8884dsi30995edq.675.2024.02.14.15.59.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 15:59:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66158-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=jaumXm3F; 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-66158-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66158-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 am.mirrors.kernel.org (Postfix) with ESMTPS id ABF621F21FE7 for ; Wed, 14 Feb 2024 23:59:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0797514E2CC; Wed, 14 Feb 2024 23:53:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="jaumXm3F" 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 3A62014AD28; Wed, 14 Feb 2024 23:53:46 +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=1707954827; cv=none; b=OEZVLuOdLaMjzfDAVohgGu+n2ou86WvN68gVDd1tFklPmyZtEylTGjxh/VpFmB8TNdZyhHCbZJ7Ym4Ch8jsm6Qr+dchASH3N5pNuTEDHO11qbNO0LWJnWoYjwIet6q4Jtjmrmv1biDWbRwpWXiiJIgHRa3pVjhbKaa3WuWw/+IQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954827; c=relaxed/simple; bh=ULCw8wBkAv3LJc8jc62DPGxWhXuGWE5SyY5bMA8HPwE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Q8m40yYoZC5ValrHegpMv2NFXZ3yp249lrQN9+/3WzKrG4Nj56oEzwmx7ELBvx3K6O2hFlPFJnFenU+F1rrpmlrC7sTgY0DTIb0Yq40Yt/rXnSlgLRCJLMfoVkhFeFhsFlZRgR4QH6FAcAK0qjtaIulpBPUYbBFEsDYp/SHlARo= 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=jaumXm3F; 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=KwS1FfUvDDDY/WvGCa7UVhhPBJO8eSHPusvHbywdvFk=; b=jaumXm3F2nQpGRXUuWC+Cv9rdg KlqKvmoEUtXGrIO+M6m2w5QZz18rRTH5ium83E3dDgECCgyt25BjDOU87iKK8wST1Z5ddvjl5E5Hb igF3KTXjg949iRb+rixzL/AcNFVYiVhkvJZ5vLTXnph6klyIUG6mQzZRQb0iVTFzcRFNXMfWPOPDd vE0/Ub1Jw4yrfocl+Wd9SBwnp0s56SAZqC2aQxS5ZoioJl5BGL+ghr/r4Z5DNOoY3F6PgvQviJDjx xG+tlvOjJOANCuIw+n6eiNAWx6MxoYfEdRBTHQm42cLEM1wBx/lVwJk/3hNDMNywpYIOnWJqizY8k yzUosXwA==; 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 1raP4Q-00GcSK-1M; Wed, 14 Feb 2024 17:53:42 -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 23/31] selftests: ntsync: Add some tests for wakeup signaling with WINESYNC_IOC_WAIT_ALL. Date: Wed, 14 Feb 2024 17:52:58 -0600 Message-ID: <20240214235307.10494-14-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240214235307.10494-1-zfigura@codeweavers.com> References: <20240214233645.9273-1-zfigura@codeweavers.com> <20240214235307.10494-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: 1790920824971400321 X-GMAIL-MSGID: 1790920824971400321 Test contended "wait-for-all" waits, to make sure that scheduling and wakeup logic works correctly, and that the wait only exits once objects are all simultaneously signaled. Signed-off-by: Elizabeth Figura --- .../testing/selftests/drivers/ntsync/ntsync.c | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/tools/testing/selftests/drivers/ntsync/ntsync.c b/tools/testing/selftests/drivers/ntsync/ntsync.c index e825e115f11f..063c0ecb3bff 100644 --- a/tools/testing/selftests/drivers/ntsync/ntsync.c +++ b/tools/testing/selftests/drivers/ntsync/ntsync.c @@ -706,4 +706,102 @@ TEST(wake_any) close(fd); } +TEST(wake_all) +{ + struct ntsync_mutex_args mutex_args = {0}; + struct ntsync_wait_args wait_args = {0}; + struct ntsync_sem_args sem_args = {0}; + struct wait_args thread_args; + int objs[2], fd, ret; + __u32 count, index; + pthread_t thread; + + fd = open("/dev/ntsync", O_CLOEXEC | O_RDONLY); + ASSERT_LE(0, fd); + + sem_args.count = 0; + sem_args.max = 3; + sem_args.sem = 0xdeadbeef; + ret = ioctl(fd, NTSYNC_IOC_CREATE_SEM, &sem_args); + EXPECT_EQ(0, ret); + EXPECT_NE(0xdeadbeef, sem_args.sem); + + mutex_args.owner = 123; + mutex_args.count = 1; + mutex_args.mutex = 0xdeadbeef; + ret = ioctl(fd, NTSYNC_IOC_CREATE_MUTEX, &mutex_args); + EXPECT_EQ(0, ret); + EXPECT_NE(0xdeadbeef, mutex_args.mutex); + + objs[0] = sem_args.sem; + objs[1] = mutex_args.mutex; + + wait_args.timeout = get_abs_timeout(1000); + wait_args.objs = (uintptr_t)objs; + wait_args.count = 2; + wait_args.owner = 456; + thread_args.fd = fd; + thread_args.args = &wait_args; + thread_args.request = NTSYNC_IOC_WAIT_ALL; + ret = pthread_create(&thread, NULL, wait_thread, &thread_args); + EXPECT_EQ(0, ret); + + ret = wait_for_thread(thread, 100); + EXPECT_EQ(ETIMEDOUT, ret); + + count = 1; + ret = post_sem(sem_args.sem, &count); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, count); + + ret = pthread_tryjoin_np(thread, NULL); + EXPECT_EQ(EBUSY, ret); + + check_sem_state(sem_args.sem, 1, 3); + + ret = wait_any(fd, 1, &sem_args.sem, 123, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, index); + + ret = unlock_mutex(mutex_args.mutex, 123, &count); + EXPECT_EQ(0, ret); + EXPECT_EQ(1, count); + + ret = pthread_tryjoin_np(thread, NULL); + EXPECT_EQ(EBUSY, ret); + + check_mutex_state(mutex_args.mutex, 0, 0); + + count = 2; + ret = post_sem(sem_args.sem, &count); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, count); + check_sem_state(sem_args.sem, 1, 3); + check_mutex_state(mutex_args.mutex, 1, 456); + + ret = wait_for_thread(thread, 100); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, thread_args.ret); + + /* delete an object while it's being waited on */ + + wait_args.timeout = get_abs_timeout(200); + wait_args.owner = 123; + ret = pthread_create(&thread, NULL, wait_thread, &thread_args); + EXPECT_EQ(0, ret); + + ret = wait_for_thread(thread, 100); + EXPECT_EQ(ETIMEDOUT, ret); + + close(sem_args.sem); + close(mutex_args.mutex); + + ret = wait_for_thread(thread, 200); + EXPECT_EQ(0, ret); + EXPECT_EQ(-1, thread_args.ret); + EXPECT_EQ(ETIMEDOUT, thread_args.err); + + close(fd); +} + TEST_HARNESS_MAIN From patchwork Wed Feb 14 23:52:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201198 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp63347dyb; Wed, 14 Feb 2024 16:00:59 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWcVLsWLv8zcrp0624dy4WppOuuBAa8kJl5VWuIQU46M7a7TOYAQbZL9N4wr3X1EUfixNWFJqizq7fgpgivlDjFyhmKGQ== X-Google-Smtp-Source: AGHT+IEIgEVdZ6sWI5AtsY4ReM1i/t1Se9f7In69UyZM0sT1Lbv2+CtSD0GWEzrsES+75jPvt6TT X-Received: by 2002:a05:6102:161f:b0:46d:3a99:7f0 with SMTP id cu31-20020a056102161f00b0046d3a9907f0mr536009vsb.2.1707955259295; Wed, 14 Feb 2024 16:00:59 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707955259; cv=pass; d=google.com; s=arc-20160816; b=lQGnz0kgEZUe41mRZ44hTQw7KZEkf/uCgPMCH6DLd/ye3jvHPb51bnKjhbeHV7KeOV bGQX8cVdM5kny4tiH83Nfne4xFyRrhUKbn3vpVJCN6xOQ59QfA7v09EO9HTMtlbZjdPg H/U/Y/is1IbRJBFNBPTbyxUURje9YljgMxScr3z4SVAByZh4KQpcGPiZaVa1CTYFEk49 120/ydxs591QKl+La9WZYTvgjv6RAeu3/jngwwJmxHIUFrH53N6bf7Xh0LMawIk0nx2N LPJQGGngoei1p5w2W9RnzSaNIId1B3JFVFYR+sEWtU7k5zpKnpyN1xyzjzlXbh1ShIgC Ae6w== 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=4TsUlLJY8QFZnk/5/kjEI+hyqZYDUecMdBE08/85OkY=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=u7gjwRswYIjQnJcvf5/mMIFEyt0SUq17M9aDXfE+ooxXYAJEhxDrdAASQ+Vk6H/tGo VMQzbSaXVAtlDWSJ4/dcc2H3C0eXvrrZTGnFxlllSpHqY1hAW2B+jP0YwQxE91Ya28Cr dItTAjZwcfgrs4rWU1diR8vIrNwGI4qmWx0XPvqrmstsUEEjXaQz5YfWWpS2zRK4GpSX JKIydM/M62d8VW3+vVwAAc/XvnBX9w/RfF4O7E3oX3pH9qNSvj/RYa2Rpsos6vnA+/oh ptE/yWFO2oOZNaXWykcGEWL4GqhSX2zfji7Q5HRbqqGnHLExovHxbaD4iStrTR5WAX3Y 7XxA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=P5U48B93; 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-66159-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66159-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id b7-20020ac85bc7000000b0042c7659583dsi115970qtb.690.2024.02.14.16.00.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 16:00:59 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66159-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=P5U48B93; 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-66159-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66159-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id C6C0D1C20EDC for ; Thu, 15 Feb 2024 00:00:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E238F14E2F2; Wed, 14 Feb 2024 23:53:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="P5U48B93" 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 4D29214C58C; Wed, 14 Feb 2024 23:53:47 +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=1707954828; cv=none; b=PVuOmevxPTM3Oad+PO61YQeTm0ijNLQ8YN/JOavC/km6sNie8AOJX6l87TUIDvHQ6YVNTSYisY2s/eB7c0OAP+lF4QNuVzL3/f77e/ytCGVaIA9nz9pQdEKUafzkSv9qBwEwaWN05OxEa05N4TajO7Im92lhMQluHOlJ8lRJG1w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954828; c=relaxed/simple; bh=pFMFdKbGwK6565Qe46cPQeiQtHcHamkOetCQ7CUhyKg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ULQkQsIXtTvO6XgzBrP1ZhOOveYfDKDnjXCAQSwylGaKNTxeo2m0AEiZvj7NdmaBtjm7ufXyChk3GpuUjapS/OEoC+h68YXQ+vBJPIFfB11g4FHkramBUS/AVvOBPAJkzCDYCve4WQaQDjilqyYrnqJc7Br03Ra+OfI6j/7KhQ8= 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=P5U48B93; 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=4TsUlLJY8QFZnk/5/kjEI+hyqZYDUecMdBE08/85OkY=; b=P5U48B93FL9CbE1r1FdXS2+BYP rGlwcDMnLD8pKk7/kCUPI+y7pHPpL7+/TkcynSZB15cB3dOkTbaxwMIRn0nZul3xVBSZZYjGIKATr gR2TbMuH/7aaIjIPphNiBkoxN3JvvV/Yat1nkQuPzyKyrzKun0uqp9yZimmeabdaD/VZcgzleHvzf hngr9RrJPHk+XiX6Kv1CxDBItDQiP8O84RXkfRcxFaktwozyOuIIeSoHSs9A5VqSX9YCZ0a7Hlmmc 9prq6H1ywm32xOgnxTqd9FVH5+Aa6yQxdU9Steh0G53mmGAa5YN81eba3Er/0A2b90244jL7EzHC6 KHT8iQ6g==; 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 1raP4R-00GcSK-21; Wed, 14 Feb 2024 17:53:43 -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 24/31] selftests: ntsync: Add some tests for manual-reset event state. Date: Wed, 14 Feb 2024 17:52:59 -0600 Message-ID: <20240214235307.10494-15-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240214235307.10494-1-zfigura@codeweavers.com> References: <20240214233645.9273-1-zfigura@codeweavers.com> <20240214235307.10494-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: 1790920893945174440 X-GMAIL-MSGID: 1790920893945174440 Test event-specific ioctls NTSYNC_IOC_EVENT_SET, NTSYNC_IOC_EVENT_RESET, NTSYNC_IOC_EVENT_PULSE, NTSYNC_IOC_EVENT_READ for manual-reset events, and waiting on manual-reset events. Signed-off-by: Elizabeth Figura --- .../testing/selftests/drivers/ntsync/ntsync.c | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/tools/testing/selftests/drivers/ntsync/ntsync.c b/tools/testing/selftests/drivers/ntsync/ntsync.c index 063c0ecb3bff..7cc6fdf10abd 100644 --- a/tools/testing/selftests/drivers/ntsync/ntsync.c +++ b/tools/testing/selftests/drivers/ntsync/ntsync.c @@ -73,6 +73,27 @@ static int unlock_mutex(int mutex, __u32 owner, __u32 *count) return ret; } +static int read_event_state(int event, __u32 *signaled, __u32 *manual) +{ + struct ntsync_event_args args; + int ret; + + memset(&args, 0xcc, sizeof(args)); + ret = ioctl(event, NTSYNC_IOC_EVENT_READ, &args); + *signaled = args.signaled; + *manual = args.manual; + return ret; +} + +#define check_event_state(event, signaled, manual) \ + ({ \ + __u32 __signaled, __manual; \ + int ret = read_event_state((event), &__signaled, &__manual); \ + EXPECT_EQ(0, ret); \ + EXPECT_EQ((signaled), __signaled); \ + EXPECT_EQ((manual), __manual); \ + }) + static int wait_objs(int fd, unsigned long request, __u32 count, const int *objs, __u32 owner, __u32 *index) { @@ -353,6 +374,74 @@ TEST(mutex_state) close(fd); } +TEST(manual_event_state) +{ + struct ntsync_event_args event_args; + __u32 index, signaled; + int fd, event, ret; + + fd = open("/dev/ntsync", O_CLOEXEC | O_RDONLY); + ASSERT_LE(0, fd); + + event_args.manual = 1; + event_args.signaled = 0; + event_args.event = 0xdeadbeef; + ret = ioctl(fd, NTSYNC_IOC_CREATE_EVENT, &event_args); + EXPECT_EQ(0, ret); + EXPECT_NE(0xdeadbeef, event_args.event); + event = event_args.event; + check_event_state(event, 0, 1); + + signaled = 0xdeadbeef; + ret = ioctl(event, NTSYNC_IOC_EVENT_SET, &signaled); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, signaled); + check_event_state(event, 1, 1); + + ret = ioctl(event, NTSYNC_IOC_EVENT_SET, &signaled); + EXPECT_EQ(0, ret); + EXPECT_EQ(1, signaled); + check_event_state(event, 1, 1); + + ret = wait_any(fd, 1, &event, 123, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, index); + check_event_state(event, 1, 1); + + signaled = 0xdeadbeef; + ret = ioctl(event, NTSYNC_IOC_EVENT_RESET, &signaled); + EXPECT_EQ(0, ret); + EXPECT_EQ(1, signaled); + check_event_state(event, 0, 1); + + ret = ioctl(event, NTSYNC_IOC_EVENT_RESET, &signaled); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, signaled); + check_event_state(event, 0, 1); + + ret = wait_any(fd, 1, &event, 123, &index); + EXPECT_EQ(-1, ret); + EXPECT_EQ(ETIMEDOUT, errno); + + ret = ioctl(event, NTSYNC_IOC_EVENT_SET, &signaled); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, signaled); + + ret = ioctl(event, NTSYNC_IOC_EVENT_PULSE, &signaled); + EXPECT_EQ(0, ret); + EXPECT_EQ(1, signaled); + check_event_state(event, 0, 1); + + ret = ioctl(event, NTSYNC_IOC_EVENT_PULSE, &signaled); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, signaled); + check_event_state(event, 0, 1); + + close(event); + + close(fd); +} + TEST(test_wait_any) { int objs[NTSYNC_MAX_WAIT_COUNT + 1], fd, ret; From patchwork Wed Feb 14 23:53:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201204 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp64456dyb; Wed, 14 Feb 2024 16:02:41 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVm28O0YK/AxWEE4oqSUnCF+xmjGWAc+CS6a4RoxlHs+ZRAI2zWVXYYIrvWwMiWu6EKGAHo2Jr4Q8T2sVAJbqWxwys1Sg== X-Google-Smtp-Source: AGHT+IFxb5m2jytFbaAd3/6pUXEp7Y+U6Jz2SjAZjU83+uGo2GNzT1cStK60940NdCwwPPUPQl2O X-Received: by 2002:a17:902:f68e:b0:1da:1dbc:bf9e with SMTP id l14-20020a170902f68e00b001da1dbcbf9emr246088plg.47.1707955361251; Wed, 14 Feb 2024 16:02:41 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707955361; cv=pass; d=google.com; s=arc-20160816; b=tmcJO5M/mDKb5DUJz1luHdRARq+LcjOdUcxSDuMP9/patC+Lpbu//VWOzzyGwwzoiW oJYlR/LfRllIPmqcwE6fcWjzi8P/Rgye8frSv1sUWKSxhTLfBbiE4bokUeuHGdidce/B OCLBqsrbCm/nqrdjS4GX6JTER4a6aZjamrZD6rlRx2kQmdSZCJEKctmI9IcGv/dAlv+N jlvWjcfb9bwZqmQjUJ72fHQlck2sr97gmwKRXzazOeN1U5EIzMi03sA4cn/O6kN4tfmY rWi8gSDo9ogVkaKsjX8tFORQqkJRkE7fZvL8NDGiFi2DduVQtS/vwTdvC7gr3MCYdtBT 7bTQ== 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=dfzB4q87+P8orUXmAGMZBiDPDRQbWh8zWpHSIv/oNb8=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=pBFjpyTkQH6YmRDQ+/fgBrri/6AYLQ4Vazy504PNDUzt+Gq6a/vXQTA2CZPqndLayD HX8t1xz/7LmtMfa3XGqRxnskGyoNICfndtF6/VuwJtlncAgKyWB9uq2c8N7UV7uHJb6B v+Vh2z5s9kvfuDcIkHVPbuPLJsU5UjFAi5zOS1iVHk76KFCHYoFUpZGV5XK8kLVEsWWA YenfG+/ut9/YkHhOD7UARvfiE7R724j8PwGi/WOqh5maA/rfRmNVS6DuPZkij+EeQ71r +yzC9fy887BaDClgc1V5vFX8HDLnMkDYZ0Y6PLHYbsTUB1W2rYXfdVcCSaTYGaPNne7b WTaA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b="LC/OsRuU"; 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-66160-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66160-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 y11-20020a17090322cb00b001da0beabfdasi36160plg.207.2024.02.14.16.02.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 16:02:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66160-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="LC/OsRuU"; 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-66160-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66160-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 2B46928393D for ; Thu, 15 Feb 2024 00:00:16 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1CC8914E2FE; Wed, 14 Feb 2024 23:53:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="LC/OsRuU" 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 C17B314C59C; Wed, 14 Feb 2024 23:53:47 +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=1707954829; cv=none; b=hS3lv0mv1NH4R/SghTwTe4QAmlYevi8miuEJXzd2CAyQ1gAtiFcpuG09/sGKspGeNZ1e9ll2NKoJUs05QdQGUt9ZKabtZRKoeRpEg1f5hlPjji5tYjs3QBdXikDBHP+eiuoYtmnMG96qbayD4HrK+l3UtMoWFwAK6TlIzf+Ffm4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954829; c=relaxed/simple; bh=UgpG6jjwC+kDfZfnjxxOjeWemeUkGXkpkWpYzrvW9hk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OoE+iM8bm8LkJBfBtbUQbJSRQMN73Rgl4eStyfMzGPuDJebkAED8wcl+A4WGleeT+mhom8ogz9PoWzY//F+65MoOCK0+N5apI6vo6XjQ7JKnlRcdBNt8BO7SpdpBRyJT+6U8h4VvuIjfSRG1ZYUcIbjwgMjVh2WbBOBHhRbToy0= 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=LC/OsRuU; 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=dfzB4q87+P8orUXmAGMZBiDPDRQbWh8zWpHSIv/oNb8=; b=LC/OsRuUYD4zN0e6gcly3uCJGZ jx7cccSnBN3hemhTKjjVvF79XI5kj4HtR1IqzU22z8tt5tEr5r0NYjfLBG2ytFT89nfwWbqYQAOff hsfk/bbn31hd7UIkXmtpreZ15FJw3yCYUTwFgFkd4Bu7JpZ4KeR+3OHXJ4Pmh8qznnEn+fYPXpWuX k0X8RCHCtdCAWNjzEcvrOemKYcpmD8Ecwq7bIsnDKI6fnQmSdUIv8mGzmdAgCUfTy+KiZmJQsZQAE PM+QSeNND3fiGrcalpTHNFHJ79mHgns9ERx0QJQE4I/gzzGVghy/v2L2L7EpVrvW/RB5Gs6589nIz y/AgHVkQ==; 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 1raP4S-00GcSK-1z; Wed, 14 Feb 2024 17:53:44 -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 25/31] selftests: ntsync: Add some tests for auto-reset event state. Date: Wed, 14 Feb 2024 17:53:00 -0600 Message-ID: <20240214235307.10494-16-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240214235307.10494-1-zfigura@codeweavers.com> References: <20240214233645.9273-1-zfigura@codeweavers.com> <20240214235307.10494-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: 1790921000867803360 X-GMAIL-MSGID: 1790921000867803360 Test event-specific ioctls NTSYNC_IOC_EVENT_SET, NTSYNC_IOC_EVENT_RESET, NTSYNC_IOC_EVENT_PULSE, NTSYNC_IOC_EVENT_READ for auto-reset events, and waiting on auto-reset events. Signed-off-by: Elizabeth Figura --- .../testing/selftests/drivers/ntsync/ntsync.c | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/tools/testing/selftests/drivers/ntsync/ntsync.c b/tools/testing/selftests/drivers/ntsync/ntsync.c index 7cc6fdf10abd..49ee4be280f6 100644 --- a/tools/testing/selftests/drivers/ntsync/ntsync.c +++ b/tools/testing/selftests/drivers/ntsync/ntsync.c @@ -442,6 +442,65 @@ TEST(manual_event_state) close(fd); } +TEST(auto_event_state) +{ + struct ntsync_event_args event_args; + __u32 index, signaled; + int fd, event, ret; + + fd = open("/dev/ntsync", O_CLOEXEC | O_RDONLY); + ASSERT_LE(0, fd); + + event_args.manual = 0; + event_args.signaled = 1; + event_args.event = 0xdeadbeef; + ret = ioctl(fd, NTSYNC_IOC_CREATE_EVENT, &event_args); + EXPECT_EQ(0, ret); + EXPECT_NE(0xdeadbeef, event_args.event); + event = event_args.event; + + check_event_state(event, 1, 0); + + signaled = 0xdeadbeef; + ret = ioctl(event, NTSYNC_IOC_EVENT_SET, &signaled); + EXPECT_EQ(0, ret); + EXPECT_EQ(1, signaled); + check_event_state(event, 1, 0); + + ret = wait_any(fd, 1, &event, 123, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, index); + check_event_state(event, 0, 0); + + signaled = 0xdeadbeef; + ret = ioctl(event, NTSYNC_IOC_EVENT_RESET, &signaled); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, signaled); + check_event_state(event, 0, 0); + + ret = wait_any(fd, 1, &event, 123, &index); + EXPECT_EQ(-1, ret); + EXPECT_EQ(ETIMEDOUT, errno); + + ret = ioctl(event, NTSYNC_IOC_EVENT_SET, &signaled); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, signaled); + + ret = ioctl(event, NTSYNC_IOC_EVENT_PULSE, &signaled); + EXPECT_EQ(0, ret); + EXPECT_EQ(1, signaled); + check_event_state(event, 0, 0); + + ret = ioctl(event, NTSYNC_IOC_EVENT_PULSE, &signaled); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, signaled); + check_event_state(event, 0, 0); + + close(event); + + close(fd); +} + TEST(test_wait_any) { int objs[NTSYNC_MAX_WAIT_COUNT + 1], fd, ret; From patchwork Wed Feb 14 23:53:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201206 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp64860dyb; Wed, 14 Feb 2024 16:03:19 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUpNt4+87xUHQtTuuDxQvgLr9hNzpXJFZSk2AkB+OoHqaf3txQnnaRjg4G6eIIQDP5w8NrvnMZQG18iVvYg5gt/I09YFA== X-Google-Smtp-Source: AGHT+IEiVf0EdNecrfXBZO0kRyBabQ7fQuQDNelCMey2omQ7fRpA48HGHtODLMLoOw/ZYWHUraGg X-Received: by 2002:a17:902:650d:b0:1d9:8222:e723 with SMTP id b13-20020a170902650d00b001d98222e723mr205388plk.26.1707955399140; Wed, 14 Feb 2024 16:03:19 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707955399; cv=pass; d=google.com; s=arc-20160816; b=tXDO4w916wk4gJS/wfHlGxWVwVCIkMbdcykG32REOo0pAhRsqcHZCqjpSr6c6VV7Wq ILtG++tre3OlScisnnG/g9kKdgDnqiwwaGGoQH8QvzqYuJCzvk+ixVFWYbKrkkPQlXZ/ NfFMZn/WjHP+MFmRXgAODyQJP+qu36Q7aZl8A0qMSnavYu2WH9/9kabQ/0YoC33B8xZD 37sf/gB9ZCg+QUjNqOd5VgYWbkjY2y0lQxr2QMo5y8nnxaLpgXRRRiCoSURjK2CIEZfn a+3XM+NMg/cmZpIYZhvorneFVCpPBaI18WS1cJ7hLkLuMQ9s5ONcyK7VObBkyt/HF3QB AI+w== 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=7dn5WolQcrK3uS9fWtUm2XVEVusDAXkpRvnTtBWnrzc=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=pmCf27IDKYoGo/wR7YOPaKmlbWOf1MKMTyj0HHSEJ/y6OliJHPWOSGPok/TlQumACj zilkKnja6nxwx6eqsfgpLEc/LSMD/i9oSFcUElQl43PCbfiNXxHs7/RtEy8MB0pQ0Zr2 0nc4tE0zMoIGnlbptkgB7+DAPfElUu4qJJSJOUc9+tGnpYAixAMcywe3FO/iMpw19YT/ z57NaS6llkvLx4EMYaj+ARtwPu6dAG75IA4cF2cSkq7YURzn2dgMYwFMVdVAnTjZG0Kf fQJsCfYRWLZloxnX9h8IQiBCkdg0p6Rz6yOfLNzr1yw1CCyaNqfV/w2zo5Ny5/8a49h6 hQUg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=V7iG6Kx8; 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-66161-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66161-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 q11-20020a170902eb8b00b001d8f126011esi48679plg.41.2024.02.14.16.03.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 16:03:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66161-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=V7iG6Kx8; 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-66161-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66161-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 D4E2028AE0B for ; Thu, 15 Feb 2024 00:01:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A8054150997; Wed, 14 Feb 2024 23:53:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="V7iG6Kx8" 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 20D6614D42C; Wed, 14 Feb 2024 23:53:49 +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=1707954830; cv=none; b=azaE2mFfZmegGwx1+jmxI4u7PXYeGuaHvrOqiXV377+SLdfhqSz4k3gPyTU659x90QPNL6aSP9tpRWAJNwGVAxTyGC7BFZgESR4V1lsDQdNNEjFqOHdKSqLNQFhw9gWBeEXGDZUn5wrfBqtVy77rqHiD/fvvk1K0fDANIAXPqrY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954830; c=relaxed/simple; bh=XMvFHFc2CGosZP7MDvd92+zIVqQ2HheCAZ7F1doC6pc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DKAOFiXKjAGI7jATWYuJN+cYj7zNpDA3mQY5ioBxLkSzTl4IV7AdBB+gWuzJcdCaDHZvWkl7zw43ZIoxeRkSuGYFPhNwstnAsSoC0dW8JuPh1NmV8i3XqXgitWYAHMDImUl5lrw2C4StUIZL9C5qG6W3DK1rNG8X9oId3doTz5w= 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=V7iG6Kx8; 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=7dn5WolQcrK3uS9fWtUm2XVEVusDAXkpRvnTtBWnrzc=; b=V7iG6Kx8qAV3QNv+I3QCkxZZzH KaWhlVeuMPoIShH0A0/zt/96+sCoHqlZg1WOKikNN3+ZrYhyBelTssmpDF79S5E6+udQwSUN8YHK1 IUeXn1QiQg+fLAJyC2DmeN7rg5kyXd1u0jlLzQycOjQX3uKDiRpVqBfN75ruKdSp18KyGw663JA/e J3zw+/PLS6ni+wWITGY+0lJmZFqdlaLjq9cP7jlueq4Qgi9fmdC70IoZLZcaWtIQGOOuO7WlK2IxU 7JiWQ/p+5/74WOvJw2zILJ8EHAb7Gd4+S9l9CA+nrtLucd7VZY8B8rojwwwWzTnwwNrpHUD/m3Fxe GWSXNCBw==; 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 1raP4T-00GcSK-27; Wed, 14 Feb 2024 17:53:45 -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 26/31] selftests: ntsync: Add some tests for wakeup signaling with events. Date: Wed, 14 Feb 2024 17:53:01 -0600 Message-ID: <20240214235307.10494-17-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240214235307.10494-1-zfigura@codeweavers.com> References: <20240214233645.9273-1-zfigura@codeweavers.com> <20240214235307.10494-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: 1790921040524000016 X-GMAIL-MSGID: 1790921040524000016 Expand the contended wait tests, which previously only covered events and semaphores, to cover events as well. Signed-off-by: Elizabeth Figura --- .../testing/selftests/drivers/ntsync/ntsync.c | 151 +++++++++++++++++- 1 file changed, 147 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/drivers/ntsync/ntsync.c b/tools/testing/selftests/drivers/ntsync/ntsync.c index 49ee4be280f6..bcf33bcf8c51 100644 --- a/tools/testing/selftests/drivers/ntsync/ntsync.c +++ b/tools/testing/selftests/drivers/ntsync/ntsync.c @@ -622,6 +622,7 @@ TEST(test_wait_any) TEST(test_wait_all) { + struct ntsync_event_args event_args = {0}; struct ntsync_mutex_args mutex_args = {0}; struct ntsync_sem_args sem_args = {0}; __u32 owner, index, count; @@ -644,6 +645,11 @@ TEST(test_wait_all) EXPECT_EQ(0, ret); EXPECT_NE(0xdeadbeef, mutex_args.mutex); + event_args.manual = true; + event_args.signaled = true; + ret = ioctl(fd, NTSYNC_IOC_CREATE_EVENT, &event_args); + EXPECT_EQ(0, ret); + objs[0] = sem_args.sem; objs[1] = mutex_args.mutex; @@ -692,6 +698,14 @@ TEST(test_wait_all) check_sem_state(sem_args.sem, 1, 3); check_mutex_state(mutex_args.mutex, 1, 123); + objs[0] = sem_args.sem; + objs[1] = event_args.event; + ret = wait_all(fd, 2, objs, 123, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, index); + check_sem_state(sem_args.sem, 0, 3); + check_event_state(event_args.event, 1, 1); + /* test waiting on the same object twice */ objs[0] = objs[1] = sem_args.sem; ret = wait_all(fd, 2, objs, 123, &index); @@ -700,6 +714,7 @@ TEST(test_wait_all) close(sem_args.sem); close(mutex_args.mutex); + close(event_args.event); close(fd); } @@ -746,12 +761,13 @@ static int wait_for_thread(pthread_t thread, unsigned int ms) TEST(wake_any) { + struct ntsync_event_args event_args = {0}; struct ntsync_mutex_args mutex_args = {0}; struct ntsync_wait_args wait_args = {0}; struct ntsync_sem_args sem_args = {0}; struct wait_args thread_args; + __u32 count, index, signaled; int objs[2], fd, ret; - __u32 count, index; pthread_t thread; fd = open("/dev/ntsync", O_CLOEXEC | O_RDONLY); @@ -833,10 +849,101 @@ TEST(wake_any) EXPECT_EQ(0, thread_args.ret); EXPECT_EQ(1, wait_args.index); + /* test waking events */ + + event_args.manual = false; + event_args.signaled = false; + ret = ioctl(fd, NTSYNC_IOC_CREATE_EVENT, &event_args); + EXPECT_EQ(0, ret); + + objs[1] = event_args.event; + wait_args.timeout = get_abs_timeout(1000); + ret = pthread_create(&thread, NULL, wait_thread, &thread_args); + EXPECT_EQ(0, ret); + + ret = wait_for_thread(thread, 100); + EXPECT_EQ(ETIMEDOUT, ret); + + ret = ioctl(event_args.event, NTSYNC_IOC_EVENT_SET, &signaled); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, signaled); + check_event_state(event_args.event, 0, 0); + + ret = wait_for_thread(thread, 100); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, thread_args.ret); + EXPECT_EQ(1, wait_args.index); + + wait_args.timeout = get_abs_timeout(1000); + ret = pthread_create(&thread, NULL, wait_thread, &thread_args); + EXPECT_EQ(0, ret); + + ret = wait_for_thread(thread, 100); + EXPECT_EQ(ETIMEDOUT, ret); + + ret = ioctl(event_args.event, NTSYNC_IOC_EVENT_PULSE, &signaled); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, signaled); + check_event_state(event_args.event, 0, 0); + + ret = wait_for_thread(thread, 100); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, thread_args.ret); + EXPECT_EQ(1, wait_args.index); + + close(event_args.event); + + event_args.manual = true; + event_args.signaled = false; + ret = ioctl(fd, NTSYNC_IOC_CREATE_EVENT, &event_args); + EXPECT_EQ(0, ret); + + objs[1] = event_args.event; + wait_args.timeout = get_abs_timeout(1000); + ret = pthread_create(&thread, NULL, wait_thread, &thread_args); + EXPECT_EQ(0, ret); + + ret = wait_for_thread(thread, 100); + EXPECT_EQ(ETIMEDOUT, ret); + + ret = ioctl(event_args.event, NTSYNC_IOC_EVENT_SET, &signaled); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, signaled); + check_event_state(event_args.event, 1, 1); + + ret = wait_for_thread(thread, 100); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, thread_args.ret); + EXPECT_EQ(1, wait_args.index); + + ret = ioctl(event_args.event, NTSYNC_IOC_EVENT_RESET, &signaled); + EXPECT_EQ(0, ret); + EXPECT_EQ(1, signaled); + + wait_args.timeout = get_abs_timeout(1000); + ret = pthread_create(&thread, NULL, wait_thread, &thread_args); + EXPECT_EQ(0, ret); + + ret = wait_for_thread(thread, 100); + EXPECT_EQ(ETIMEDOUT, ret); + + ret = ioctl(event_args.event, NTSYNC_IOC_EVENT_PULSE, &signaled); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, signaled); + check_event_state(event_args.event, 0, 1); + + ret = wait_for_thread(thread, 100); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, thread_args.ret); + EXPECT_EQ(1, wait_args.index); + + close(event_args.event); + /* delete an object while it's being waited on */ wait_args.timeout = get_abs_timeout(200); wait_args.owner = 123; + objs[1] = mutex_args.mutex; ret = pthread_create(&thread, NULL, wait_thread, &thread_args); EXPECT_EQ(0, ret); @@ -856,12 +963,14 @@ TEST(wake_any) TEST(wake_all) { + struct ntsync_event_args manual_event_args = {0}; + struct ntsync_event_args auto_event_args = {0}; struct ntsync_mutex_args mutex_args = {0}; struct ntsync_wait_args wait_args = {0}; struct ntsync_sem_args sem_args = {0}; struct wait_args thread_args; - int objs[2], fd, ret; - __u32 count, index; + __u32 count, index, signaled; + int objs[4], fd, ret; pthread_t thread; fd = open("/dev/ntsync", O_CLOEXEC | O_RDONLY); @@ -881,12 +990,24 @@ TEST(wake_all) EXPECT_EQ(0, ret); EXPECT_NE(0xdeadbeef, mutex_args.mutex); + manual_event_args.manual = true; + manual_event_args.signaled = true; + ret = ioctl(fd, NTSYNC_IOC_CREATE_EVENT, &manual_event_args); + EXPECT_EQ(0, ret); + + auto_event_args.manual = false; + auto_event_args.signaled = true; + ret = ioctl(fd, NTSYNC_IOC_CREATE_EVENT, &auto_event_args); + EXPECT_EQ(0, ret); + objs[0] = sem_args.sem; objs[1] = mutex_args.mutex; + objs[2] = manual_event_args.event; + objs[3] = auto_event_args.event; wait_args.timeout = get_abs_timeout(1000); wait_args.objs = (uintptr_t)objs; - wait_args.count = 2; + wait_args.count = 4; wait_args.owner = 456; thread_args.fd = fd; thread_args.args = &wait_args; @@ -920,12 +1041,32 @@ TEST(wake_all) check_mutex_state(mutex_args.mutex, 0, 0); + ret = ioctl(manual_event_args.event, NTSYNC_IOC_EVENT_RESET, &signaled); + EXPECT_EQ(0, ret); + EXPECT_EQ(1, signaled); + count = 2; ret = post_sem(sem_args.sem, &count); EXPECT_EQ(0, ret); EXPECT_EQ(0, count); + check_sem_state(sem_args.sem, 2, 3); + + ret = ioctl(auto_event_args.event, NTSYNC_IOC_EVENT_RESET, &signaled); + EXPECT_EQ(0, ret); + EXPECT_EQ(1, signaled); + + ret = ioctl(manual_event_args.event, NTSYNC_IOC_EVENT_SET, &signaled); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, signaled); + + ret = ioctl(auto_event_args.event, NTSYNC_IOC_EVENT_SET, &signaled); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, signaled); + check_sem_state(sem_args.sem, 1, 3); check_mutex_state(mutex_args.mutex, 1, 456); + check_event_state(manual_event_args.event, 1, 1); + check_event_state(auto_event_args.event, 0, 0); ret = wait_for_thread(thread, 100); EXPECT_EQ(0, ret); @@ -943,6 +1084,8 @@ TEST(wake_all) close(sem_args.sem); close(mutex_args.mutex); + close(manual_event_args.event); + close(auto_event_args.event); ret = wait_for_thread(thread, 200); EXPECT_EQ(0, ret); From patchwork Wed Feb 14 23:53:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201205 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp64810dyb; Wed, 14 Feb 2024 16:03:14 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXpcG3Xic/c8iNIZYg/u8sjP+t1tAXnmsjYiIUVtH+EJgMsWyccH+2zrLj+le1pZUlaQGYP2W5muxbAaYv7uhklnSapaw== X-Google-Smtp-Source: AGHT+IEajBpHWdg2xD8+MIHJFE4U+D1gh8vG05Qd3eR/L+j+mlC2ukJHLN84m3menYFe73n+Kurj X-Received: by 2002:a05:6a00:2354:b0:6e0:4759:88f2 with SMTP id j20-20020a056a00235400b006e0475988f2mr361912pfj.4.1707955394054; Wed, 14 Feb 2024 16:03:14 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707955394; cv=pass; d=google.com; s=arc-20160816; b=FZE17TLC1H4mJFSkhzxT95AJJbPCP+VT2XglmWvrQKFRlVAy7pPixTGrawAUX6q4NT 04dtjoibwr7CMs3Sa3clwSi9ErThlTSKYXxuIWcHRtbr2lFfAJkY5jFlxBWDiQvDoZQI lY9M+0HoWSB99nnkwYbeNbEZuiJDYnpUVNxj4k0IYoiFqJ70xTOiVYnyXa7+hl7Gg3qc 2XiyadfxQ5Hy3et/UfyF+6gJwhtDW5yBjQkl7FpCZQVe6WP5uFT6UBUGBydegn+UwT3E RN7yBNyZ4ntsANKehjcbb15QvggB8B0IhTq2WrLYb+MmVN6MMdIeOAHq17TnViDxMEA6 0ThA== 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=wAxHkc1KRM5IG5HCO70haB7ZsnprDYue3SbtBV3eDKM=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=cQlJy0uQnVDsfQGJrlz3qZnUyRyRb6SDRNsip4MXvAaFmzdatTnXcA9zYivdO5lRCQ cmAm0WezXYIg6KUrK6guaJGkU6EM4PEmt4mblb3aOjfLjs4su2RhpCUITLafl9d8sk3j 3QgCem8ZvfsbTOj/RLKe/HsIn1EoYH1QAhmW1rcY862hZNXZ65ZmeSlkCGUqrE8hB1IY VaTipa1GQckkiJgnUoZe4mIT5d/xjastbQDSYisVa2Yr8kc8t0obVlYfdXEC7rUkTcyy cLIBIf1gantBEVNofpLBrunaXBf14O4EDlfHPpBl4HpFVtIYCowCJ4d5SjsS7iYKugfp Yh6w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b="o1t/pmpg"; 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-66162-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66162-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. [139.178.88.99]) by mx.google.com with ESMTPS id bn9-20020a056a00324900b006e05260e1easi60470pfb.100.2024.02.14.16.03.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 16:03:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66162-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b="o1t/pmpg"; 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-66162-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66162-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 52DC7289C81 for ; Thu, 15 Feb 2024 00:00:55 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 77A4B150992; Wed, 14 Feb 2024 23:53:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="o1t/pmpg" 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 51C9E14D433; Wed, 14 Feb 2024 23:53:49 +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=1707954831; cv=none; b=VTBTLOJFJQRo8zIArp0dNOhDQiulhkVx1YEgva7MQ2Lo8oSKDQU2+l0gxWh3uxF/oOPeZgBUQ95pTWSkXKVcnDSK+/EVbCBMXUXLgE5ZEN/l5LqYXcDOgaFcbhquUQJ5bgpRqRQvf9JP/UT9BKoS2iV2UbgHyKIC1xloi1UYfYM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954831; c=relaxed/simple; bh=Ddjx78QUtC0RbxcDkYV8RaG2W8+XPCha3K5iES8++eQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QpJLMJynzgwR/b+YGFmBvTfknPto0qkSIw0KBPHx/180amY6UCnGHBKSpZi0+UFPHfSwnn1HeniXAgp9RISxh4ZqfPqKlV78yYzwslW8m24LsRXMpTNjTBz2GwYj5UDE7Ny7LnQ/9IP+BK8AghKSc8jNW3pgyG1p3EoEYq0CpxY= 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=o1t/pmpg; 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=wAxHkc1KRM5IG5HCO70haB7ZsnprDYue3SbtBV3eDKM=; b=o1t/pmpgwGWoAmXrVwe6+cZ6wr O3xTkrcltn0bCz3kT21I6Au57veA578Jgb8ibgTUAJR8INP0ZFE0lSXUmwYeqWh5OCwMUN/2udphh uOB4AeEIf/MaokUZjMb7NBBuuBzdjocZxoHxw294DGE4ATLYNe5dJubAMNTzcDQ+4pMvT6uk4IeJG wG4VSJhVGHdcRAVxlHPIpHMfNEfTUBYK6oGhTYkB1wGlPpWqMxGqeShelIyt0HE5rUwQH3qJKb9X3 ikTQhXQZ78gP+3ULpB/J//8GP2xKR62a6ZoCRixXRbQhWh0vMa2ySJf0QdgsbriQUh+UlKPSWigZS Kryg+IOw==; 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 1raP4U-00GcSK-2R; Wed, 14 Feb 2024 17:53:46 -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 27/31] selftests: ntsync: Add tests for alertable waits. Date: Wed, 14 Feb 2024 17:53:02 -0600 Message-ID: <20240214235307.10494-18-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240214235307.10494-1-zfigura@codeweavers.com> References: <20240214233645.9273-1-zfigura@codeweavers.com> <20240214235307.10494-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: 1790921034810630685 X-GMAIL-MSGID: 1790921034810630685 Test the "alert" functionality of NTSYNC_IOC_WAIT_ALL and NTSYNC_IOC_WAIT_ANY, when a wait is woken with an alert and when it is woken by an object. Signed-off-by: Elizabeth Figura --- .../testing/selftests/drivers/ntsync/ntsync.c | 179 +++++++++++++++++- 1 file changed, 176 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/drivers/ntsync/ntsync.c b/tools/testing/selftests/drivers/ntsync/ntsync.c index bcf33bcf8c51..6c13b1f25d38 100644 --- a/tools/testing/selftests/drivers/ntsync/ntsync.c +++ b/tools/testing/selftests/drivers/ntsync/ntsync.c @@ -95,7 +95,7 @@ static int read_event_state(int event, __u32 *signaled, __u32 *manual) }) static int wait_objs(int fd, unsigned long request, __u32 count, - const int *objs, __u32 owner, __u32 *index) + const int *objs, __u32 owner, int alert, __u32 *index) { struct ntsync_wait_args args = {0}; struct timespec timeout; @@ -108,6 +108,7 @@ static int wait_objs(int fd, unsigned long request, __u32 count, args.objs = (uintptr_t)objs; args.owner = owner; args.index = 0xdeadbeef; + args.alert = alert; ret = ioctl(fd, request, &args); *index = args.index; return ret; @@ -115,12 +116,26 @@ static int wait_objs(int fd, unsigned long request, __u32 count, static int wait_any(int fd, __u32 count, const int *objs, __u32 owner, __u32 *index) { - return wait_objs(fd, NTSYNC_IOC_WAIT_ANY, count, objs, owner, index); + return wait_objs(fd, NTSYNC_IOC_WAIT_ANY, count, objs, owner, 0, index); } static int wait_all(int fd, __u32 count, const int *objs, __u32 owner, __u32 *index) { - return wait_objs(fd, NTSYNC_IOC_WAIT_ALL, count, objs, owner, index); + return wait_objs(fd, NTSYNC_IOC_WAIT_ALL, count, objs, owner, 0, index); +} + +static int wait_any_alert(int fd, __u32 count, const int *objs, + __u32 owner, int alert, __u32 *index) +{ + return wait_objs(fd, NTSYNC_IOC_WAIT_ANY, + count, objs, owner, alert, index); +} + +static int wait_all_alert(int fd, __u32 count, const int *objs, + __u32 owner, int alert, __u32 *index) +{ + return wait_objs(fd, NTSYNC_IOC_WAIT_ALL, + count, objs, owner, alert, index); } TEST(semaphore_state) @@ -1095,4 +1110,162 @@ TEST(wake_all) close(fd); } +TEST(alert_any) +{ + struct ntsync_event_args event_args = {0}; + struct ntsync_sem_args sem_args = {0}; + __u32 index, count, signaled; + int objs[2], fd, ret; + + fd = open("/dev/ntsync", O_CLOEXEC | O_RDONLY); + ASSERT_LE(0, fd); + + sem_args.count = 0; + sem_args.max = 2; + sem_args.sem = 0xdeadbeef; + ret = ioctl(fd, NTSYNC_IOC_CREATE_SEM, &sem_args); + EXPECT_EQ(0, ret); + EXPECT_NE(0xdeadbeef, sem_args.sem); + objs[0] = sem_args.sem; + + sem_args.count = 1; + sem_args.max = 2; + sem_args.sem = 0xdeadbeef; + ret = ioctl(fd, NTSYNC_IOC_CREATE_SEM, &sem_args); + EXPECT_EQ(0, ret); + EXPECT_NE(0xdeadbeef, sem_args.sem); + objs[1] = sem_args.sem; + + event_args.manual = true; + event_args.signaled = true; + ret = ioctl(fd, NTSYNC_IOC_CREATE_EVENT, &event_args); + EXPECT_EQ(0, ret); + + ret = wait_any_alert(fd, 0, NULL, 123, event_args.event, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, index); + + ret = ioctl(event_args.event, NTSYNC_IOC_EVENT_RESET, &signaled); + EXPECT_EQ(0, ret); + + ret = wait_any_alert(fd, 0, NULL, 123, event_args.event, &index); + EXPECT_EQ(-1, ret); + EXPECT_EQ(ETIMEDOUT, errno); + + ret = ioctl(event_args.event, NTSYNC_IOC_EVENT_SET, &signaled); + EXPECT_EQ(0, ret); + + ret = wait_any_alert(fd, 2, objs, 123, event_args.event, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(1, index); + + ret = wait_any_alert(fd, 2, objs, 123, event_args.event, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(2, index); + + close(event_args.event); + + /* test with an auto-reset event */ + + event_args.manual = false; + event_args.signaled = true; + ret = ioctl(fd, NTSYNC_IOC_CREATE_EVENT, &event_args); + EXPECT_EQ(0, ret); + + count = 1; + ret = post_sem(objs[0], &count); + EXPECT_EQ(0, ret); + + ret = wait_any_alert(fd, 2, objs, 123, event_args.event, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, index); + + ret = wait_any_alert(fd, 2, objs, 123, event_args.event, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(2, index); + + ret = wait_any_alert(fd, 2, objs, 123, event_args.event, &index); + EXPECT_EQ(-1, ret); + EXPECT_EQ(ETIMEDOUT, errno); + + close(event_args.event); + + close(objs[0]); + close(objs[1]); + + close(fd); +} + +TEST(alert_all) +{ + struct ntsync_event_args event_args = {0}; + struct ntsync_sem_args sem_args = {0}; + __u32 index, count, signaled; + int objs[2], fd, ret; + + fd = open("/dev/ntsync", O_CLOEXEC | O_RDONLY); + ASSERT_LE(0, fd); + + sem_args.count = 2; + sem_args.max = 2; + sem_args.sem = 0xdeadbeef; + ret = ioctl(fd, NTSYNC_IOC_CREATE_SEM, &sem_args); + EXPECT_EQ(0, ret); + EXPECT_NE(0xdeadbeef, sem_args.sem); + objs[0] = sem_args.sem; + + sem_args.count = 1; + sem_args.max = 2; + sem_args.sem = 0xdeadbeef; + ret = ioctl(fd, NTSYNC_IOC_CREATE_SEM, &sem_args); + EXPECT_EQ(0, ret); + EXPECT_NE(0xdeadbeef, sem_args.sem); + objs[1] = sem_args.sem; + + event_args.manual = true; + event_args.signaled = true; + ret = ioctl(fd, NTSYNC_IOC_CREATE_EVENT, &event_args); + EXPECT_EQ(0, ret); + + ret = wait_all_alert(fd, 2, objs, 123, event_args.event, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, index); + + ret = wait_all_alert(fd, 2, objs, 123, event_args.event, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(2, index); + + close(event_args.event); + + /* test with an auto-reset event */ + + event_args.manual = false; + event_args.signaled = true; + ret = ioctl(fd, NTSYNC_IOC_CREATE_EVENT, &event_args); + EXPECT_EQ(0, ret); + + count = 2; + ret = post_sem(objs[1], &count); + EXPECT_EQ(0, ret); + + ret = wait_all_alert(fd, 2, objs, 123, event_args.event, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, index); + + ret = wait_all_alert(fd, 2, objs, 123, event_args.event, &index); + EXPECT_EQ(0, ret); + EXPECT_EQ(2, index); + + ret = wait_all_alert(fd, 2, objs, 123, event_args.event, &index); + EXPECT_EQ(-1, ret); + EXPECT_EQ(ETIMEDOUT, errno); + + close(event_args.event); + + close(objs[0]); + close(objs[1]); + + close(fd); +} + TEST_HARNESS_MAIN From patchwork Wed Feb 14 23:53:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201199 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp63677dyb; Wed, 14 Feb 2024 16:01:28 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU+Hbl879BXr1b/vmPCwsBzZxE2Z2XGbvMwdIXBF1Hb2juyTZ5E3Luo8yDrLaXRxhRHnyDsYryS/b9dJptVTMf8GZ9O2w== X-Google-Smtp-Source: AGHT+IEAeoQxVXWKr4amQUqg4eaybLGkaIepo2JeHabp05vu2i2MP4WTnuoZ4sSw2UltWKCIPggH X-Received: by 2002:a0c:cb12:0:b0:68f:135e:a051 with SMTP id o18-20020a0ccb12000000b0068f135ea051mr147728qvk.56.1707955288056; Wed, 14 Feb 2024 16:01:28 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707955288; cv=pass; d=google.com; s=arc-20160816; b=UeDTrdruHgVg4ROs7jqQe4JqAnzP8Ego2qiSKPiiGiXIecVc59QLK8npzBYSEyzw4F VWjYvIXzEsPtShQlSoMUpzuo1+SBRAIkSIPPJ/D1wI4DLINz256uFDkfBX3J/582PoFJ yrWTfTSEHCCRKkr5kwroBJGvTqiGEVr2Fvz7QLwzcWxFDUsFGBXyMXCLuVpChH2wZg29 2VDpcAQCvASTt33VXR56ngdO5Fhw/nA0KFiDxOLQYMO51JNiUm/tgigNZuDouTqwE+fF MZ1iRptvmmyEOVrj3Pyx6xxsr+B48DfxQN03dCZhMdctfnPa36R1pQjboOaStLlpTP78 VqVg== 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=nreuQ1mkx4GK2wPOFpIbOuQtSeY/XQ+g3hSeDJlq7hA=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=BxUg4EmoGzmRGRi+inuMLY0BLh+V/MDHo/izglqBMl9VFUeDRTEJzTtSIaYNXqgydv NnOhXcS3KxMHqWIu4jnlTXM4Rs2JQrRdS6PN59KhP2tro+zlHK1coD5D7/dCmrSQjCQ6 qcRXomj8jHUQMO1TOFHXp7kTTDb2YLNIoN2SQw7yMvIP6FDPfW/Hd2q+UObAAfD22fdH xd7pXgCHom6m9BPa5Hb8ydojYiiNt5q96RQLLNFCuI1g9/JYxRSg/FtUzuA7kvCgrSQ6 Pk8yWUWj4zSOs6Y5eWUtMVzvn9up8ePbeHHk9d68mBjcrAfSP9IVPD0mafKCy1VYswDR FHlw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=JLlDhLzs; 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-66163-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66163-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id iw1-20020a0562140f2100b0068c8b401467si176453qvb.597.2024.02.14.16.01.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 16:01:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66163-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=JLlDhLzs; 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-66163-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66163-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 41AB11C220ED for ; Thu, 15 Feb 2024 00:01:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D6CE91509A7; Wed, 14 Feb 2024 23:53:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="JLlDhLzs" 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 5D8AD14D456; Wed, 14 Feb 2024 23:53:50 +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=1707954831; cv=none; b=D/DXYXYdePwVQFAKmegaMq7T7sFIjd3LiEf7OS2PwlwYGBFsrBW23cT58GFyFARu5GTDqzVoCIFFWEkbVW63X4Dpdgwa235t2MBm11A4Z8qbU+1uGZ/+6NnEHDHk6oWNjUZst5Xmhu0joETvBfTyCuJZDxZtpnfA314f6FZmd5o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954831; c=relaxed/simple; bh=/pKkTBCauQqQ72O32eox8sahDmutQPf7m0HkhTATFWk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mxjqLfu40e54Bnc/fhw5AKuyYQGlmmg73wx97Fah8nOYrQaqb3Z7PYEYXbkHyWMS+oZt3xJRVvSIVMYG1wVVigbKHcBNeHLz5gHp/J3UOso1O0mF6ajpggIyZG0sPO0gcO2+U48j8NWFxsccboaH1GCwqHRt/vMrY+PlLO1eCHI= 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=JLlDhLzs; 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=nreuQ1mkx4GK2wPOFpIbOuQtSeY/XQ+g3hSeDJlq7hA=; b=JLlDhLzsE1Hl+EnS3mh0kTbMa4 wi5820ZyYUkuxukF6D3lTIyOVQSOaMGemu2lCt8SjRztrXRS+/mF0tererKUciLh3xAxWfSQMM35W NwKGuV/msH5EtcOtCzmINzQ9bu5Nk6mg4XzqEyKbKHBE6988kE4MlCD6/X+UvmwB+JqChgpF2wHjl GlI/Q4XzmmYBYArvyKmfBtckGCCrBcPLf1Ennlhy9lYJCIKqDpGgbQ/VCzPL5LcHoPLT64y+YnD69 7tPwJ6UW7Xl3vQ5hkoCFgq4Yy3r4zZ1RHjWuqLZp85w2ydFFUo/QrCA5pdPDmdygTKkGcyCnqyAXO zloAbOTA==; 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 1raP4V-00GcSK-1A; Wed, 14 Feb 2024 17:53:47 -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 28/31] selftests: ntsync: Add some tests for wakeup signaling via alerts. Date: Wed, 14 Feb 2024 17:53:03 -0600 Message-ID: <20240214235307.10494-19-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240214235307.10494-1-zfigura@codeweavers.com> References: <20240214233645.9273-1-zfigura@codeweavers.com> <20240214235307.10494-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: 1790920923853210256 X-GMAIL-MSGID: 1790920923853210256 Expand the alert tests to cover alerting a thread mid-wait, to test that the relevant scheduling logic works correctly. Signed-off-by: Elizabeth Figura --- .../testing/selftests/drivers/ntsync/ntsync.c | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/tools/testing/selftests/drivers/ntsync/ntsync.c b/tools/testing/selftests/drivers/ntsync/ntsync.c index 6c13b1f25d38..40cfdd356ef4 100644 --- a/tools/testing/selftests/drivers/ntsync/ntsync.c +++ b/tools/testing/selftests/drivers/ntsync/ntsync.c @@ -1113,9 +1113,12 @@ TEST(wake_all) TEST(alert_any) { struct ntsync_event_args event_args = {0}; + struct ntsync_wait_args wait_args = {0}; struct ntsync_sem_args sem_args = {0}; __u32 index, count, signaled; + struct wait_args thread_args; int objs[2], fd, ret; + pthread_t thread; fd = open("/dev/ntsync", O_CLOEXEC | O_RDONLY); ASSERT_LE(0, fd); @@ -1163,6 +1166,34 @@ TEST(alert_any) EXPECT_EQ(0, ret); EXPECT_EQ(2, index); + /* test wakeup via alert */ + + ret = ioctl(event_args.event, NTSYNC_IOC_EVENT_RESET, &signaled); + EXPECT_EQ(0, ret); + + wait_args.timeout = get_abs_timeout(1000); + wait_args.objs = (uintptr_t)objs; + wait_args.count = 2; + wait_args.owner = 123; + wait_args.index = 0xdeadbeef; + wait_args.alert = event_args.event; + thread_args.fd = fd; + thread_args.args = &wait_args; + thread_args.request = NTSYNC_IOC_WAIT_ANY; + ret = pthread_create(&thread, NULL, wait_thread, &thread_args); + EXPECT_EQ(0, ret); + + ret = wait_for_thread(thread, 100); + EXPECT_EQ(ETIMEDOUT, ret); + + ret = ioctl(event_args.event, NTSYNC_IOC_EVENT_SET, &signaled); + EXPECT_EQ(0, ret); + + ret = wait_for_thread(thread, 100); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, thread_args.ret); + EXPECT_EQ(2, wait_args.index); + close(event_args.event); /* test with an auto-reset event */ @@ -1199,9 +1230,12 @@ TEST(alert_any) TEST(alert_all) { struct ntsync_event_args event_args = {0}; + struct ntsync_wait_args wait_args = {0}; struct ntsync_sem_args sem_args = {0}; + struct wait_args thread_args; __u32 index, count, signaled; int objs[2], fd, ret; + pthread_t thread; fd = open("/dev/ntsync", O_CLOEXEC | O_RDONLY); ASSERT_LE(0, fd); @@ -1235,6 +1269,34 @@ TEST(alert_all) EXPECT_EQ(0, ret); EXPECT_EQ(2, index); + /* test wakeup via alert */ + + ret = ioctl(event_args.event, NTSYNC_IOC_EVENT_RESET, &signaled); + EXPECT_EQ(0, ret); + + wait_args.timeout = get_abs_timeout(1000); + wait_args.objs = (uintptr_t)objs; + wait_args.count = 2; + wait_args.owner = 123; + wait_args.index = 0xdeadbeef; + wait_args.alert = event_args.event; + thread_args.fd = fd; + thread_args.args = &wait_args; + thread_args.request = NTSYNC_IOC_WAIT_ALL; + ret = pthread_create(&thread, NULL, wait_thread, &thread_args); + EXPECT_EQ(0, ret); + + ret = wait_for_thread(thread, 100); + EXPECT_EQ(ETIMEDOUT, ret); + + ret = ioctl(event_args.event, NTSYNC_IOC_EVENT_SET, &signaled); + EXPECT_EQ(0, ret); + + ret = wait_for_thread(thread, 100); + EXPECT_EQ(0, ret); + EXPECT_EQ(0, thread_args.ret); + EXPECT_EQ(2, wait_args.index); + close(event_args.event); /* test with an auto-reset event */ From patchwork Wed Feb 14 23:53:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201200 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp63781dyb; Wed, 14 Feb 2024 16:01:38 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU3A8+9HMuYig9LbNCQGRUdBcKAk0j6ZfRWOkbL8H9wu/3Is9gOZGoefqiaj0eym4J8b6jzX9FXwkUCJmK92FZTvV/AkQ== X-Google-Smtp-Source: AGHT+IFE7pQtRmB1mFPRmqVdyyjdKMRpLC85vpnylLLYBwJX1dfDZQw8rykOJJ/7B9kokkAycSIX X-Received: by 2002:a05:6830:12cf:b0:6e4:2202:ff9f with SMTP id a15-20020a05683012cf00b006e42202ff9fmr124784otq.17.1707955298672; Wed, 14 Feb 2024 16:01:38 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707955298; cv=pass; d=google.com; s=arc-20160816; b=shPhEzfPx7sjNpB/4esGDoil+886RyBAkiKf2cCzYbKL4PNbEc/ytFm+U7l4ZVaxsp ASAMmTZAYqgcAhHZUVe4aSJfHb5MBTCl1kLPWcLnec1bX0KZOkWUugL45spcLlPMWp5W oF5pVaiXtf6Jh1U0vJFW1y/Y8vU44vSk6SctK7NerMB99/TTE8jfxD8VAugeX79KsbgO GK88g959jP60bm8ZI8EZp4ykIEq7LuZzKSYbCOGvk1HqZQx+hBFjpKK/En5J2mESzWRQ dOxSY8OVI85cPBG2KT6m+erG/xXaRPtEXznmBxa9GdmceluJ/OGpYfEky+HijdUAg3KC f6dg== 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=O5x1+ZiMnbiqyFsH/wrAVPmGqzEerXqsvIsYFOmQUQU=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=RA8PQUjgIWdw+gGyWmCqclAKqDooTldeB9pKOr9VMU1Z4D6M15peGGJxaaKa/ASzL6 iYcCcANmuYbUAgNxMvwnzL1eO8Ko46jnHD4svDS8Wa04s3OmA1XKzKM2FeP2BBkr2Hng 5ukHmrNj+pdlw6ftpAJmHIzm7sXfMJlkDvFrsZUrwhyr3czXOBYNJwGwetljGtfvZQQ0 pq+7EkgnFl/hKyqABHh4kFEiyN/h34TpoeERrJVgiPExN2+9rHL3mddh1RSsAsOnAKKB ITBCvcG1pno5O1R7oT2SaV/6+0DLVT6HFFQ+KEZLhUHtN5Q5dflD329th1p1JiXy9wvx IzTQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=pagQl5wR; 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-66165-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66165-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id r13-20020a67f44d000000b0046d1c56398dsi19486vsn.159.2024.02.14.16.01.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 16:01:38 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66165-ouuuleilei=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=pagQl5wR; 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-66165-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66165-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 2657D1C2138E for ; Thu, 15 Feb 2024 00:01:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 68A48151CCC; Wed, 14 Feb 2024 23:53:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="pagQl5wR" 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 3069E14E2FF; Wed, 14 Feb 2024 23:53:51 +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=1707954833; cv=none; b=LoKhLV7KiTqKEYev7djmC+IYI6NSAqZlVLnZ3bfU925HlSNNlyPaAySfxbFxL/yBZOZWV62BcJLM6vw+sVgDgqOYu+Pf2BmrvxzTU/DymET4gv4Fqi9A0172EOewl9Kq09KKif+Lj8bdbEdKL2N81J0pK2L8BvgPrlnNnKHLgvg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954833; c=relaxed/simple; bh=JSJjrS6/HIsLRNVgq0a/m5f+xAZEV6uV7P3DcKoDXdM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sSAJTUsXeXxlmU4deGJmak+BL0zHnRWcxEEgIXAjr41eXLAWckSygtW12tNji9lKgfSxelbblSsUkptPp+ZkB1Y7nByA6zsrKd//G9LGhbkCMLLYWY/vAIU7phSJG9IXML4yxzE48c95uHq1smiMmg39mUSJ+30Oi4F2foLXvP0= 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=pagQl5wR; 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=O5x1+ZiMnbiqyFsH/wrAVPmGqzEerXqsvIsYFOmQUQU=; b=pagQl5wRuWCa6WkER4yA4C8ryk 6+tmAPG7Xrs4dfTp1JZ7K5i5EbTB/3nSSCBQPOJKMb/JQWxh9aehokEIazZU+gSgka3YrfqYMO8/o zRhh/7fVSACZs6EOV0zaDWWDIM3vn9V0J4Lgbkvagfis1lRX1wU/9whsMR8pkDznBrI69N4ocxlzx ZrcUJRC/uAEmiYWLRGmFXSGZ2DFDvaEEzIyv+eJHYhyMgEP3x7UdQ4YUBpfyKPXGQFr5rNJyY87lA BoN61CkpnhrCa8D9VbcON8x7UuoQR+Qh1P28ULatk/upY9IpDcsoklHigMZBOMG+/b+DI9gU0398W fbUi7IIg==; 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 1raP4W-00GcSK-1F; Wed, 14 Feb 2024 17:53:48 -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 29/31] selftests: ntsync: Add a stress test for contended waits. Date: Wed, 14 Feb 2024 17:53:04 -0600 Message-ID: <20240214235307.10494-20-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240214235307.10494-1-zfigura@codeweavers.com> References: <20240214233645.9273-1-zfigura@codeweavers.com> <20240214235307.10494-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: 1790920935242156817 X-GMAIL-MSGID: 1790920935242156817 Test a more realistic usage pattern, and one with heavy contention, in order to actually exercise ntsync's internal synchronization. This test has several threads in a tight loop acquiring a mutex, modifying some shared data, and then releasing the mutex. At the end we check if the data is consistent. Signed-off-by: Elizabeth Figura --- .../testing/selftests/drivers/ntsync/ntsync.c | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/tools/testing/selftests/drivers/ntsync/ntsync.c b/tools/testing/selftests/drivers/ntsync/ntsync.c index 40cfdd356ef4..aba513c9af01 100644 --- a/tools/testing/selftests/drivers/ntsync/ntsync.c +++ b/tools/testing/selftests/drivers/ntsync/ntsync.c @@ -1330,4 +1330,78 @@ TEST(alert_all) close(fd); } +#define STRESS_LOOPS 10000 +#define STRESS_THREADS 4 + +static unsigned int stress_counter; +static int stress_device, stress_start_event, stress_mutex; + +static void *stress_thread(void *arg) +{ + struct ntsync_wait_args wait_args = {0}; + __u32 index, count, i; + int ret; + + wait_args.timeout = UINT64_MAX; + wait_args.count = 1; + wait_args.objs = (uintptr_t)&stress_start_event; + wait_args.owner = gettid(); + wait_args.index = 0xdeadbeef; + + ioctl(stress_device, NTSYNC_IOC_WAIT_ANY, &wait_args); + + wait_args.objs = (uintptr_t)&stress_mutex; + + for (i = 0; i < STRESS_LOOPS; ++i) { + ioctl(stress_device, NTSYNC_IOC_WAIT_ANY, &wait_args); + + ++stress_counter; + + unlock_mutex(stress_mutex, wait_args.owner, &count); + } + + return NULL; +} + +TEST(stress_wait) +{ + struct ntsync_event_args event_args; + struct ntsync_mutex_args mutex_args; + pthread_t threads[STRESS_THREADS]; + __u32 signaled, i; + int ret; + + stress_device = open("/dev/ntsync", O_CLOEXEC | O_RDONLY); + ASSERT_LE(0, stress_device); + + mutex_args.owner = 0; + mutex_args.count = 0; + ret = ioctl(stress_device, NTSYNC_IOC_CREATE_MUTEX, &mutex_args); + EXPECT_EQ(0, ret); + stress_mutex = mutex_args.mutex; + + event_args.manual = 1; + event_args.signaled = 0; + ret = ioctl(stress_device, NTSYNC_IOC_CREATE_EVENT, &event_args); + EXPECT_EQ(0, ret); + stress_start_event = event_args.event; + + for (i = 0; i < STRESS_THREADS; ++i) + pthread_create(&threads[i], NULL, stress_thread, NULL); + + ret = ioctl(stress_start_event, NTSYNC_IOC_EVENT_SET, &signaled); + EXPECT_EQ(0, ret); + + for (i = 0; i < STRESS_THREADS; ++i) { + ret = pthread_join(threads[i], NULL); + EXPECT_EQ(0, ret); + } + + EXPECT_EQ(STRESS_LOOPS * STRESS_THREADS, stress_counter); + + close(stress_start_event); + close(stress_mutex); + close(stress_device); +} + TEST_HARNESS_MAIN From patchwork Wed Feb 14 23:53:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201202 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp64012dyb; Wed, 14 Feb 2024 16:01:57 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWgM5hLmcUF8VkLgafWcYJk/Wh78Q23NFleHDcQd9IRftz8haHAnlJmUNKgU0OfSDLH5Eg/myQV7bU3NQ2d971wP8fzxA== X-Google-Smtp-Source: AGHT+IGtwe1itAAskHXvnjvdO4Dppk6O0EG46MT8F29ihZWhOGDegW48IvaDfzEPN3h4o/QDjaFM X-Received: by 2002:a2e:868a:0:b0:2d0:ed54:178e with SMTP id l10-20020a2e868a000000b002d0ed54178emr199060lji.6.1707955317468; Wed, 14 Feb 2024 16:01:57 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707955317; cv=pass; d=google.com; s=arc-20160816; b=01rEPB9OxzC9mUMjicQM6A1DnnRtJhexH50lM7XFTcF4hCzFsIAksuZGFgVI8NGMHp K0n3CGKrdnb3jPHFsjMtjgNYhKdxWXimGRqJGAMS+dAi9vV0RtaGD6OD18dlcPeSA1iM m4tHimnsDfYLiO/IS20pUjFB13x73zhrvTdP/6y81oAdA/SwThTBL2a60vc5Vw26DoTm rZ7LAEPm7/S3xY1gGC0TlPXaLHVrZG+jFCvPOvu7qUpx3nc6zGeWcenRbk6cmpnxXJ+L GbMSZoCp5iyZRWARaZlsX57Gc8Bwwn9qWRwNCjSOoKWiHJd6+1M+7bDkt+0F/Pupqs5D M2ZA== 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=KNfTFrOlykYG/fleH9ulVSplLfHApmFBo701D/t2NDY=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=JDlWQKiU611sslvzpL6VwHkwZlq/kv8JT62CmafLVq2EjppBrBlkhr6gFN2Ns0iowl YbixWzZaO1OWsWZXUSIyeYNmdan9msWgMi3jH/JlIdCjqTFmJVytf6StNk5EU/+2/qWv DxGfTYu6Xklu8wZJyBt4/9TQ0le+T1dGWWqTg/iSI0sya6BTF0jn08YQdXVDzd5D0Yaw gb0B4mQza3pisK0Sxig/MrdlUapkTPULhMtGWzvKL8ElhPNUkeiuFrbhXM4Qirh5+HKf 5bM80lYLn1HdCMt7dVdzfLjNfTPWK/GLn4KcfIs5dROy+uQDjPoP7q14ejgqu+f2x1Wa o3KA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=YmtLhx3z; 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-66164-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66164-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id o18-20020aa7d3d2000000b005624c953f93si37376edr.256.2024.02.14.16.01.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 16:01:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66164-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=YmtLhx3z; 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-66164-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66164-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 am.mirrors.kernel.org (Postfix) with ESMTPS id D3B541F23F69 for ; Thu, 15 Feb 2024 00:01:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 22C48151CC6; Wed, 14 Feb 2024 23:53:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="YmtLhx3z" 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 0E07A14E2FD; Wed, 14 Feb 2024 23:53:52 +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=1707954833; cv=none; b=ls3DwkJRZcZwXzB+dC8Yh9CNjQCadlrmolXuQg+jbKE6byS9QmM5gX6TwDZQPBelQS0zYL32y0QLl+x6JM2+SFy9Y/TfluNa1EllmqZ85ogZ2JxN8eNw8ufO+uWWZoxDClKQDg7a1wJpC9QKVD1ZKuT17QLpf+t5iFB5UChydAM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954833; c=relaxed/simple; bh=SJHckSnO59KNn4GgMh+3HCnN8XA5Fp4zU2xtYlg35+o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QacwrXzdrgCDgm2L4Kc7/9rndQiwqZZ9EzJhz+jXtRtdi6BWkwaNavkCaPbsb+abgtWgd01fH92wNi7Xv+sv4f7avkobPO48I0xbt8saly9p6BnxJ0FEA/qJy/u8+Zu3hdr9labkgDc/k26+9h1bBpStaM16wVGXY35+CWDFI1Y= 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=YmtLhx3z; 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=KNfTFrOlykYG/fleH9ulVSplLfHApmFBo701D/t2NDY=; b=YmtLhx3zkmHA/dpXVcH93FP23h hB+IaXS7Vuatgkf5hHyT35+L/TOabJkc11O/CoP88JZUhZmk0SzVZrgA6pe/KPwpXNj1MSGgvHcQb EWytT+lFqAdH/SmKEJH0lzKmdmOYJQiFsDCk9Px1a1nGyv8rPKMLNTHLcnNUnsC3NiKb3nW8/QbrL PFea1lWKDTjQnGjPSVFrm4jGYqubwWESj3l2YMwVAR8f7CWrzJLSAPj9+NCBH9dcaby7P6ctQ+FnG c+xG+DRO5OGvi8d52LJKn6586dmRsHKqAWfV/fFB7HXS3+VyL6zPrZ4Rm5GFWtt4Ba0uC1Vhxxeh3 CVZCby7Q==; 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 1raP4X-00GcSK-0Z; Wed, 14 Feb 2024 17:53:49 -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 30/31] maintainers: Add an entry for ntsync. Date: Wed, 14 Feb 2024 17:53:05 -0600 Message-ID: <20240214235307.10494-21-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240214235307.10494-1-zfigura@codeweavers.com> References: <20240214233645.9273-1-zfigura@codeweavers.com> <20240214235307.10494-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: 1790920954940959937 X-GMAIL-MSGID: 1790920954940959937 Add myself as maintainer, supported by CodeWeavers. Signed-off-by: Elizabeth Figura --- MAINTAINERS | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 960512bec428..e80ed255951b 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -15591,6 +15591,15 @@ T: git https://github.com/Paragon-Software-Group/linux-ntfs3.git F: Documentation/filesystems/ntfs3.rst F: fs/ntfs3/ +NTSYNC SYNCHRONIZATION PRIMITIVE DRIVER +M: Elizabeth Figura +L: wine-devel@winehq.org +S: Supported +F: Documentation/userspace-api/ntsync.rst +F: drivers/misc/ntsync.c +F: include/uapi/linux/ntsync.h +F: tools/testing/selftests/drivers/ntsync/ + NUBUS SUBSYSTEM M: Finn Thain L: linux-m68k@lists.linux-m68k.org From patchwork Wed Feb 14 23:53:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 201201 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:b825:b0:106:860b:bbdd with SMTP id da37csp63857dyb; Wed, 14 Feb 2024 16:01:45 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV3FahE5NxF/zqYwPsiqfceB4ZM5xLOWDhUMVElQatdJ2F6jZmCUIX5/HoztijHMN0kvyM3MlukyC0D3Yn+qLMj3Q2b+g== X-Google-Smtp-Source: AGHT+IELldnA6rt4KiLC5jn/eF3lbMIpDCNmSDBqKR/Nn2PUqWqNKjCGrf6FrBmKXKwxTWQdHznJ X-Received: by 2002:aca:d07:0:b0:3c1:34b9:469 with SMTP id 7-20020aca0d07000000b003c134b90469mr401807oin.17.1707955305174; Wed, 14 Feb 2024 16:01:45 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707955305; cv=pass; d=google.com; s=arc-20160816; b=Yux8LproK5LENPVxrcnMPej9RVq5DCdImLifNHfhv5kqxT/Ys99UmJdcCJGHDEERVp VO3zGEUFo1+Q8Rg/MxMzt2SQHijqwp8NpijU+QFx4W+SFuAIy3zbM2NSj5xboXYxt166 DSRy1c2iAmaUXHzta3xsUBnOzjLlDPlUMVdHwSIZR0AAmAeDKZbscyIDhcTix2Zg45Fq plyoGnr5s/p33o95jY+mPWRM1r+AQLEkgaNazL2jL2z7EMQMv/AOzfxuyXJ3doNWtyyT me0XTTp4vKqfF8re9A0zRBP1+x+h9B+CMxWL++Skc4JvwSimb+8UoPG6JL4C2QX1tIiM fDqQ== 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=samSw3M+aqxF7X67rQctdSDxKLgriXpySBxR0bQ8q1k=; fh=sOAzP1IXJnllEbSCBRlwdfT0MN3UiaRLwzx1Tpw7GJA=; b=tOcqIwMEFzPL5BHUV4XhlUZ6ghAzd4sPwp9d74DcDarItGk+qzBJtWWhwTqBdycen9 G58Ch1Z0o3nDLCnHoGNeTFVPMf/6bphS1ktsLq4MVOMNLKmX+mRRyDYH+3Hl5EeeFh+y Dfa03FiZ6xnVFg/MYW532BfCCM1ykdJw60gpp0wQ+tQV9ECsDFdgP8OCscLMDZ0FO4H3 cnAxD9/0kqD7Ieqgumv84RhDMlHID8e7xsrSVSz14l2BLDiIxYkYODOZdcdQ7s4Oh97M Ibb0KtJS+ulO9vC/BIsGiPddVF695Gyv8DO+sIzD+nYknKd9D6Eo8Sd0g47/wb3fznD3 8QuA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=G82fqSD7; 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-66166-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66166-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id r15-20020ad4576f000000b0068cb6942fccsi214445qvx.116.2024.02.14.16.01.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Feb 2024 16:01:45 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-66166-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=G82fqSD7; 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-66166-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-66166-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id B68681C222E6 for ; Thu, 15 Feb 2024 00:01:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C01D9151CE6; Wed, 14 Feb 2024 23:53:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="G82fqSD7" 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 83DD314F9CF; Wed, 14 Feb 2024 23:53:52 +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=1707954834; cv=none; b=BXnOSC/1a+KdawUm2wU6E/SpVvkHng/Ba1wvYN5xAp34eZzVmvLuAI3rfYqahmQJtz9348d/9oklVbIAY+J39VjrabMKKCmI+0qNARC0fcAJluUXXMMwqfTx8d4PbnBP3CjdJ8tpkhJL2W70sypdvfQ5cBFisPfFvOVc9HnQp3w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707954834; c=relaxed/simple; bh=SUJpBI1b1llhJRkiQaApOLQii+xerSJMjLyEjf4MEck=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jxPjomAXzjIj1wMcbqfU9aUtaSxsNWyc6yK2+6g9m1eN+iL6k1c9X2GDza2y/B+yVoVIyaqddoHV8VwPhuDU8V2o8Nq6tlPh9eWwiE5FLUHvVkwLc5fuMlrWKiwNUI7U81K9YCqwRwdGGPnrzkOYCOflQ3IM2pt4niHmptuRqZQ= 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=G82fqSD7; 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=samSw3M+aqxF7X67rQctdSDxKLgriXpySBxR0bQ8q1k=; b=G82fqSD7ZWquz2z+jaYUOgmgoj 6HN3Tr5ndT3E792Bw+uC8N6V17lDcOaSZ+eEm1Ww/GDUIZnky80ZOSjGma/ZRnVQ3E0PD9YpQe6gM GbTmMwF2yf1q89lZukoc+jfIbCUILFGN9jL0/oNJ92m0JqCEV6E6Sw7UiApGWf2glQRx1tm6MS8+s Z73y3WKTf8alAtoTtdQRRylBs0DlQTDgCaCjYXP2jrGajMEkyHQYtG/TSdWi9nBe6H5tP6KBtlIba lyPf+UW8YbDiXTB+MqasL/d5Izz95v8v45hiPPFb++4NeKPzvl5h7Ius0582wMr/qcZRcjl44boAD TgoC268A==; 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 1raP4X-00GcSK-2a; Wed, 14 Feb 2024 17:53:50 -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 31/31] docs: ntsync: Add documentation for the ntsync uAPI. Date: Wed, 14 Feb 2024 17:53:06 -0600 Message-ID: <20240214235307.10494-22-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240214235307.10494-1-zfigura@codeweavers.com> References: <20240214233645.9273-1-zfigura@codeweavers.com> <20240214235307.10494-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: 1790920942007968020 X-GMAIL-MSGID: 1790920942007968020 Add an overall explanation of the driver architecture, and complete and precise specification for its intended behaviour. Signed-off-by: Elizabeth Figura --- Documentation/userspace-api/index.rst | 1 + Documentation/userspace-api/ntsync.rst | 399 +++++++++++++++++++++++++ 2 files changed, 400 insertions(+) create mode 100644 Documentation/userspace-api/ntsync.rst diff --git a/Documentation/userspace-api/index.rst b/Documentation/userspace-api/index.rst index 09f61bd2ac2e..f5a72ed27def 100644 --- a/Documentation/userspace-api/index.rst +++ b/Documentation/userspace-api/index.rst @@ -34,6 +34,7 @@ place where this information is gathered. tee isapnp dcdbas + ntsync .. only:: subproject and html diff --git a/Documentation/userspace-api/ntsync.rst b/Documentation/userspace-api/ntsync.rst new file mode 100644 index 000000000000..202c2350d3af --- /dev/null +++ b/Documentation/userspace-api/ntsync.rst @@ -0,0 +1,399 @@ +=================================== +NT synchronization primitive driver +=================================== + +This page documents the user-space API for the ntsync driver. + +ntsync is a support driver for emulation of NT synchronization +primitives by user-space NT emulators. It exists because implementation +in user-space, using existing tools, cannot match Windows performance +while offering accurate semantics. It is implemented entirely in +software, and does not drive any hardware device. + +This interface is meant as a compatibility tool only, and should not +be used for general synchronization. Instead use generic, versatile +interfaces such as futex(2) and poll(2). + +Synchronization primitives +========================== + +The ntsync driver exposes three types of synchronization primitives: +semaphores, mutexes, and events. + +A semaphore holds a single volatile 32-bit counter, and a static 32-bit +integer denoting the maximum value. It is considered signaled when the +counter is nonzero. The counter is decremented by one when a wait is +satisfied. Both the initial and maximum count are established when the +semaphore is created. + +A mutex holds a volatile 32-bit recursion count, and a volatile 32-bit +identifier denoting its owner. A mutex is considered signaled when its +owner is zero (indicating that it is not owned). The recursion count is +incremented when a wait is satisfied, and ownership is set to the given +identifier. + +A mutex also holds an internal flag denoting whether its previous owner +has died; such a mutex is said to be abandoned. Owner death is not +tracked automatically based on thread death, but rather must be +communicated using ``NTSYNC_IOC_MUTEX_KILL``. An abandoned mutex is +inherently considered unowned. + +Except for the "unowned" semantics of zero, the actual value of the +owner identifier is not interpreted by the ntsync driver at all. The +intended use is to store a thread identifier; however, the ntsync +driver does not actually validate that a calling thread provides +consistent or unique identifiers. + +An event holds a volatile boolean state denoting whether it is signaled +or not. There are two types of events, auto-reset and manual-reset. An +auto-reset event is designaled when a wait is satisfied; a manual-reset +event is not. The event type is specified when the event is created. + +Unless specified otherwise, all operations on an object are atomic and +totally ordered with respect to other operations on the same object. + +Objects are represented by files. When all file descriptors to an +object are closed, that object is deleted. + +Char device +=========== + +The ntsync driver creates a single char device /dev/ntsync. Each file +description opened on the device represents a unique instance intended +to back an individual NT virtual machine. Objects created by one ntsync +instance may only be used with other objects created by the same +instance. + +ioctl reference +=============== + +All operations on the device are done through ioctls. There are four +structures used in ioctl calls:: + + struct ntsync_sem_args { + __u32 sem; + __u32 count; + __u32 max; + }; + + struct ntsync_mutex_args { + __u32 mutex; + __u32 owner; + __u32 count; + }; + + struct ntsync_event_args { + __u32 event; + __u32 signaled; + __u32 manual; + }; + + struct ntsync_wait_args { + __u64 timeout; + __u64 objs; + __u32 count; + __u32 owner; + __u32 index; + __u32 alert; + __u32 flags; + __u32 pad; + }; + +Depending on the ioctl, members of the structure may be used as input, +output, or not at all. All ioctls return 0 on success. + +The ioctls on the device file are as follows: + +.. c:macro:: NTSYNC_IOC_CREATE_SEM + + Create a semaphore object. Takes a pointer to struct + :c:type:`ntsync_sem_args`, which is used as follows: + + .. list-table:: + + * - ``sem`` + - On output, contains a file descriptor to the created semaphore. + * - ``count`` + - Initial count of the semaphore. + * - ``max`` + - Maximum count of the semaphore. + + Fails with ``EINVAL`` if ``count`` is greater than ``max``. + +.. c:macro:: NTSYNC_IOC_CREATE_MUTEX + + Create a mutex object. Takes a pointer to struct + :c:type:`ntsync_mutex_args`, which is used as follows: + + .. list-table:: + + * - ``mutex`` + - On output, contains a file descriptor to the created mutex. + * - ``count`` + - Initial recursion count of the mutex. + * - ``owner`` + - Initial owner of the mutex. + + If ``owner`` is nonzero and ``count`` is zero, or if ``owner`` is + zero and ``count`` is nonzero, the function fails with ``EINVAL``. + +.. c:macro:: NTSYNC_IOC_CREATE_EVENT + + Create an event object. Takes a pointer to struct + :c:type:`ntsync_event_args`, which is used as follows: + + .. list-table:: + + * - ``event`` + - On output, contains a file descriptor to the created event. + * - ``signaled`` + - If nonzero, the event is initially signaled, otherwise + nonsignaled. + * - ``manual`` + - If nonzero, the event is a manual-reset event, otherwise + auto-reset. + +The ioctls on the individual objects are as follows: + +.. c:macro:: NTSYNC_IOC_SEM_POST + + Post to a semaphore object. Takes a pointer to a 32-bit integer, + which on input holds the count to be added to the semaphore, and on + output contains its previous count. + + If adding to the semaphore's current count would raise the latter + past the semaphore's maximum count, the ioctl fails with + ``EOVERFLOW`` and the semaphore is not affected. If raising the + semaphore's count causes it to become signaled, eligible threads + waiting on this semaphore will be woken and the semaphore's count + decremented appropriately. + +.. c:macro:: NTSYNC_IOC_MUTEX_UNLOCK + + Release a mutex object. Takes a pointer to struct + :c:type:`ntsync_mutex_args`, which is used as follows: + + .. list-table:: + + * - ``mutex`` + - Ignored. + * - ``owner`` + - Specifies the owner trying to release this mutex. + * - ``count`` + - On output, contains the previous recursion count. + + If ``owner`` is zero, the ioctl fails with ``EINVAL``. If ``owner`` + is not the current owner of the mutex, the ioctl fails with + ``EPERM``. + + The mutex's count will be decremented by one. If decrementing the + mutex's count causes it to become zero, the mutex is marked as + unowned and signaled, and eligible threads waiting on it will be + woken as appropriate. + +.. c:macro:: NTSYNC_IOC_SET_EVENT + + Signal an event object. Takes a pointer to a 32-bit integer, which on + output contains the previous state of the event. + + Eligible threads will be woken, and auto-reset events will be + designaled appropriately. + +.. c:macro:: NTSYNC_IOC_RESET_EVENT + + Designal an event object. Takes a pointer to a 32-bit integer, which + on output contains the previous state of the event. + +.. c:macro:: NTSYNC_IOC_PULSE_EVENT + + Wake threads waiting on an event object while leaving it in an + unsignaled state. Takes a pointer to a 32-bit integer, which on + output contains the previous state of the event. + + A pulse operation can be thought of as a set followed by a reset, + performed as a single atomic operation. If two threads are waiting on + an auto-reset event which is pulsed, only one will be woken. If two + threads are waiting a manual-reset event which is pulsed, both will + be woken. However, in both cases, the event will be unsignaled + afterwards, and a simultaneous read operation will always report the + event as unsignaled. + +.. c:macro:: NTSYNC_IOC_READ_SEM + + Read the current state of a semaphore object. Takes a pointer to + struct :c:type:`ntsync_sem_args`, which is used as follows: + + .. list-table:: + + * - ``sem`` + - Ignored. + * - ``count`` + - On output, contains the current count of the semaphore. + * - ``max`` + - On output, contains the maximum count of the semaphore. + +.. c:macro:: NTSYNC_IOC_READ_MUTEX + + Read the current state of a mutex object. Takes a pointer to struct + :c:type:`ntsync_mutex_args`, which is used as follows: + + .. list-table:: + + * - ``mutex`` + - Ignored. + * - ``owner`` + - On output, contains the current owner of the mutex, or zero + if the mutex is not currently owned. + * - ``count`` + - On output, contains the current recursion count of the mutex. + + If the mutex is marked as abandoned, the function fails with + ``EOWNERDEAD``. In this case, ``count`` and ``owner`` are set to + zero. + +.. c:macro:: NTSYNC_IOC_READ_EVENT + + Read the current state of an event object. Takes a pointer to struct + :c:type:`ntsync_event_args`, which is used as follows: + + .. list-table:: + + * - ``event`` + - Ignored. + * - ``signaled`` + - On output, contains the current state of the event. + * - ``manual`` + - On output, contains 1 if the event is a manual-reset event, + and 0 otherwise. + +.. c:macro:: NTSYNC_IOC_KILL_OWNER + + Mark a mutex as unowned and abandoned if it is owned by the given + owner. Takes an input-only pointer to a 32-bit integer denoting the + owner. If the owner is zero, the ioctl fails with ``EINVAL``. If the + owner does not own the mutex, the function fails with ``EPERM``. + + Eligible threads waiting on the mutex will be woken as appropriate + (and such waits will fail with ``EOWNERDEAD``, as described below). + +.. c:macro:: NTSYNC_IOC_WAIT_ANY + + Poll on any of a list of objects, atomically acquiring at most one. + Takes a pointer to struct :c:type:`ntsync_wait_args`, which is + used as follows: + + .. list-table:: + + * - ``timeout`` + - Absolute timeout in nanoseconds. If ``NTSYNC_WAIT_REALTIME`` + is set, the timeout is measured against the REALTIME clock; + otherwise it is measured against the MONOTONIC clock. If the + timeout is equal to or earlier than the current time, the + function returns immediately without sleeping. If ``timeout`` + is U64_MAX, the function will sleep until an object is + signaled, and will not fail with ``ETIMEDOUT``. + * - ``objs`` + - Pointer to an array of ``count`` file descriptors + (specified as an integer so that the structure has the same + size regardless of architecture). If any object is + invalid, the function fails with ``EINVAL``. + * - ``count`` + - Number of objects specified in the ``objs`` array. + If greater than ``NTSYNC_MAX_WAIT_COUNT``, the function fails + with ``EINVAL``. + * - ``owner`` + - Mutex owner identifier. If any object in ``objs`` is a mutex, + the ioctl will attempt to acquire that mutex on behalf of + ``owner``. If ``owner`` is zero, the ioctl fails with + ``EINVAL``. + * - ``index`` + - On success, contains the index (into ``objs``) of the object + which was signaled. If ``alert`` was signaled instead, + this contains ``count``. + * - ``alert`` + - Optional event object file descriptor. If nonzero, this + specifies an "alert" event object which, if signaled, will + terminate the wait. If nonzero, the identifier must point to a + valid event. + * - ``flags`` + - Zero or more flags. Currently the only flag is + ``NTSYNC_WAIT_REALTIME``, which causes the timeout to be + measured against the REALTIME clock instead of MONOTONIC. + * - ``pad`` + - Unused, must be set to zero. + + This function attempts to acquire one of the given objects. If unable + to do so, it sleeps until an object becomes signaled, subsequently + acquiring it, or the timeout expires. In the latter case the ioctl + fails with ``ETIMEDOUT``. The function only acquires one object, even + if multiple objects are signaled. + + A semaphore is considered to be signaled if its count is nonzero, and + is acquired by decrementing its count by one. A mutex is considered + to be signaled if it is unowned or if its owner matches the ``owner`` + argument, and is acquired by incrementing its recursion count by one + and setting its owner to the ``owner`` argument. An auto-reset event + is acquired by designaling it; a manual-reset event is not affected + by acquisition. + + Acquisition is atomic and totally ordered with respect to other + operations on the same object. If two wait operations (with different + ``owner`` identifiers) are queued on the same mutex, only one is + signaled. If two wait operations are queued on the same semaphore, + and a value of one is posted to it, only one is signaled. The order + in which threads are signaled is not specified. + + If an abandoned mutex is acquired, the ioctl fails with + ``EOWNERDEAD``. Although this is a failure return, the function may + otherwise be considered successful. The mutex is marked as owned by + the given owner (with a recursion count of 1) and as no longer + abandoned, and ``index`` is still set to the index of the mutex. + + The ``alert`` argument is an "extra" event which can terminate the + wait, independently of all other objects. If members of ``objs`` and + ``alert`` are both simultaneously signaled, a member of ``objs`` will + always be given priority and acquired first. + + It is valid to pass the same object more than once, including by + passing the same event in the ``objs`` array and in ``alert``. If a + wakeup occurs due to that object being signaled, ``index`` is set to + the lowest index corresponding to that object. + + The function may fail with ``EINTR`` if a signal is received. + +.. c:macro:: NTSYNC_IOC_WAIT_ALL + + Poll on a list of objects, atomically acquiring all of them. Takes a + pointer to struct :c:type:`ntsync_wait_args`, which is used + identically to ``NTSYNC_IOC_WAIT_ANY``, except that ``index`` is + always filled with zero on success if not woken via alert. + + This function attempts to simultaneously acquire all of the given + objects. If unable to do so, it sleeps until all objects become + simultaneously signaled, subsequently acquiring them, or the timeout + expires. In the latter case the ioctl fails with ``ETIMEDOUT`` and no + objects are modified. + + Objects may become signaled and subsequently designaled (through + acquisition by other threads) while this thread is sleeping. Only + once all objects are simultaneously signaled does the ioctl acquire + them and return. The entire acquisition is atomic and totally ordered + with respect to other operations on any of the given objects. + + If an abandoned mutex is acquired, the ioctl fails with + ``EOWNERDEAD``. Similarly to ``NTSYNC_IOC_WAIT_ANY``, all objects are + nevertheless marked as acquired. Note that if multiple mutex objects + are specified, there is no way to know which were marked as + abandoned. + + As with "any" waits, the ``alert`` argument is an "extra" event which + can terminate the wait. Critically, however, an "all" wait will + succeed if all members in ``objs`` are signaled, *or* if ``alert`` is + signaled. In the latter case ``index`` will be set to ``count``. As + with "any" waits, if both conditions are filled, the former takes + priority, and objects in ``objs`` will be acquired. + + Unlike ``NTSYNC_IOC_WAIT_ANY``, it is not valid to pass the same + object more than once, nor is it valid to pass the same object in + ``objs`` and in ``alert``. If this is attempted, the function fails + with ``EINVAL``.