linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jan Kara <jack@suse.cz>
To: Al Viro <viro@zeniv.linux.org.uk>
Cc: linux-fsdevel@vger.kernel.org,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Mo Zou <lostzoumo@gmail.com>, Jan Kara <jack@suse.cz>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 9/9] rename(): avoid a deadlock in the case of parents having no common ancestor
Date: Thu, 23 Nov 2023 11:36:22 +0100	[thread overview]
Message-ID: <20231123103622.4mfjwrmxr4tl53hi@quack3> (raw)
In-Reply-To: <20231122193652.419091-9-viro@zeniv.linux.org.uk>

On Wed 22-11-23 19:36:52, Al Viro wrote:
> ... and fix the directory locking documentation and proof of correctness.
> Holding ->s_vfs_rename_mutex *almost* prevents ->d_parent changes; the
> case where we really don't want it is splicing the root of disconnected
> tree to somewhere.
> 
> In other words, ->s_vfs_rename_mutex is sufficient to stabilize "X is an
> ancestor of Y" only if X and Y are already in the same tree.  Otherwise
> it can go from false to true, and one can construct a deadlock on that.
> 
> Make lock_two_directories() report an error in such case and update the
> callers of lock_rename()/lock_rename_child() to handle such errors.
> 
> And yes, such conditions are not impossible to create ;-/
> 
> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>

Looks good to me. Just one nit below but whether you decide to address it
or not, feel free to add:

Reviewed-by: Jan Kara <jack@suse.cz>

> +// p1 != p2, both are on the same filesystem, ->s_vfs_rename_mutex is held
>  static struct dentry *lock_two_directories(struct dentry *p1, struct dentry *p2)
>  {
> -	struct dentry *p;
> +	struct dentry *p = p1, *q = p2, *r;
>  
> -	p = d_ancestor(p2, p1);
> -	if (p) {
> +	while ((r = p->d_parent) != p2 && r != p)
> +		p = r;
> +	if (r == p2) {
> +		// p is a child of p2 and an ancestor of p1 or p1 itself
>  		inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
>  		inode_lock_nested(p1->d_inode, I_MUTEX_PARENT2);
>  		return p;
>  	}
> -
> -	p = d_ancestor(p1, p2);
> -	inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
> -	inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
> -	return p;
> +	// p is the root of connected component that contains p1
> +	// p2 does not occur on the path from p to p1
> +	while ((r = q->d_parent) != p1 && r != p && r != q)
> +		q = r;
> +	if (r == p1) {
> +		// q is a child of p1 and an ancestor of p2 or p2 itself
> +		inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
> +		inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
> +		return q;
> +	} else if (likely(r == p)) {
> +		// both p2 and p1 are descendents of p
> +		inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
> +		inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
> +		return NULL;
> +	} else { // no common ancestor at the time we'd been called
> +		mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);

It would look more natural to me if s_vfs_rename_mutex got dropped in the
callers (lock_rename(), lock_rename_child()) which have acquired the lock
instead of here. I agree it results in a bit more boiler plate code though.

> +		return ERR_PTR(-EXDEV);
> +	}
>  }

								Honza
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR

  reply	other threads:[~2023-11-24  4:18 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-22 19:30 [PATCHES][CFT] rename deadlock fixes Al Viro
2023-11-22 19:36 ` [PATCH 1/9] reiserfs: Avoid touching renamed directory if parent does not change Al Viro
2023-11-22 19:36   ` [PATCH 2/9] ocfs2: " Al Viro
2023-11-22 19:36   ` [PATCH 3/9] udf_rename(): only access the child content on cross-directory rename Al Viro
2023-11-23  9:22     ` Jan Kara
2023-11-22 19:36   ` [PATCH 4/9] ext2: Avoid reading renamed directory if parent does not change Al Viro
2023-11-22 19:36   ` [PATCH 5/9] ext4: don't access the source subdirectory content on same-directory rename Al Viro
2023-11-23  9:31     ` Jan Kara
2023-11-22 19:36   ` [PATCH 6/9] f2fs: Avoid reading renamed directory if parent does not change Al Viro
2023-11-22 19:36   ` [PATCH 7/9] rename(): fix the locking of subdirectories Al Viro
2023-11-23  9:50     ` Jan Kara
2023-11-22 19:36   ` [PATCH 8/9] kill lock_two_inodes() Al Viro
2023-11-23  9:53     ` Jan Kara
2023-11-22 19:36   ` [PATCH 9/9] rename(): avoid a deadlock in the case of parents having no common ancestor Al Viro
2023-11-23 10:36     ` Jan Kara [this message]
2023-11-24  6:15       ` Al Viro
2023-11-24  7:24     ` Amir Goldstein
2023-11-25 20:10 ` [PATCHES v2][CFT] rename deadlock fixes Al Viro
2023-11-25 20:11   ` [PATCH v2 1/9] reiserfs: Avoid touching renamed directory if parent does not change Al Viro
2023-11-25 20:11     ` [PATCH v2 2/9] ocfs2: " Al Viro
2023-11-25 20:11     ` [PATCH v2 3/9] udf_rename(): only access the child content on cross-directory rename Al Viro
2023-11-25 20:11     ` [PATCH v2 4/9] ext2: Avoid reading renamed directory if parent does not change Al Viro
2023-11-25 20:11     ` [PATCH v2 5/9] ext4: don't access the source subdirectory content on same-directory rename Al Viro
2023-11-25 20:11     ` [PATCH v2 6/9] f2fs: Avoid reading renamed directory if parent does not change Al Viro
2023-11-25 20:11     ` [PATCH v2 7/9] rename(): fix the locking of subdirectories Al Viro
2023-11-25 20:11     ` [PATCH v2 8/9] kill lock_two_inodes() Al Viro
2023-11-25 20:11     ` [PATCH v2 9/9] rename(): avoid a deadlock in the case of parents having no common ancestor Al Viro

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=20231123103622.4mfjwrmxr4tl53hi@quack3 \
    --to=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lostzoumo@gmail.com \
    --cc=torvalds@linux-foundation.org \
    --cc=viro@zeniv.linux.org.uk \
    /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;
as well as URLs for NNTP newsgroup(s).