Skip to content

Commit 940b8c4

Browse files
Jens Axboeaxboe
authored andcommitted
io_uring/io-wq: make io_wq_work flags atomic
The work flags can be set/accessed from different tasks, both the originator of the request, and the io-wq workers. While modifications aren't concurrent, it still makes KMSAN unhappy. There's no real downside to just making the flag reading/manipulation use proper atomics here. Signed-off-by: Jens Axboe <[email protected]>
1 parent 7cc167a commit 940b8c4

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

include/linux/io_uring_types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct io_wq_work_list {
5050

5151
struct io_wq_work {
5252
struct io_wq_work_node list;
53-
unsigned flags;
53+
atomic_t flags;
5454
/* place it here instead of io_kiocb as it fills padding and saves 4B */
5555
int cancel_seq;
5656
};

io_uring/io-wq.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ static inline struct io_wq_acct *io_get_acct(struct io_wq *wq, bool bound)
159159
static inline struct io_wq_acct *io_work_get_acct(struct io_wq *wq,
160160
struct io_wq_work *work)
161161
{
162-
return io_get_acct(wq, !(work->flags & IO_WQ_WORK_UNBOUND));
162+
return io_get_acct(wq, !(atomic_read(&work->flags) & IO_WQ_WORK_UNBOUND));
163163
}
164164

165165
static inline struct io_wq_acct *io_wq_get_acct(struct io_worker *worker)
@@ -451,7 +451,7 @@ static void __io_worker_idle(struct io_wq *wq, struct io_worker *worker)
451451

452452
static inline unsigned int io_get_work_hash(struct io_wq_work *work)
453453
{
454-
return work->flags >> IO_WQ_HASH_SHIFT;
454+
return atomic_read(&work->flags) >> IO_WQ_HASH_SHIFT;
455455
}
456456

457457
static bool io_wait_on_hash(struct io_wq *wq, unsigned int hash)
@@ -592,8 +592,9 @@ static void io_worker_handle_work(struct io_wq_acct *acct,
592592

593593
next_hashed = wq_next_work(work);
594594

595-
if (unlikely(do_kill) && (work->flags & IO_WQ_WORK_UNBOUND))
596-
work->flags |= IO_WQ_WORK_CANCEL;
595+
if (do_kill &&
596+
(atomic_read(&work->flags) & IO_WQ_WORK_UNBOUND))
597+
atomic_or(IO_WQ_WORK_CANCEL, &work->flags);
597598
wq->do_work(work);
598599
io_assign_current_work(worker, NULL);
599600

@@ -891,7 +892,7 @@ static bool io_wq_worker_wake(struct io_worker *worker, void *data)
891892
static void io_run_cancel(struct io_wq_work *work, struct io_wq *wq)
892893
{
893894
do {
894-
work->flags |= IO_WQ_WORK_CANCEL;
895+
atomic_or(IO_WQ_WORK_CANCEL, &work->flags);
895896
wq->do_work(work);
896897
work = wq->free_work(work);
897898
} while (work);
@@ -926,7 +927,7 @@ static bool io_wq_work_match_item(struct io_wq_work *work, void *data)
926927
void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
927928
{
928929
struct io_wq_acct *acct = io_work_get_acct(wq, work);
929-
unsigned long work_flags = work->flags;
930+
unsigned int work_flags = atomic_read(&work->flags);
930931
struct io_cb_cancel_data match = {
931932
.fn = io_wq_work_match_item,
932933
.data = work,
@@ -939,7 +940,7 @@ void io_wq_enqueue(struct io_wq *wq, struct io_wq_work *work)
939940
* been marked as one that should not get executed, cancel it here.
940941
*/
941942
if (test_bit(IO_WQ_BIT_EXIT, &wq->state) ||
942-
(work->flags & IO_WQ_WORK_CANCEL)) {
943+
(work_flags & IO_WQ_WORK_CANCEL)) {
943944
io_run_cancel(work, wq);
944945
return;
945946
}
@@ -982,15 +983,15 @@ void io_wq_hash_work(struct io_wq_work *work, void *val)
982983
unsigned int bit;
983984

984985
bit = hash_ptr(val, IO_WQ_HASH_ORDER);
985-
work->flags |= (IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT));
986+
atomic_or(IO_WQ_WORK_HASHED | (bit << IO_WQ_HASH_SHIFT), &work->flags);
986987
}
987988

988989
static bool __io_wq_worker_cancel(struct io_worker *worker,
989990
struct io_cb_cancel_data *match,
990991
struct io_wq_work *work)
991992
{
992993
if (work && match->fn(work, match->data)) {
993-
work->flags |= IO_WQ_WORK_CANCEL;
994+
atomic_or(IO_WQ_WORK_CANCEL, &work->flags);
994995
__set_notify_signal(worker->task);
995996
return true;
996997
}

io_uring/io-wq.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ bool io_wq_worker_stopped(void);
5656

5757
static inline bool io_wq_is_hashed(struct io_wq_work *work)
5858
{
59-
return work->flags & IO_WQ_WORK_HASHED;
59+
return atomic_read(&work->flags) & IO_WQ_WORK_HASHED;
6060
}
6161

6262
typedef bool (work_cancel_fn)(struct io_wq_work *, void *);

io_uring/io_uring.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,9 @@ static void io_prep_async_work(struct io_kiocb *req)
462462
}
463463

464464
req->work.list.next = NULL;
465-
req->work.flags = 0;
465+
atomic_set(&req->work.flags, 0);
466466
if (req->flags & REQ_F_FORCE_ASYNC)
467-
req->work.flags |= IO_WQ_WORK_CONCURRENT;
467+
atomic_or(IO_WQ_WORK_CONCURRENT, &req->work.flags);
468468

469469
if (req->file && !(req->flags & REQ_F_FIXED_FILE))
470470
req->flags |= io_file_get_flags(req->file);
@@ -480,7 +480,7 @@ static void io_prep_async_work(struct io_kiocb *req)
480480
io_wq_hash_work(&req->work, file_inode(req->file));
481481
} else if (!req->file || !S_ISBLK(file_inode(req->file)->i_mode)) {
482482
if (def->unbound_nonreg_file)
483-
req->work.flags |= IO_WQ_WORK_UNBOUND;
483+
atomic_or(IO_WQ_WORK_UNBOUND, &req->work.flags);
484484
}
485485
}
486486

@@ -520,7 +520,7 @@ static void io_queue_iowq(struct io_kiocb *req)
520520
* worker for it).
521521
*/
522522
if (WARN_ON_ONCE(!same_thread_group(req->task, current)))
523-
req->work.flags |= IO_WQ_WORK_CANCEL;
523+
atomic_or(IO_WQ_WORK_CANCEL, &req->work.flags);
524524

525525
trace_io_uring_queue_async_work(req, io_wq_is_hashed(&req->work));
526526
io_wq_enqueue(tctx->io_wq, &req->work);
@@ -1736,14 +1736,14 @@ void io_wq_submit_work(struct io_wq_work *work)
17361736
io_arm_ltimeout(req);
17371737

17381738
/* either cancelled or io-wq is dying, so don't touch tctx->iowq */
1739-
if (work->flags & IO_WQ_WORK_CANCEL) {
1739+
if (atomic_read(&work->flags) & IO_WQ_WORK_CANCEL) {
17401740
fail:
17411741
io_req_task_queue_fail(req, err);
17421742
return;
17431743
}
17441744
if (!io_assign_file(req, def, issue_flags)) {
17451745
err = -EBADF;
1746-
work->flags |= IO_WQ_WORK_CANCEL;
1746+
atomic_or(IO_WQ_WORK_CANCEL, &work->flags);
17471747
goto fail;
17481748
}
17491749

0 commit comments

Comments
 (0)