diff mbox series

eventfd: use wait_event_interruptible_locked_irq() helper

Message ID tencent_47F9893DA354D9509F06DD4C52A7EB30130A@qq.com (mailing list archive)
State New, archived
Headers show
Series eventfd: use wait_event_interruptible_locked_irq() helper | expand

Commit Message

Wen Yang Feb. 16, 2023, 1:17 p.m. UTC
From: Wen Yang <wenyang.linux@foxmail.com>

wait_event_interruptible_locked_irq was introduced by commit 22c43c81a51e
("wait_event_interruptible_locked() interface"), but older code such as
eventfd_{write,read} still uses the open code implementation.
Inspired by commit 8120a8aadb20
("fs/timerfd.c: make use of wait_event_interruptible_locked_irq()"), this
patch replaces the open code implementation with a single macro call.

No functional change intended.

Signed-off-by: Wen Yang <wenyang.linux@foxmail.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Dylan Yudaken <dylany@fb.com>
Cc: Jens Axboe <axboe@kernel.dk>
Cc: David Woodhouse <dwmw@amazon.co.uk>
Cc: Fu Wei <wefu@redhat.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Michal Nazarewicz <m.nazarewicz@samsung.com>
Cc: linux-fsdevel@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
---
 fs/eventfd.c | 40 ++++++----------------------------------
 1 file changed, 6 insertions(+), 34 deletions(-)

Comments

Matthew Wilcox Feb. 16, 2023, 1:29 p.m. UTC | #1
On Thu, Feb 16, 2023 at 09:17:39PM +0800, wenyang.linux@foxmail.com wrote:
> +		res = wait_event_interruptible_locked_irq(
> +				ctx->wqh, ULLONG_MAX - ctx->count > ucnt) ?
> +			-ERESTARTSYS : sizeof(ucnt);

You've broken the line here in a weird way.  I'd've done it as:

		res = wait_event_interruptible_locked_irq(ctx->wqh,
				ULLONG_MAX - ctx->count > ucnt) ?
					-ERESTARTSYS : sizeof(ucnt));

... also the patch you've sent here doesn't even compile.  Have you
tested it?
Matthew Wilcox Feb. 16, 2023, 1:33 p.m. UTC | #2
On Thu, Feb 16, 2023 at 01:29:00PM +0000, Matthew Wilcox wrote:
> On Thu, Feb 16, 2023 at 09:17:39PM +0800, wenyang.linux@foxmail.com wrote:
> > +		res = wait_event_interruptible_locked_irq(
> > +				ctx->wqh, ULLONG_MAX - ctx->count > ucnt) ?
> > +			-ERESTARTSYS : sizeof(ucnt);
> 
> You've broken the line here in a weird way.  I'd've done it as:
> 
> 		res = wait_event_interruptible_locked_irq(ctx->wqh,
> 				ULLONG_MAX - ctx->count > ucnt) ?
> 					-ERESTARTSYS : sizeof(ucnt));
> 
> ... also the patch you've sent here doesn't even compile.  Have you
> tested it?

Sorry, I misread it.  But I would have avoided use of the ?: operator
here ...

		res = wait_event_interruptible_locked_irq(ctx->wqh,
				ULLONG_MAX - ctx->count > ucnt);
		if (res == 0)
			res = sizeof(ucnt);
diff mbox series

Patch

diff --git a/fs/eventfd.c b/fs/eventfd.c
index 249ca6c0b784..2b6a8a4d80a1 100644
--- a/fs/eventfd.c
+++ b/fs/eventfd.c
@@ -228,7 +228,6 @@  static ssize_t eventfd_read(struct kiocb *iocb, struct iov_iter *to)
 	struct file *file = iocb->ki_filp;
 	struct eventfd_ctx *ctx = file->private_data;
 	__u64 ucnt = 0;
-	DECLARE_WAITQUEUE(wait, current);
 
 	if (iov_iter_count(to) < sizeof(ucnt))
 		return -EINVAL;
@@ -239,23 +238,11 @@  static ssize_t eventfd_read(struct kiocb *iocb, struct iov_iter *to)
 			spin_unlock_irq(&ctx->wqh.lock);
 			return -EAGAIN;
 		}
-		__add_wait_queue(&ctx->wqh, &wait);
-		for (;;) {
-			set_current_state(TASK_INTERRUPTIBLE);
-			if (ctx->count)
-				break;
-			if (signal_pending(current)) {
-				__remove_wait_queue(&ctx->wqh, &wait);
-				__set_current_state(TASK_RUNNING);
-				spin_unlock_irq(&ctx->wqh.lock);
-				return -ERESTARTSYS;
-			}
+
+		if (wait_event_interruptible_locked_irq(ctx->wqh, ctx->count)) {
 			spin_unlock_irq(&ctx->wqh.lock);
-			schedule();
-			spin_lock_irq(&ctx->wqh.lock);
+			return -ERESTARTSYS;
 		}
-		__remove_wait_queue(&ctx->wqh, &wait);
-		__set_current_state(TASK_RUNNING);
 	}
 	eventfd_ctx_do_read(ctx, &ucnt);
 	current->in_eventfd = 1;
@@ -275,7 +262,6 @@  static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t c
 	struct eventfd_ctx *ctx = file->private_data;
 	ssize_t res;
 	__u64 ucnt;
-	DECLARE_WAITQUEUE(wait, current);
 
 	if (count < sizeof(ucnt))
 		return -EINVAL;
@@ -288,23 +274,9 @@  static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t c
 	if (ULLONG_MAX - ctx->count > ucnt)
 		res = sizeof(ucnt);
 	else if (!(file->f_flags & O_NONBLOCK)) {
-		__add_wait_queue(&ctx->wqh, &wait);
-		for (res = 0;;) {
-			set_current_state(TASK_INTERRUPTIBLE);
-			if (ULLONG_MAX - ctx->count > ucnt) {
-				res = sizeof(ucnt);
-				break;
-			}
-			if (signal_pending(current)) {
-				res = -ERESTARTSYS;
-				break;
-			}
-			spin_unlock_irq(&ctx->wqh.lock);
-			schedule();
-			spin_lock_irq(&ctx->wqh.lock);
-		}
-		__remove_wait_queue(&ctx->wqh, &wait);
-		__set_current_state(TASK_RUNNING);
+		res = wait_event_interruptible_locked_irq(
+				ctx->wqh, ULLONG_MAX - ctx->count > ucnt) ?
+			-ERESTARTSYS : sizeof(ucnt);
 	}
 	if (likely(res > 0)) {
 		ctx->count += ucnt;