From patchwork Mon Feb 19 22:38:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elizabeth Figura X-Patchwork-Id: 203333 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp78375dyc; Mon, 19 Feb 2024 14:51:31 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWh+7sZ+PPG/OLMpQq9ohutujwDWf5wLcnAb2MxVDk7vSIDe/s36iznisLcbAkDbTH/ngSPg51GKOtc45yPiGrpgMFzqw== X-Google-Smtp-Source: AGHT+IEyYgOzt5zm/TEV005J1S5of98wZc9L7wEFvT/+z3FAtONupf790gNlcvm/jwtaEbM4j4CU X-Received: by 2002:a17:902:a513:b0:1d9:855a:3130 with SMTP id s19-20020a170902a51300b001d9855a3130mr11586366plq.33.1708383091011; Mon, 19 Feb 2024 14:51:31 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708383090; cv=pass; d=google.com; s=arc-20160816; b=ME96kua3D+OAtPugn8e6eqSv3/7IMVvmLzFI3kEPYox3V/zPMwPbTU3tneT5ZCj7oZ ONbe/em2lnxOPDqaQz2ub1R+vMa/DhouQnvKF9wgBf7OgQnuNFvfJP0lGS9y4tRCIFiM GOuJ5IHvn3+OHzdzgJQ8IxBkuBHxpkaZpjPhhxMgnAeXGy7qG/rWHeJxsZ6VL6aT+uRt aHnlWqQLj+/EDKQxdQyDGqRExLY2nH0syxVM2hIRgouEF0rT/5Q5J0aJBWsp6SBl/fOB 2PdW3wKaxaoxqSVdodiorCd0boI8ejmMm++4HySVun1SqJoLs1cYZYBajJW7y5Mm261T NBRQ== 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=K0cW3d5t42AkImUgjDNVCCgUjYNfQnZTDPHDMI8GqfA=; fh=hR3Xqo6s6FufP5gEw1XgbtLHrluZu7hd5XGp+hLIfiM=; b=R3keBfYnxEyL8fcmPnjKzaG3T1MU3Q9uuJ6rB+7Wh88QJQjOOFTq4zKbaUDKPXQTsE QcHO8fISKOy/zXS1ywcYZNEgPO8/I9En6F6LG7LFfY+CNVF0EJ3Kj+4FpAoc4yseQnDR +7MnTgQJYQKpjwS7Bl0PwVlGtvbqBGYE5eHiixsY9LNRqhumZf7H/zbmujHJ7vvju3rg h1/3X6I4J9Abo7mZ2TfMpz1TmkrrnIhBhCUycAlquZlvDvCw2WA59kLcm4hAnRTE3kFt gjS3dCCbmfaY+yd+TU7UaTQyMR3fhSCYhRs/AfN6VeRVwheLp8S9ih/AFdoqynKu+jiC g2xA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=dal7TowD; 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-72082-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72082-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. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id ll12-20020a170903090c00b001dbf2d899c6si2462573plb.433.2024.02.19.14.51.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 14:51:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-72082-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@codeweavers.com header.s=s1 header.b=dal7TowD; 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-72082-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72082-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 E13DDB26335 for ; Mon, 19 Feb 2024 22:43:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B493860DE7; 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="dal7TowD" 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 EE0CC5644B; 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=WNbSstHJ5F17pT8awRUmtQPCD7rl7yM3sOXqUE18E4peMzK15LpTcrZguodOmU32k6njhcXbgSq65sMK2h5/S6VVzjIcGHrfUKxfBJWLxDLEudon6ayAHxL5HUCQvhCQWjbmKaAWnXOOWX07a1D0t05v3lugguMjHIGeUJIFH/M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708382380; c=relaxed/simple; bh=ho35sDy8q6vTSWxOgKap78dL81SAppW0zva5v9aBiMo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WOPsJSnOGu1ufG3zuc0WJSiIBaYQEDky626v2VzC+pZiVUx/KOScz9YirVpBlMOEm+5ezgopo3ifLrKAuGbADndcbCPg3g+05D2l9xYGqcyHjNjrtRc7XRrH0I5rGNZcD9mqyJg7oFBO2pDA6P/I5FIEjw858F/ek64dNx643vA= 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=dal7TowD; 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=K0cW3d5t42AkImUgjDNVCCgUjYNfQnZTDPHDMI8GqfA=; b=dal7TowDhJB5lljh4ACNLMrVOX Zdaf63eZHDvXGpjRUxqHXUGdVf3+POrqXE5fIQWyxI4kfJMAcSCWnBa8KG2v5/C/ml/KzR4wWSK72 OlSz7xu/RfJU/dVfjnlNy8uPSbViF3dCrfT0jeNsOp9OeLTBoiT5+foQkRwqnPWJlWc4eLF8wB8v7 EdwvYKGPwx0mkTOcZkIWr5KGL73cEmkjujodjIBUD3Yb4SEyQ9dfWmA9CwJ/0ton2OPKbkKl/gkOO XEvGb9GFTKOF+6aEVysE+7p9TO7CVQdmUCumgz19DD+x7MTW7UEhlE1hbuPzfH1DTwFg0b2FiUX+4 rC+odrPA==; 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-0a; 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 08/31] ntsync: Introduce NTSYNC_IOC_MUTEX_KILL. Date: Mon, 19 Feb 2024 16:38:10 -0600 Message-ID: <20240219223833.95710-9-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: 1791369508065563471 X-GMAIL-MSGID: 1791369508065563471 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 222ebead8eba..a3466be50c45 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[]; }; @@ -184,6 +186,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; @@ -243,6 +248,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); @@ -374,6 +382,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; @@ -395,6 +459,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; } @@ -579,6 +645,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++) { @@ -686,7 +753,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; @@ -767,7 +834,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 265503d441b1..4800941fcbda 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