Skip to content

Commit 09a6f4e

Browse files
isilenceaxboe
authored andcommitted
io_uring: replace sqd rw_semaphore with mutex
The only user of read-locking of sqd->rw_lock is sq_thread itself, which is by definition alone, so we don't really need rw_semaphore, but mutex will do. Replace it with a mutex, and kill read-to-write upgrading and extra task_work handling in io_sq_thread(). Signed-off-by: Pavel Begunkov <asml.silence@gmail.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 180f829 commit 09a6f4e

1 file changed

Lines changed: 14 additions & 22 deletions

File tree

fs/io_uring.c

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ enum {
258258

259259
struct io_sq_data {
260260
refcount_t refs;
261-
struct rw_semaphore rw_lock;
261+
struct mutex lock;
262262

263263
/* ctx's that are using this sqd */
264264
struct list_head ctx_list;
@@ -6689,16 +6689,15 @@ static int io_sq_thread(void *data)
66896689
set_cpus_allowed_ptr(current, cpu_online_mask);
66906690
current->flags |= PF_NO_SETAFFINITY;
66916691

6692-
down_read(&sqd->rw_lock);
6693-
6692+
mutex_lock(&sqd->lock);
66946693
while (!test_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state)) {
66956694
int ret;
66966695
bool cap_entries, sqt_spin, needs_sched;
66976696

66986697
if (test_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state)) {
6699-
up_read(&sqd->rw_lock);
6698+
mutex_unlock(&sqd->lock);
67006699
cond_resched();
6701-
down_read(&sqd->rw_lock);
6700+
mutex_lock(&sqd->lock);
67026701
io_run_task_work();
67036702
timeout = jiffies + sqd->sq_thread_idle;
67046703
continue;
@@ -6745,31 +6744,24 @@ static int io_sq_thread(void *data)
67456744
list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
67466745
io_ring_set_wakeup_flag(ctx);
67476746

6748-
up_read(&sqd->rw_lock);
6747+
mutex_unlock(&sqd->lock);
67496748
schedule();
67506749
try_to_freeze();
6751-
down_read(&sqd->rw_lock);
6750+
mutex_lock(&sqd->lock);
67526751
list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
67536752
io_ring_clear_wakeup_flag(ctx);
67546753
}
67556754

67566755
finish_wait(&sqd->wait, &wait);
67576756
timeout = jiffies + sqd->sq_thread_idle;
67586757
}
6759-
up_read(&sqd->rw_lock);
6760-
down_write(&sqd->rw_lock);
6761-
/*
6762-
* someone may have parked and added a cancellation task_work, run
6763-
* it first because we don't want it in io_uring_cancel_sqpoll()
6764-
*/
6765-
io_run_task_work();
67666758

67676759
list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
67686760
io_uring_cancel_sqpoll(ctx);
67696761
sqd->thread = NULL;
67706762
list_for_each_entry(ctx, &sqd->ctx_list, sqd_list)
67716763
io_ring_set_wakeup_flag(ctx);
6772-
up_write(&sqd->rw_lock);
6764+
mutex_unlock(&sqd->lock);
67736765

67746766
io_run_task_work();
67756767
complete(&sqd->exited);
@@ -7071,21 +7063,21 @@ static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
70717063
}
70727064

70737065
static void io_sq_thread_unpark(struct io_sq_data *sqd)
7074-
__releases(&sqd->rw_lock)
7066+
__releases(&sqd->lock)
70757067
{
70767068
WARN_ON_ONCE(sqd->thread == current);
70777069

70787070
clear_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
7079-
up_write(&sqd->rw_lock);
7071+
mutex_unlock(&sqd->lock);
70807072
}
70817073

70827074
static void io_sq_thread_park(struct io_sq_data *sqd)
7083-
__acquires(&sqd->rw_lock)
7075+
__acquires(&sqd->lock)
70847076
{
70857077
WARN_ON_ONCE(sqd->thread == current);
70867078

70877079
set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
7088-
down_write(&sqd->rw_lock);
7080+
mutex_lock(&sqd->lock);
70897081
/* set again for consistency, in case concurrent parks are happening */
70907082
set_bit(IO_SQ_THREAD_SHOULD_PARK, &sqd->state);
70917083
if (sqd->thread)
@@ -7096,11 +7088,11 @@ static void io_sq_thread_stop(struct io_sq_data *sqd)
70967088
{
70977089
WARN_ON_ONCE(sqd->thread == current);
70987090

7099-
down_write(&sqd->rw_lock);
7091+
mutex_lock(&sqd->lock);
71007092
set_bit(IO_SQ_THREAD_SHOULD_STOP, &sqd->state);
71017093
if (sqd->thread)
71027094
wake_up_process(sqd->thread);
7103-
up_write(&sqd->rw_lock);
7095+
mutex_unlock(&sqd->lock);
71047096
wait_for_completion(&sqd->exited);
71057097
}
71067098

@@ -7182,7 +7174,7 @@ static struct io_sq_data *io_get_sq_data(struct io_uring_params *p,
71827174

71837175
refcount_set(&sqd->refs, 1);
71847176
INIT_LIST_HEAD(&sqd->ctx_list);
7185-
init_rwsem(&sqd->rw_lock);
7177+
mutex_init(&sqd->lock);
71867178
init_waitqueue_head(&sqd->wait);
71877179
init_completion(&sqd->exited);
71887180
return sqd;

0 commit comments

Comments
 (0)