Skip to content

Commit

Permalink
kernfs: protect lazy kernfs_iattrs allocation with mutex
Browse files Browse the repository at this point in the history
kernfs_iattrs is allocated lazily when operations which require it
take place; unfortunately, the lazy allocation and returning weren't
properly synchronized and when there are multiple concurrent
operations, it might end up returning kernfs_iattrs which hasn't
finished initialization yet or different copies to different callers.

Fix it by synchronizing with a mutex.  This can be smarter with memory
barriers but let's go there if it actually turns out to be necessary.

Signed-off-by: Tejun Heo <tj@kernel.org>
Link: http://lkml.kernel.org/g/533ABA32.9080602@oracle.com
Reported-by: Sasha Levin <sasha.levin@oracle.com>
Cc: stable@vger.kernel.org # 3.14
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  • Loading branch information
Tejun Heo authored and Greg Kroah-Hartman committed Apr 16, 2014
1 parent a2a4dc4 commit 4afddd6
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions fs/kernfs/inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,18 @@ void __init kernfs_inode_init(void)

static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn)
{
static DEFINE_MUTEX(iattr_mutex);
struct kernfs_iattrs *ret;
struct iattr *iattrs;

mutex_lock(&iattr_mutex);

if (kn->iattr)
return kn->iattr;
goto out_unlock;

kn->iattr = kzalloc(sizeof(struct kernfs_iattrs), GFP_KERNEL);
if (!kn->iattr)
return NULL;
goto out_unlock;
iattrs = &kn->iattr->ia_iattr;

/* assign default attributes */
Expand All @@ -65,8 +69,10 @@ static struct kernfs_iattrs *kernfs_iattrs(struct kernfs_node *kn)
iattrs->ia_atime = iattrs->ia_mtime = iattrs->ia_ctime = CURRENT_TIME;

simple_xattrs_init(&kn->iattr->xattrs);

return kn->iattr;
out_unlock:
ret = kn->iattr;
mutex_unlock(&iattr_mutex);
return ret;
}

static int __kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
Expand Down

0 comments on commit 4afddd6

Please sign in to comment.