eventfd: avoid unnecessary wakeups in eventfd_write()
PATCH 1/1 eventfd: avoid unnecessary wakeups in eventfd_write()
From: Wen Yang <[email protected]>
In eventfd_write(), when ucnt is 0 and ctx->count is also 0, current->in_eventfd will be set to 1, which may affect eventfd_signal(), and unnecessary wakeups will also be performed.
Fix this issue by ensuring that ctx->count is not zero.
--- fs/eventfd.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
fs/eventfd.c +6 -4
@@ -281,10 +281,12 @@ static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t c }if (likely (res >0 )) { ctx->count += ucnt; - current->in_eventfd =1 ; -if (waitqueue_active (&ctx->wqh)) -wake_up_locked_poll (&ctx->wqh,EPOLLIN ); - current->in_eventfd =0 ; +if (ctx->count) { + current->in_eventfd =1 ; +if (waitqueue_active (&ctx->wqh)) +wake_up_locked_poll (&ctx->wqh,EPOLLIN ); + current->in_eventfd =0 ; + } }spin_unlock_irq (&ctx->wqh.lock);
Signature · 1 line
2.25.1
Christian Brauner
maintainerQuoted 32 lines · On Thu, Jul 13, 2023 at 12:42:32AM +0800, [email protected] wrote:
From: Wen Yang <[email protected]>
In
eventfd_write(), when ucnt is 0 andctx->countis also 0,current->in_eventfdwill be set to 1, which may affecteventfd_signal(), and unnecessary wakeups will also be performed.Fix this issue by ensuring that
ctx->countis not zero.Signed-off-by Wen Yang <[email protected]>Cc Alexander Viro <[email protected]>Cc Jens Axboe <[email protected]>Cc Christian Brauner <[email protected]>Cc Christoph Hellwig <[email protected]>Cc Dylan Yudaken <[email protected]>Cc David Woodhouse <[email protected]>Cc Matthew Wilcox <[email protected]>--- fs/eventfd.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)fs/eventfd.c +0
-2@@ -281,10 +281,12 @@ static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t c }if (likely (res >0 )) { ctx->count += ucnt; - current->in_eventfd =1 ; -if (waitqueue_active (&ctx->wqh))
fs/eventfd.c static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t c · line 291
Quoted 10 lines
fs/eventfd.c +6
-2-wake_up_locked_poll (&ctx->wqh,EPOLLIN ); - current->in_eventfd =0 ; +if (ctx->count) { + current->in_eventfd =1 ; +if (waitqueue_active (&ctx->wqh)) +wake_up_locked_poll (&ctx->wqh,EPOLLIN ); + current->in_eventfd =0 ; + } }spin_unlock_irq (&ctx->wqh.lock);
I don't think we can do this. Consider the following:
struct pollfd pfd = {
.events = POLLIN | POLLOUT,
};
int fd = eventfd(0, 0);
if (fd < 0)
return -1;
write(fd, &w, sizeof(__u64));
poll(&pfd, 1, -1);
printf("%d\n", pfd.revents & POLLOUT);Currently, the eventfd_poll() will do:
ULLONG_MAX - 1 > ctx->count
informing pollers with POLLOUT that the eventfd is writable, iow, that the count has overflowed.
After your change such POLLOUT waiters will hang forever even though the eventfd is writable.
So currently, a zero write on an eventfd can be used to inform another process that they can write. This breaks this completely.
Caller's that don't want to be woken up on zero writes should just not set POLLOUT:
struct pollfd pfd = {
.events = POLLIN,
};
int fd = eventfd(0, 0);
if (fd < 0)
return -1;
write(fd, &w, sizeof(__u64));
poll(&pfd, 1, -1);This will wait until someone actually writes something.