Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp2104717lqo; Sun, 19 May 2024 13:48:09 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVr8tt/lsMvh1vjgi2J8RD0x014Ck8+nLMXoXNtShklcqU5bWaDtMzNjF36Z2ypBuloZNGxdDW80y9//XfCPHOi9v62ojXyVyhBFVMNxw== X-Google-Smtp-Source: AGHT+IHatLOUIUL9jemDSWxhZ4LEdY7f97CjWnl7LtxKzfkAioYZ5I8eDwcForG5twvbYl374bg9 X-Received: by 2002:a05:6512:3b20:b0:51d:2eba:614 with SMTP id 2adb3069b0e04-5220fe7a025mr21030950e87.53.1716151689693; Sun, 19 May 2024 13:48:09 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716151689; cv=pass; d=google.com; s=arc-20160816; b=Svczi1Q3AD3Y6u3a2m44VdgCpaREGgb6LXnuyIMHVZodc26ogtN9SclhONJLcad8EH Wy0MP+O3vSendVEINg3wXl9l3gupZmSXKIh5CgQYwh6QXfMRWSvwwMe3JcuH0Ydo5HCj x/XNVIOXASfP+5T9kOHForVYMnf1plifgCNzVux+CEAQ9SLn+nzXzv/q5KKRiWbiHcGq 6RgT02I7ahawKyarZx28kpQQWvdCqlSLkZYUJtI0+Y55U3igmTv1Bez2YfIZ1rMkMQuv MzCCJPS2z1HYOLnF1SkesP31dG2CcSEBq+K14vDplKzdRSNLn3nGpSJyOj5G8DQjspJd Ku1Q== 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=mTVrQMAIEqBosG3pkv+IJR4y3PjjTGcGSmKq26KRI50=; fh=eVY06L/C5FDTmF2RNqfudsnjwkB3tbhT/y33cGhZc0I=; b=crXFJGRqx8oY7Mku21MCUur7/gwp/4qVBqaWQeUjOXilyS0rD8K8BoWYc8hEQX8HnL GRQ6iMm1eklcNY8K/07ISXyMWc40OaAOsXGa0Syp3pcBaAL5hsvzl/tfPzkWiXp9Ptdv IAUtLWdXDGxH6KsuYxBSkKYN2wTNmvIRLPSzdO8NHK7KF1492WwuE45nhGuc0gSf38vd LR+O/kPxYaN+1HAKyRJ35WHr+o06KUYHqbEbNcoVvrV3iCWiXevpcUvmcczFMDeCaDu3 kWK8a7IuD+TQEAG9/qWO2gTGLU7FtCnuQYulkLliyEty6roHJWA3+qnxlISzcHdrgNAU SB6g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=kA6oV0pP; 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-183255-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-183255-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a5a5d844e6dsi912074466b.452.2024.05.19.13.48.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 May 2024 13:48:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-183255-linux.lists.archive=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=kA6oV0pP; 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-183255-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-183255-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 486AE1F2345E for ; Sun, 19 May 2024 20:31:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8C0A481ACB; Sun, 19 May 2024 20:25:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="kA6oV0pP" 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 6B57C745E4; Sun, 19 May 2024 20:25: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=1716150320; cv=none; b=OAUJ3BNE/Cnwuac6MQFLRagwKHSx2C4orkS3fPG0HkWGioX9k48r3bMSqnRhZ+slMa0htfg//TK7B38JT9R3ot6URG9xQEEZo1kKZnmwmJ6FtZ+Z4Ond6qCcyVhVz38DqiQMKjeJmrr7rq2+3OwMX8gHNmtzoBClTidPH8Q/0Eo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716150320; c=relaxed/simple; bh=/SCsDcIFIj64ZDboquiqjhZR9XtURfbTg0qdg0iowEw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=h0H75kSWXGy2bZBNAn8xY7V1C+FkdbZVdK1fGmTCeQ0nvNTGlRKFjwmhr62rKk7WLa3WirJKCZzAOmKqRHBhb7DVwxlQsEoccyj2MQQh8sZgueBU/726eBo9I2CWF0LjJoLV5akpZ0MEn1ftsFb5BKb9JDMK2SU3wEDAScUeV9k= 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=kA6oV0pP; 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=mTVrQMAIEqBosG3pkv+IJR4y3PjjTGcGSmKq26KRI50=; b=kA6oV0pPnmyUY0ozmTktPqXeCs iTAaLMHNkZCN609cyPq06/6QDlZ43mLiNcz+jQmlZucRj/0Gl1p7x+9mZ6YoOcgtGYE2jWPZ3SZ+m PeFs7iiMUMlBBTOZE1W3KzcOZ0fkzEvmUJjr5+S85ET4CYT2kk00F/JOM8NtpAWj2Ex0wCqPf+V4I L9jYmYEOwCopkbZdhyH1yhpc3l9qXrlKCibqt5EsS7WZqaHn+/zaLOXlTQzIR1veR36COdHavixO4 oZZ9YLjgktEhyd9ECa9+qDuOPCB3Tclmv7cGKKjAcWpO2coGz0cu2gQE1LhKNggTHIE/SYmS0xHGx wkfJ2zfw==; 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 1s8n5b-008wIn-2b; Sun, 19 May 2024 15:25:03 -0500 From: Elizabeth Figura To: Arnd Bergmann , Greg Kroah-Hartman , Jonathan Corbet , Shuah Khan Cc: linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, wine-devel@winehq.org, =?UTF-8?q?Andr=C3=A9=20Almeida?= , Wolfram Sang , Arkadiusz Hiler , Peter Zijlstra , Andy Lutomirski , linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, Randy Dunlap , Ingo Molnar , Will Deacon , Waiman Long , Boqun Feng , Elizabeth Figura Subject: [PATCH v5 05/28] ntsync: Introduce NTSYNC_IOC_MUTEX_KILL. Date: Sun, 19 May 2024 15:24:31 -0500 Message-ID: <20240519202454.1192826-6-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240519202454.1192826-1-zfigura@codeweavers.com> References: <20240519202454.1192826-1-zfigura@codeweavers.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit This does not correspond to any NT syscall. Rather, when a thread dies, it should be called by the NT emulator for each mutex, with the TID of the dying thread. 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 | 61 +++++++++++++++++++++++++++++++++++-- include/uapi/linux/ntsync.h | 1 + 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/drivers/misc/ntsync.c b/drivers/misc/ntsync.c index f00af9b15164..5aaf9dad76b6 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -59,6 +59,7 @@ struct ntsync_obj { struct { __u32 count; pid_t owner; + bool ownerdead; } mutex; } u; @@ -107,6 +108,7 @@ struct ntsync_q { atomic_t signaled; bool all; + bool ownerdead; __u32 count; struct ntsync_q_entry entries[]; }; @@ -275,6 +277,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; @@ -338,6 +343,9 @@ static void try_wake_any_mutex(struct ntsync_obj *mutex) continue; if (atomic_try_cmpxchg(&q->signaled, &signaled, entry->index)) { + 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); @@ -447,6 +455,52 @@ 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) +{ + ntsync_assert_held(mutex); + + 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; + bool all; + int ret; + + if (get_user(owner, (__u32 __user *)argp)) + return -EFAULT; + if (!owner) + return -EINVAL; + + if (mutex->type != NTSYNC_TYPE_MUTEX) + return -EINVAL; + + all = ntsync_lock_obj(dev, mutex); + + ret = kill_mutex_state(mutex, owner); + if (!ret) { + if (all) + try_wake_all_obj(dev, mutex); + try_wake_any_mutex(mutex); + } + + ntsync_unlock_obj(dev, mutex, all); + + return ret; +} + static int ntsync_obj_release(struct inode *inode, struct file *file) { struct ntsync_obj *obj = file->private_data; @@ -468,6 +522,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; } @@ -659,6 +715,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++) { @@ -767,7 +824,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; @@ -848,7 +905,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 a633db34f284..d7996180c1d2 100644 --- a/include/uapi/linux/ntsync.h +++ b/include/uapi/linux/ntsync.h @@ -43,5 +43,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 -- 2.43.0