Skip to contentSkip to conversation

eventfd: use wait_event_interruptible_locked_irq() helper

PATCH RESEND Applied [email protected] 3 participants 5 messages Apr 5, 2023 – Apr 6, 2023 VFS

lore
Share & tools
lore
#0 lore raw

PATCH RESEND v2 1/1 eventfd: use wait_event_interruptible_locked_irq() helper

From: Wen Yang <[email protected]>

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.

Cc Alexander Viro <[email protected]>
Cc Christoph Hellwig <[email protected]>
Cc Dylan Yudaken <[email protected]>
Cc Jens Axboe <[email protected]>
Cc David Woodhouse <[email protected]>
Cc Fu Wei <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Michal Nazarewicz <[email protected]>
Cc Matthew Wilcox <[email protected]>
---
 fs/eventfd.c | 41 +++++++----------------------------------
 1 file changed, 7 insertions(+), 34 deletions(-)
fs/eventfd.c +7 -34
index 249ca6c0b784..95850a13ce8d 100644
@@ -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,10 @@ 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);
+		if (!res)
+			res = sizeof(ucnt);
 	}
 	if (likely(res > 0)) {
 		ctx->count += ucnt;
Signature · 1 line
2.37.2
#1 lore raw
Quoted 10 lines · On 4/5/23 1:20 PM, [email protected] wrote:

From: Wen Yang <[email protected]>

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.

Looks pretty reasonable to me. How did you test it?

Signature · 1 line
Jens Axboe
author
#2 lore raw
Quoted 10 lines · Jens Axboe
Quoted 9 lines · On 4/5/23 1:20 PM, [email protected] wrote:

From: Wen Yang <[email protected]>

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.

Quoted 3 lines
Quoted 1 line

No functional change intended.

Looks pretty reasonable to me. How did you test it?

Thanks.

We have verified it in some local testing environments, and the intel-lab-lkp has also tested it more than a month, as follows:

https://github.com/intel-lab-lkp/linux/tree/wenyang-linux-foxmail-com/eventfd-use-wait_event_interruptible_locked_irq-helper/20230217-023039

--

Best wishes,

Wen

Reviewed-by
#3 lore raw
Quoted 12 lines · Wen Yang
Quoted 10 lines · Jens Axboe
Quoted 9 lines · On 4/5/23 1:20 PM, [email protected] wrote:

From: Wen Yang <[email protected]>

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.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

No functional change intended.

Looks pretty reasonable to me. How did you test it?

Thanks.

We have verified it in some local testing environments, and the intel-lab-lkp has also tested it more than a month, as follows:

https://github.com/intel-lab-lkp/linux/tree/wenyang-linux-foxmail-com/eventfd-use-wait_event_interruptible_locked_irq-helper/20230217-023039

Nice, you can add my:

to it as well. Adding Christian as well, as I guess this should go through his/al/vfs tree.

Signature · 1 line
Jens Axboe
maintainer Applied
#4 lore raw
In reply to [email protected]
Quoted 8 lines · On Thu, 06 Apr 2023 03:20:02 +0800, [email protected] wrote:

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.

[...]

I ran LTP with ./runltp -f syscalls -s eventfd passes and aligns with what was done for timerfd.

Applied, thanks!

tree: git://git.kernel.org/pub/scm/linux/kernel/git/vfs/idmapping.git
branch: fs.misc
[1/1] eventfd: use wait_event_interruptible_locked_irq() helper

      commit: 113348a44b8622b497fb884f41c8659481ad0b04