kernel/cobalt/posix: Stop supporting timeout parameter in recvmmsg
PATCH 1/1 kernel/cobalt/posix: Stop supporting timeout parameter in recvmmsg
From: Jan Kiszka <[email protected]>
It turned out that trying to put a timer-based timeout on top of the loop that calls recvmsg_rt multiple times is broken in an almost unfixable way.
First of all, timing out by setting XNTIMEO and resuming the waiter (=caller of recvmmsg) races with entering and leaving the blocking state inside the respective driver. When the timer fires while the thread is not inside xnthread_suspend, the condition is lost, and we may even enter infinite blocking later on. This was observed in qemu setups with overloaded hosts, but it could also happen in proper deployment if the caller of recvmmsg is interrupted before suspending.
Second, the driver has no idea about the outer timeout and will therefore happily use its own per-socket timeout in addition when set. That can lead to unexpected results for the caller. Unfortunately, there is no standard way in RTDM to control the socket timeout, not to speak of an efficient one.
Finally, it would need an interface change in RTDM to collaborate with drivers for implementing the out timeout without races and without interference by the socket timeout. This appears not worth the effort.
Given that the socket timeout can still be used in place of the recvmmsg timeout, though not with identical semantics, we avoid complex refactoring in favor of discontinuing the timeout feature. Programs that continue to pass non-NULL timeout values will receive an error from now on.
As this obsoletes (and breaks) functional testing of the timeout via recvmmsg64, drop the related test case.
---
This is for next right now. I still haven't made up my mind how to deal best with stable (3.1..3.3). Removing the feature would break the ABI, but keeping it allows people to run into the race, even if only rarely. An option could be to disable the feature by default in Kconfig, allowing users to re-enable it while (hopefully) reading the related warning.
include/cobalt/kernel/rtdm/fd.h | 3 +- kernel/cobalt/posix/io.c | 8 +- kernel/cobalt/posix/syscall32.c | 9 +- kernel/cobalt/rtdm/fd.c | 66 ++----------- testsuite/smokey/y2038/syscall-tests.c | 129 ------------------------- 5 files changed, 11 insertions(+), 204 deletions(-)
include/cobalt/kernel/rtdm/fd.h +1 -2
@@ -381,8 +381,7 @@ ssize_t rtdm_fd_recvmsg(int ufd, struct user_msghdr *msg, int flags);int __rtdm_fd_recvmmsg (int ufd,void __user *u_msgvec,unsigned int vlen,unsigned int flags,void __user *u_timeout,int (*get_mmsg)(struct mmsghdr *mmsg,void __user *u_mmsg), -int (*put_mmsg)(void __user **u_mmsg_p,const struct mmsghdr *mmsg), -int (*get_timespec)(struct timespec64 *ts,const void __user *u_ts)); +int (*put_mmsg)(void __user **u_mmsg_p,const struct mmsghdr *mmsg)); int __rtdm_fd_recvmmsg64(int ufd, void __user *u_msgvec, unsigned int vlen, unsigned int flags, void __user *u_timeout,
kernel/cobalt/posix/io.c +1 -7
@@ -95,12 +95,6 @@ COBALT_SYSCALL(recvmsg, handover,return cobalt_copy_to_user (umsg, &m,sizeof (*umsg)) ?: ret; } -static int get_timespec (struct timespec64 *ts, -const void __user *u_ts) -{ -return cobalt_get_old_timespec (ts, u_ts); -} -static int get_mmsg (struct mmsghdr *mmsg,void __user *u_mmsg) {return cobalt_copy_from_user (mmsg, u_mmsg,sizeof (*mmsg));
@@ -119,7 +113,7 @@ COBALT_SYSCALL(recvmmsg, primary, unsigned int flags, struct __kernel_old_timespec __user *u_timeout)) { return __rtdm_fd_recvmmsg(fd, u_msgvec, vlen, flags, u_timeout, -get_mmsg ,put_mmsg ,get_timespec ); +get_mmsg ,put_mmsg ); } COBALT_SYSCALL(recvmmsg64, primary,
kernel/cobalt/posix/syscall32.c +1 -8
@@ -552,12 +552,6 @@ COBALT_SYSCALL32emu(recvmsg, handover,return sys32_put_msghdr (umsg, &m) ?: ret; } -static int get_timespec32 (struct timespec64 *ts, -const void __user *u_ts) -{ -return sys32_get_timespec (ts, u_ts); -} -static int get_mmsg32 (struct mmsghdr *mmsg,void __user *u_mmsg) {return sys32_get_mmsghdr (mmsg, u_mmsg);
@@ -576,8 +570,7 @@ COBALT_SYSCALL32emu(recvmmsg, primary, unsigned int flags, struct old_timespec32 *u_timeout)) { return __rtdm_fd_recvmmsg(ufd, u_msgvec, vlen, flags, u_timeout, -get_mmsg32 ,put_mmsg32 , -get_timespec32 ); +get_mmsg32 ,put_mmsg32 ); } COBALT_SYSCALL32emu(recvmmsg64, primary,
kernel/cobalt/rtdm/fd.c +8 -58
105 lines not on this page · raw message
testsuite/smokey/y2038/syscall-tests.c +0 -129
141 lines not on this page · raw message
Signature · 1 line
2.43.0
Quoted 368 lines · Jan Kiszka
From: Jan Kiszka <[email protected]>
It turned out that trying to put a timer-based timeout on top of the loop that calls
recvmsg_rtmultiple times is broken in an almost unfixable way.First of all, timing out by setting XNTIMEO and resuming the waiter (=caller of recvmmsg) races with entering and leaving the blocking state inside the respective driver. When the timer fires while the thread is not inside
xnthread_suspend, the condition is lost, and we may even enter infinite blocking later on. This was observed in qemu setups with overloaded hosts, but it could also happen in proper deployment if the caller of recvmmsg is interrupted before suspending.Second, the driver has no idea about the outer timeout and will therefore happily use its own per-socket timeout in addition when set. That can lead to unexpected results for the caller. Unfortunately, there is no standard way in RTDM to control the socket timeout, not to speak of an efficient one.
Finally, it would need an interface change in RTDM to collaborate with drivers for implementing the out timeout without races and without interference by the socket timeout. This appears not worth the effort.
Given that the socket timeout can still be used in place of the recvmmsg timeout, though not with identical semantics, we avoid complex refactoring in favor of discontinuing the timeout feature. Programs that continue to pass non-NULL timeout values will receive an error from now on.
As this obsoletes (and breaks) functional testing of the timeout via recvmmsg64, drop the related test case.
Signed-off-by Jan Kiszka <[email protected]>---
This is for next right now. I still haven't made up my mind how to deal best with stable (3.1..3.3). Removing the feature would break the ABI, but keeping it allows people to run into the race, even if only rarely. An option could be to disable the feature by default in
Kconfig, allowing users to re-enable it while (hopefully) reading the related warning.include/cobalt/kernel/rtdm/fd.h | 3 +- kernel/cobalt/posix/io.c | 8 +- kernel/cobalt/posix/syscall32.c | 9 +- kernel/cobalt/rtdm/fd.c | 66 ++----------- testsuite/smokey/y2038/syscall-tests.c | 129 ------------------------- 5 files changed, 11 insertions(+), 204 deletions(-)include/cobalt/kernel/rtdm/fd.h +1
-2@@ -381,8 +381,7 @@ ssize_t rtdm_fd_recvmsg(int ufd, struct user_msghdr *msg, int flags);int __rtdm_fd_recvmmsg (int ufd,void __user *u_msgvec,unsigned int vlen,unsigned int flags,void __user *u_timeout,int (*get_mmsg)(struct mmsghdr *mmsg,void __user *u_mmsg), -int (*put_mmsg)(void __user **u_mmsg_p,const struct mmsghdr *mmsg), -int (*get_timespec)(struct timespec64 *ts,const void __user *u_ts)); +int (*put_mmsg)(void __user **u_mmsg_p,const struct mmsghdr *mmsg)); int __rtdm_fd_recvmmsg64(int ufd, void __user *u_msgvec, unsigned int vlen, unsigned int flags, void __user *u_timeout,kernel/cobalt/posix/io.c +1
-7@@ -95,12 +95,6 @@ COBALT_SYSCALL(recvmsg, handover,return cobalt_copy_to_user (umsg, &m,sizeof (*umsg)) ?: ret; } -static int get_timespec (struct timespec64 *ts, -const void __user *u_ts) -{ -return cobalt_get_old_timespec (ts, u_ts); -} -static int get_mmsg (struct mmsghdr *mmsg,void __user *u_mmsg) {return cobalt_copy_from_user (mmsg, u_mmsg,sizeof (*mmsg));@@ -119,7 +113,7 @@ COBALT_SYSCALL(recvmmsg, primary, unsigned int flags, struct __kernel_old_timespec __user *u_timeout)) { return __rtdm_fd_recvmmsg(fd, u_msgvec, vlen, flags, u_timeout, -get_mmsg ,put_mmsg ,get_timespec ); +get_mmsg ,put_mmsg ); } COBALT_SYSCALL(recvmmsg64, primary,kernel/cobalt/posix/syscall32.c +1
-8@@ -552,12 +552,6 @@ COBALT_SYSCALL32emu(recvmsg, handover,return sys32_put_msghdr (umsg, &m) ?: ret; } -static int get_timespec32 (struct timespec64 *ts, -const void __user *u_ts) -{ -return sys32_get_timespec (ts, u_ts); -} -static int get_mmsg32 (struct mmsghdr *mmsg,void __user *u_mmsg) {return sys32_get_mmsghdr (mmsg, u_mmsg);@@ -576,8 +570,7 @@ COBALT_SYSCALL32emu(recvmmsg, primary, unsigned int flags, struct old_timespec32 *u_timeout)) { return __rtdm_fd_recvmmsg(ufd, u_msgvec, vlen, flags, u_timeout, -get_mmsg32 ,put_mmsg32 , -get_timespec32 ); +get_mmsg32 ,put_mmsg32 ); } COBALT_SYSCALL32emu(recvmmsg64, primary,kernel/cobalt/rtdm/fd.c +8
-58@@ -641,36 +641,16 @@ out: }EXPORT_SYMBOL_GPL (rtdm_fd_recvmsg); -struct cobalt_recvmmsg_timer { -struct xntimer timer; -struct xnthread *waiter; -}; - -static void recvmmsg_timeout_handler (struct xntimer *timer) -{ -struct cobalt_recvmmsg_timer *rq; - - rq = container_of(timer, struct cobalt_recvmmsg_timer, timer); -xnthread_set_info (rq->waiter,XNTIMEO ); -xnthread_resume (rq->waiter,XNDELAY ); -} -int __rtdm_fd_recvmmsg (int ufd,void __user *u_msgvec,unsigned int vlen,unsigned int flags,void __user *u_timeout,int (*get_mmsg)(struct mmsghdr *mmsg,void __user *u_mmsg), -int (*put_mmsg)(void __user **u_mmsg_p,const struct mmsghdr *mmsg), -int (*get_timespec)(struct timespec64 *ts,const void __user *u_ts)) +int (*put_mmsg)(void __user **u_mmsg_p,const struct mmsghdr *mmsg)) { -struct cobalt_recvmmsg_timer rq; -xntmode_t tmode =XN_RELATIVE ; -struct timespec64 ts = {0 };int ret =0 , datagrams =0 ; -xnticks_t timeout =0 ;struct mmsghdr mmsg;struct rtdm_fd *fd;void __user *u_p;ssize_t len; -spl_t s; fd =rtdm_fd_get (ufd,0 );if (IS_ERR (fd)) {@@ -683,30 +663,12 @@ int __rtdm_fd_recvmmsg(int ufd, void __user *u_msgvec, unsigned int vlen,trace_cobalt_fd_recvmmsg (current, fd, ufd, flags);if (u_timeout) { - ret =get_timespec (&ts, u_timeout); -if (ret) - goto fail; - -if (!timespec64_valid (&ts)) { - ret = -EINVAL ; - goto fail; - } - - tmode =XN_ABSOLUTE ; - timeout =ts2ns (&ts); -if (timeout ==0 ) - flags |=MSG_DONTWAIT ; -else { - timeout +=xnclock_read_monotonic (&nkclock); - rq.waiter =xnthread_current (); -xntimer_init (&rq.timer, &nkclock, - recvmmsg_timeout_handler, -NULL ,XNTIMER_IGRAVITY ); -xnlock_get_irqsave (&nklock, s); - ret =xntimer_start (&rq.timer, timeout, -XN_INFINITE , tmode); -xnlock_put_irqrestore (&nklock, s); - } +/* +* The timeout parameter is not supported. +* Use recvmsg timeouts instead. +*/ + ret = -EINVAL ; + goto fail; }if (fd->oflags &O_NONBLOCK )@@ -733,12 +695,6 @@ int __rtdm_fd_recvmmsg(int ufd, void __user *u_msgvec, unsigned int vlen, flags |=MSG_DONTWAIT ; } -if (timeout) { -xnlock_get_irqsave (&nklock, s); -xntimer_destroy (&rq.timer); -xnlock_put_irqrestore (&nklock, s); - } - fail:rtdm_fd_put (fd);@@ -751,12 +707,6 @@ out:return ret; } -static inline int __rtdm_fetch_timeout64 (struct timespec64 *ts, -const void __user *u_ts) -{ -return u_ts ==NULL ? -EFAULT :cobalt_get_timespec64 (ts, u_ts); -} - int __rtdm_fd_recvmmsg64(int ufd, void __user *u_msgvec, unsigned int vlen, unsigned int flags, void __user *u_timeout, int (*get_mmsg)(struct mmsghdr *mmsg,@@ -765,7 +715,7 @@ int __rtdm_fd_recvmmsg64(int ufd, void __user *u_msgvec, unsigned int vlen, const struct mmsghdr *mmsg)) {return __rtdm_fd_recvmmsg (ufd, u_msgvec, vlen, flags, u_timeout, - get_mmsg, put_mmsg, __rtdm_fetch_timeout64); + get_mmsg, put_mmsg); }testsuite/smokey/y2038/syscall-tests.c +0
-125@@ -970,131 +970,6 @@ static int test_sc_cobalt_event_wait64(void)return 0 ; } -static int test_sc_cobalt_recvmmsg64 (void ) -{ -int sc_nr = sc_cobalt_recvmmsg64; -struct rtipc_port_label plabel; -struct xn_timespec64 t1, t2; -struct sockaddr_ipc saddr; -struct timespec ts_nat; -int ret =0 ; -long data; -int sock; - -struct iovec iov = { - .iov_base = &data, - .iov_len =sizeof (data), - }; -struct msghdr msg = { - .msg_name =NULL , - .msg_namelen =0 , - .msg_iov = &iov, - .msg_iovlen =1 , - .msg_control =NULL , - .msg_controllen =0 , - }; - - sock =smokey_check_errno (socket (AF_RTIPC ,SOCK_DGRAM ,IPCPROTO_XDDP )); -if (sock == -EAFNOSUPPORT ) { -smokey_note ("recvmmsg64: skipped. (no kernel support)" ); -return 0 ; - } -if (sock <0 ) -return sock; - -strcpy (plabel.label,"y2038:recvmmsg64" ); - ret =smokey_check_errno (setsockopt (sock,SOL_XDDP ,XDDP_LABEL , &plabel, -sizeof (plabel))); -if (ret) - goto out; - -memset (&saddr,0 ,sizeof (saddr)); - saddr.sipc_family =AF_RTIPC ; - saddr.sipc_port =-1 ; - ret =smokey_check_errno (bind (sock, (struct sockaddr *)&saddr, -sizeof (saddr))); -if (ret) - goto out; - -/* Make sure we don't crash because of NULL pointers */ - ret =XENOMAI_SYSCALL5 (sc_nr,NULL ,NULL ,NULL ,NULL ,NULL ); -if (ret == -ENOSYS ) { -smokey_note ("recvmmsg64: skipped. (no kernel support)" ); - goto out;// Not implemented, nothing to test, success - } -if (!smokey_assert (ret == -EADV )) { - ret = ret ? ret : -EINVAL ; - goto out; - } - -/* Providing an invalid address has to deliver EFAULT */ - ret =XENOMAI_SYSCALL5 (sc_nr, sock, &msg,sizeof (msg),0 , - (void *)0xdeadbeefUL ); -if (!smokey_assert (ret == -EFAULT )) { - ret = ret ? ret : -EINVAL ; - goto out; - } - -/* -* providing an invalid timeout has to deliver EINVAL -*/ - t1.tv_sec =-1 ; - ret =XENOMAI_SYSCALL5 (sc_nr, sock, &msg,sizeof (msg),0 , &t1); -if (!smokey_assert (ret == -EINVAL )) { - ret = ret ? ret : -EINVAL ; - goto out; - } - -/* -* providing a zero timeout, -* should come back immediately with EWOULDBLOCK -*/ - t1.tv_sec =0 ; - t1.tv_nsec =0 ; - ret =XENOMAI_SYSCALL5 (sc_nr, sock, &msg,sizeof (msg),0 , &t1); -if (!smokey_assert (ret == -EWOULDBLOCK )) { - ret = ret ? ret : -EINVAL ; - goto out; - } - -/* -* Providing a valid timeout, waiting for it to time out and check -* that we didn't come back to early. -*/ - ret =smokey_check_errno (clock_gettime (CLOCK_MONOTONIC , &ts_nat)); -if (ret) - goto out; - - t1.tv_sec =0 ; - t1.tv_nsec =500000 ; - - ret =XENOMAI_SYSCALL5 (sc_nr, sock, &msg,sizeof (msg),0 , &t1); -if (!smokey_assert (ret == -ETIMEDOUT )) { - ret = ret ? ret : -EINVAL ; - goto out; - } - - t1.tv_sec = ts_nat.tv_sec; - t1.tv_nsec = ts_nat.tv_nsec; - - ret =smokey_check_errno (clock_gettime (CLOCK_MONOTONIC , &ts_nat)); -if (ret) - goto out; - - t2.tv_sec = ts_nat.tv_sec; - t2.tv_nsec = ts_nat.tv_nsec; - -if (ts_less (&t2, &t1)) -smokey_warning ("recvmmsg64 returned to early!\n" -"Expected wakeup at: %lld sec %lld nsec\n" -"Back at : %lld sec %lld nsec\n" , - t1.tv_sec, t1.tv_nsec, t2.tv_sec, t2.tv_nsec); - -out: -close (sock); -return ret; -} -static int test_sc_cobalt_cond_wait_prologue64 (void ) {int sc_nr = sc_cobalt_cond_wait_prologue64;
testsuite/smokey/y2038/syscall-tests.c static int run_y2038(struct smokey_test *t, int argc, char *const argv[]) · line 1730
Quoted 7 lines
testsuite/smokey/y2038/syscall-tests.c +0
-4@@ -1724,10 +1599,6 @@ static int run_y2038(struct smokey_test *t, int argc, char *const argv[])if (ret)return ret; - ret =test_sc_cobalt_recvmmsg64 (); -if (ret) -return ret; -
Instead of removing the complete testcase we could change the test to cover the -EINVAL return value if someone tries to set a timeout.
Quoted 5 lines
testsuite/smokey/y2038/syscall-tests.c +0
-0ret =test_sc_cobalt_cond_wait_prologue64 ();if (ret)return ret;Signature · 1 line
2.43.0