linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] eventfd: clean up unneeded cruft
@ 2018-01-06 17:45 Eric Biggers
  2018-01-06 17:45 ` [PATCH 1/3] eventfd: convert to use anon_inode_getfd() Eric Biggers
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Eric Biggers @ 2018-01-06 17:45 UTC (permalink / raw)
  To: linux-fsdevel; +Cc: linux-kernel, Andrew Morton, Davide Libenzi, Eric Biggers

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(-)

-- 
2.15.1

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/3] eventfd: convert to use anon_inode_getfd()
  2018-01-06 17:45 [PATCH 0/3] eventfd: clean up unneeded cruft Eric Biggers
@ 2018-01-06 17:45 ` Eric Biggers
  2018-01-06 17:45 ` [PATCH 2/3] eventfd: fold eventfd_ctx_read() into eventfd_read() Eric Biggers
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: Eric Biggers @ 2018-01-06 17:45 UTC (permalink / raw)
  To: linux-fsdevel; +Cc: linux-kernel, Andrew Morton, Davide Libenzi, Eric Biggers

From: Eric Biggers <ebiggers@google.com>

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.)

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/eventfd.c            | 53 +++++++------------------------------------------
 include/linux/eventfd.h |  5 -----
 2 files changed, 7 insertions(+), 51 deletions(-)

diff --git a/fs/eventfd.c b/fs/eventfd.c
index 2fb4eadaa118..4167e670ed4d 100644
--- a/fs/eventfd.c
+++ b/fs/eventfd.c
@@ -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)
diff --git a/include/linux/eventfd.h b/include/linux/eventfd.h
index 60b2985e8a18..15826192cc23 100644
--- a/include/linux/eventfd.h
+++ b/include/linux/eventfd.h
@@ -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)
 {
-- 
2.15.1

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/3] eventfd: fold eventfd_ctx_read() into eventfd_read()
  2018-01-06 17:45 [PATCH 0/3] eventfd: clean up unneeded cruft Eric Biggers
  2018-01-06 17:45 ` [PATCH 1/3] eventfd: convert to use anon_inode_getfd() Eric Biggers
@ 2018-01-06 17:45 ` Eric Biggers
  2018-01-06 17:45 ` [PATCH 3/3] eventfd: fold eventfd_ctx_get() into eventfd_ctx_fileget() Eric Biggers
  2018-01-06 18:46 ` [PATCH 0/3] eventfd: clean up unneeded cruft Al Viro
  3 siblings, 0 replies; 7+ messages in thread
From: Eric Biggers @ 2018-01-06 17:45 UTC (permalink / raw)
  To: linux-fsdevel; +Cc: linux-kernel, Andrew Morton, Davide Libenzi, Eric Biggers

From: Eric Biggers <ebiggers@google.com>

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.)

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/eventfd.c            | 53 ++++++++++++++-----------------------------------
 include/linux/eventfd.h |  7 -------
 2 files changed, 15 insertions(+), 45 deletions(-)

diff --git a/fs/eventfd.c b/fs/eventfd.c
index 4167e670ed4d..6138d2b5cdeb 100644
--- a/fs/eventfd.c
+++ b/fs/eventfd.c
@@ -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,
diff --git a/include/linux/eventfd.h b/include/linux/eventfd.h
index 15826192cc23..566fef14d0a6 100644
--- a/include/linux/eventfd.h
+++ b/include/linux/eventfd.h
@@ -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)
 {
-- 
2.15.1

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 3/3] eventfd: fold eventfd_ctx_get() into eventfd_ctx_fileget()
  2018-01-06 17:45 [PATCH 0/3] eventfd: clean up unneeded cruft Eric Biggers
  2018-01-06 17:45 ` [PATCH 1/3] eventfd: convert to use anon_inode_getfd() Eric Biggers
  2018-01-06 17:45 ` [PATCH 2/3] eventfd: fold eventfd_ctx_read() into eventfd_read() Eric Biggers
@ 2018-01-06 17:45 ` Eric Biggers
  2018-01-06 18:46 ` [PATCH 0/3] eventfd: clean up unneeded cruft Al Viro
  3 siblings, 0 replies; 7+ messages in thread
From: Eric Biggers @ 2018-01-06 17:45 UTC (permalink / raw)
  To: linux-fsdevel; +Cc: linux-kernel, Andrew Morton, Davide Libenzi, Eric Biggers

From: Eric Biggers <ebiggers@google.com>

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.)

Signed-off-by: Eric Biggers <ebiggers@google.com>
---
 fs/eventfd.c            | 21 ++++++---------------
 include/linux/eventfd.h |  2 +-
 2 files changed, 7 insertions(+), 16 deletions(-)

diff --git a/fs/eventfd.c b/fs/eventfd.c
index 6138d2b5cdeb..bc0105ae253f 100644
--- a/fs/eventfd.c
+++ b/fs/eventfd.c
@@ -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);
 
diff --git a/include/linux/eventfd.h b/include/linux/eventfd.h
index 566fef14d0a6..7094718b653b 100644
--- a/include/linux/eventfd.h
+++ b/include/linux/eventfd.h
@@ -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);
-- 
2.15.1

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/3] eventfd: clean up unneeded cruft
  2018-01-06 17:45 [PATCH 0/3] eventfd: clean up unneeded cruft Eric Biggers
                   ` (2 preceding siblings ...)
  2018-01-06 17:45 ` [PATCH 3/3] eventfd: fold eventfd_ctx_get() into eventfd_ctx_fileget() Eric Biggers
@ 2018-01-06 18:46 ` Al Viro
  2018-01-06 19:00   ` Al Viro
  3 siblings, 1 reply; 7+ messages in thread
From: Al Viro @ 2018-01-06 18:46 UTC (permalink / raw)
  To: Eric Biggers; +Cc: linux-fsdevel, linux-kernel, Andrew Morton, Davide Libenzi

On Sat, Jan 06, 2018 at 09:45:41AM -0800, Eric Biggers wrote:
> 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.

Applied.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/3] eventfd: clean up unneeded cruft
  2018-01-06 18:46 ` [PATCH 0/3] eventfd: clean up unneeded cruft Al Viro
