Re: [PATCH 1/4] xfs: change xfs_isilocked() to always use lockdep()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Jan 29, 2020 at 02:25:32PM -0800, Darrick J. Wong wrote:
> On Thu, Jan 30, 2020 at 09:18:19AM +1100, Dave Chinner wrote:
> > On Tue, Jan 28, 2020 at 03:55:25PM +0100, Pavel Reichl wrote:
> > > mr_writer is obsolete and the information it contains is accesible
> > > from mr_lock.
> > > 
> > > Signed-off-by: Pavel Reichl <preichl@xxxxxxxxxx>
> > > ---
> > >  fs/xfs/xfs_inode.c | 8 ++++++--
> > >  1 file changed, 6 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/fs/xfs/xfs_inode.c b/fs/xfs/xfs_inode.c
> > > index c5077e6326c7..32fac6152dc3 100644
> > > --- a/fs/xfs/xfs_inode.c
> > > +++ b/fs/xfs/xfs_inode.c
> > > @@ -352,13 +352,17 @@ xfs_isilocked(
> > >  {
> > >  	if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
> > >  		if (!(lock_flags & XFS_ILOCK_SHARED))
> > > -			return !!ip->i_lock.mr_writer;
> > > +			return !debug_locks ||
> > > +				lockdep_is_held_type(&ip->i_lock.mr_lock, 0);
> > >  		return rwsem_is_locked(&ip->i_lock.mr_lock);
> > >  	}
> > >  
> > >  	if (lock_flags & (XFS_MMAPLOCK_EXCL|XFS_MMAPLOCK_SHARED)) {
> > >  		if (!(lock_flags & XFS_MMAPLOCK_SHARED))
> > > -			return !!ip->i_mmaplock.mr_writer;
> > > +			return !debug_locks ||
> > > +				lockdep_is_held_type(
> > > +					&ip->i_mmaplock.mr_lock,
> > > +					0);
> > >  		return rwsem_is_locked(&ip->i_mmaplock.mr_lock);
> > >  	}
> > 
> > Ok, so this code is only called from ASSERT() statements, which
> > means this turns off write lock checking for XFS debug kernels if
> > lockdep is not enabled. Hence I think these checks need to be
> > restructured to be based around rwsem_is_locked() first and lockdep
> > second.
> > 
> > That is:
> > 
> > /* In all implementations count != 0 means locked */
> > static inline int rwsem_is_locked(struct rw_semaphore *sem)
> > {
> >         return atomic_long_read(&sem->count) != 0;
> > }
> > 
> > This captures both read and write locks on the rwsem, and doesn't
> > discriminate at all. Now we don't have explicit writer lock checking
> > in CONFIG_XFS_DEBUG=y kernels, I think we need to at least check
> > that the rwsem is locked in all cases to catch cases where we are
> > calling a function without the lock held. That will ctach most
> > programming mistakes, and then lockdep will provide the
> > read-vs-write discrimination to catch the "hold the wrong lock type"
> > mistakes.
> > 
> > Hence I think this code should end up looking like this:
> > 
> > 	if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED)) {
> > 		bool locked = false;
> > 
> > 		if (!rwsem_is_locked(&ip->i_lock))
> > 			return false;
> > 		if (!debug_locks)
> > 			return true;
> > 		if (lock_flags & XFS_ILOCK_EXCL)
> > 			locked = lockdep_is_held_type(&ip->i_lock, 0);
> > 		if (lock_flags & XFS_ILOCK_SHARED)
> > 			locked |= lockdep_is_held_type(&ip->i_lock, 1);
> > 		return locked;
> > 	}
> > 
> > Thoughts?
> 
> I like that a lot better, though perhaps the if body should be factored
> into a separate static inline so we don't repeat that 3x.

Yup, I had thoughts along those lines, too, but each lock type uses
different flags and that makes it more verbose than it could be.
Maybe something like this?

static inline bool
__xfs_is_ilocked(
	struct rwsem	*rwsem,
	bool		shared,
	bool		excl)
{
	bool locked = false;

	if (!rwsem_is_locked(rwsem))
		return false;
	if (!debug_locks)
		return true;
	if (shared)
		locked = lockdep_is_held_type(&ip->i_lock, 0);
	if (excl)
		locked |= lockdep_is_held_type(&ip->i_lock, 1);
	return locked;
}

bool
xfs_isilocked(
	struct xfs_inode	*ip,
	int			lock_flags)
{
	if (lock_flags & (XFS_ILOCK_EXCL|XFS_ILOCK_SHARED))
		return __xfs_is_ilocked(&ip->i_lock,
				(lock_flags & XFS_ILOCK_SHARED),
				(lock_flags & XFS_ILOCK_EXCL));

	if (lock_flags & (XFS_MMAPLOCK_EXCL|XFS_MMAPLOCK_SHARED))
		return __xfs_is_ilocked(&ip->i_mmaplock,
				(lock_flags & XFS_MMAPLOCK_SHARED),
				(lock_flags & XFS_MMAPLOCK_EXCL));

	if (lock_flags & (XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED))
		return __xfs_is_ilocked(&VFS_I(ip)->i_rwsem,
				(lock_flags & XFS_IOLOCK_SHARED),
				(lock_flags & XFS_IOLOCK_EXCL));

	ASSERT(0);
	return false;
}

At which point I wonder if it would simply be better to have:

bool
xfs_is_ilocked(
	struct xfs_inode	*ip,
	int			lock_flags)
{
	return __xfs_is_ilocked(&ip->i_lock, (lock_flags & XFS_ILOCK_SHARED),
				(lock_flags & XFS_ILOCK_EXCL));
}

bool
xfs_is_mmaplocked(
	struct xfs_inode	*ip,
	int			lock_flags)
{
	return __xfs_is_ilocked(&ip->i_mmaplock,
				(lock_flags & XFS_MMAPLOCK_SHARED),
				(lock_flags & XFS_MMAPLOCK_EXCL));
}

bool
xfs_is_iolocked(
	struct xfs_inode	*ip,
	int			lock_flags)
{
	return __xfs_is_ilocked(&VFS_I(ip)->i_rwsem,
				(lock_flags & XFS_IOLOCK_SHARED),
				(lock_flags & XFS_IOLOCK_EXCL));
}

Cheers,

Dave.
-- 
Dave Chinner
david@xxxxxxxxxxxxx



[Index of Archives]     [XFS Filesystem Development (older mail)]     [Linux Filesystem Development]     [Linux Audio Users]     [Yosemite Trails]     [Linux Kernel]     [Linux RAID]     [Linux SCSI]


  Powered by Linux