Skip to contentSkip to conversation

improve net busy polling time conversion

PATCH Applied Pavel Begunkov 2 participants 4 messages Jul 26, 2024 io_uring

lore
Share & tools
lore
#0 lore raw

cover improve net busy polling time conversion

Time conversions in io_uring/napi.* is a mess, clean it up, get rid of intermediate conversions to timespec and keep everything as ktime. Better than before, but still can use some extra changes in net/.

Pavel Begunkov (2):
io_uring/napi: use ktime in busy polling io_uring/napi: pass ktime to io_napi_adjust_timeout

 include/linux/io_uring_types.h |  2 +-
 io_uring/io_uring.c            |  6 ++--
 io_uring/io_uring.h            |  2 +-
 io_uring/napi.c                | 58 ++++++++++++++++------------------
 io_uring/napi.h                | 10 +++---
 5 files changed, 39 insertions(+), 39 deletions(-)
Signature · 1 line
2.45.2
#1 lore raw

PATCH 1/2 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.

---
 include/linux/io_uring_types.h |  2 +-
 io_uring/io_uring.h            |  2 +-
 io_uring/napi.c                | 48 +++++++++++++++++++---------------
 io_uring/napi.h                |  2 +-
 4 files changed, 30 insertions(+), 24 deletions(-)
include/linux/io_uring_types.h +1 -1
index e62aa9f0629f..3315005df117 100644
@@ -404,7 +404,7 @@ struct io_ring_ctx {
 	spinlock_t		napi_lock;	/* napi_list lock */
 
 	/* napi busy poll default timeout */
-	unsigned int		napi_busy_poll_to;
+	ktime_t			napi_busy_poll_dt;
 	bool			napi_prefer_busy_poll;
 	bool			napi_enabled;
 
io_uring/io_uring.h +1 -1
index e1ce908f0679..c2acf6180845 100644
@@ -43,7 +43,7 @@ struct io_wait_queue {
 	ktime_t timeout;
 
 #ifdef CONFIG_NET_RX_BUSY_POLL
-	unsigned int napi_busy_poll_to;
+	ktime_t napi_busy_poll_dt;
 	bool napi_prefer_busy_poll;
 #endif
 };
io_uring/napi.c +27 -21

122 lines not on this page · raw message

io_uring/napi.h +1 -1
index 6fc0393d0dbe..babbee36cd3e 100644
@@ -55,7 +55,7 @@ static inline void io_napi_add(struct io_kiocb *req)
 	struct io_ring_ctx *ctx = req->ctx;
 	struct socket *sock;
 
-	if (!READ_ONCE(ctx->napi_busy_poll_to))
+	if (!READ_ONCE(ctx->napi_busy_poll_dt))
 		return;
 
 	sock = sock_from_file(req->file);
Signature · 1 line
2.45.2
#2 lore raw

PATCH 2/2 io_uring/napi: pass ktime to io_napi_adjust_timeout

In reply to Pavel Begunkov

Pass the waiting time for __io_napi_adjust_timeout as ktime and get rid of all timespec64 conversions. It's especially simpler since the caller already have a ktime.

---
 io_uring/io_uring.c |  6 ++++--
 io_uring/napi.c     | 14 +++-----------
 io_uring/napi.h     |  8 ++++----
 3 files changed, 11 insertions(+), 17 deletions(-)
io_uring/io_uring.c +4 -2
index 2626424f5d73..3942db160f18 100644
@@ -2416,12 +2416,14 @@ static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events,
 
 	if (uts) {
 		struct timespec64 ts;
+		ktime_t dt;
 
 		if (get_timespec64(&ts, uts))
 			return -EFAULT;
 
-		iowq.timeout = ktime_add_ns(timespec64_to_ktime(ts), ktime_get_ns());
-		io_napi_adjust_timeout(ctx, &iowq, &ts);
+		dt = timespec64_to_ktime(ts);
+		iowq.timeout = ktime_add(dt, ktime_get());
+		io_napi_adjust_timeout(ctx, &iowq, dt);
 	}
 
 	if (sig) {
io_uring/napi.c +3 -11
index 6bdb267e9c33..4fd6bb331e1e 100644
@@ -282,20 +282,12 @@ int io_unregister_napi(struct io_ring_ctx *ctx, void __user *arg)
  * the NAPI timeout accordingly.
  */
 void __io_napi_adjust_timeout(struct io_ring_ctx *ctx, struct io_wait_queue *iowq,
-			      struct timespec64 *ts)
+			      ktime_t to_wait)
 {
 	ktime_t poll_dt = READ_ONCE(ctx->napi_busy_poll_dt);
 
-	if (ts) {
-		struct timespec64 poll_to_ts;
-
-		poll_to_ts = ns_to_timespec64(ktime_to_ns(poll_dt));
-		if (timespec64_compare(ts, &poll_to_ts) < 0) {
-			s64 poll_to_ns = timespec64_to_ns(ts);
-			if (poll_to_ns > 0)
-				poll_dt = ns_to_ktime(poll_to_ns);
-		}
-	}
+	if (to_wait)
+		poll_dt = min(poll_dt, to_wait);
 
 	iowq->napi_busy_poll_dt = poll_dt;
 }
io_uring/napi.h +4 -4
index babbee36cd3e..88f1c21d5548 100644
@@ -18,7 +18,7 @@ int io_unregister_napi(struct io_ring_ctx *ctx, void __user *arg);
 void __io_napi_add(struct io_ring_ctx *ctx, struct socket *sock);
 
 void __io_napi_adjust_timeout(struct io_ring_ctx *ctx,
-		struct io_wait_queue *iowq, struct timespec64 *ts);
+		struct io_wait_queue *iowq, ktime_t to_wait);
 void __io_napi_busy_loop(struct io_ring_ctx *ctx, struct io_wait_queue *iowq);
 int io_napi_sqpoll_busy_poll(struct io_ring_ctx *ctx);
 
