Skip to content

Commit 8c93074

Browse files
committed
Merge tag 'io_uring-6.11-20240726' of git://git.kernel.dk/linux
Pull io_uring fixes from Jens Axboe: - Fix a syzbot issue for the msg ring cache added in this release. No ill effects from this one, but it did make KMSAN unhappy (me) - Sanitize the NAPI timeout handling, by unifying the value handling into all ktime_t rather than converting back and forth (Pavel) - Fail NAPI registration for IOPOLL rings, it's not supported (Pavel) - Fix a theoretical issue with ring polling and cancelations (Pavel) - Various little cleanups and fixes (Pavel) * tag 'io_uring-6.11-20240726' of git://git.kernel.dk/linux: io_uring/napi: pass ktime to io_napi_adjust_timeout io_uring/napi: use ktime in busy polling io_uring/msg_ring: fix uninitialized use of target_req->flags io_uring: align iowq and task request error handling io_uring: kill REQ_F_CANCEL_SEQ io_uring: simplify io_uring_cmd return io_uring: fix io_match_task must_hold io_uring: don't allow netpolling with SETUP_IOPOLL io_uring: tighten task exit cancellations
2 parents bc4eee8 + 3581696 commit 8c93074

File tree

8 files changed

+51
-49
lines changed

8 files changed

+51
-49
lines changed

include/linux/io_uring_types.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ struct io_ring_ctx {
404404
spinlock_t napi_lock; /* napi_list lock */
405405

406406
/* napi busy poll default timeout */
407-
unsigned int napi_busy_poll_to;
407+
ktime_t napi_busy_poll_dt;
408408
bool napi_prefer_busy_poll;
409409
bool napi_enabled;
410410

@@ -461,7 +461,6 @@ enum {
461461
REQ_F_SUPPORT_NOWAIT_BIT,
462462
REQ_F_ISREG_BIT,
463463
REQ_F_POLL_NO_LAZY_BIT,
464-
REQ_F_CANCEL_SEQ_BIT,
465464
REQ_F_CAN_POLL_BIT,
466465
REQ_F_BL_EMPTY_BIT,
467466
REQ_F_BL_NO_RECYCLE_BIT,
@@ -536,8 +535,6 @@ enum {
536535
REQ_F_HASH_LOCKED = IO_REQ_FLAG(REQ_F_HASH_LOCKED_BIT),
537536
/* don't use lazy poll wake for this request */
538537
REQ_F_POLL_NO_LAZY = IO_REQ_FLAG(REQ_F_POLL_NO_LAZY_BIT),
539-
/* cancel sequence is set and valid */
540-
REQ_F_CANCEL_SEQ = IO_REQ_FLAG(REQ_F_CANCEL_SEQ_BIT),
541538
/* file is pollable */
542539
REQ_F_CAN_POLL = IO_REQ_FLAG(REQ_F_CAN_POLL_BIT),
543540
/* buffer list was empty after selection of buffer */

io_uring/io_uring.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,7 +1849,7 @@ void io_wq_submit_work(struct io_wq_work *work)
18491849
} while (1);
18501850

18511851
/* avoid locking problems by failing it from a clean context */
1852-
if (ret < 0)
1852+
if (ret)
18531853
io_req_task_queue_fail(req, ret);
18541854
}
18551855

@@ -2416,12 +2416,14 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
24162416

24172417
if (uts) {
24182418
struct timespec64 ts;
2419+
ktime_t dt;
24192420

24202421
if (get_timespec64(&ts, uts))
24212422
return -EFAULT;
24222423

2423-
iowq.timeout = ktime_add_ns(timespec64_to_ktime(ts), ktime_get_ns());
2424-
io_napi_adjust_timeout(ctx, &iowq, &ts);
2424+
dt = timespec64_to_ktime(ts);
2425+
iowq.timeout = ktime_add(dt, ktime_get());
2426+
io_napi_adjust_timeout(ctx, &iowq, dt);
24252427
}
24262428

24272429
if (sig) {
@@ -3031,8 +3033,11 @@ __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd)
30313033
bool loop = false;
30323034

30333035
io_uring_drop_tctx_refs(current);
3036+
if (!tctx_inflight(tctx, !cancel_all))
3037+
break;
3038+
30343039
/* read completions before cancelations */
3035-
inflight = tctx_inflight(tctx, !cancel_all);
3040+
inflight = tctx_inflight(tctx, false);
30363041
if (!inflight)
30373042
break;
30383043

io_uring/io_uring.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct io_wait_queue {
4343
ktime_t timeout;
4444

4545
#ifdef CONFIG_NET_RX_BUSY_POLL
46-
unsigned int napi_busy_poll_to;
46+
ktime_t napi_busy_poll_dt;
4747
bool napi_prefer_busy_poll;
4848
#endif
4949
};

io_uring/msg_ring.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ static struct io_kiocb *io_msg_get_kiocb(struct io_ring_ctx *ctx)
110110
if (spin_trylock(&ctx->msg_lock)) {
111111
req = io_alloc_cache_get(&ctx->msg_cache);
112112
spin_unlock(&ctx->msg_lock);
113+
if (req)
114+
return req;
113115
}
114-
if (req)
115-
return req;
116-
return kmem_cache_alloc(req_cachep, GFP_KERNEL | __GFP_NOWARN);
116+
return kmem_cache_alloc(req_cachep, GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO);
117117
}
118118

