Re: [PATCH v6 4/7] kernfs: switch kernfs to use an rwsem

From: Al Viro
Date: Fri Jun 11 2021 - 21:24:24 EST


On Wed, Jun 09, 2021 at 04:50:52PM +0800, Ian Kent wrote:
> The kernfs global lock restricts the ability to perform kernfs node
> lookup operations in parallel during path walks.
>
> Change the kernfs mutex to an rwsem so that, when opportunity arises,
> node searches can be done in parallel with path walk lookups.

> diff --git a/fs/kernfs/symlink.c b/fs/kernfs/symlink.c
> index 5432883d819f2..c8f8e41b84110 100644
> --- a/fs/kernfs/symlink.c
> +++ b/fs/kernfs/symlink.c
> @@ -116,9 +116,9 @@ static int kernfs_getlink(struct inode *inode, char *path)
> struct kernfs_node *target = kn->symlink.target_kn;
> int error;
>
> - mutex_lock(&kernfs_mutex);
> + down_read(&kernfs_rwsem);
> error = kernfs_get_target_path(parent, target, path);
> - mutex_unlock(&kernfs_mutex);
> + up_read(&kernfs_rwsem);

Unrelated to this patchset, two notes from reading through that area:
1) parent is fetched outside of rwsem. Unstable, IOW.
2) kernfs_get_target_path() is an atrocity. On *any* symlink you
get an arseload of ../ (up to kernfs root), followed by into whatever
directory we want. Even if the target is in the same directory.
Think what happens if you mount --bind a subtree that contains both the
symlink and its destination. And try to follow that symlink.
It really ought to generate the minimal relative pathname.
And it's not hard to do:
calculate the depth of source
calculate the depth of destination
walk up from the deeper one until we get to the depth of the
shallower one.
walk up from both in tandem until two paths converge.
Now we have the LCA of those nodes and can use the to generate the relative
pathname.