Re: [PATCH] 9p: add missing locking around taking dentry fid list

From: Christian Schoenebeck
Date: Thu May 23 2024 - 04:34:39 EST


On Wednesday, May 22, 2024 7:25:06 PM CEST Dominique Martinet wrote:
> Christian Schoenebeck wrote on Wed, May 22, 2024 at 04:35:19PM +0200:
[...]
> > > diff --git a/fs/9p/vfs_dentry.c b/fs/9p/vfs_dentry.c
> > > index f16f73581634..01338d4c2d9e 100644
> > > --- a/fs/9p/vfs_dentry.c
> > > +++ b/fs/9p/vfs_dentry.c
> > > @@ -48,12 +48,17 @@ static int v9fs_cached_dentry_delete(const struct dentry *dentry)
> > > static void v9fs_dentry_release(struct dentry *dentry)
> > > {
> > > struct hlist_node *p, *n;
> > > + struct hlist_head head;
> > >
> > > p9_debug(P9_DEBUG_VFS, " dentry: %pd (%p)\n",
> > > dentry, dentry);
> > > - hlist_for_each_safe(p, n, (struct hlist_head *)&dentry->d_fsdata)
> > > +
> > > + spin_lock(&dentry->d_lock);
> > > + hlist_move_list((struct hlist_head *)&dentry->d_fsdata, &head);
> > > + spin_unlock(&dentry->d_lock);
> > > +
> > > + hlist_for_each_safe(p, n, &head)
> > > p9_fid_put(hlist_entry(p, struct p9_fid, dlist));
> > > - dentry->d_fsdata = NULL;
> > > }
> >
> > I'm not sure if that works out. So you are moving the list from dentry to a
> > local variable. But if you look at v9fs_fid_find() [fs/9p/fid.c#123] it reads
> > dentry->d_fsdata (twice) and holds it as local variable before taking a
> > lock. So the lock in v9fs_fid_find() should happen earlier, no?
>
> The comment still works -- if detry->d_fsdata is NULL then
> hlist_for_each_entry will stop short and not iterate over anything (it
> won't bug out), so that part is fine in my opinion.

I meant the opposite: dentry->d_fsdata not being NULL. In this case
v9fs_fid_find() takes a local copy of the list head pointer as `h` without
taking a lock before.

Then v9fs_fid_find() takes the lock to run hlist_for_each_entry(), but at this
point `h` could already point at garbage.

/Christian