119119
static int io_msg_data_remote(struct io_kiocb *req)

io_uring/napi.c

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ static struct io_napi_entry *io_napi_hash_find(struct hlist_head *hash_list,
3333
return NULL;
3434
}
3535

36+
static inline ktime_t net_to_ktime(unsigned long t)
37+
{
38+
/* napi approximating usecs, reverse busy_loop_current_time */
39+
return ns_to_ktime(t << 10);
40+
}
41+
3642
void __io_napi_add(struct io_ring_ctx *ctx, struct socket *sock)
3743
{
3844
struct hlist_head *hash_list;
@@ -102,14 +108,14 @@ static inline void io_napi_remove_stale(struct io_ring_ctx *ctx, bool is_stale)
102108
__io_napi_remove_stale(ctx);
103109
}
104110

105-
static inline bool io_napi_busy_loop_timeout(unsigned long start_time,
106-
unsigned long bp_usec)
111+
static inline bool io_napi_busy_loop_timeout(ktime_t start_time,
112+
ktime_t bp)
107113
{
108-
if (bp_usec) {
109-
unsigned long end_time = start_time + bp_usec;
110-
unsigned long now = busy_loop_current_time();
114+
if (bp) {
115+
ktime_t end_time = ktime_add(start_time, bp);
116+
ktime_t now = net_to_ktime(busy_loop_current_time());
111117

112-
return time_after(now, end_time);
118+
return ktime_after(now, end_time);
113119
}
114120

115121
return true;
@@ -124,7 +130,8 @@ static bool io_napi_busy_loop_should_end(void *data,
124130
return true;
125131
if (io_should_wake(iowq) || io_has_work(iowq->ctx))
126132
return true;
127-
if (io_napi_busy_loop_timeout(start_time, iowq->napi_busy_poll_to))
133+
if (io_napi_busy_loop_timeout(net_to_ktime(start_time),
134+
iowq->napi_busy_poll_dt))
128135
return true;
129136

130137
return false;
@@ -181,10 +188,12 @@ static void io_napi_blocking_busy_loop(struct io_ring_ctx *ctx,
181188
*/
182189
void io_napi_init(struct io_ring_ctx *ctx)
183190
{
191+
u64 sys_dt = READ_ONCE(sysctl_net_busy_poll) * NSEC_PER_USEC;
192+
184193
INIT_LIST_HEAD(&ctx->napi_list);
185194
spin_lock_init(&ctx->napi_lock);
186195
ctx->napi_prefer_busy_poll = false;
187-
ctx->napi_busy_poll_to = READ_ONCE(sysctl_net_busy_poll);
196+
ctx->napi_busy_poll_dt = ns_to_ktime(sys_dt);
188197
}
189198

190199
/*
@@ -217,11 +226,13 @@ void io_napi_free(struct io_ring_ctx *ctx)
217226
int io_register_napi(struct io_ring_ctx *ctx, void __user *arg)
218227
{
219228
const struct io_uring_napi curr = {
220-
.busy_poll_to = ctx->napi_busy_poll_to,
229+
.busy_poll_to = ktime_to_us(ctx->napi_busy_poll_dt),
221230
.prefer_busy_poll = ctx->napi_prefer_busy_poll
222231
};
223232
struct io_uring_napi napi;
224233

234+
if (ctx->flags & IORING_SETUP_IOPOLL)
235+
return -EINVAL;
225236
if (copy_from_user(&napi, arg, sizeof(napi)))
226237
return -EFAULT;
227238
if (napi.pad[0] || napi.pad[1] || napi.pad[2] || napi.resv)
@@ -230,7 +241,7 @@ int io_register_napi(struct io_ring_ctx *ctx, void __user *arg)
230241
if (copy_to_user(arg, &curr, sizeof(curr)))
231242
return -EFAULT;
232243

233-
WRITE_ONCE(ctx->napi_busy_poll_to, napi.busy_poll_to);
244+
WRITE_ONCE(ctx->napi_busy_poll_dt, napi.busy_poll_to * NSEC_PER_USEC);
234245
WRITE_ONCE(ctx->napi_prefer_busy_poll, !!napi.prefer_busy_poll);
235246
WRITE_ONCE(ctx->napi_enabled, true);
236247
return 0;
@@ -247,14 +258,14 @@ int io_register_napi(struct io_ring_ctx *ctx, void __user *arg)
247258
int io_unregister_napi(struct io_ring_ctx *ctx, void __user *arg)
248259
{
249260
const struct io_uring_napi curr = {
250-
.busy_poll_to = ctx->napi_busy_poll_to,
261+
.busy_poll_to = ktime_to_us(ctx->napi_busy_poll_dt),
251262
.prefer_busy_poll = ctx->napi_prefer_busy_poll
252263
};
253264

254265
if (arg && copy_to_user(arg, &curr, sizeof(curr)))
255266
return -EFAULT;
256267

257-
WRITE_ONCE(ctx->napi_busy_poll_to, 0);
268+
WRITE_ONCE(ctx->napi_busy_poll_dt, 0);
258269
WRITE_ONCE(ctx->napi_prefer_busy_poll, false);
259270
WRITE_ONCE(ctx->napi_enabled, false);
260271
return 0;
@@ -271,25 +282,14 @@ int io_unregister_napi(struct io_ring_ctx *ctx, void __user *arg)
271282
* the NAPI timeout accordingly.
272283
*/
273284
void __io_napi_adjust_timeout(struct io_ring_ctx *ctx, struct io_wait_queue *iowq,
274-
struct timespec64 *ts)
285+
ktime_t to_wait)
275286
{
276-
unsigned int poll_to = READ_ONCE(ctx->napi_busy_poll_to);
277-
278-
if (ts) {
279-
struct timespec64 poll_to_ts;
280-
281-
poll_to_ts = ns_to_timespec64(1000 * (s64)poll_to);
282-
if (timespec64_compare(ts, &poll_to_ts) < 0) {
283-
s64 poll_to_ns = timespec64_to_ns(ts);
284-
if (poll_to_ns > 0) {
285-
u64 val = poll_to_ns + 999;
286-
do_div(val, 1000);
287-
poll_to = val;
288-
}
289-
}
290-
}
287+
ktime_t poll_dt = READ_ONCE(ctx->napi_busy_poll_dt);
288+
289+
if (to_wait)
290+
poll_dt = min(poll_dt, to_wait);
291291

292-
iowq->napi_busy_poll_to = poll_to;
292+
iowq->napi_busy_poll_dt = poll_dt;
293293
}
294294

295295
/*
@@ -318,7 +318,7 @@ int io_napi_sqpoll_busy_poll(struct io_ring_ctx *ctx)
318318
LIST_HEAD(napi_list);
319319
bool is_stale = false;
320320

321-
if (!READ_ONCE(ctx->napi_busy_poll_to))
321+
if (!READ_ONCE(ctx->napi_busy_poll_dt))
322322
return 0;
323323
if (list_empty_careful(&ctx->napi_list))
324324
return 0;

io_uring/napi.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ int io_unregister_napi(struct io_ring_ctx *ctx, void __user *arg);
1818
void __io_napi_add(struct io_ring_ctx *ctx, struct socket *sock);
1919

2020
void __io_napi_adjust_timeout(struct io_ring_ctx *ctx,
21-
struct io_wait_queue *iowq, struct timespec64 *ts);
21+
struct io_wait_queue *iowq, ktime_t to_wait);
2222
void __io_napi_busy_loop(struct io_ring_ctx *ctx, struct io_wait_queue *iowq);
2323
int io_napi_sqpoll_busy_poll(struct io_ring_ctx *ctx);
2424

@@ -29,11 +29,11 @@ static inline bool io_napi(struct io_ring_ctx *ctx)
2929

3030
static inline void io_napi_adjust_timeout(struct io_ring_ctx *ctx,
3131
struct io_wait_queue *iowq,
32-
struct timespec64 *ts)
32+
ktime_t to_wait)
3333
{
3434
if (!io_napi(ctx))
3535
return;
36-
__io_napi_adjust_timeout(ctx, iowq, ts);
36+
__io_napi_adjust_timeout(ctx, iowq, to_wait);
3737
}
3838

3939
static inline void io_napi_busy_loop(struct io_ring_ctx *ctx,
@@ -55,7 +55,7 @@ static inline void io_napi_add(struct io_kiocb *req)
5555
struct io_ring_ctx *ctx = req->ctx;
5656
struct socket *sock;
5757

58-
if (!READ_ONCE(ctx->napi_busy_poll_to))
58+
if (!READ_ONCE(ctx->napi_busy_poll_dt))
5959
return;
6060

6161
sock = sock_from_file(req->file);
@@ -88,7 +88,7 @@ static inline void io_napi_add(struct io_kiocb *req)
8888
}
8989
static inline void io_napi_adjust_timeout(struct io_ring_ctx *ctx,
9090
struct io_wait_queue *iowq,
91-
struct timespec64 *ts)
91+
ktime_t to_wait)
9292
{
9393
}
9494
static inline void io_napi_busy_loop(struct io_ring_ctx *ctx,

io_uring/timeout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ void io_queue_linked_timeout(struct io_kiocb *req)
639639

640640
static bool io_match_task(struct io_kiocb *head, struct task_struct *task,
641641
bool cancel_all)
642-
__must_hold(&req->ctx->timeout_lock)
642+
__must_hold(&head->ctx->timeout_lock)
643643
{
644644
struct io_kiocb *req;
645645

io_uring/uring_cmd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags)
265265
req_set_fail(req);
266266
io_req_uring_cleanup(req, issue_flags);
267267
io_req_set_res(req, ret, 0);
268-
return ret < 0 ? ret : IOU_OK;
268+
return IOU_OK;
269269
}
270270

271271
int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw,

0 commit comments

Comments
 (0)