@@ -29,11 +29,11 @@ static inline bool io_napi(struct io_ring_ctx *ctx)
 
 static inline void io_napi_adjust_timeout(struct io_ring_ctx *ctx,
 					  struct io_wait_queue *iowq,
-					  struct timespec64 *ts)
+					  ktime_t to_wait)
 {
 	if (!io_napi(ctx))
 		return;
-	__io_napi_adjust_timeout(ctx, iowq, ts);
+	__io_napi_adjust_timeout(ctx, iowq, to_wait);
 }
 
 static inline void io_napi_busy_loop(struct io_ring_ctx *ctx,
@@ -88,7 +88,7 @@ static inline void io_napi_add(struct io_kiocb *req)
 }
 static inline void io_napi_adjust_timeout(struct io_ring_ctx *ctx,
 					  struct io_wait_queue *iowq,
-					  struct timespec64 *ts)
+					  ktime_t to_wait)
 {
 }
 static inline void io_napi_busy_loop(struct io_ring_ctx *ctx,
Signature · 1 line
2.45.2
maintainer Applied
#3 lore raw
In reply to Pavel Begunkov
Quoted 9 lines · Pavel Begunkov

Time conversions in io_uring/napi.* is a mess, clean it up, get rid of intermediate conversions to timespec and keep everything as ktime. Better than before, but still can use some extra changes in net/.

Pavel Begunkov (2):
io_uring/napi: use ktime in busy polling io_uring/napi: pass ktime to io_napi_adjust_timeout

[...]

Applied, thanks!

[1/2] io_uring/napi: use ktime in busy polling

      commit: 342b2e395d5f34c9f111a818556e617939f83a8c
[2/2] io_uring/napi: pass ktime to io_napi_adjust_timeout
      commit: 358169617602f6f71b31e5c9532a09b95a34b043

Best regards,

Signature · 1 line
Jens Axboe