Skip to content

Commit a6a4b66

Browse files
committed
Merge tag 'for-linus-20190516' of git://git.kernel.dk/linux-block
Pull io_uring fixes from Jens Axboe: "A small set of fixes for io_uring. This contains: - smp_rmb() cleanup for io_cqring_events() (Jackie) - io_cqring_wait() simplification (Jackie) - removal of dead 'ev_flags' passing (me) - SQ poll CPU affinity verification fix (me) - SQ poll wait fix (Roman) - SQE command prep cleanup and fix (Stefan)" * tag 'for-linus-20190516' of git://git.kernel.dk/linux-block: io_uring: use wait_event_interruptible for cq_wait conditional wait io_uring: adjust smp_rmb inside io_cqring_events io_uring: fix infinite wait in khread_park() on io_finish_async() io_uring: remove 'ev_flags' argument io_uring: fix failure to verify SQ_AFF cpu io_uring: fix race condition reading SQE data
2 parents 1718de7 + fdb288a commit a6a4b66

File tree

1 file changed

+31
-57
lines changed

1 file changed

+31
-57
lines changed

fs/io_uring.c

Lines changed: 31 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ struct io_ring_ctx {
231231
struct task_struct *sqo_thread; /* if using sq thread polling */
232232
struct mm_struct *sqo_mm;
233233
wait_queue_head_t sqo_wait;
234-
unsigned sqo_stop;
235234

236235
struct {
237236
/* CQ ring */
@@ -329,9 +328,8 @@ struct io_kiocb {
329328
#define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */
330329
#define REQ_F_FIXED_FILE 4 /* ctx owns file */
331330
#define REQ_F_SEQ_PREV 8 /* sequential with previous */
332-
#define REQ_F_PREPPED 16 /* prep already done */
333-
#define REQ_F_IO_DRAIN 32 /* drain existing IO first */
334-
#define REQ_F_IO_DRAINED 64 /* drain done */
331+
#define REQ_F_IO_DRAIN 16 /* drain existing IO first */
332+
#define REQ_F_IO_DRAINED 32 /* drain done */
335333
u64 user_data;
336334
u32 error; /* iopoll result from callback */
337335
u32 sequence;
@@ -490,7 +488,7 @@ static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx)
490488
}
491489

492490
static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
493-
long res, unsigned ev_flags)
491+
long res)
494492
{
495493
struct io_uring_cqe *cqe;
496494

@@ -503,7 +501,7 @@ static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data,
503501
if (cqe) {
504502
WRITE_ONCE(cqe->user_data, ki_user_data);
505503
WRITE_ONCE(cqe->res, res);
506-
WRITE_ONCE(cqe->flags, ev_flags);
504+
WRITE_ONCE(cqe->flags, 0);
507505
} else {
508506
unsigned overflow = READ_ONCE(ctx->cq_ring->overflow);
509507

@@ -522,12 +520,12 @@ static void io_cqring_ev_posted(struct io_ring_ctx *ctx)
522520
}
523521

524522
static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data,
525-
long res, unsigned ev_flags)
523+
long res)
526524
{
527525
unsigned long flags;
528526

529527
spin_lock_irqsave(&ctx->completion_lock, flags);
530-
io_cqring_fill_event(ctx, user_data, res, ev_flags);
528+
io_cqring_fill_event(ctx, user_data, res);
531529
io_commit_cqring(ctx);
532530
spin_unlock_irqrestore(&ctx->completion_lock, flags);
533531

@@ -629,7 +627,7 @@ static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events,
629627
req = list_first_entry(done, struct io_kiocb, list);
630628
list_del(&req->list);
631629

632-
io_cqring_fill_event(ctx, req->user_data, req->error, 0);
630+
io_cqring_fill_event(ctx, req->user_data, req->error);
633631
(*nr_events)++;
634632

635633
if (refcount_dec_and_test(&req->refs)) {
@@ -777,7 +775,7 @@ static void io_complete_rw(struct kiocb *kiocb, long res, long res2)
777775

778776
kiocb_end_write(kiocb);
779777

780-
io_cqring_add_event(req->ctx, req->user_data, res, 0);
778+
io_cqring_add_event(req->ctx, req->user_data, res);
781779
io_put_req(req);
782780
}
783781

@@ -896,9 +894,6 @@ static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
896894

897895
if (!req->file)
898896
return -EBADF;
899-
/* For -EAGAIN retry, everything is already prepped */
900-
if (req->flags & REQ_F_PREPPED)
901-
return 0;
902897

903898
if (force_nonblock && !io_file_supports_async(req->file))
904899
force_nonblock = false;
@@ -941,7 +936,6 @@ static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s,
941936
return -EINVAL;
942937
kiocb->ki_complete = io_complete_rw;
943938
}
944-
req->flags |= REQ_F_PREPPED;
945939
return 0;
946940
}
947941

