io_uring: fix concurrent parking
authorPavel Begunkov <asml.silence@gmail.com>
Sun, 14 Mar 2021 20:57:12 +0000 (20:57 +0000)
committerJens Axboe <axboe@kernel.dk>
Mon, 15 Mar 2021 15:32:40 +0000 (09:32 -0600)
commit9e138a48345427fa42f6076396ea069cebf3c08f
tree68225f0800698772485a206aac799d7c80259047
parentf6d54255f4235448d4bbe442362d4caa62da97d5
io_uring: fix concurrent parking

If io_sq_thread_park() of one task got rescheduled right after
set_bit(), before it gets back to mutex_lock() there can happen
park()/unpark() by another task with SQPOLL locking again and
continuing running never seeing that first set_bit(SHOULD_PARK),
so won't even try to put the mutex down for parking.

It will get parked eventually when SQPOLL drops the lock for reschedule,
but may be problematic and will get in the way of further fixes.

Account number of tasks waiting for parking with a new atomic variable
park_pending and adjust SHOULD_PARK accordingly. It doesn't entirely
replaces SHOULD_PARK bit with this atomic var because it's convenient
to have it as a bit in the state and will help to do optimisations
later.

Signed-off-by: Pavel Begunkov <asml.silence@gmail.com>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
fs/io_uring.c