double poll fixes
cover double poll fixes
2/2 is about recent syzbot report, 1/2 is just a small additional fix
Pavel Begunkov (2):
io_uring: explicitly count entries for poll reqs
io_uring: remove double poll entry on arm failure
fs/io_uring.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)
Signature · 1 line
2.32.0
PATCH 1/2 io_uring: explicitly count entries for poll reqs
If __io_queue_proc() fails to add a second poll entry, e.g. kmalloc() failed, but it goes on with a third waitqueue, it may succeed and overwrite the error status. Count the number of poll entries we added, so we can set pt->error to zero at the beginning and find out when the mentioned scenario happens.
--- fs/io_uring.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
fs/io_uring.c +10 -6
@@ -4802,6 +4802,7 @@ IO_NETOP_FN(recv);struct io_poll_table {struct poll_table_struct pt;struct io_kiocb *req; +int nr_entries;int error; };
@@ -4995,11 +4996,11 @@ static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt,struct io_kiocb *req = pt->req;/* -* If poll->head is already set, it's because the file being polled -* uses multiple waitqueues for poll handling (eg one for read, one -* for write). Setup a separate io_poll_iocb if this happens. +* The file being polled uses multiple waitqueues for poll handling +* (e.g. one for read, one for write). Setup a separate io_poll_iocb +* if this happens. */ -if (unlikely (poll->head)) { +if (unlikely (pt->nr_entries)) {struct io_poll_iocb *poll_one = poll;/* already have a 2nd entry, fail a third attempt */
@@ -5027,7 +5028,7 @@ static void __io_queue_proc(struct io_poll_iocb *poll, struct io_poll_table *pt, *poll_ptr = poll; } - pt->error =0 ; + pt->nr_entries++; poll->head = head;if (poll->events &EPOLLEXCLUSIVE )
@@ -5104,9 +5105,12 @@ static __poll_t __io_arm_poll_handler(struct io_kiocb *req, ipt->pt._key = mask; ipt->req = req; - ipt->error = -EINVAL ; + ipt->error =0 ; + ipt->nr_entries =0 ; mask =vfs_poll (req->file, &ipt->pt) & poll->events; +if (unlikely (!ipt->nr_entries) && !ipt->error) + ipt->error = -EINVAL ;spin_lock_irq (&ctx->completion_lock);if (likely (poll->head)) {
Signature · 1 line
2.32.0
PATCH 2/2 io_uring: remove double poll entry on arm failure
In reply to Pavel Begunkov__io_queue_proc() can enqueue both poll entries and still fail afterwards, so the callers trying to cancel it should also try to remove the second poll entry (if any).
For example, it may leave the request alive referencing a io_uring context but not accessible for cancellation:
[ 282.599913][ T1620] task:iou-sqp-23145 state:D stack:28720 pid:23155 ppid: 8844 flags:0x00004004 [ 282.609927][ T1620] Call Trace: [ 282.613711][ T1620] __schedule+0x93a/0x26f0 [ 282.634647][ T1620] schedule+0xd3/0x270 [ 282.638874][ T1620] io_uring_cancel_generic+0x54d/0x890 [ 282.660346][ T1620] io_sq_thread+0xaac/0x1250 [ 282.696394][ T1620] ret_from_fork+0x1f/0x30
--- fs/io_uring.c | 2 ++ 1 file changed, 2 insertions(+)
fs/io_uring.c +2 -0
@@ -5113,6 +5113,8 @@ static __poll_t __io_arm_poll_handler(struct io_kiocb *req, ipt->error = -EINVAL ;spin_lock_irq (&ctx->completion_lock); +if (ipt->error) +io_poll_remove_double (req);if (likely (poll->head)) {spin_lock (&poll->head->lock);if (unlikely (list_empty (&poll->wait.entry))) {
Signature · 1 line
2.32.0
Quoted 8 lines · Pavel Begunkov
2/2 is about recent syzbot report, 1/2 is just a small additional fix
Pavel Begunkov (2):
io_uring: explicitly count entries for poll reqs
io_uring: remove double poll entry on arm failurefs/io_uring.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)
Applied, thanks.
Signature · 1 line
Jens Axboe