From nobody Wed Nov 27 15:32:27 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 C2C9D1946AA; Wed, 9 Oct 2024 13:20:20 +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=1728480022; cv=none; b=Vy1lBW4Xwk/fXE3DYL3DzcZYYxPQQqS34yPQEGZCS7tOUpsHDQEaKFSIzK0R80WKhVzLW6j1rwT6WZFHsG9WrkE+eVdDiv43UNciazyYgpBc1+XfsHfDBGVBZgPVE/QMbeL/NJifYeoDou5djqmpxryQHrCWXO/9NkQkiu9LvJQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728480022; c=relaxed/simple; bh=oHNuaVKic/oAOteDuFpQyqqyIJ0/JG7gBlXQvwypzJk=; h=Date:From:To:Subject:Cc:In-Reply-To:References:MIME-Version: Message-ID:Content-Type; b=XsATTNY35rB3FMdvSwAzTqg8iiLxylj6ETGvTtcLBKSaL/wxGV2QGp3feQmJAzT7midhc4TfBq2T36/nNPmKyZkTAjgv7x0JIpXHDfGVJtx8r+18zrJtd4+XSpjFk8ZB3YYtRh5+guW/TBUEB3PvDmk86YWdTBoahV3aI4oRiHE= 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=lHXxjx86; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=E04UY479; 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="lHXxjx86"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="E04UY479" Date: Wed, 09 Oct 2024 13:20:17 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1728480018; 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=MRIrAT20AqLIabLdRFA1CP2VKeoHlgJElvZxF9JNROU=; b=lHXxjx86XGPxXA15b7AUAMoUvYFzyiK+BSRpOsai5P7fNiuE5x9N6LYbJds2pVAGPAJ6R8 YNEe5aLjCS664JHK1Hto+VmK1zCYJfaahRivOU97SviFXSWFzBWEyOFspN5fzBJ971IAuw 5baypcpbcSDklTji9d9ui35hjYIe8wsaObfJTL9bWooVOXia/B+XLN72q4ydyVmJ1krcEI /A49kg9M8iOzJQLmk8B+vBSWtsizVLxO6Y2f/4PGolQVwJTyC4Q2wTjePIflxSPZGfrdhe jgsrYS7PX00Vi1+AM9IahpGZow3zpO5B4UBQNLVwNlsoqWSOGJA7R+Tiv1tueg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1728480018; 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=MRIrAT20AqLIabLdRFA1CP2VKeoHlgJElvZxF9JNROU=; b=E04UY479UJugaaTnF5KaBrfkB1o/IKTe2H1/70JI2/AazsiHXXoY2FaWliVLrywVO2az5u DvdpBk9V4SZ9hXAw== 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: <20241009092031.6356-1-thomas.hellstrom@linux.intel.com> References: <20241009092031.6356-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: <172848001711.1442.15752580154749129748.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: 823a566221a5639f6c69424897218e5d6431a970 Gitweb: https://git.kernel.org/tip/823a566221a5639f6c69424897218e5d6= 431a970 Author: Thomas Hellstr=C3=B6m AuthorDate: Wed, 09 Oct 2024 11:20:31 +02:00 Committer: Peter Zijlstra CommitterDate: Wed, 09 Oct 2024 15:08:25 +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/20241009092031.6356-1-thomas.hellstrom@linu= x.intel.com --- include/linux/ww_mutex.h | 14 ++++++++++++++ kernel/locking/test-ww_mutex.c | 8 +++++--- 2 files changed, 19 insertions(+), 3 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..5d58b2c 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", @@ -679,7 +681,7 @@ static int __init test_ww_mutex_init(void) if (ret) return ret; =20 - ret =3D stress(2047, hweight32(STRESS_ALL)*ncpus, STRESS_ALL); + ret =3D stress(2046, hweight32(STRESS_ALL)*ncpus, STRESS_ALL); if (ret) return ret; =20