public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: Hillf Danton <hdanton@sina.com>
Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org,
	Peter Zijlstra <peterz@infradead.org>,
	"Paul E. McKenney" <paulmck@kernel.org>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Joel Fernandes <joel@joelfernandes.org>
Subject: Re: [PATCH 2/3] rcu: Equip sleepable RCU with lockdep dependency graph checks
Date: Sat, 14 Jan 2023 16:18:57 -0800	[thread overview]
Message-ID: <Y8NGcaMmFgxBmhXN@boqun-archlinux> (raw)
In-Reply-To: <20230114102659.1219-1-hdanton@sina.com>

On Sat, Jan 14, 2023 at 06:26:59PM +0800, Hillf Danton wrote:
> On Fri, 13 Jan 2023 23:32:01 -0800 Boqun Feng <boqun.feng@gmail.com>
> > On Sat, Jan 14, 2023 at 03:18:32PM +0800, Hillf Danton wrote:
> > >
> > >  	task X			task Y
> > >  	---			---
> > >  	mutex_lock(A);
> > >  				srcu_read_lock(B);
> > > 				srcu_lock_acquire(&B->dep_map);
> > > 				a) lock_map_acquire_read(&B->dep_map);
> > >  	synchronze_srcu(B);
> > > 	__synchronize_srcu(B);
> > > 	srcu_lock_sync(&B->dep_map);
> > > 	lock_map_sync(&B->dep_map);
> > > 	lock_sync(&B->dep_map);
> > > 	__lock_acquire(&B->dep_map);
> > 
> > At this time, lockdep add dependency A -> B in the dependency graph.
> > 
> > > 				b) lock_map_acquire_read(&B->dep_map);
> > > 	__lock_release(&B->dep_map);
> > > 				c) lock_map_acquire_read(&B->dep_map);
> > >  				mutex_lock(A);
> > 
> > and here, lockdep will try to add dependency B -> A into the dependency
> > graph, and find that A -> B -> A will form a circle (with strong
> > dependency), therefore lockdep knows it's a deadlock. 
> 
> Is the strong dependency applying to mode c)?
> If yes then deadlock should be also detected in the following locking
> pattern that has no deadlock.
> 
> 	cpu0			cpu1
> 	---			---
> 	mutex_lock A
> 	mutex_lock B
> 	mutex_unlock B
> 				mutex_lock B
> 				mutex_lock A

Well, of course, this is how lockdep works. Lockdep detects the
*potential* deadlocks rather than detects the deadlocks when they
really happen. Otherwise lockdep is useless.

The execution in your example shows the potential deadlocks, i.e. one
task acquires A and then acquires B, the other task acquires B and then
acquires A. Potential deadlocks mean given a correct timing, a deadlock
may happen.

Regards,
Boqun

> > 
> > >  
> > > No deadlock could be detected if taskY takes mutexA after taskX releases B,
> > 
> > The timing that taskX releases B doesn't master, since lockdep uses
> > graph to detect deadlocks rather than after-fact detection.
> > 
> > > and how taskY acquires B does not matter as per the a), b) and c) modes in
> > > the above chart, again because releasing lock can break deadlock in general.
> > 
> > I have test cases showing the above deadlock can be detected, so if you
> > think there is a deadlock that may dodge from my change, feel free to
> > add a test case in lib/locking-selftest.c ;-)
> > 
> > Regards,
> > Boqun

  parent reply	other threads:[~2023-01-15  0:19 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-13  6:59 [PATCH 0/3] Detect SRCU related deadlocks Boqun Feng
2023-01-13  6:59 ` [PATCH 1/3] locking/lockdep: Introduce lock_sync() Boqun Feng
2023-01-16 21:56   ` Waiman Long
2023-01-13  6:59 ` [PATCH 2/3] rcu: Equip sleepable RCU with lockdep dependency graph checks Boqun Feng
2023-01-13 11:29   ` Paul E. McKenney
2023-01-13 18:05     ` Boqun Feng
2023-01-13 19:11       ` Paul E. McKenney
2023-01-16 17:36         ` Paolo Bonzini
2023-01-16 17:54           ` Boqun Feng
2023-01-16 18:56             ` Paul E. McKenney
2023-01-16 22:01   ` Waiman Long
2023-01-13  6:59 ` [PATCH 3/3] WIP: locking/lockdep: selftests: Add selftests for SRCU Boqun Feng
2023-01-13 12:46 ` [PATCH 0/3] KVM: Make use of SRCU deadlock detection support David Woodhouse
2023-01-13 12:46   ` [PATCH 1/3] KVM: Show lockdep the kvm->mutex vs. kvm->srcu ordering rule David Woodhouse
2023-01-13 12:46   ` [PATCH 2/3] KVM: selftests: Use enum for test numbers in xen_shinfo_test David Woodhouse
2023-01-13 17:13     ` David Woodhouse
2023-01-13 12:46   ` [PATCH 3/3] KVM: selftests: Add EVTCHNOP_send slow path test to xen_shinfo_test David Woodhouse
2023-02-04  2:32     ` Sean Christopherson
2023-02-04  2:34   ` [PATCH 0/3] KVM: Make use of SRCU deadlock detection support Sean Christopherson
     [not found] ` <20230113130330.1027-1-hdanton@sina.com>
2023-01-13 17:58   ` [PATCH 2/3] rcu: Equip sleepable RCU with lockdep dependency graph checks Boqun Feng
     [not found]   ` <20230113235809.1085-1-hdanton@sina.com>
2023-01-14  0:17     ` Boqun Feng
     [not found]     ` <20230114071832.1162-1-hdanton@sina.com>
2023-01-14  7:32       ` Boqun Feng
     [not found]       ` <20230114102659.1219-1-hdanton@sina.com>
2023-01-15  0:18         ` Boqun Feng [this message]
2023-01-13 23:57 ` [PATCH 4/3] locking/lockdep: Improve the deadlock scenario print for sync and read lock Boqun Feng
2023-01-16 22:21   ` Waiman Long
2023-01-16 22:35     ` Boqun Feng
2023-01-17  1:36       ` Waiman Long

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y8NGcaMmFgxBmhXN@boqun-archlinux \
    --to=boqun.feng@gmail.com \
    --cc=hdanton@sina.com \
    --cc=joel@joelfernandes.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=paulmck@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox