Skip to content

Commit a484c3d

Browse files
bonzinitorvalds
authored andcommitted
eventfd: document lockless access in eventfd_poll
Since commit e22553e ("eventfd: don't take the spinlock in eventfd_poll", 2015-02-17), eventfd is reading ctx->count outside ctx->wqh.lock. However, things aren't as simple as the read barrier in eventfd_poll would suggest. In fact, the read barrier, besides lacking a comment, is not paired in any obvious manner with another read barrier, and it is pointless because it is sitting between a write (deep in poll_wait) and the read of ctx->count. The read barrier is acting just as a compiler barrier, for which we can use READ_ONCE instead. This is what the code change in this patch does. The documentation change is just as important, however. The question, posed by Andrea Arcangeli, is then why the thing is safe on architectures where spin_unlock does not imply a store-load memory barrier. The answer is that it's safe because writes of ctx->count use the same lock as poll_wait, and hence an acquire barrier implicit in poll_wait provides the necessary synchronization between eventfd_poll and callers of wake_up_locked_poll. This is sort of mentioned in the commit message with respect to eventfd_ctx_read ("eventfd_read is similar, it will do a single decrement with the lock held") but it applies to all other callers too. It's tricky enough that it should be documented in the code. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Reviewed-by: Andrea Arcangeli <aarcange@redhat.com> Cc: Chris Mason <clm@fb.com> Cc: Davide Libenzi <davidel@xmailserver.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 0335695 commit a484c3d

1 file changed

Lines changed: 40 additions & 2 deletions

File tree

‎fs/eventfd.c‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,46 @@ static unsigned int eventfd_poll(struct file *file, poll_table *wait)
121121
u64 count;
122122

123123
poll_wait(file, &ctx->wqh, wait);
124-
smp_rmb();
125-
count = ctx->count;
124+
125+
/*
126+
* All writes to ctx->count occur within ctx->wqh.lock. This read
127+
* can be done outside ctx->wqh.lock because we know that poll_wait
128+
* takes that lock (through add_wait_queue) if our caller will sleep.
129+
*
130+
* The read _can_ therefore seep into add_wait_queue's critical
131+
* section, but cannot move above it! add_wait_queue's spin_lock acts
132+
* as an acquire barrier and ensures that the read be ordered properly
133+
* against the writes. The following CAN happen and is safe:
134+
*
135+
* poll write
136+
* ----------------- ------------
137+
* lock ctx->wqh.lock (in poll_wait)
138+
* count = ctx->count
139+
* __add_wait_queue
140+
* unlock ctx->wqh.lock
141+
* lock ctx->qwh.lock
142+
* ctx->count += n
143+
* if (waitqueue_active)
144+
* wake_up_locked_poll
145+
* unlock ctx->qwh.lock
146+
* eventfd_poll returns 0
147+
*
148+
* but the following, which would miss a wakeup, cannot happen:
149+
*
150+
* poll write
151+
* ----------------- ------------
152+
* count = ctx->count (INVALID!)
153+
* lock ctx->qwh.lock
154+
* ctx->count += n
155+
* **waitqueue_active is false**
156+
* **no wake_up_locked_poll!**
157+
* unlock ctx->qwh.lock
158+
* lock ctx->wqh.lock (in poll_wait)
159+
* __add_wait_queue
160+
* unlock ctx->wqh.lock
161+
* eventfd_poll returns 0
162+
*/
163+
count = READ_ONCE(ctx->count);
126164

127165
if (count > 0)
128166
events |= POLLIN;

0 commit comments

Comments
 (0)