Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp791597lqh; Thu, 28 Mar 2024 17:25:27 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVlvn+OkrYyZkaWdjOGZJ9n16TOhzjgcAFmQAjYh71CiDsaUNFcJwrsSjdbVPtOQrj0UeFF/QanvubQ5Vq6BWoaWBHtQLocrw39j9RR0A== X-Google-Smtp-Source: AGHT+IE6t3h2jbBU0kmf5zZJE+uA5t+qyvoBnlrThPqyMDuQuT0rmvxm3Y8jc+eTlF1jI731+wAB X-Received: by 2002:a25:fc0f:0:b0:dcc:4cdc:e98f with SMTP id v15-20020a25fc0f000000b00dcc4cdce98fmr899493ybd.34.1711671927738; Thu, 28 Mar 2024 17:25:27 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711671927; cv=pass; d=google.com; s=arc-20160816; b=RwASAaFsLiwD9+8ZQatSbmLNrHlOKldQpMjQzJXGkqMLQpC+146brI3uRsZIBiXJgb ZFt1z5uf2Oxp1TsWjZhO3aC3TIPj0a9aPEkX2zxcQpqYtC4brYtZTPE0XfMECRxLVanU XpMjtbfwTxA9qbf1b9ZKNTbPOcFNjkNPEB89mnxHQrwCpqzYzcaWVbQAOJZ3L7V9QvtR Zp1JLFM3IJ2msSUWVeUr5McdMT2G2U1mvcMJU37Wlb7y26sVwM5DORyJ2FO2/ZoJjPYS XGsNGcU6d/4DnV8pXfN52xnrU2v5bXOGAeU0w9V3qvUI/c/X7QDZWYjzwRU5ijx3ksxs zi7A== 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=IdS3njZ516lDixLKMQGfXljzOoGHuaZPkJDeVL0RQ74=; fh=hR3Xqo6s6FufP5gEw1XgbtLHrluZu7hd5XGp+hLIfiM=; b=TKc/95UmxroQpwz4magigTXhvWYt1hR1QV+Cko31U6VCC26dPPMB8uvJ008geDblcs +Q0ujQjYhMTyqsO4CzAxrkk/6TAk18doxm6IntZILLC/qKPOqYzSSdyOCu+9YR6Y4yzz K2EKH5cMFftjIkCTbRaRWSIHOhHVnsN9uUt1HjtAopODHRnQ+FeTSJQ/Ciq7WkUpUjwD FupFPm1NpcKTX7L1hDCSoOOZBYNxugbrZMrw6rj2o6CuvFNcAcDBOLxEaEPQ5AnXnl0T OOLt7B/Dw0qzejX2+/QiYi4uHndZqpXY17RiSyM0fAFBzV4gVQzVDQXZIwpOSgX68OxG rwbw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=D7Y8eEE7; 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-123886-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-123886-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=codeweavers.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id f8-20020a05621400c800b00696b2572e10si2573282qvs.313.2024.03.28.17.25.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 17:25:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-123886-linux.lists.archive=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=D7Y8eEE7; 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-123886-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-123886-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 64E981C21554 for ; Fri, 29 Mar 2024 00:25:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B31C21E885; Fri, 29 Mar 2024 00:23:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=codeweavers.com header.i=@codeweavers.com header.b="D7Y8eEE7" 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 65FC2249E4; Fri, 29 Mar 2024 00:23:42 +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=1711671823; cv=none; b=klU+B4J8WBC/Fb1unsmZdrsq60LpfttgFjI1z/kGh2tvI6qfNycsDXrNTvTk32OKrAbtkgQUY237pR4vqnXkyU7mzG6SzFBqyt+W6rnxRewvsKqlgPhpUI51uD5jWvXbwKydpSeN4KBgg9wIDEf2JwqN2rj2yIRYRS/+Ub7wS0I= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711671823; c=relaxed/simple; bh=jMvq7fx/BS69F42/Buz/xgKn5BhcZLV7lKdNJQgqWcU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=D62WCeMobtjvgfDnfLbfpJHCIILLwKN+98/xle21V3j+Ojj2WshyJNQfvzH36wmuf/ywIRAq2hgggy+SrSsBcV/eMR33BxON1Nz7lYgTxnDlsdFqHKoGB0CgSTxR2sbD+QMFj19rU/Tn0HMe2TyhGRib1QJv9NZhrypOA18t6o4= 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=D7Y8eEE7; 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=IdS3njZ516lDixLKMQGfXljzOoGHuaZPkJDeVL0RQ74=; b=D7Y8eEE7EkL+qoRK84TGzogt74 pCG5PB24DsqKjKoH503NJg4GhdIT9XIuR8AqQTFht09dGoNOOwRGtCxXT7eb5b1WQxdTiH6hvGwpr i5Zafnqq5zbnnWISga0DRmNesfws6DJUMNtkKg6X8hsGC6xZiLpx9wGkmlXPJvLXlsE60IZDl7L79 YhuxP8qy9hJdTcPuSeZzmcX8XuA0h8jga9s2gGH0m/a8uCFDxld4NPXebgY1l3zU7MXwldBL/sIgh IcrRG4OEIigSI+XE/1CEttgnm943QMPpVtL7tO7mxe14cf+8E34YiNQWo0vG4JpIe3mJxLN1UX7Jk D9qdCTQQ==; 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 1rpzlN-000iik-0q; Thu, 28 Mar 2024 19:06:29 -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 , Elizabeth Figura Subject: [PATCH v3 08/30] ntsync: Introduce NTSYNC_IOC_MUTEX_KILL. Date: Thu, 28 Mar 2024 19:05:59 -0500 Message-ID: <20240329000621.148791-9-zfigura@codeweavers.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240329000621.148791-1-zfigura@codeweavers.com> References: <20240329000621.148791-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. 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 f7911ef78d5b..1e68f96bc2a6 100644 --- a/drivers/misc/ntsync.c +++ b/drivers/misc/ntsync.c @@ -57,6 +57,7 @@ struct ntsync_obj { struct { __u32 count; __u32 owner; + bool ownerdead; } mutex; } u; @@ -109,6 +110,7 @@ struct ntsync_q { atomic_t signaled; bool all; + bool ownerdead; __u32 count; struct ntsync_q_entry entries[]; }; @@ -185,6 +187,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; @@ -246,6 +251,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); @@ -377,6 +385,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; @@ -398,6 +462,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; } @@ -592,6 +658,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++) { @@ -699,7 +766,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; @@ -780,7 +847,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 fa2c9f638d77..1bff8f19d6d9 100644 --- a/include/uapi/linux/ntsync.h +++ b/include/uapi/linux/ntsync.h @@ -44,5 +44,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