Module: xenomai-jki
Branch: for-forge
Commit: 775197bf4978364baffb66de34f47163682968b9
URL:    
http://git.xenomai.org/?p=xenomai-jki.git;a=commit;h=775197bf4978364baffb66de34f47163682968b9

Author: Jan Kiszka <jan.kis...@siemens.com>
Date:   Fri May 13 20:35:26 2016 +0200

cobalt/kernel: Allow to restart clock_nanosleep and select after signal 
processing

Only if a signal was actually delivered to a thread that was blocked on
sleep, [clock_]nanosleep or select, those calls should return -EINTR.
Otherwise, they should resume with the timeout, accordingly adjusted in
case of relative timeout. So far we returned -EINTR immediately which
particularly disturbed the debugging of applications (SIGSTOP/CONT
terminated those syscalls).

This approach reuses the Linux restart mechanism to find out if those
syscalls should be restarted or actually terminated after the signal
was handled: Linux sets current->restart_block.fn in case a termination
is required, unconditionally, thus also when the syscall did not return
ERESTART_RESTARTBLOCK. We also use the restart_block.nanosleep.expires
to transfer the remaining timeout to the restarted syscall.

We can't use the original restart mechanism of Linux because it directs
all ERESTART_RESTARTBLOCK through a special, Linux-only syscall. In our
case, we would have to migrate the caller in that context to primary in
order to resume the sleep, but this is not possible under Xenomai (we
need to migration from within the syscall hooks).

Signed-off-by: Jan Kiszka <jan.kis...@siemens.com>

---

 include/cobalt/uapi/kernel/thread.h |    1 +
 kernel/cobalt/posix/clock.c         |   34 ++++++++++++++++++++++++++++++---
 kernel/cobalt/posix/io.c            |   36 ++++++++++++++++++++++++++++-------
 3 files changed, 61 insertions(+), 10 deletions(-)

diff --git a/include/cobalt/uapi/kernel/thread.h 
b/include/cobalt/uapi/kernel/thread.h
index 8d26f16..1f1dca7 100644
--- a/include/cobalt/uapi/kernel/thread.h
+++ b/include/cobalt/uapi/kernel/thread.h
@@ -77,6 +77,7 @@
 
 #define XNMOVED   0x00000001 /**< CPU migration in primary mode occurred */
 #define XNLBALERT 0x00000002 /**< Scheduler lock break alert (SIGDEBUG sent) */
+#define XNRESTART 0x00000004 /**< Thread awaiting syscall restart after signal 
*/
 
 /** @} */
 
diff --git a/kernel/cobalt/posix/clock.c b/kernel/cobalt/posix/clock.c
index b51cb4c..dcb5a91 100644
--- a/kernel/cobalt/posix/clock.c
+++ b/kernel/cobalt/posix/clock.c
@@ -237,7 +237,7 @@ int __cobalt_clock_nanosleep(clockid_t clock_id, int flags,
                             struct timespec *rmt)
 {
        struct xnthread *cur;
-       xnsticks_t rem;
+       xnsticks_t timeout, rem;
        int ret = 0;
        spl_t s;
 
@@ -261,10 +261,38 @@ int __cobalt_clock_nanosleep(clockid_t clock_id, int 
flags,
 
        xnlock_get_irqsave(&nklock, s);
 
-       xnthread_suspend(cur, XNDELAY, ts2ns(rqt) + 1,
+       if (xnthread_test_localinfo(cur, XNLBALERT)) {
+               xnthread_clear_localinfo(cur, XNLBALERT);
+
+               if (current->restart_block.fn != NULL) {
+                       xnlock_put_irqrestore(&nklock, s);
+
+                       if (rmt)
+                               ns2ts(rmt, rem > 1 ? rem : 0);
+                       return -EINTR;
+               }
+
+               timeout = current->restart_block.nanosleep.expires;
+       } else
+               timeout = ts2ns(rqt);
+
+       xnthread_suspend(cur, XNDELAY, timeout + 1,
                         clock_flag(flags, clock_id), NULL);
 
        if (xnthread_test_info(cur, XNBREAK)) {
+               if (signal_pending(current)) {
+                       xnthread_set_localinfo(cur, XNLBALERT);
+
+                       current->restart_block.fn = NULL;
+                       current->restart_block.nanosleep.expires =
+                               (flags & TIMER_ABSTIME) ? timout :
+                                   xntimer_get_timeout_stopped(&cur->rtimer) :
+
+                       xnlock_put_irqrestore(&nklock, s);
+
+                       return -ERESTARTSYS;
+               }
+
                if (flags == 0 && rmt) {
                        rem = xntimer_get_timeout_stopped(&cur->rtimer);
                        xnlock_put_irqrestore(&nklock, s);
@@ -280,7 +308,7 @@ int __cobalt_clock_nanosleep(clockid_t clock_id, int flags,
        return ret;
 }
 
-COBALT_SYSCALL(clock_nanosleep, nonrestartable,
+COBALT_SYSCALL(clock_nanosleep, primary,
               (clockid_t clock_id, int flags,
                const struct timespec __user *u_rqt,
                struct timespec __user *u_rmt))
diff --git a/kernel/cobalt/posix/io.c b/kernel/cobalt/posix/io.c
index 7110d21..99b31ca 100644
--- a/kernel/cobalt/posix/io.c
+++ b/kernel/cobalt/posix/io.c
@@ -170,7 +170,7 @@ int __cobalt_select_bind_all(struct xnselector *selector,
 }
 
 /* int select(int, fd_set *, fd_set *, fd_set *, struct timeval *) */
-COBALT_SYSCALL(select, nonrestartable,
+COBALT_SYSCALL(select, primary,
               (int nfds,
                fd_set __user *u_rfds,
                fd_set __user *u_wfds,
@@ -197,14 +197,26 @@ COBALT_SYSCALL(select, nonrestartable,
        curr = xnthread_current();
 
        if (u_tv) {
-               if (!access_wok(u_tv, sizeof(tv))
-                   || cobalt_copy_from_user(&tv, u_tv, sizeof(tv)))
-                       return -EFAULT;
+               if (xnthread_test_localinfo(curr, XNLBALERT)) {
+                       xnthread_clear_localinfo(curr, XNLBALERT);
+
+                       timeout = current->restart_block.nanosleep.expires;
+
+                       if (current->restart_block.fn != NULL) {
+                               err = -EINTR;
+                               goto out;
+                       }
+               } else {
+                       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;
+                       if (tv.tv_usec > 1000000)
+                               return -EINVAL;
+
+                       timeout = clock_get_ticks(CLOCK_MONOTONIC) + tv2ns(&tv);
+               }
 
-               timeout = clock_get_ticks(CLOCK_MONOTONIC) + tv2ns(&tv);
                mode = XN_ABSOLUTE;
        }
 
@@ -253,6 +265,16 @@ COBALT_SYSCALL(select, nonrestartable,
                }
        } while (err == -ECHRNG);
 
+       if (err == -EINTR && signal_pending(current)) {
+               xnthread_set_localinfo(curr, XNLBALERT);
+
+               current->restart_block.fn = NULL;
+               current->restart_block.nanosleep.expires = timeout;
+
+               return -ERESTARTSYS;
+       }
+
+out:
        if (u_tv && (err > 0 || err == -EINTR)) {
                xnsticks_t diff = timeout - clock_get_ticks(CLOCK_MONOTONIC);
                if (diff > 0)


_______________________________________________
Xenomai-git mailing list
Xenomai-git@xenomai.org
https://xenomai.org/mailman/listinfo/xenomai-git

Reply via email to