@@ -1216,7 +1210,7 @@ static int io_nop(struct io_kiocb *req, u64 user_data)
12161210
if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
12171211
return -EINVAL;
12181212

1219-
io_cqring_add_event(ctx, user_data, err, 0);
1213+
io_cqring_add_event(ctx, user_data, err);
12201214
io_put_req(req);
12211215
return 0;
12221216
}
@@ -1227,16 +1221,12 @@ static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe)
12271221

12281222
if (!req->file)
12291223
return -EBADF;
1230-
/* Prep already done (EAGAIN retry) */
1231-
if (req->flags & REQ_F_PREPPED)
1232-
return 0;
12331224

12341225
if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
12351226
return -EINVAL;
12361227
if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
12371228
return -EINVAL;
12381229

1239-
req->flags |= REQ_F_PREPPED;
12401230
return 0;
12411231
}
12421232

@@ -1265,7 +1255,7 @@ static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe,
12651255
end > 0 ? end : LLONG_MAX,
12661256
fsync_flags & IORING_FSYNC_DATASYNC);
12671257

1268-
io_cqring_add_event(req->ctx, sqe->user_data, ret, 0);
1258+
io_cqring_add_event(req->ctx, sqe->user_data, ret);
12691259
io_put_req(req);
12701260
return 0;
12711261
}
@@ -1277,16 +1267,12 @@ static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe)
12771267

12781268
if (!req->file)
12791269
return -EBADF;
1280-
/* Prep already done (EAGAIN retry) */
1281-
if (req->flags & REQ_F_PREPPED)
1282-
return 0;
12831270

12841271
if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
12851272
return -EINVAL;
12861273
if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index))
12871274
return -EINVAL;
12881275

1289-
req->flags |= REQ_F_PREPPED;
12901276
return ret;
12911277
}
12921278

@@ -1313,7 +1299,7 @@ static int io_sync_file_range(struct io_kiocb *req,
13131299

13141300
ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags);
13151301

1316-
io_cqring_add_event(req->ctx, sqe->user_data, ret, 0);
1302+
io_cqring_add_event(req->ctx, sqe->user_data, ret);
13171303
io_put_req(req);
13181304
return 0;
13191305
}
@@ -1371,7 +1357,7 @@ static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe)
13711357
}
13721358
spin_unlock_irq(&ctx->completion_lock);
13731359

1374-
io_cqring_add_event(req->ctx, sqe->user_data, ret, 0);
1360+
io_cqring_add_event(req->ctx, sqe->user_data, ret);
13751361
io_put_req(req);
13761362
return 0;
13771363
}
@@ -1380,7 +1366,7 @@ static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req,
13801366
__poll_t mask)
13811367
{
13821368
req->poll.done = true;
1383-
io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask), 0);
1369+
io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask));
13841370
io_commit_cqring(ctx);
13851371
}
13861372

@@ -1700,7 +1686,7 @@ static void io_sq_wq_submit_work(struct work_struct *work)
17001686
io_put_req(req);
17011687

17021688
if (ret) {
1703-
io_cqring_add_event(ctx, sqe->user_data, ret, 0);
1689+
io_cqring_add_event(ctx, sqe->user_data, ret);
17041690
io_put_req(req);
17051691
}
17061692

@@ -2005,7 +1991,7 @@ static int io_submit_sqes(struct io_ring_ctx *ctx, struct sqe_submit *sqes,
20051991
continue;
20061992
}
20071993

2008-
io_cqring_add_event(ctx, sqes[i].sqe->user_data, ret, 0);
1994+
io_cqring_add_event(ctx, sqes[i].sqe->user_data, ret);
20091995
}
20101996

20111997
if (statep)
@@ -2028,7 +2014,7 @@ static int io_sq_thread(void *data)
20282014
set_fs(USER_DS);
20292015

