Skip to content

Commit 342b2e3

Browse files
isilenceaxboe
authored andcommitted
io_uring/napi: use ktime in busy polling
It's more natural to use ktime/ns instead of keeping around usec, especially since we're comparing it against user provided timers, so convert napi busy poll internal handling to ktime. It's also nicer since the type (ktime_t vs unsigned long) now tells the unit of measure. Keep everything as ktime, which we convert to/from micro seconds for IORING_[UN]REGISTER_NAPI. The net/ busy polling works seems to work with usec, however it's not real usec as shift by 10 is used to get it from nsecs, see busy_loop_current_time(), so it's easy to get truncated nsec back and we get back better precision. Note, we can further improve it later by removing the truncation and maybe convincing net/ to use ktime/ns instead. Signed-off-by: Pavel Begunkov <[email protected]> Link: https://lore.kernel.org/r/95e7ec8d095069a3ed5d40a4bc6f8b586698bc7e.1722003776.git.asml.silence@gmail.com Signed-off-by: Jens Axboe <[email protected]>
1 parent 0db4618 commit 342b2e3

File tree

4 files changed

+30
-24
lines changed

4 files changed

+30
-24
lines changed

include/linux/io_uring_types.h

Lines changed: 1 addition & 1 deletion
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

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/napi.c

Lines changed: 27 additions & 21 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,7 +226,7 @@ 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;
@@ -232,7 +241,7 @@ int io_register_napi(struct io_ring_ctx *ctx, void __user *arg)
232241
if (copy_to_user(arg, &curr, sizeof(curr)))
233242
return -EFAULT;
234243

235-
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);
236245
WRITE_ONCE(ctx->napi_prefer_busy_poll, !!napi.prefer_busy_poll);
237246
WRITE_ONCE(ctx->napi_enabled, true);
238247
return 0;
@@ -249,14 +258,14 @@ int io_register_napi(struct io_ring_ctx *ctx, void __user *arg)
249258
int io_unregister_napi(struct io_ring_ctx *ctx, void __user *arg)
250259
{
251260
const struct io_uring_napi curr = {
252-
.busy_poll_to = ctx->napi_busy_poll_to,
261+
.busy_poll_to = ktime_to_us(ctx->napi_busy_poll_dt),
253262
.prefer_busy_poll = ctx->napi_prefer_busy_poll
254263
};
255264

256265
if (arg && copy_to_user(arg, &curr, sizeof(curr)))
257266
return -EFAULT;
258267

259-
WRITE_ONCE(ctx->napi_busy_poll_to, 0);
268+
WRITE_ONCE(ctx->napi_busy_poll_dt, 0);
260269
WRITE_ONCE(ctx->napi_prefer_busy_poll, false);
261270
WRITE_ONCE(ctx->napi_enabled, false);
262271
return 0;
@@ -275,23 +284,20 @@ int io_unregister_napi(struct io_ring_ctx *ctx, void __user *arg)
275284
void __io_napi_adjust_timeout(struct io_ring_ctx *ctx, struct io_wait_queue *iowq,
276285
struct timespec64 *ts)
277286
{
278-
unsigned int poll_to = READ_ONCE(ctx->napi_busy_poll_to);
287+
ktime_t poll_dt = READ_ONCE(ctx->napi_busy_poll_dt);
279288

280289
if (ts) {
281290
struct timespec64 poll_to_ts;
282291

283-
poll_to_ts = ns_to_timespec64(1000 * (s64)poll_to);
292+
poll_to_ts = ns_to_timespec64(ktime_to_ns(poll_dt));
284293
if (timespec64_compare(ts, &poll_to_ts) < 0) {
285294
s64 poll_to_ns = timespec64_to_ns(ts);
286-
if (poll_to_ns > 0) {
287-
u64 val = poll_to_ns + 999;
288-
do_div(val, 1000);
289-
poll_to = val;
290-
}
295+
if (poll_to_ns > 0)
296+
poll_dt = ns_to_ktime(poll_to_ns);
291297
}
292298
}
293299

294-
iowq->napi_busy_poll_to = poll_to;
300+
iowq->napi_busy_poll_dt = poll_dt;
295301
}
296302

297303
/*
@@ -320,7 +326,7 @@ int io_napi_sqpoll_busy_poll(struct io_ring_ctx *ctx)
320326
LIST_HEAD(napi_list);
321327
bool is_stale = false;
322328

323-
if (!READ_ONCE(ctx->napi_busy_poll_to))
329+
if (!READ_ONCE(ctx->napi_busy_poll_dt))
324330
return 0;
325331
if (list_empty_careful(&ctx->napi_list))
326332
return 0;

io_uring/napi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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);

0 commit comments

Comments
 (0)