From nobody Wed Nov 27 18:56:36 2024 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 86A4E20B1F2; Tue, 8 Oct 2024 07:55:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728374126; cv=none; b=SZrJuG9bFOAvhE3Y7GhDQk9CzYlQ2LEdc8xd3c3CRR1+BflIh9v9O+KiRJrkrqhFJ1lidumfoGVOLoKtTFI0VY9+kGFDXg8qc5Sp681rGk6lvshRtJ2mPq8CRcUAS1aN5c292pSOEZkpUKJO5Xnm918yqdTUzd+6v0zOuBQHNOE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728374126; c=relaxed/simple; bh=qZ2oYFYVClaM6JBAQ5HVbRen+p65QXZ6WjhQ2qPM9uM=; h=Date:From:To:Subject:Cc:In-Reply-To:References:MIME-Version: Message-ID:Content-Type; b=uc/P1n32mSLAIX/r0GFOR4g6aMC7hqUn3Qk8q7cQaiG0KUdCEeZeMCZAS4+02RfGWtfspz9XA8DmpflRZieaElCWlWRwkvo76pKOVSS2z778kK9WW8ey1gaYiEbCw4+P1kHotvAWLbyGSr4IiRymE2DH0nkEeB5iFsVQdyzQLl4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=hZ8JsLxq; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=OPerFbUf; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="hZ8JsLxq"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="OPerFbUf" Date: Tue, 08 Oct 2024 07:55:21 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1728374122; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ocE19dZWXVQ81hBeks3BFI2at/11ecXDX0W220Ix3fU=; b=hZ8JsLxqlUcVRUp4dCGpyNeT5PT28pFu1WUQzhcbUkoU6iEK6tlWpFJXkJeq9p5FNUzZIn NKqr+sektzgjAmtQrIizwY0tlB5BX3vgTgy93VS40auQo7PaKkxhmmFqIpLFoAmIEsUjXV 0V9VIBngtGEmZHevBkzrq8/bdtDk1pUjozCDR/K4AA12ieMMlXtF6Vj2E/v6sYDfSI2DQr QbTKidwK/umOLDRoVfbGvugTXz5ucVAEV2AsWskIEXmQi7pNanFcZEMI/WmehNYpyQB0ss pmB6Pe4WD5ZfKhv/VGvhQW3KPPibu1QXJAw0vbWL95WtxHMbEVNaGOw4qIfgAg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1728374122; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ocE19dZWXVQ81hBeks3BFI2at/11ecXDX0W220Ix3fU=; b=OPerFbUfUkM9cqLIHwOxqS0DmrVS9D3Vk+pg0lWmY/x/mzs7Wn/YAuAdxM4VTcCAoUtQ8K 6Gfp8TwaNLdcBRCQ== From: tip-bot2 for Thomas =?utf-8?q?Hellstr=C3=B6m?= Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: locking/core] locking/ww_mutex: Adjust to lockdep nest_lock requirements Cc: thomas.hellstrom@linux.intel.com, "Peter Zijlstra (Intel)" , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20241002125611.361001-1-thomas.hellstrom@linux.intel.com> References: <20241002125611.361001-1-thomas.hellstrom@linux.intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <172837412149.1442.1382529165947446095.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The following commit has been merged into the locking/core branch of tip: Commit-ID: 59f812dd6d95bed605c7265bf193ec8155cf6bcc Gitweb: https://git.kernel.org/tip/59f812dd6d95bed605c7265bf193ec815= 5cf6bcc Author: Thomas Hellstr=C3=B6m AuthorDate: Wed, 02 Oct 2024 14:56:11 +02:00 Committer: Peter Zijlstra CommitterDate: Mon, 07 Oct 2024 09:28:35 +02:00 locking/ww_mutex: Adjust to lockdep nest_lock requirements When using mutex_acquire_nest() with a nest_lock, lockdep refcounts the number of acquired lockdep_maps of mutexes of the same class, and also keeps a pointer to the first acquired lockdep_map of a class. That pointer is then used for various comparison-, printing- and checking purposes, but there is no mechanism to actively ensure that lockdep_map stays in memory. Instead, a warning is printed if the lockdep_map is freed and there are still held locks of the same lock class, even if the lockdep_map itself has been released. In the context of WW/WD transactions that means that if a user unlocks and frees a ww_mutex from within an ongoing ww transaction, and that mutex happens to be the first ww_mutex grabbed in the transaction, such a warning is printed and there might be a risk of a UAF. Note that this is only problem when lockdep is enabled and affects only dereferences of struct lockdep_map. Adjust to this by adding a fake lockdep_map to the acquired context and make sure it is the first acquired lockdep map of the associated ww_mutex class. Then hold it for the duration of the WW/WD transaction. This has the side effect that trying to lock a ww mutex *without* a ww_acquire_context but where a such context has been acquire, we'd see a lockdep splat. The test-ww_mutex.c selftest attempts to do that, so modify that particular test to not acquire a ww_acquire_context if it is not going to be used. Signed-off-by: Thomas Hellstr=C3=B6m Signed-off-by: Peter Zijlstra (Intel) Link: https://lkml.kernel.org/r/20241002125611.361001-1-thomas.hellstrom@li= nux.intel.com --- include/linux/ww_mutex.h | 14 ++++++++++++++ kernel/locking/test-ww_mutex.c | 6 ++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/include/linux/ww_mutex.h b/include/linux/ww_mutex.h index bb76308..a401a2f 100644 --- a/include/linux/ww_mutex.h +++ b/include/linux/ww_mutex.h @@ -65,6 +65,16 @@ struct ww_acquire_ctx { #endif #ifdef CONFIG_DEBUG_LOCK_ALLOC struct lockdep_map dep_map; + /** + * @first_lock_dep_map: fake lockdep_map for first locked ww_mutex. + * + * lockdep requires the lockdep_map for the first locked ww_mutex + * in a ww transaction to remain in memory until all ww_mutexes of + * the transaction have been unlocked. Ensure this by keeping a + * fake locked ww_mutex lockdep map between ww_acquire_init() and + * ww_acquire_fini(). + */ + struct lockdep_map first_lock_dep_map; #endif #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH unsigned int deadlock_inject_interval; @@ -146,7 +156,10 @@ static inline void ww_acquire_init(struct ww_acquire_c= tx *ctx, debug_check_no_locks_freed((void *)ctx, sizeof(*ctx)); lockdep_init_map(&ctx->dep_map, ww_class->acquire_name, &ww_class->acquire_key, 0); + lockdep_init_map(&ctx->first_lock_dep_map, ww_class->mutex_name, + &ww_class->mutex_key, 0); mutex_acquire(&ctx->dep_map, 0, 0, _RET_IP_); + mutex_acquire_nest(&ctx->first_lock_dep_map, 0, 0, &ctx->dep_map, _RET_IP= _); #endif #ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH ctx->deadlock_inject_interval =3D 1; @@ -185,6 +198,7 @@ static inline void ww_acquire_done(struct ww_acquire_ct= x *ctx) static inline void ww_acquire_fini(struct ww_acquire_ctx *ctx) { #ifdef CONFIG_DEBUG_LOCK_ALLOC + mutex_release(&ctx->first_lock_dep_map, _THIS_IP_); mutex_release(&ctx->dep_map, _THIS_IP_); #endif #ifdef DEBUG_WW_MUTEXES diff --git a/kernel/locking/test-ww_mutex.c b/kernel/locking/test-ww_mutex.c index 10a5736..4c2b8b5 100644 --- a/kernel/locking/test-ww_mutex.c +++ b/kernel/locking/test-ww_mutex.c @@ -62,7 +62,8 @@ static int __test_mutex(unsigned int flags) int ret; =20 ww_mutex_init(&mtx.mutex, &ww_class); - ww_acquire_init(&ctx, &ww_class); + if (flags & TEST_MTX_CTX) + ww_acquire_init(&ctx, &ww_class); =20 INIT_WORK_ONSTACK(&mtx.work, test_mutex_work); init_completion(&mtx.ready); @@ -90,7 +91,8 @@ static int __test_mutex(unsigned int flags) ret =3D wait_for_completion_timeout(&mtx.done, TIMEOUT); } ww_mutex_unlock(&mtx.mutex); - ww_acquire_fini(&ctx); + if (flags & TEST_MTX_CTX) + ww_acquire_fini(&ctx); =20 if (ret) { pr_err("%s(flags=3D%x): mutual exclusion failure\n",