EADV-related fixes, select refactorings, restartable compat syscalls
cover EADV-related fixes, select refactorings, restartable compat syscalls
This should resolved the EADV bug seen with select when called with Linux fds. It also fixes wrong EADV reporting for other services. And it adds the missing syscall restart support for clock_nanosleep and select when taking the compat path.
Please review and test carefully, some details are rather tricky.
I'm considering the EADV fixes for stable as well, though they will alter the return codes. But the current scheme was broken anyway.
Jan
Jan Kiszka (9):
cobalt/posix/io: Remove unneeded special case of select_bind_one cobalt/posix/io: Avoid err variable shadowing cobalt/rtdm/fd: Fix comment typo
cobalt/rtdm/fd: Fix error code documentation of rtdm_fd_select cobalt/rtdm/fd: Fix default handler setups cobalt/select: Add proper handling of EADV cobalt/posix/syscall32: Mark compat clock_nanosleep restartable cobalt/posix/compat: Privatize sys32_get/put_fdset cobalt/posix/io: Unify native and compat versions of select
include/cobalt/kernel/compat.h | 8 +- kernel/cobalt/posix/compat.c | 14 ++-- kernel/cobalt/posix/io.c | 131 +++++++++++++++++++++----------- kernel/cobalt/posix/io.h | 3 + kernel/cobalt/posix/syscall32.c | 93 +---------------------- kernel/cobalt/rtdm/fd.c | 69 ++++++++++------- lib/cobalt/select.c | 2 +- 7 files changed, 146 insertions(+), 174 deletions(-)
Signature · 1 line
2.26.2
PATCH 1/9 cobalt/posix/io: Remove unneeded special case of select_bind_one
From: Jan Kiszka <[email protected]>
Already at the time of introduction (8ac3db811d21), -ENOENT was not returned by any sane function behind rtdm_fd_select(). Today rtdm_fd_select() officially only returns -EBADF or -EINVAL. By now it also returns -EADV, via rtdm_fd_get() or a default select handler. The only remaining possibility for -ENOENT is a select handler. In-tree, a4l_rt_select actually has such a case, but for no sane reason this or any out-of-tree driver case should receive special handling here.
Remove this case, these is also no need for the select_bind_one wrapper anymore.
--- kernel/cobalt/posix/io.c | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-)
kernel/cobalt/posix/io.c +1 -12
@@ -180,17 +180,6 @@ int __cobalt_first_fd_valid_p(fd_set *fds[XNSELECT_MAX_TYPES], int nfds)return 1 ; } -static int select_bind_one (struct xnselector *selector,unsigned type,int fd) -{ -int rc; - - rc =rtdm_fd_select (fd, selector, type); -if (rc != -ENOENT ) -return rc; - -return -EBADF ; -} -int __cobalt_select_bind_all (struct xnselector *selector,fd_set *fds[XNSELECT_MAX_TYPES ],int nfds) {
@@ -203,7 +192,7 @@ int __cobalt_select_bind_all(struct xnselector *selector,for (fd =find_first_bit (set->fds_bits, nfds); fd < nfds; fd =find_next_bit (set->fds_bits, nfds, fd +1 )) { - err =select_bind_one (selector, type, fd); + err =rtdm_fd_select (fd, selector, type);if (err)return err; }
Signature · 1 line
2.26.2
PATCH 2/9 cobalt/posix/io: Avoid err variable shadowing
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
It worked, but it is not nice to read.
--- kernel/cobalt/posix/io.c | 8 ++++---- kernel/cobalt/posix/syscall32.c | 7 ++++--- 2 files changed, 8 insertions(+), 7 deletions(-)
kernel/cobalt/posix/io.c +4 -4
@@ -291,11 +291,11 @@ COBALT_SYSCALL(select, primary,do { err =xnselect (selector, out_fds, in_fds, nfds, timeout, mode); -if (err == -ECHRNG ) { -int err =__cobalt_select_bind_all (selector, out_fds, nfds); -if (err) -return err; +int bind_err =__cobalt_select_bind_all (selector, + out_fds, nfds); +if (bind_err) +return bind_err; } }while (err == -ECHRNG );
kernel/cobalt/posix/syscall32.c +4 -3
@@ -814,9 +814,10 @@ COBALT_SYSCALL32emu(select, nonrestartable,do { err =xnselect (selector, out_fds, in_fds, nfds, timeout, mode);if (err == -ECHRNG ) { -int err =__cobalt_select_bind_all (selector, out_fds, nfds); -if (err) -return err; +int bind_err =__cobalt_select_bind_all (selector, + out_fds, nfds); +if (bind_err) +return bind_err; } }while (err == -ECHRNG );
Signature · 1 line
2.26.2
PATCH 3/9 cobalt/rtdm/fd: Fix comment typo
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
--- kernel/cobalt/rtdm/fd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/cobalt/rtdm/fd.c +1 -1
@@ -223,7 +223,7 @@ int rtdm_device_new_fd(struct rtdm_fd *fd, int ufd,* - ERR_PTR(-EADV) if the use-space handle is either invalid, or not * managed by RTDM. * -* - ERR_PTR(-EBADF) if the underlying device is being torned down at +* - ERR_PTR(-EBADF) if the underlying device is being torn down at * the time of the call. * * @note The file descriptor returned must be later released by a call
Signature · 1 line
2.26.2
PATCH 4/9 cobalt/rtdm/fd: Fix error code documentation of rtdm_fd_select
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
It also returns -EADV since 2db562ad62ec - causing troubles in select.
--- kernel/cobalt/rtdm/fd.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/cobalt/rtdm/fd.c +5 -1
@@ -938,7 +938,11 @@ int rtdm_fd_valid_p(int ufd)* * @return 0 on success, otherwise: * -* - -EBADF is returned if the file descriptor @a ufd cannot be resolved. +* - -EADV is returned if the file descriptor @a ufd cannot be resolved. +* +* - -EBADF is returned if the underlying device is being torn down at the time +* of the call. +* * - -EINVAL is returned if @a type is invalid. * * @coretags{task-unrestricted}
Signature · 1 line
2.26.2
PATCH 5/9 cobalt/rtdm/fd: Fix default handler setups
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
2db562ad62ec had a point in avoiding EBADF for file descriptors that are not under RTDM management. But it unfortunately got a lot of other cases wrong along this.
This one is about returning the right error code if an RTDM driver does not implement a particular handler. That must never be EADV because that error would signal "not an RTDM fd, retry with Linux" which is obviously wrong. Even worse, mmap clearly defines the right error to return in case it is not implemented, and that is ENODEV as was returned before.
So this restores ENODEV as invalid default for mmap, uses ENOTTY for unimplemented IOCTL and switches all others to EBADF which is a match for select and a reasonable choice for read/write/recvmsg/sendmsg.
--- kernel/cobalt/rtdm/fd.c | 61 ++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 25 deletions(-)
kernel/cobalt/rtdm/fd.c +36 -25
@@ -52,9 +52,19 @@ static int enosys(void)return -ENOSYS ; } -static int eadv (void ) +static int enotty (void ) { -return -EADV ; +return -ENOTTY ; +} + +static int ebadf (void ) +{ +return -EBADF ; +} + +static int enodev (void ) +{ +return -ENODEV ; } static inline struct rtdm_fd_index *
@@ -76,24 +86,23 @@ static struct rtdm_fd *fetch_fd(struct cobalt_ppd *p, int ufd)return idx->fd; } -#define assign_invalid_handler (__handler) \ +#define assign_invalid_handler (__handler, __invalid) \ do \ - (__handler) = (typeof(__handler))eadv; \ + (__handler) = (typeof(__handler))__invalid; \ while (0) -#define __assign_default_handler (__handler, __placeholder) \ +/* Calling this handler should beget ENOSYS if not implemented. */ +#define assign_switch_handler (__handler) \ do \ if ((__handler) == NULL) \ - (__handler) = (typeof(__handler))__placeholder; \ + assign_invalid_handler(__handler, enosys); \ while (0) -/* Calling this handler should beget EADV if not implemented. */ -#define assign_invalid_default_handler (__handler) \ - __assign_default_handler(__handler, eadv) - -/* Calling this handler should beget ENOSYS if not implemented. */ -#define assign_default_handler (__handler) \ - __assign_default_handler(__handler, enosys) +#define assign_default_handler (__handler, __invalid) \ + do \ + if ((__handler) == NULL) \ + assign_invalid_handler(__handler, __invalid); \ + while (0)#define __rt (__handler) __handler ## _rt#define __nrt (__handler) __handler ## _nrt
@@ -103,14 +112,16 @@ static struct rtdm_fd *fetch_fd(struct cobalt_ppd *p, int ufd)* are implemented, ENOSYS otherwise for NULL handlers to trigger the * adaptive switch. */ -#define assign_default_dual_handlers (__handler) \ +#define assign_default_dual_handlers (__handler, __invalid_handler) \ do \ if (__rt(__handler) || __nrt(__handler)) { \ - assign_default_handler(__rt(__handler)); \ - assign_default_handler(__nrt(__handler)); \ + assign_switch_handler(__rt(__handler)); \ + assign_switch_handler(__nrt(__handler)); \ } else { \ - assign_invalid_handler(__rt(__handler)); \ - assign_invalid_handler(__nrt(__handler)); \ + assign_invalid_handler(__rt(__handler), \ + __invalid_handler); \ + assign_invalid_handler(__nrt(__handler), \ + __invalid_handler); \ } \ while (0)
@@ -147,13 +158,13 @@ int rtdm_fd_enter(struct rtdm_fd *fd, int ufd, unsigned int magic,if (magic ==0 )return -EINVAL ; -assign_default_dual_handlers (ops->ioctl); -assign_default_dual_handlers (ops->read); -assign_default_dual_handlers (ops->write); -assign_default_dual_handlers (ops->recvmsg); -assign_default_dual_handlers (ops->sendmsg); -assign_invalid_default_handler (ops->select); -assign_invalid_default_handler (ops->mmap); +assign_default_dual_handlers (ops->ioctl, enotty); +assign_default_dual_handlers (ops->read, ebadf); +assign_default_dual_handlers (ops->write, ebadf); +assign_default_dual_handlers (ops->recvmsg, ebadf); +assign_default_dual_handlers (ops->sendmsg, ebadf); +assign_default_handler (ops->select, ebadf); +assign_default_handler (ops->mmap, enodev); ppd =cobalt_ppd_get (0 ); fd->magic = magic;
Signature · 1 line
2.26.2
PATCH 6/9 cobalt/select: Add proper handling of EADV
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
Since rtdm_fd_select started to return EADV for unknown fds (2db562ad62ec), we were returning that code to applications in case a selector had been allocated for the caller already. That was because libcobalt lacked handling of this error.
This fixes it and also establish proper signaling of EBADF in case the is a real error related to an RTDM fd. For that purpose, detect if __cobalt_select_bind_all got EADV for the first fd or for a succeeding one, i.e. on an unsupported mixed fd set.
Also return EADV if the early __cobalt_first_fd_valid_p test fails. That ensures that we only retry with the regular Linux call when it makes sense.
--- kernel/cobalt/posix/io.c | 13 +++++++++++-- kernel/cobalt/posix/syscall32.c | 2 +- lib/cobalt/select.c | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-)
kernel/cobalt/posix/io.c +11 -2
@@ -183,6 +183,7 @@ int __cobalt_first_fd_valid_p(fd_set *fds[XNSELECT_MAX_TYPES], int nfds)int __cobalt_select_bind_all (struct xnselector *selector,fd_set *fds[XNSELECT_MAX_TYPES ],int nfds) { +bool first_fd = true;unsigned fd, type;int err;
@@ -193,8 +194,16 @@ int __cobalt_select_bind_all(struct xnselector *selector, fd < nfds; fd = find_next_bit(set->fds_bits, nfds, fd + 1)) { err =rtdm_fd_select (fd, selector, type); -if (err) +if (err) { +/* +* Do not needlessly signal "retry +* under Linux" for mixed fd sets. +*/ +if (err == -EADV && !first_fd) +return -EBADF ;return err; + } + first_fd = false; } }
@@ -275,7 +284,7 @@ COBALT_SYSCALL(select, primary,simple test: test if the first file descriptor we find in the fd_set is an RTDM descriptor or a message queue descriptor. */ if (!__cobalt_first_fd_valid_p (in_fds, nfds)) -return -EBADF ; +return -EADV ; selector =xnmalloc (sizeof (*curr->selector));if (selector ==NULL )
kernel/cobalt/posix/syscall32.c +1 -1
@@ -796,7 +796,7 @@ COBALT_SYSCALL32emu(select, nonrestartable,if (selector ==NULL ) {/* Bail out if non-RTDM fildes is found. */ if (!__cobalt_first_fd_valid_p (in_fds, nfds)) -return -EBADF ; +return -EADV ; selector =xnmalloc (sizeof (*curr->selector));if (selector ==NULL )
lib/cobalt/select.c +1 -1
@@ -36,7 +36,7 @@ COBALT_IMPL(int, select, (int __nfds, fd_set *__restrict __readfds,pthread_setcanceltype (oldtype,NULL ); -if (err == -EBADF || err == -EPERM || err == -ENOSYS ) +if (err == -EADV || err == -EPERM || err == -ENOSYS )return __STD (select (__nfds, __readfds, __writefds, __exceptfds, __timeout));
Signature · 1 line
2.26.2
PATCH 7/9 cobalt/posix/syscall32: Mark compat clock_nanosleep restartable
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
Commit 36132cdb21cd forgot to align this with the native syscall.
--- kernel/cobalt/posix/syscall32.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/cobalt/posix/syscall32.c +1 -1
@@ -229,7 +229,7 @@ COBALT_SYSCALL32emu(clock_adjtime64, current, } -COBALT_SYSCALL32emu (clock_nanosleep, nonrestartable, +COBALT_SYSCALL32emu (clock_nanosleep, primary, (clockid_t clock_id, int flags, const struct old_timespec32 __user *u_rqt, struct old_timespec32 __user *u_rmt))
Signature · 1 line
2.26.2
PATCH 8/9 cobalt/posix/compat: Privatize sys32_get/put_fdset
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
There is no use for this outside of the core that implements select. So stop exporting these helpers. As there are no users outside, simplify their return value on success to 0 because their callers do not care about more.
--- include/cobalt/kernel/compat.h | 8 ++++---- kernel/cobalt/posix/compat.c | 14 ++++++-------- 2 files changed, 10 insertions(+), 12 deletions(-)
include/cobalt/kernel/compat.h +4 -4
@@ -110,11 +110,11 @@ int sys32_get_timex(struct __kernel_timex *tx,int sys32_put_timex (struct old_timex32 __user *ctx,const struct __kernel_timex *tx); -ssize_t sys32_get_fdset (fd_set *fds,const compat_fd_set __user *cfds, -size_t cfdsize); +int sys32_get_fdset (fd_set *fds,const compat_fd_set __user *cfds, +size_t cfdsize); -ssize_t sys32_put_fdset (compat_fd_set __user *cfds,const fd_set *fds, -size_t fdsize); +int sys32_put_fdset (compat_fd_set __user *cfds,const fd_set *fds, +size_t fdsize); int sys32_get_param_ex(int policy, struct sched_param_ex *p,
kernel/cobalt/posix/compat.c +6 -8
@@ -176,8 +176,8 @@ int sys32_put_timex(struct old_timex32 __user *ctx, }EXPORT_SYMBOL_GPL (sys32_put_timex); -ssize_t sys32_get_fdset (fd_set *fds,const compat_fd_set __user *cfds, -size_t cfdsize) +int sys32_get_fdset (fd_set *fds,const compat_fd_set __user *cfds, +size_t cfdsize) {int rdpos, wrpos, rdlim = cfdsize /sizeof (compat_ulong_t);
@@ -188,12 +188,11 @@ ssize_t sys32_get_fdset(fd_set *fds, const compat_fd_set __user *cfds,if (__xn_get_user (fds->fds_bits[wrpos], cfds->fds_bits + rdpos))return -EFAULT ; -return (ssize_t )rdlim *sizeof (long ); +return 0 ; } -EXPORT_SYMBOL_GPL (sys32_get_fdset); -ssize_t sys32_put_fdset (compat_fd_set __user *cfds,const fd_set *fds, -size_t fdsize) +int sys32_put_fdset (compat_fd_set __user *cfds,const fd_set *fds, +size_t fdsize) {int rdpos, wrpos, wrlim = fdsize /sizeof (long );
@@ -204,9 +203,8 @@ ssize_t sys32_put_fdset(compat_fd_set __user *cfds, const fd_set *fds,if (__xn_put_user (fds->fds_bits[rdpos], cfds->fds_bits + wrpos))return -EFAULT ; -return (ssize_t )wrlim *sizeof (compat_ulong_t); +return 0 ; } -EXPORT_SYMBOL_GPL (sys32_put_fdset); int sys32_get_param_ex(int policy, struct sched_param_ex *p,
Signature · 1 line
2.26.2
PATCH 9/9 cobalt/posix/io: Unify native and compat versions of select
In reply to Jan KiszkaFrom: Jan Kiszka <[email protected]>
Rather than duplicating lots of code for the sake of compat userspace accesses, provide a common version that is given a flag to decide whether to take the compat or regular code paths.
This also brings restart support to the compat selection version which was forgotten by 36132cdb21cd.
--- kernel/cobalt/posix/io.c | 97 ++++++++++++++++++++++++--------- kernel/cobalt/posix/io.h | 3 + kernel/cobalt/posix/syscall32.c | 92 +------------------------------ 3 files changed, 76 insertions(+), 116 deletions(-)
kernel/cobalt/posix/io.c +71 -26
@@ -19,6 +19,7 @@*/ #include <linux/err.h> #include <linux/fs.h> +#include <cobalt/kernel/compat.h> #include <cobalt/kernel/ppd.h> #include <xenomai/rtdm/internal.h> #include "process.h"
@@ -210,15 +211,10 @@ int __cobalt_select_bind_all(struct xnselector *selector,return 0 ; } -/* int select(int, fd_set *, fd_set *, fd_set *, struct __kernel_old_timeval *) */ -COBALT_SYSCALL (select, primary, - (int nfds, - fd_set__user *u_rfds, - fd_set__user *u_wfds, - fd_set__user *u_xfds, - struct __kernel_old_timeval __user *u_tv)) +int __cobalt_select (int nfds,void __user *u_rfds,void __user *u_wfds, +void __user *u_xfds,void __user *u_tv,bool compat) { -fd_set __user *ufd_sets[XNSELECT_MAX_TYPES ] = { +void __user *ufd_sets[XNSELECT_MAX_TYPES ] = { [XNSELECT_READ ] = u_rfds, [XNSELECT_WRITE ] = u_wfds, [XNSELECT_EXCEPT ] = u_xfds
@@ -250,9 +246,18 @@ COBALT_SYSCALL(select, primary, goto out; } }else { -if (!access_wok (u_tv,sizeof (tv)) - ||cobalt_copy_from_user (&tv, u_tv,sizeof (tv))) -return -EFAULT ; +#ifdef CONFIG_XENO_ARCH_SYS3264 +if (compat) { +if (sys32_get_timeval (&tv, u_tv)) +return -EFAULT ; + } else +#endif + { +if (!access_wok (u_tv,sizeof (tv)) + ||cobalt_copy_from_user (&tv, u_tv, +sizeof (tv))) +return -EFAULT ; + }if (tv.tv_usec >=1000000 )return -EINVAL ;
@@ -268,13 +273,22 @@ COBALT_SYSCALL(select, primary,for (i =0 ; i <XNSELECT_MAX_TYPES ; i++)if (ufd_sets[i]) { in_fds[i] = &in_fds_storage[i]; - out_fds[i] = & out_fds_storage[i]; -if (!access_wok ((void __user *) ufd_sets[i], -sizeof (fd_set)) - ||cobalt_copy_from_user (in_fds[i], - (void __user *) ufd_sets[i], - fds_size)) -return -EFAULT ; + out_fds[i] = &out_fds_storage[i]; +#ifdef CONFIG_XENO_ARCH_SYS3264 +if (compat) { +if (sys32_get_fdset (in_fds[i], ufd_sets[i], + fds_size)) +return -EFAULT ; + } else +#endif + { +if (!access_wok ((void __user *) ufd_sets[i], +sizeof (fd_set)) + ||cobalt_copy_from_user (in_fds[i], + (void __user *)ufd_sets[i], + fds_size)) +return -EFAULT ; + } } selector = curr->selector;
@@ -294,7 +308,8 @@ COBALT_SYSCALL(select, primary,/* Bind directly the file descriptors, we do not need to go through xnselect returning -ECHRNG */ -if ((err =__cobalt_select_bind_all (selector, in_fds, nfds))) + err =__cobalt_select_bind_all (selector, in_fds, nfds); +if (err)return err; }
@@ -326,15 +341,45 @@ out:else tv.tv_sec = tv.tv_usec =0 ; -if (cobalt_copy_to_user (u_tv, &tv,sizeof (tv))) -return -EFAULT ; +#ifdef CONFIG_XENO_ARCH_SYS3264 +if (compat) { +if (sys32_put_timeval (u_tv, &tv)) +return -EFAULT ; + } else +#endif + { +if (cobalt_copy_to_user (u_tv, &tv,sizeof (tv))) +return -EFAULT ; + } }if (err >=0 ) -for (i =0 ; i <XNSELECT_MAX_TYPES ; i++) -if (ufd_sets[i] - &&cobalt_copy_to_user ((void __user *) ufd_sets[i], - out_fds[i],sizeof (fd_set))) -return -EFAULT ; +for (i =0 ; i <XNSELECT_MAX_TYPES ; i++) { +if (!ufd_sets[i]) +continue ; +#ifdef CONFIG_XENO_ARCH_SYS3264 +if (compat) { +if (sys32_put_fdset (ufd_sets[i], out_fds[i], +sizeof (fd_set))) +return -EFAULT ; + } else +#endif + { +if (cobalt_copy_to_user ((void __user *)ufd_sets[i], + out_fds[i],sizeof (fd_set))) +return -EFAULT ; + } + }return err; } + +/* int select(int, fd_set *, fd_set *, fd_set *, struct __kernel_old_timeval *) */ +COBALT_SYSCALL (select, primary, + (int nfds, + fd_set__user *u_rfds, + fd_set__user *u_wfds, + fd_set__user *u_xfds, + struct __kernel_old_timeval __user *u_tv)) +{ +return __cobalt_select (nfds, u_rfds, u_wfds, u_xfds, u_tv, false); +}
kernel/cobalt/posix/io.h +3 -0
@@ -28,6 +28,9 @@ int __cobalt_first_fd_valid_p(fd_set *fds[XNSELECT_MAX_TYPES], int nfds);int __cobalt_select_bind_all (struct xnselector *selector,fd_set *fds[XNSELECT_MAX_TYPES ],int nfds); +int __cobalt_select (int nfds,void __user *u_rfds,void __user *u_wfds, +void __user *u_xfds,void __user *u_tv,bool compat); +COBALT_SYSCALL_DECL (open, (const char __user *u_path, int oflag));
kernel/cobalt/posix/syscall32.c +2 -90
104 lines not on this page · raw message
Signature · 1 line
2.26.2