From patchwork Mon Feb 19 22:38:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 203331 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp78290dyc; Mon, 19 Feb 2024 14:51:12 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVnWPnkUFu7ny0ri+jYeG+PAHSMlDoN8sL//cql98EH6Cymt1xtjFgVO5jO9/S6IcqE6hXPuxD3YLKWYK18yGGvwjsfJw== X-Google-Smtp-Source: AGHT+IG3zP75sD9cCpqqIO66Olrpt0h92F/sQcrwnkA7wjawluvWEOfuRWdmOqTlUzjFMYC/W+lr X-Received: by 2002:a17:903:452:b0:1d0:b1f0:1005 with SMTP id iw18-20020a170903045200b001d0b1f01005mr11144146plb.63.1708383072125; Mon, 19 Feb 2024 14:51:12 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708383072; cv=pass; d=google.com; s=arc-20160816; b=BJOWeBOOTCAx3A/ddKoZjZV9UAaAVxAunLGby/6iDLdnZkJoAwfyAMfitCJPTVbXOw 1eyswdEvTt37nsU4XX1HFp4fFrF6cziKrbYlVEjfWPJEXleNrokibfiNh0kRmxbNylh7 L0ceXjL5E0p6aYy+aWCagJVcyuSD5u5KQBLEErBc1gLeVbAxK1RpUbt+WXG/XzsaCGrj GWJjW5RXntwlYL5v3LTymYJR8bau3/oAdjrpDKjj4iC2EUArbPdy/q6ucXF1BiUxUUId nHxdxk7Hmiq0cJy7dMIP4i+sNfj9fNOx9lY05RMUTmx2IE6/gTFDbjubVg1tCUM/5rb6 cHjg== 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=sVBgbjKuQjJ/kbJrb39AqVEiR/UlasFEifUy1/TwaHs=; fh=hR3Xqo6s6FufP5gEw1XgbtLHrluZu7hd5XGp+hLIfiM=; b=NBRhXMh1jMABVLd5PHHcEAM4PyQw8MofPCz/TnWTdFzKY+L9p7O6lZXW/IqBXRboo0 SjpIhwjweewkr7ExdPoi0gSwEoCnHs7pZtkFO/Lbjt870LhtGfxqh7D/XQXLT9snOXWn pfwJ7izunkieJGHzGOvPtef8Lv2RykdVc4MaXWeoFZMcDF6MXsFdbfLwFGqy7JYeT+1l 2xdiq0Cy9x0S9Kb+r+yswbd0DhU2nwUMVLJACxp7kE0Jm1HgrOCDC6SXLGIgbFNEuMau 2u3MufM//fgGUadfirIBgsuIiPAfZEyVtR4Z0XSB0y2jPfk1/2Xy3knhDtwTnnhh//tl yxtA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=ECIr6K0v; 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-72087-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72087-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 l4-20020a170902f68400b001d7857c99d0si5178441plg.282.2024.02.19.14.51.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 14:51:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-72087-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=ECIr6K0v; 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-72087-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72087-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 5BE00B261CF for ; Mon, 19 Feb 2024 22:43:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6557560BAD; Mon, 19 Feb 2024 22:39:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="ECIr6K0v" 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 F20F956452; Mon, 19 Feb 2024 22:39: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=1708382380; cv=none; b=mshwePSQYIvtm8bJQg0PkctGjTl9ZRYOi+687EcgPBz9kkHNsrgDqoRLJrzf7BYvncMpNKoA6ec7dy+K1oHiLCFa5UkbSFwDA40J3MHuk0GsEeTy/Q+f67B7WsoWCzMon6qonZ+hanK7qbz/BF8cijb4rSnmgYIJ+gq33ddAyE8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708382380; c=relaxed/simple; bh=gG7Ru9rJnhHWV7DicYU0C+ZtRJvrPSrbWiE17OjMLbY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=G7YuwONOGfw/kIU1/Ux7c9wH1Jc3yUEMwCNopdoI10aj+P2OLaRF0Dg8lHAFBy+7hOxBV9Ctr/wXBEwaS4FApvC7GvNUS2Am4M0Er9c4OOLzBfvASMGFdlemIaBs7vGakcArLcR/SEzO2POY8sp8lLBPyN83WEK8APQqfosuGyA= 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=ECIr6K0v; 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=sVBgbjKuQjJ/kbJrb39AqVEiR/UlasFEifUy1/TwaHs=; b=ECIr6K0vFi2FCp8FGJOQglh9Ir tg2clvVe4LsfERAAozkza5aJMewy2R8fK+o15++jy6pN9saTRXZPitW/jdnNpr0wh5dNtA1eLRHEC r5dfledyc42RcTYCDRl3fI8oW8nz6OtS2QrXRISycQkecu55ctvYzYi1BLPj9cnFUr9RJxJXRe1hD bD/IHz79S0zwnXnjAvI5rcXzN+jtoHBEcx2qIXXaVIgrzOgeW4VK/PCLsenDVB5ARk25NGt6b6E2T b0qmy4Gxd3UOy47zi67vqlVwpEf2ZGKeLv7mzHs1odfMO0SRSI33s9c+MnZtCaweH7pRHqKajqCaC +VXwUznQ==; 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 1rcCIM-0037Oz-01; Mon, 19 Feb 2024 16:39:30 -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, Randy Dunlap , Elizabeth Figura Subject: [PATCH v2 07/31] ntsync: Introduce NTSYNC_IOC_MUTEX_UNLOCK. Date: Mon, 19 Feb 2024 16:38:09 -0600 Message-ID: <20240219223833.95710-8-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240219223833.95710-1-zfigura@codeweavers.com> References: <20240219223833.95710-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: 1791369488039671920 X-GMAIL-MSGID: 1791369488039671920 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 6f7086d0440a..222ebead8eba 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -312,6 +312,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; @@ -331,6 +393,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 8ac9d419c360..265503d441b1 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