@ 2018-01-06 19:00   ` Al Viro
  2018-01-06 21:53     ` Eric Biggers
  0 siblings, 1 reply; 7+ messages in thread
From: Al Viro @ 2018-01-06 19:00 UTC (permalink / raw)
  To: Eric Biggers; +Cc: linux-fsdevel, linux-kernel, Andrew Morton, Davide Libenzi

On Sat, Jan 06, 2018 at 06:46:19PM +0000, Al Viro wrote:
> On Sat, Jan 06, 2018 at 09:45:41AM -0800, Eric Biggers wrote:
> > 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.
> 
> 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.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/3] eventfd: clean up unneeded cruft
  2018-01-06 19:00   ` Al Viro
@ 2018-01-06 21:53     ` Eric Biggers
  0 siblings, 0 replies; 7+ messages in thread
From: Eric Biggers @ 2018-01-06 21:53 UTC (permalink / raw)
  To: Al Viro; +Cc: linux-fsdevel, linux-kernel, Andrew Morton, Davide Libenzi

On Sat, Jan 06, 2018 at 07:00:48PM +0000, Al Viro wrote:
> On Sat, Jan 06, 2018 at 06:46:19PM +0000, Al Viro wrote:
> > On Sat, Jan 06, 2018 at 09:45:41AM -0800, Eric Biggers wrote:
> > > 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.
> > 
> > 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.

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2018-01-06 21:53 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-06 17:45 [PATCH 0/3] eventfd: clean up unneeded cruft Eric Biggers
2018-01-06 17:45 ` [PATCH 1/3] eventfd: convert to use anon_inode_getfd() Eric Biggers
2018-01-06 17:45 ` [PATCH 2/3] eventfd: fold eventfd_ctx_read() into eventfd_read() Eric Biggers
2018-01-06 17:45 ` [PATCH 3/3] eventfd: fold eventfd_ctx_get() into eventfd_ctx_fileget() Eric Biggers
2018-01-06 18:46 ` [PATCH 0/3] eventfd: clean up unneeded cruft Al Viro
2018-01-06 19:00   ` Al Viro
2018-01-06 21:53     ` Eric Biggers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).