Subject: Re: sleeping on a userland address
To: None <sommerfeld@netbsd.org>
From: Love <lha@netbsd.org>
List: tech-kern
Date: 07/02/2002 02:00:06
--=-=-=

Bill Sommerfeld <sommerfeld@netbsd.org> writes:

> BTW, all code calling sleep() or its successors should always be
> prepared for spurious wakeups -- i.e., always use sleep() in a loop.
> They are rare, but happen in the vicinity of things like signals and
> the like.

This reminded me of one in the interesting things the ptrace interface can
do to you and your processes.

ptrace sets the process runnable with setrunnable() when
detaching/continuing, this make ltsleep & co wakeup spurious.

I don't know if the patch should look at the tv_usec component too, what
should the granuality be (break if within N/HZ) ?

Love


--=-=-=
Content-Disposition: attachment
Content-Description: ptrace+nanosleep

Index: kern_time.c
===================================================================
RCS file: /sources/netbsd/NetBSD-cvs/syssrc/sys/kern/kern_time.c,v
retrieving revision 1.61
diff -u -r1.61 kern_time.c
--- kern_time.c 2002/01/31 00:13:08     1.61
+++ kern_time.c 2002/07/01 23:27:46
@@ -294,6 +294,7 @@
 
        s = splclock();
        timeradd(&atv,&time,&atv);
+ resleep:
        timo = hzto(&atv);
        /* 
         * Avoid inadvertantly sleeping forever
@@ -308,14 +309,18 @@
        if (error == EWOULDBLOCK)
                error = 0;
 
+       s = splclock();
+       utv = time;
+
+       /* Check if someone woke us up too early */
+       timersub(&atv, &utv, &utv);
+       if (error == 0 && utv.tv_sec > 0)
+               goto resleep;
+       splx(s);
+
        if (SCARG(uap, rmtp)) {
                int error;
 
-               s = splclock();
-               utv = time;
-               splx(s);
-
-               timersub(&atv, &utv, &utv);
                if (utv.tv_sec < 0)
                        timerclear(&utv);
 

--=-=-=


Regression part in

http://www.e.kth.se/~lha/patches/netbsd/ptrace-regress.tar.gz

--=-=-=--