Skip to contentSkip to conversation

io_uring: use __kernel_timespec in timeout ABI

PATCH In review Arnd Bergmann 3 participants 9 messages Sep 30, 2019 – Oct 1, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 io_uring: use __kernel_timespec in timeout ABI

All system calls use struct __kernel_timespec instead of the old struct timespec, but this one was just added with the old-style ABI. Change it now to enforce the use of __kernel_timespec, avoiding ABI confusion and the need for compat handlers on 32-bit architectures.

Any user space caller will have to use __kernel_timespec now, but this is unambiguous and works for any C library regardless of the time_t definition. A nicer way to specify the timeout would have been a less ambiguous 64-bit nanosecond value, but I suppose it's too late now to change that as this would impact both 32-bit and 64-bit users.

Fixes 5262f567987d ("io_uring: IORING_OP_TIMEOUT support")
---
 fs/io_uring.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
fs/io_uring.c +4 -4
index aa8ac557493c..8a0381f1a43b 100644
@@ -1892,15 +1892,15 @@ static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 	unsigned count, req_dist, tail_index;
 	struct io_ring_ctx *ctx = req->ctx;
 	struct list_head *entry;
-	struct timespec ts;
+	struct timespec64 ts;
 
 	if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
 		return -EINVAL;
 	if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->timeout_flags ||
 	    sqe->len != 1)
 		return -EINVAL;
