eventfd: clean up unneeded cruft
cover eventfd: clean up unneeded cruft
This series removes some cruft (mainly exported functions) from fs/eventfd.c which seems to have been added while the KVM irqfd feature was under development some years ago, but was never actually used.
Eric Biggers (3):
eventfd: convert to use anon_inode_getfd()
eventfd: fold eventfd_ctx_read() into eventfd_read()
eventfd: fold eventfd_ctx_get() into eventfd_ctx_fileget()
fs/eventfd.c | 127 +++++++++++------------------------------------- include/linux/eventfd.h | 14 +----- 2 files changed, 29 insertions(+), 112 deletions(-)
Signature · 1 line
2.15.1
PATCH 1/3 eventfd: convert to use anon_inode_getfd()
From: Eric Biggers <[email protected]>
Nothing actually calls eventfd_file_create() besides the eventfd2() system call itself. So simplify things by folding it into the system call and using anon_inode_getfd() instead of anon_inode_getfile(). This removes over 40 lines with no change in functionality.
(eventfd_file_create() was apparently added years ago for KVM irqfd's, but was never used.)
--- fs/eventfd.c | 53 +++++++------------------------------------------ include/linux/eventfd.h | 5 ----- 2 files changed, 7 insertions(+), 51 deletions(-)
fs/eventfd.c +7 -46
@@ -412,72 +412,33 @@ struct eventfd_ctx *eventfd_ctx_fileget(struct file *file) }EXPORT_SYMBOL_GPL (eventfd_ctx_fileget); -/** -* eventfd_file_create - Creates an eventfd file pointer. -* @count: Initial eventfd counter value. -* @flags: Flags for the eventfd file. -* -* This function creates an eventfd file pointer, w/out installing it into -* the fd table. This is useful when the eventfd file is used during the -* initialization of data structures that require extra setup after the eventfd -* creation. So the eventfd creation is split into the file pointer creation -* phase, and the file descriptor installation phase. -* In this way races with userspace closing the newly installed file descriptor -* can be avoided. -* Returns an eventfd file pointer, or a proper error pointer. -*/ -struct file *eventfd_file_create (unsigned int count,int flags) +SYSCALL_DEFINE2(eventfd2, unsigned int, count, int, flags) { -struct file *file;struct eventfd_ctx *ctx; +int fd;/* Check the EFD_* constants for consistency. */ BUILD_BUG_ON (EFD_CLOEXEC !=O_CLOEXEC );BUILD_BUG_ON (EFD_NONBLOCK !=O_NONBLOCK );if (flags & ~EFD_FLAGS_SET ) -return ERR_PTR (-EINVAL ); +return -EINVAL ; ctx =kmalloc (sizeof (*ctx),GFP_KERNEL );if (!ctx) -return ERR_PTR (-ENOMEM ); +return -ENOMEM ;kref_init (&ctx->kref);init_waitqueue_head (&ctx->wqh); ctx->count = count; ctx->flags = flags; - file =anon_inode_getfile ("[eventfd]" , &eventfd_fops, ctx, -O_RDWR | (flags &EFD_SHARED_FCNTL_FLAGS )); -if (IS_ERR (file)) + fd =anon_inode_getfd ("[eventfd]" , &eventfd_fops, ctx, +O_RDWR | (flags &EFD_SHARED_FCNTL_FLAGS )); +if (fd <0 )eventfd_free_ctx (ctx); -return file; -} - -SYSCALL_DEFINE2(eventfd2, unsigned int, count, int, flags) -{ -int fd, error; -struct file *file; - - error =get_unused_fd_flags (flags &EFD_SHARED_FCNTL_FLAGS ); -if (error <0 ) -return error; - fd = error; - - file =eventfd_file_create (count, flags); -if (IS_ERR (file)) { - error =PTR_ERR (file); - goto err_put_unused_fd; - } -fd_install (fd, file); -return fd; - -err_put_unused_fd: -put_unused_fd (fd); - -return error; } SYSCALL_DEFINE1(eventfd, unsigned int, count)
include/linux/eventfd.h +0 -5
@@ -30,7 +30,6 @@ struct file;#ifdef CONFIG_EVENTFD -struct file *eventfd_file_create (unsigned int count,int flags);struct eventfd_ctx *eventfd_ctx_get (struct eventfd_ctx *ctx);void eventfd_ctx_put (struct eventfd_ctx *ctx);struct file *eventfd_fget (int fd);
@@ -47,10 +46,6 @@ int eventfd_ctx_remove_wait_queue(struct eventfd_ctx *ctx, wait_queue_entry_t *w* Ugly ugly ugly error layer to support modules that uses eventfd but * pretend to work in !CONFIG_EVENTFD configurations. Namely, AIO. */ -static inline struct file *eventfd_file_create (unsigned int count,int flags) -{ -return ERR_PTR (-ENOSYS ); -}static inline struct eventfd_ctx *eventfd_ctx_fdget (int fd) {
Signature · 1 line
2.15.1
PATCH 2/3 eventfd: fold eventfd_ctx_read() into eventfd_read()
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
eventfd_ctx_read() is not used outside of eventfd.c, so unexport it and fold it into eventfd_read(). This slightly simplifies the code and makes it more analogous to eventfd_write().
(eventfd_ctx_read() was apparently added years ago for KVM irqfd's, but was never used.)
--- fs/eventfd.c | 53 ++++++++++++++----------------------------------- include/linux/eventfd.h | 7 ------- 2 files changed, 15 insertions(+), 45 deletions(-)
fs/eventfd.c +15 -38
@@ -207,36 +207,27 @@ int eventfd_ctx_remove_wait_queue(struct eventfd_ctx *ctx, wait_queue_entry_t *w }EXPORT_SYMBOL_GPL (eventfd_ctx_remove_wait_queue); -/** -* eventfd_ctx_read - Reads the eventfd counter or wait if it is zero. -* @ctx: [in] Pointer to eventfd context. -* @no_wait: [in] Different from zero if the operation should not block. -* @cnt: [out] Pointer to the 64-bit counter value. -* -* Returns %0 if successful, or the following error codes: -* -* - -EAGAIN : The operation would have blocked but @no_wait was non-zero. -* - -ERESTARTSYS : A signal interrupted the wait operation. -* -* If @no_wait is zero, the function might sleep until the eventfd internal -* counter becomes greater than zero. -*/ -ssize_t eventfd_ctx_read (struct eventfd_ctx *ctx,int no_wait,__u64 *cnt) +static ssize_t eventfd_read (struct file *file,char __user *buf,size_t count, +loff_t *ppos) { +struct eventfd_ctx *ctx = file->private_data;ssize_t res; +__u64 ucnt =0 ;DECLARE_WAITQUEUE (wait, current); +if (count <sizeof (ucnt)) +return -EINVAL ; +spin_lock_irq (&ctx->wqh.lock); - *cnt =0 ; res = -EAGAIN ;if (ctx->count >0 ) - res =0 ; -else if (!no_wait) { + res =sizeof (ucnt); +else if (!(file->f_flags &O_NONBLOCK )) {__add_wait_queue (&ctx->wqh, &wait);for (;;) {set_current_state (TASK_INTERRUPTIBLE );if (ctx->count >0 ) { - res =0 ; + res =sizeof (ucnt);break ; }if (signal_pending (current)) {
@@ -250,31 +241,17 @@ ssize_t eventfd_ctx_read(struct eventfd_ctx *ctx, int no_wait, __u64 *cnt)__remove_wait_queue (&ctx->wqh, &wait);__set_current_state (TASK_RUNNING ); } -if (likely (res ==0 )) { -eventfd_ctx_do_read (ctx, cnt); +if (likely (res >0 )) { +eventfd_ctx_do_read (ctx, &ucnt);if (waitqueue_active (&ctx->wqh))wake_up_locked_poll (&ctx->wqh,POLLOUT ); }spin_unlock_irq (&ctx->wqh.lock); -return res; -} -EXPORT_SYMBOL_GPL (eventfd_ctx_read); - -static ssize_t eventfd_read (struct file *file,char __user *buf,size_t count, -loff_t *ppos) -{ -struct eventfd_ctx *ctx = file->private_data; -ssize_t res; -__u64 cnt; - -if (count <sizeof (cnt)) -return -EINVAL ; - res =eventfd_ctx_read (ctx, file->f_flags &O_NONBLOCK , &cnt); -if (res <0 ) -return res; +if (res >0 &&put_user (ucnt, (__u64 __user *)buf)) +return -EFAULT ; -return put_user (cnt, (__u64 __user *) buf) ? -EFAULT :sizeof (cnt); +return res; } static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t count,
include/linux/eventfd.h +0 -7
@@ -36,7 +36,6 @@ struct file *eventfd_fget(int fd);struct eventfd_ctx *eventfd_ctx_fdget (int fd);struct eventfd_ctx *eventfd_ctx_fileget (struct file *file);__u64 eventfd_signal (struct eventfd_ctx *ctx,__u64 n); -ssize_t eventfd_ctx_read (struct eventfd_ctx *ctx,int no_wait,__u64 *cnt);int eventfd_ctx_remove_wait_queue (struct eventfd_ctx *ctx,wait_queue_entry_t *wait,__u64 *cnt);
@@ -62,12 +61,6 @@ static inline void eventfd_ctx_put(struct eventfd_ctx *ctx) } -static inline ssize_t eventfd_ctx_read (struct eventfd_ctx *ctx,int no_wait, -__u64 *cnt) -{ -return -ENOSYS ; -} -static inline int eventfd_ctx_remove_wait_queue (struct eventfd_ctx *ctx,wait_queue_entry_t *wait,__u64 *cnt) {
Signature · 1 line
2.15.1
PATCH 3/3 eventfd: fold eventfd_ctx_get() into eventfd_ctx_fileget()
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
eventfd_ctx_get() is not used outside of eventfd.c, so unexport it and fold it into eventfd_ctx_fileget().
(eventfd_ctx_get() was apparently added years ago for KVM irqfd's, but was never used.)
--- fs/eventfd.c | 21 ++++++--------------- include/linux/eventfd.h | 2 +- 2 files changed, 7 insertions(+), 16 deletions(-)
fs/eventfd.c +6 -15
@@ -79,25 +79,12 @@ static void eventfd_free(struct kref *kref)eventfd_free_ctx (ctx); } -/** -* eventfd_ctx_get - Acquires a reference to the internal eventfd context. -* @ctx: [in] Pointer to the eventfd context. -* -* Returns: In case of success, returns a pointer to the eventfd context. -*/ -struct eventfd_ctx *eventfd_ctx_get (struct eventfd_ctx *ctx) -{ -kref_get (&ctx->kref); -return ctx; -} -EXPORT_SYMBOL_GPL (eventfd_ctx_get); -/** * eventfd_ctx_put - Releases a reference to the internal eventfd context. * @ctx: [in] Pointer to eventfd context. * * The eventfd context reference must have been previously acquired either -* with eventfd_ctx_get() or eventfd_ctx_fdget(). +* with eventfd_ctx_fdget() or eventfd_ctx_fileget(). */ void eventfd_ctx_put (struct eventfd_ctx *ctx) {
@@ -382,10 +369,14 @@ EXPORT_SYMBOL_GPL(eventfd_ctx_fdget);*/ struct eventfd_ctx *eventfd_ctx_fileget (struct file *file) { +struct eventfd_ctx *ctx; +if (file->f_op != &eventfd_fops)return ERR_PTR (-EINVAL ); -return eventfd_ctx_get (file->private_data); + ctx = file->private_data; +kref_get (&ctx->kref); +return ctx; }EXPORT_SYMBOL_GPL (eventfd_ctx_fileget);
include/linux/eventfd.h +1 -1
@@ -26,11 +26,11 @@#define EFD_SHARED_FCNTL_FLAGS (O_CLOEXEC | O_NONBLOCK)#define EFD_FLAGS_SET (EFD_SHARED_FCNTL_FLAGS | EFD_SEMAPHORE) +struct eventfd_ctx ;struct file ;#ifdef CONFIG_EVENTFD -struct eventfd_ctx *eventfd_ctx_get (struct eventfd_ctx *ctx);void eventfd_ctx_put (struct eventfd_ctx *ctx);struct file *eventfd_fget (int fd);struct eventfd_ctx *eventfd_ctx_fdget (int fd);
Signature · 1 line
2.15.1
Quoted 3 lines · Eric Biggers
This series removes some cruft (mainly exported functions) from
fs/eventfd.cwhich seems to have been added while the KVM irqfd feature was under development some years ago, but was never actually used.
Applied.
Al Viro
maintainerQuoted 6 lines · Al Viro
Quoted 3 lines · Eric Biggers
This series removes some cruft (mainly exported functions) from
fs/eventfd.cwhich seems to have been added while the KVM irqfd feature was under development some years ago, but was never actually used.Applied.
Said that, is there any reason for keeping e.g. vhost_dev->log_file around? We already keep ->log_ctx, so... Ditto for vq->call and vq->error, and if we did it for vq->kick as well, we'd be able to get rid of separate eventfd_fget() as well.
Quoted 6 lines · Al Viro
Quoted 5 lines · Al Viro
Quoted 3 lines · Eric Biggers
This series removes some cruft (mainly exported functions) from
fs/eventfd.cwhich seems to have been added while the KVM irqfd feature was under development some years ago, but was never actually used.
Quoted 7 lines
Quoted 1 line
Applied.
Said that, is there any reason for keeping e.g.
vhost_dev->log_filearound? We already keep ->log_ctx, so... Ditto forvq->callandvq->error, and if we did it forvq->kickas well, we'd be able to get rid of separateeventfd_fget()as well.
We can remove vhost_dev->log_file, vq->call, and vq->error since for those only the eventfd_ctx is needed (they are only used to send notifications). But vq->kick is different because it doesn't use the eventfd_ctx but rather ->poll()s the file. I'll probably leave that part as-is, but perhaps the VHOST_SET_VRING_KICK ioctl should be changed to allow any file with a ->poll() method, since it doesn't actually use any eventfd-specific functionality of the file.
Eric