20302016
timeout = inflight = 0;
2031-
while (!kthread_should_stop() && !ctx->sqo_stop) {
2017+
while (!kthread_should_park()) {
20322018
bool all_fixed, mm_fault = false;
20332019
int i;
20342020

@@ -2090,7 +2076,7 @@ static int io_sq_thread(void *data)
20902076
smp_mb();
20912077

20922078
if (!io_get_sqring(ctx, &sqes[0])) {
2093-
if (kthread_should_stop()) {
2079+
if (kthread_should_park()) {
20942080
finish_wait(&ctx->sqo_wait, &wait);
20952081
break;
20962082
}
@@ -2140,8 +2126,7 @@ static int io_sq_thread(void *data)
21402126
mmput(cur_mm);
21412127
}
21422128

2143-
if (kthread_should_park())
2144-
kthread_parkme();
2129+
kthread_parkme();
21452130

21462131
return 0;
21472132
}
@@ -2170,7 +2155,7 @@ static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
21702155

21712156
ret = io_submit_sqe(ctx, &s, statep);
21722157
if (ret)
2173-
io_cqring_add_event(ctx, s.sqe->user_data, ret, 0);
2158+
io_cqring_add_event(ctx, s.sqe->user_data, ret);
21742159
}
21752160
io_commit_sqring(ctx);
21762161

@@ -2182,6 +2167,8 @@ static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit)
21822167

21832168
static unsigned io_cqring_events(struct io_cq_ring *ring)
21842169
{
2170+
/* See comment at the top of this file */
2171+
smp_rmb();
21852172
return READ_ONCE(ring->r.tail) - READ_ONCE(ring->r.head);
21862173
}
21872174

@@ -2194,11 +2181,8 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
21942181
{
21952182
struct io_cq_ring *ring = ctx->cq_ring;
21962183
sigset_t ksigmask, sigsaved;
2197-
DEFINE_WAIT(wait);
21982184
int ret;
21992185

2200-
/* See comment at the top of this file */
2201-
smp_rmb();
22022186
if (io_cqring_events(ring) >= min_events)
22032187
return 0;
22042188

@@ -2216,23 +2200,9 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
22162200
return ret;
22172201
}
22182202

2219-
do {
2220-
prepare_to_wait(&ctx->wait, &wait, TASK_INTERRUPTIBLE);
2221-
2222-
ret = 0;
2223-
/* See comment at the top of this file */
2224-
smp_rmb();
2225-
if (io_cqring_events(ring) >= min_events)
2226-
break;
2227-
2228-
schedule();
2229-
2203+
ret = wait_event_interruptible(ctx->wait, io_cqring_events(ring) >= min_events);
2204+
if (ret == -ERESTARTSYS)
22302205
ret = -EINTR;
2231-
if (signal_pending(current))
2232-
break;
2233-
} while (1);
2234-
2235-
finish_wait(&ctx->wait, &wait);
22362206

22372207
if (sig)
22382208
restore_user_sigmask(sig, &sigsaved);
@@ -2273,8 +2243,11 @@ static int io_sqe_files_unregister(struct io_ring_ctx *ctx)
22732243
static void io_sq_thread_stop(struct io_ring_ctx *ctx)
22742244
{
22752245
if (ctx->sqo_thread) {
2276-
ctx->sqo_stop = 1;
2277-
mb();
2246+
/*
2247+
* The park is a bit of a work-around, without it we get
2248+
* warning spews on shutdown with SQPOLL set and affinity
2249+
* set to a single CPU.
2250+
*/
22782251
kthread_park(ctx->sqo_thread);
22792252
kthread_stop(ctx->sqo_thread);
22802253
ctx->sqo_thread = NULL;
@@ -2467,10 +2440,11 @@ static int io_sq_offload_start(struct io_ring_ctx *ctx,
24672440
ctx->sq_thread_idle = HZ;
24682441

24692442
if (p->flags & IORING_SETUP_SQ_AFF) {
2470-
int cpu = array_index_nospec(p->sq_thread_cpu,
2471-
nr_cpu_ids);
2443+
int cpu = p->sq_thread_cpu;
24722444

24732445
ret = -EINVAL;
2446+
if (cpu >= nr_cpu_ids)
2447+
goto err;
24742448
if (!cpu_online(cpu))
24752449
goto err;
24762450

0 commit comments

Comments
 (0)