-	if (copy_from_user(&ts, (void __user *) (unsigned long) sqe->addr,
-	    sizeof(ts)))
+
+	if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr)))
 		return -EFAULT;
 
 	/*
@@ -1934,7 +1934,7 @@ static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 
 	hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
 	req->timeout.timer.function = io_timeout_fn;
-	hrtimer_start(&req->timeout.timer, timespec_to_ktime(ts),
+	hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts),
 			HRTIMER_MODE_REL);
 	return 0;
 }
Signature · 6 lines
2.20.0

_______________________________________________
Y2038 mailing list
[email protected]
https://lists.linaro.org/mailman/listinfo/y2038
maintainer
#1 lore raw
Quoted 10 lines · Arnd Bergmann

All system calls use struct __kernel_timespec instead of the old struct timespec, but this one was just added with the old-style ABI. Change it now to enforce the use of __kernel_timespec, avoiding ABI confusion and the need for compat handlers on 32-bit architectures.

Any user space caller will have to use __kernel_timespec now, but this is unambiguous and works for any C library regardless of the time_t definition. A nicer way to specify the timeout would have been a less ambiguous 64-bit nanosecond value, but I suppose it's too late now to change that as this would impact both 32-bit and 64-bit users.

Thanks for catching that, Arnd. Applied.

Signature · 1 line
Jens Axboe
maintainer
#2 lore raw
Quoted 6 lines · Jens Axboe
Quoted 5 lines · Arnd Bergmann

All system calls use struct __kernel_timespec instead of the old struct timespec, but this one was just added with the old-style ABI. Change it now to enforce the use of __kernel_timespec, avoiding ABI confusion and the need for compat handlers on 32-bit architectures.

Quoted 7 lines
Quoted 5 lines

Any user space caller will have to use __kernel_timespec now, but this is unambiguous and works for any C library regardless of the time_t definition. A nicer way to specify the timeout would have been a less ambiguous 64-bit nanosecond value, but I suppose it's too late now to change that as this would impact both 32-bit and 64-bit users.

Thanks for catching that, Arnd. Applied.

On second thought - since there appears to be no good 64-bit timespec available to userspace, the alternative here is including on in liburing. That seems kinda crappy in terms of API, so why not just use a 64-bit nsec value as you suggest? There's on released kernel with this feature yet, so there's nothing stopping us from just changing the API to be based on a single 64-bit nanosecond timeout.

fs/io_uring.c +3 -5
index dd094b387cab..de3d14fe3025 100644
@@ -1892,16 +1892,13 @@ static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 	unsigned count, req_dist, tail_index;
 	struct io_ring_ctx *ctx = req->ctx;
 	struct list_head *entry;
-	struct timespec ts;
+	u64 timeout;
 
 	if (unlikely(ctx->flags & IORING_SETUP_IOPOLL))
 		return -EINVAL;
 	if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->timeout_flags ||
 	    sqe->len != 1)
 		return -EINVAL;
-	if (copy_from_user(&ts, (void __user *) (unsigned long) sqe->addr,
-	    sizeof(ts)))
-		return -EFAULT;
 
 	/*
 	 * sqe->off holds how many events that need to occur for this
@@ -1932,9 +1929,10 @@ static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe)
 	list_add(&req->list, entry);
 	spin_unlock_irq(&ctx->completion_lock);
 
+	timeout = READ_ONCE(sqe->addr);
 	hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
 	req->timeout.timer.function = io_timeout_fn;
-	hrtimer_start(&req->timeout.timer, timespec_to_ktime(ts),
+	hrtimer_start(&req->timeout.timer, ns_to_ktime(timeout),
 			HRTIMER_MODE_REL);
 	return 0;
 }
Signature · 1 line
Jens Axboe
#3 lore raw
Quoted 14 lines · Jens Axboe
Quoted 12 lines · Jens Axboe
Quoted 10 lines · Arnd Bergmann

All system calls use struct __kernel_timespec instead of the old struct timespec, but this one was just added with the old-style ABI. Change it now to enforce the use of __kernel_timespec, avoiding ABI confusion and the need for compat handlers on 32-bit architectures.

Any user space caller will have to use __kernel_timespec now, but this is unambiguous and works for any C library regardless of the time_t definition. A nicer way to specify the timeout would have been a less ambiguous 64-bit nanosecond value, but I suppose it's too late now to change that as this would impact both 32-bit and 64-bit users.

Quoted 4 lines
Quoted 1 line

Thanks for catching that, Arnd. Applied.

On second thought - since there appears to be no good 64-bit timespec available to userspace, the alternative here is including on in liburing.

What's wrong with using __kernel_timespec? Just the name? I suppose liburing could add a macro to give it a different name for its users.

Quoted 4 lines

That seems kinda crappy in terms of API, so why not just use a 64-bit nsec value as you suggest? There's on released kernel with this feature yet, so there's nothing stopping us from just changing the API to be based on a single 64-bit nanosecond timeout.

Certainly fine with me.

fs/io_uring.c static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe) · line 1935

Quoted 5 lines
fs/io_uring.c +2 -1
+       timeout = READ_ONCE(sqe->addr);
        hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
        req->timeout.timer.function = io_timeout_fn;
-       hrtimer_start(&req->timeout.timer, timespec_to_ktime(ts),
+       hrtimer_start(&req->timeout.timer, ns_to_ktime(timeout),

It seems a little odd to use the 'addr' field as something that's not an address,
and I'm not sure I understand the logic behind when you use a READ_ONCE() as opposed to simply accessing the sqe the way it is done a few lines earlier.

The time handling definitely looks good to me.

Arnd

maintainer
#4 lore raw
Quoted 15 lines · Arnd Bergmann
Quoted 14 lines · Jens Axboe
Quoted 12 lines · Jens Axboe
Quoted 10 lines · Arnd Bergmann

All system calls use struct __kernel_timespec instead of the old struct timespec, but this one was just added with the old-style ABI. Change it now to enforce the use of __kernel_timespec, avoiding ABI confusion and the need for compat handlers on 32-bit architectures.

Any user space caller will have to use __kernel_timespec now, but this is unambiguous and works for any C library regardless of the time_t definition. A nicer way to specify the timeout would have been a less ambiguous 64-bit nanosecond value, but I suppose it's too late now to change that as this would impact both 32-bit and 64-bit users.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Thanks for catching that, Arnd. Applied.

On second thought - since there appears to be no good 64-bit timespec available to userspace, the alternative here is including on in liburing.

What's wrong with using __kernel_timespec? Just the name? I suppose liburing could add a macro to give it a different name for its users.

Just that it seems I need to make it available through liburing on systems that don't have it yet. Not a big deal, though.

Quoted 13 lines
Quoted 4 lines

That seems kinda crappy in terms of API, so why not just use a 64-bit nsec value as you suggest? There's on released kernel with this feature yet, so there's nothing stopping us from just changing the API to be based on a single 64-bit nanosecond timeout.

Certainly fine with me.

fs/io_uring.c static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe) · line 1935

Quoted 5 lines
fs/io_uring.c +2 -1
+       timeout = READ_ONCE(sqe->addr);
         hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
         req->timeout.timer.function = io_timeout_fn;
-       hrtimer_start(&req->timeout.timer, timespec_to_ktime(ts),
+       hrtimer_start(&req->timeout.timer, ns_to_ktime(timeout),
Quoted 7 lines

It seems a little odd to use the 'addr' field as something that's not an address,
and I'm not sure I understand the logic behind when you use a READ_ONCE() as opposed to simply accessing the sqe the way it is done a few lines earlier.

The time handling definitely looks good to me.

One thing that struck me about this approach - we then lose the ability to differentiate between "don't want a timed timeout" with ts == NULL, vs tv_sec and tv_nsec both being 0.

I think I'll stuck with that you had and just use __kernel_timespec in liburing.

Signature · 1 line
Jens Axboe
#5 lore raw
Quoted 9 lines · Jens Axboe
Quoted 5 lines · Arnd Bergmann

On Tue, Oct 1, 2019 at 5:38 PM Jens Axboe <[email protected]> wrote:

What's wrong with using __kernel_timespec? Just the name? I suppose liburing could add a macro to give it a different name for its users.

Just that it seems I need to make it available through liburing on systems that don't have it yet. Not a big deal, though.

Ah, right. I t would not cover the case of building against kernel headers earlier than linux-5.1 but running on a 5.4+ kernel.

I assumed that that you would require new kernel headers anyway, but if you have a copy of the io_uring header, that is not necessary.

Quoted 3 lines

One thing that struck me about this approach - we then lose the ability to differentiate between "don't want a timed timeout" with ts == NULL, vs tv_sec and tv_nsec both being 0.

You could always define a special constant such as
'#define IO_URING_TIMEOUT_NEVER -1ull' if you want to
support for 'never wait if it's not already done' and 'wait indefinitely'.

Quoted 2 lines

I think I'll stuck with that you had and just use __kernel_timespec in liburing.

Ok.

Arnd
_______________________________________________
Y2038 mailing list
[email protected]
https://lists.linaro.org/mailman/listinfo/y2038

maintainer
#6 lore raw
Quoted 8 lines · Arnd Bergmann
Quoted 7 lines · Jens Axboe
Quoted 5 lines · Arnd Bergmann

On Tue, Oct 1, 2019 at 5:38 PM Jens Axboe <[email protected]> wrote:

What's wrong with using __kernel_timespec? Just the name? I suppose liburing could add a macro to give it a different name for its users.

Quoted 8 lines
Quoted 2 lines

Just that it seems I need to make it available through liburing on systems that don't have it yet. Not a big deal, though.

Ah, right. I t would not cover the case of building against kernel headers earlier than linux-5.1 but running on a 5.4+ kernel.

I assumed that that you would require new kernel headers anyway, but if you have a copy of the io_uring header, that is not necessary.

Since I rely mostly on folks using liburing, we include the header as well. So I'm just going to use __kernel_timespec in liburing, and have a check to define it if we don't have it.

Quoted 7 lines
Quoted 3 lines

One thing that struck me about this approach - we then lose the ability to differentiate between "don't want a timed timeout" with ts == NULL, vs tv_sec and tv_nsec both being 0.

You could always define a special constant such as
'#define IO_URING_TIMEOUT_NEVER -1ull' if you want to
support for 'never wait if it's not already done' and 'wait indefinitely'.

That thought did occur to me, but that seems pretty ugly... The ts == NULL vs ts != NULL and timeout set is a more well understood pattern.

Signature · 1 line
Jens Axboe
#7 lore raw
In reply to Arnd Bergmann

* Arnd Bergmann:

Quoted 15 lines · Jens Axboe
Quoted 14 lines · Jens Axboe
Quoted 12 lines · Arnd Bergmann
Quoted 10 lines

All system calls use struct __kernel_timespec instead of the old struct timespec, but this one was just added with the old-style ABI. Change it now to enforce the use of __kernel_timespec, avoiding ABI confusion and the need for compat handlers on 32-bit architectures.

Any user space caller will have to use __kernel_timespec now, but this is unambiguous and works for any C library regardless of the time_t definition. A nicer way to specify the timeout would have been a less ambiguous 64-bit nanosecond value, but I suppose it's too late now to change that as this would impact both 32-bit and 64-bit users.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Thanks for catching that, Arnd. Applied.

On second thought - since there appears to be no good 64-bit timespec available to userspace, the alternative here is including on in liburing.

What's wrong with using __kernel_timespec? Just the name? I suppose liburing could add a macro to give it a different name for its users.

Yes, mostly the name.

__ names are reserved for the C/C++ implementation (which does not include the kernel). __kernel_timespec looks like an internal kernel type to the uninitiated, not a UAPI type.

Once we have struct timespec64 in userspace, you also end up with copying stuff around or introducing aliasing violations.

I'm not saying those concerns are valid, but you asked what's wrong with it. 8-)

Thanks,
Florian

maintainer
#8 lore raw
Quoted 32 lines · Florian Weimer

* Arnd Bergmann:

Quoted 15 lines · Jens Axboe
Quoted 14 lines · Jens Axboe
Quoted 12 lines · Arnd Bergmann
Quoted 10 lines

All system calls use struct __kernel_timespec instead of the old struct timespec, but this one was just added with the old-style ABI. Change it now to enforce the use of __kernel_timespec, avoiding ABI confusion and the need for compat handlers on 32-bit architectures.

Any user space caller will have to use __kernel_timespec now, but this is unambiguous and works for any C library regardless of the time_t definition. A nicer way to specify the timeout would have been a less ambiguous 64-bit nanosecond value, but I suppose it's too late now to change that as this would impact both 32-bit and 64-bit users.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Thanks for catching that, Arnd. Applied.

On second thought - since there appears to be no good 64-bit timespec available to userspace, the alternative here is including on in liburing.

What's wrong with using __kernel_timespec? Just the name? I suppose liburing could add a macro to give it a different name for its users.

Yes, mostly the name.

__ names are reserved for the C/C++ implementation (which does not include the kernel). __kernel_timespec looks like an internal kernel type to the uninitiated, not a UAPI type.

Quoted 5 lines

Once we have struct timespec64 in userspace, you also end up with copying stuff around or introducing aliasing violations.

I'm not saying those concerns are valid, but you asked what's wrong with it. 8-)

FWIW, I do agree, __kernel_timespec sounds like an internal type, not something apps should be using. timespec64 works a lot better for that. Oh well.

Signature · 1 line
Jens Axboe