From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (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 0B67C18BC05 for ; Wed, 16 Oct 2024 06:17:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729059480; cv=none; b=hqpABXacMafZU3fwIXHrNHG0hErKMNfAQEBEgp/dhv0b8Ov/3eyDUVj1EtQhRQUdgpZwazn+tr6kT2XC4krw7qW/5LXkPzY9F446m+6b/8n3LS4Q+qlrCjRTTiPZS+4NssrvuLaMX7HT7LJ4EbTs9cGyBNPTxrreEdBjcHKpMcA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729059480; c=relaxed/simple; bh=DbGFQO1PVOYb+7WvhwRXAC3V0LwgDa2w/LNZHgQKhBU=; h=Message-ID:Subject:From:To:Cc:Date:In-Reply-To:References: Content-Type:MIME-Version; b=PU92i8+qX6dmRScDbHjAIcbBoxJF0kCtkrFqmo7NNXZcS89M0tLQPOotbTz+nYsGiuSGEbfYCIW2Pe58DeRY/WzTYjoCTWdMzVbIn/rGQ0d0iiitaGpm6w8D9Zyu2QSWLklN9JSQ2PAqXBj8gaWSIiZwf6J4DTDQEHavWilCyuI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=cese27kP; arc=none smtp.client-ip=192.198.163.17 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="cese27kP" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1729059478; x=1760595478; h=message-id:subject:from:to:cc:date:in-reply-to: references:content-transfer-encoding:mime-version; bh=DbGFQO1PVOYb+7WvhwRXAC3V0LwgDa2w/LNZHgQKhBU=; b=cese27kPS1toEWkKjwdbJsaVPwFKn0qfnVkCM19SILIxrGJr264q0Rwl 5vpT5LeRlawORC76mat1lyo8kvzdiniOUB4rlEPGHnxlERYGk2q2ciRuP XJsMxGmTBbJKjzl1Q62ebhD9Ayvzj/GPO1KCN3jYoXo26yoT3DQvbks4r lJxnP5s4KcsgNIAwGeZs0We0onHagavWVH6FrU84Of+j0Nr/HOOn/F61x xU5ZkXTpaAg4AOcE1wb1e+v3EEWBpaK3X27XpuZQF4WfUi6KAof3gUkdz K8e4QvNPSyX7JYsFltvvDyzLVEAtDr2FV+AUkjHikQYMb+aGl1xnX2f0S w==; X-CSE-ConnectionGUID: V8XxA+wuRP6R1lRq0bafYg== X-CSE-MsgGUID: rSliFnsWQ7K3ZgI8UerOrg== X-IronPort-AV: E=McAfee;i="6700,10204,11225"; a="28367119" X-IronPort-AV: E=Sophos;i="6.11,207,1725346800"; d="scan'208";a="28367119" Received: from fmviesa010.fm.intel.com ([10.60.135.150]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Oct 2024 23:17:57 -0700 X-CSE-ConnectionGUID: vbhxgz5HQSOemlec6166lA== X-CSE-MsgGUID: Yv/2DeicReqkGatn6QXyGg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,207,1725346800"; d="scan'208";a="78475506" Received: from ettammin-mobl2.ger.corp.intel.com (HELO [10.245.246.80]) ([10.245.246.80]) by fmviesa010-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Oct 2024 23:17:54 -0700 Message-ID: Subject: Re: [PATCH v2] locking/ww_mutex: Adjust to lockdep nest_lock requirements From: Thomas =?ISO-8859-1?Q?Hellstr=F6m?= To: Boqun Feng Cc: intel-xe@lists.freedesktop.org, Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Maarten Lankhorst , Christian =?ISO-8859-1?Q?K=F6nig?= , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org Date: Wed, 16 Oct 2024 08:17:50 +0200 In-Reply-To: References: <20241009092031.6356-1-thomas.hellstrom@linux.intel.com> Autocrypt: addr=thomas.hellstrom@linux.intel.com; prefer-encrypt=mutual; keydata=mDMEZaWU6xYJKwYBBAHaRw8BAQdAj/We1UBCIrAm9H5t5Z7+elYJowdlhiYE8zUXgxcFz360SFRob21hcyBIZWxsc3Ryw7ZtIChJbnRlbCBMaW51eCBlbWFpbCkgPHRob21hcy5oZWxsc3Ryb21AbGludXguaW50ZWwuY29tPoiTBBMWCgA7FiEEbJFDO8NaBua8diGTuBaTVQrGBr8FAmWllOsCGwMFCwkIBwICIgIGFQoJCAsCBBYCAwECHgcCF4AACgkQuBaTVQrGBr/yQAD/Z1B+Kzy2JTuIy9LsKfC9FJmt1K/4qgaVeZMIKCAxf2UBAJhmZ5jmkDIf6YghfINZlYq6ixyWnOkWMuSLmELwOsgPuDgEZaWU6xIKKwYBBAGXVQEFAQEHQF9v/LNGegctctMWGHvmV/6oKOWWf/vd4MeqoSYTxVBTAwEIB4h4BBgWCgAgFiEEbJFDO8NaBua8diGTuBaTVQrGBr8FAmWllOsCGwwACgkQuBaTVQrGBr/P2QD9Gts6Ee91w3SzOelNjsus/DcCTBb3fRugJoqcfxjKU0gBAKIFVMvVUGbhlEi6EFTZmBZ0QIZEIzOOVfkaIgWelFEH Organization: Intel Sweden AB, Registration Number: 556189-6027 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.50.4 (3.50.4-1.fc39) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 On Tue, 2024-10-15 at 11:00 -0700, Boqun Feng wrote: > On Tue, Oct 15, 2024 at 05:27:28PM +0200, Thomas Hellstr=C3=B6m wrote: > [..] > > > diff --git a/lib/locking-selftest.c b/lib/locking-selftest.c > > > index 6f6a5fc85b42..6750321e3e9a 100644 > > > --- a/lib/locking-selftest.c > > > +++ b/lib/locking-selftest.c > > > @@ -1720,8 +1720,6 @@ static void ww_test_normal(void) > > > =C2=A0{ > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 int ret; > > >=20 > > > -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 WWAI(&t); > > > - > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 /* > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * None of the ww_mut= ex codepaths should be taken in the > > > 'normal' > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * mutex calls. The e= asiest way to verify this is by > > > using > > > the > > > @@ -1770,6 +1768,8 @@ static void ww_test_normal(void) > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ww_mutex_base_unlock(&o.ba= se); > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 WARN_ON(o.ctx !=3D (void *= )~0UL); > > >=20 > > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 WWAI(&t); > > > + > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 /* nest_lock */ > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 o.ctx =3D (void *)~0UL; > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ww_mutex_base_lock_nest_lo= ck(&o.base, &t); > > >=20 > > > Please confirm whether this change is intended. > >=20 > > This fix looks correct and while this change was not intended, I > > think > > it makes sense and if this locking order is present in existing > > code > > apart from this selftest, it's probably easily fixable. > >=20 > > >=20 > > > The second is a case as follow: > > >=20 > > > ww_acquire_init(...); > > > spin_lock(...); > > > ww_mutex_lock(...); // this should trigger a context > > > =C2=A0=C2=A0=C2=A0 // invalidation. But the mutex was > > > =C2=A0=C2=A0=C2=A0 // initialized by ww_acquire_init() > > > as a > > > =C2=A0=C2=A0=C2=A0 // LD_WAIT_INV lock. > > >=20 > > > The following could fix this: > > >=20 > > > diff --git a/include/linux/ww_mutex.h b/include/linux/ww_mutex.h > > > index a401a2f31a77..45ff6f7a872b 100644 > > > --- a/include/linux/ww_mutex.h > > > +++ b/include/linux/ww_mutex.h > > > @@ -156,8 +156,8 @@ static inline void ww_acquire_init(struct > > > ww_acquire_ctx *ctx, > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 debug_check_no_locks_freed= ((void *)ctx, sizeof(*ctx)); > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 lockdep_init_map(&ctx->dep= _map, ww_class->acquire_name, > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= &ww_class->acquire_key, 0); > > > -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 lockdep_init_map(&ctx->first_lo= ck_dep_map, ww_class- > > > > mutex_name, > > > -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 &w= w_class->mutex_key, 0); > > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 lockdep_init_map_wait(&ctx->fir= st_lock_dep_map, ww_class- > > > > mutex_name, > > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0 &ww_class->mutex_key, 0, > > > LD_WAIT_SLEEP); > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 mutex_acquire(&ctx->dep_ma= p, 0, 0, _RET_IP_); > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 mutex_acquire_nest(&ctx->f= irst_lock_dep_map, 0, 0, &ctx- > > > > dep_map, _RET_IP_); > > > =C2=A0#endif > > >=20 > > > A v3 with all these fixed would look good to me, and I can add a > > > Tested-by tag to it. Thanks! > >=20 > > The fix here is a bit confusing. It looks like this test is crafted > > to > > fail because we take a sleeping ww_mutex inside a spinlock. But the > > ww_mutex lockdep map is already initialized as LD_WAIT_SLEEP. How > > come > > the first_lock_dep_map locking mode LD_WAIT_INV is used in the > > ww_mutex_lock()? Is that because of the lockdep hlock refcounting? > >=20 >=20 > No, it's not because of refcounting, actually in this case > refcounting > won't happen because there is a spin_lock sitting in between: >=20 > held_locks stack: >=20 > ww_lockdep_acquire > =C2=A0 ww_lockdep_mutex > =C2=A0=C2=A0=C2=A0 lock_A >=20 > because there is a lock_A here, the following "if" will be false for > ww_mutex_lock() in the test case: >=20 > hlock =3D curr->held_locks + depth - 1; > if (hlock->class_idx =3D=3D class_idx && nest_lock) { >=20 > The reason why the wait types of 'first_lock_dep_map' matter is > because > the lock class it shares with ww_mutex_lock() are registered at > *acquire* time. So because we do >=20 > ww_acquire_init(): > =C2=A0 ... > =C2=A0 lockdep_init_map(...); > =C2=A0 ... > =C2=A0 mutex_acquire_nest(...); > ... > ww_mutex_lock(): > =C2=A0 __mutex_lock_common(): > =C2=A0=C2=A0=C2=A0 mutex_acquire_nest(...); >=20 > in the test case, these two mutex_acquire_nest()s use different > lockdep_maps but share the same key, therefore whoever call > mutex_acquire_nest() registers the lock class with its wait types. >=20 > So even though first_lock_dep_map is a fake lock, it has to have the > same wait types as a real mutex. Understood. >=20 > Does this make sense? Yes it does. I'll update to a v3, and add a Tested-by: tag. Would you like a Co-developed-by: tag as well? Thanks, Thomas >=20 > Regards, > Boqun >=20 > > Thanks, > > Thomas > >=20 > >=20 > >=20 > > >=20 > > > Regards, > > > Boqun > > >=20 > > > > Cc: Peter Zijlstra > > > > Cc: Ingo Molnar > > > > Cc: Will Deacon > > > > Cc: Waiman Long > > > > Cc: Boqun Feng > > > > Cc: Maarten Lankhorst > > > > Cc: Christian K=C3=B6nig > > > > Cc: dri-devel@lists.freedesktop.org > > > > Cc: linux-kernel@vger.kernel.org > > > > Signed-off-by: Thomas Hellstr=C3=B6m > > > > > > > > --- > > > > =C2=A0include/linux/ww_mutex.h=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 = | 14 ++++++++++++++ > > > > =C2=A0kernel/locking/test-ww_mutex.c |=C2=A0 8 +++++--- > > > > =C2=A02 files changed, 19 insertions(+), 3 deletions(-) > > > >=20 > > > > diff --git a/include/linux/ww_mutex.h > > > > b/include/linux/ww_mutex.h > > > > index bb763085479a..a401a2f31a77 100644 > > > > --- a/include/linux/ww_mutex.h > > > > +++ b/include/linux/ww_mutex.h > > > > @@ -65,6 +65,16 @@ struct ww_acquire_ctx { > > > > =C2=A0#endif > > > > =C2=A0#ifdef CONFIG_DEBUG_LOCK_ALLOC > > > > =C2=A0 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; > > > > =C2=A0#endif > > > > =C2=A0#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH > > > > =C2=A0 unsigned int deadlock_inject_interval; > > > > @@ -146,7 +156,10 @@ static inline void ww_acquire_init(struct > > > > ww_acquire_ctx *ctx, > > > > =C2=A0 debug_check_no_locks_freed((void *)ctx, sizeof(*ctx)); > > > > =C2=A0 lockdep_init_map(&ctx->dep_map, ww_class- > > > > >acquire_name, > > > > =C2=A0 &ww_class->acquire_key, 0); > > > > + lockdep_init_map(&ctx->first_lock_dep_map, ww_class- > > > > > mutex_name, > > > > + &ww_class->mutex_key, 0); > > > > =C2=A0 mutex_acquire(&ctx->dep_map, 0, 0, _RET_IP_); > > > > + mutex_acquire_nest(&ctx->first_lock_dep_map, 0, 0, > > > > &ctx- > > > > > dep_map, _RET_IP_); > > > > =C2=A0#endif > > > > =C2=A0#ifdef CONFIG_DEBUG_WW_MUTEX_SLOWPATH > > > > =C2=A0 ctx->deadlock_inject_interval =3D 1; > > > > @@ -185,6 +198,7 @@ static inline void ww_acquire_done(struct > > > > ww_acquire_ctx *ctx) > > > > =C2=A0static inline void ww_acquire_fini(struct ww_acquire_ctx *ctx= ) > > > > =C2=A0{ > > > > =C2=A0#ifdef CONFIG_DEBUG_LOCK_ALLOC > > > > + mutex_release(&ctx->first_lock_dep_map, _THIS_IP_); > > > > =C2=A0 mutex_release(&ctx->dep_map, _THIS_IP_); > > > > =C2=A0#endif > > > > =C2=A0#ifdef DEBUG_WW_MUTEXES > > > > diff --git a/kernel/locking/test-ww_mutex.c > > > > b/kernel/locking/test- > > > > ww_mutex.c > > > > index 10a5736a21c2..5d58b2c0ef98 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) > > > > =C2=A0 int ret; > > > > =C2=A0 > > > > =C2=A0 ww_mutex_init(&mtx.mutex, &ww_class); > > > > - ww_acquire_init(&ctx, &ww_class); > > > > + if (flags & TEST_MTX_CTX) > > > > + ww_acquire_init(&ctx, &ww_class); > > > > =C2=A0 > > > > =C2=A0 INIT_WORK_ONSTACK(&mtx.work, test_mutex_work); > > > > =C2=A0 init_completion(&mtx.ready); > > > > @@ -90,7 +91,8 @@ static int __test_mutex(unsigned int flags) > > > > =C2=A0 ret =3D wait_for_completion_timeout(&mtx.done, > > > > TIMEOUT); > > > > =C2=A0 } > > > > =C2=A0 ww_mutex_unlock(&mtx.mutex); > > > > - ww_acquire_fini(&ctx); > > > > + if (flags & TEST_MTX_CTX) > > > > + ww_acquire_fini(&ctx); > > > > =C2=A0 > > > > =C2=A0 if (ret) { > > > > =C2=A0 pr_err("%s(flags=3D%x): mutual exclusion > > > > failure\n", > > > > @@ -679,7 +681,7 @@ static int __init test_ww_mutex_init(void) > > > > =C2=A0 if (ret) > > > > =C2=A0 return ret; > > > > =C2=A0 > > > > - ret =3D stress(2047, hweight32(STRESS_ALL)*ncpus, > > > > STRESS_ALL); > > > > + ret =3D stress(2046, hweight32(STRESS_ALL)*ncpus, > > > > STRESS_ALL); > > > > =C2=A0 if (ret) > > > > =C2=A0 return ret; > > > > =C2=A0 > > > > --=20 > > > > 2.46.0 > > > >=20 > >=20