Skip to content

Commit e22553e

Browse files
Chris Masontorvalds
authored andcommitted
eventfd: don't take the spinlock in eventfd_poll
The spinlock in eventfd_poll is trying to protect the count of events so it can decide if it should return POLLIN, POLLERR, or POLLOUT. But, because of the way we drop the lock after calling poll_wait, and drop it again before returning, we have the same pile of races with the lock as we do with a single read of ctx->count(). This replaces the lock with a read barrier and single read. eventfd_write does a single bump of ctx->count, so this should not add new races with adding events. eventfd_read is similar, it will do a single decrement with the lock held, and so we're making the race with concurrent readers slightly larger. This spinlock is the top CPU user in kernel code during one of our workloads. Removing it gives us a ~2% boost. [arnd@arndb.de: avoid unused variable warning] [dan.carpenter@oracle.com: type bug in eventfd_poll()] Signed-off-by: Chris Mason <clm@fb.com> Cc: Davide Libenzi <davidel@xmailserver.org> Signed-off-by: Arnd Bergmann <arnd@arndb.de> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 7647f14 commit e22553e

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

‎fs/eventfd.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,18 @@ static unsigned int eventfd_poll(struct file *file, poll_table *wait)
118118
{
119119
struct eventfd_ctx *ctx = file->private_data;
120120
unsigned int events = 0;
121-
unsigned long flags;
121+
u64 count;
122122

123123
poll_wait(file, &ctx->wqh, wait);
124+
smp_rmb();
125+
count = ctx->count;
124126

125-
spin_lock_irqsave(&ctx->wqh.lock, flags);
126-
if (ctx->count > 0)
127+
if (count > 0)
127128
events |= POLLIN;
128-
if (ctx->count == ULLONG_MAX)
129+
if (count == ULLONG_MAX)
129130
events |= POLLERR;
130-
if (ULLONG_MAX - 1 > ctx->count)
131+
if (ULLONG_MAX - 1 > count)
131132
events |= POLLOUT;
132-
spin_unlock_irqrestore(&ctx->wqh.lock, flags);
133133

134134
return events;
135135
}

0 commit comments

Comments
 (0)