Skip to content

Commit

Permalink
anon_inodes: use fops->owner for module refcount
Browse files Browse the repository at this point in the history
There is an imbalance for anonymous inodes. If the fops->owner field is set,
the module reference count of owner is decreases on release.
("filp_close" --> "__fput" ---> "fops_put")

On the other hand, anon_inode_getfd does not increase the module reference
count of owner. This causes two problems:

- if owner is set, the module refcount goes negative
- if owner is not set, the module can be unloaded while code is running

This patch changes anon_inode_getfd to be symmetric regarding fops->owner
handling.

I have checked all existing users of anon_inode_getfd. Noone sets fops->owner,
thats why nobody has seen the module refcount negative. The refcounting was
tested with a patched and unpatched KVM module.(see patch 2/2) I also did an
epoll_open/close test.

Signed-off-by: Christian Borntraeger <borntraeger@de.ibm.com>
Reviewed-by: Davide Libenzi <davidel@xmailserver.org>
Signed-off-by: Avi Kivity <avi@redhat.com>
  • Loading branch information
Christian Borntraeger authored and Avi Kivity committed Dec 31, 2008
1 parent e93353c commit e3a2a0d
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion fs/anon_inodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,12 @@ int anon_inode_getfd(const char *name, const struct file_operations *fops,
if (IS_ERR(anon_inode_inode))
return -ENODEV;

if (fops->owner && !try_module_get(fops->owner))
return -ENOENT;

error = get_unused_fd_flags(flags);
if (error < 0)
return error;
goto err_module;
fd = error;

/*
Expand Down Expand Up @@ -128,6 +131,8 @@ int anon_inode_getfd(const char *name, const struct file_operations *fops,
dput(dentry);
err_put_unused_fd:
put_unused_fd(fd);
err_module:
module_put(fops->owner);
return error;
}
EXPORT_SYMBOL_GPL(anon_inode_getfd);
Expand Down

0 comments on commit e3a2a0d

Please sign in to comment.