@@ -5462,23 +5462,41 @@ static void io_init_poll_iocb(struct io_poll_iocb *poll, __poll_t events,
54625462
54635463static inline void io_poll_remove_entry (struct io_poll_iocb * poll )
54645464{
5465- struct wait_queue_head * head = poll -> head ;
5465+ struct wait_queue_head * head = smp_load_acquire ( & poll -> head ) ;
54665466
5467- spin_lock_irq (& head -> lock );
5468- list_del_init (& poll -> wait .entry );
5469- poll -> head = NULL ;
5470- spin_unlock_irq (& head -> lock );
5467+ if (head ) {
5468+ spin_lock_irq (& head -> lock );
5469+ list_del_init (& poll -> wait .entry );
5470+ poll -> head = NULL ;
5471+ spin_unlock_irq (& head -> lock );
5472+ }
54715473}
54725474
54735475static void io_poll_remove_entries (struct io_kiocb * req )
54745476{
54755477 struct io_poll_iocb * poll = io_poll_get_single (req );
54765478 struct io_poll_iocb * poll_double = io_poll_get_double (req );
54775479
5478- if (poll -> head )
5479- io_poll_remove_entry (poll );
5480- if (poll_double && poll_double -> head )
5480+ /*
5481+ * While we hold the waitqueue lock and the waitqueue is nonempty,
5482+ * wake_up_pollfree() will wait for us. However, taking the waitqueue
5483+ * lock in the first place can race with the waitqueue being freed.
5484+ *
5485+ * We solve this as eventpoll does: by taking advantage of the fact that
5486+ * all users of wake_up_pollfree() will RCU-delay the actual free. If
5487+ * we enter rcu_read_lock() and see that the pointer to the queue is
5488+ * non-NULL, we can then lock it without the memory being freed out from
5489+ * under us.
5490+ *
5491+ * Keep holding rcu_read_lock() as long as we hold the queue lock, in
5492+ * case the caller deletes the entry from the queue, leaving it empty.
5493+ * In that case, only RCU prevents the queue memory from being freed.
5494+ */
5495+ rcu_read_lock ();
5496+ io_poll_remove_entry (poll );
5497+ if (poll_double )
54815498 io_poll_remove_entry (poll_double );
5499+ rcu_read_unlock ();
54825500}
54835501
54845502/*
@@ -5618,6 +5636,30 @@ static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
56185636 wait );
56195637 __poll_t mask = key_to_poll (key );
56205638
5639+ if (unlikely (mask & POLLFREE )) {
5640+ io_poll_mark_cancelled (req );
5641+ /* we have to kick tw in case it's not already */
5642+ io_poll_execute (req , 0 );
5643+
5644+ /*
5645+ * If the waitqueue is being freed early but someone is already
5646+ * holds ownership over it, we have to tear down the request as
5647+ * best we can. That means immediately removing the request from
5648+ * its waitqueue and preventing all further accesses to the
5649+ * waitqueue via the request.
5650+ */
5651+ list_del_init (& poll -> wait .entry );
5652+
5653+ /*
5654+ * Careful: this *must* be the last step, since as soon
5655+ * as req->head is NULL'ed out, the request can be
5656+ * completed and freed, since aio_poll_complete_work()
5657+ * will no longer need to take the waitqueue lock.
5658+ */
5659+ smp_store_release (& poll -> head , NULL );
5660+ return 1 ;
5661+ }
5662+
56215663 /* for instances that support it check for an event match first */
56225664 if (mask && !(mask & poll -> events ))
56235665 return 0 ;
0 commit comments