From nobody@FreeBSD.ORG  Tue Jul 18 15:27:50 2000
Return-Path: <nobody@FreeBSD.ORG>
Received: by hub.freebsd.org (Postfix, from userid 32767)
	id 0210537B6CF; Tue, 18 Jul 2000 15:27:50 -0700 (PDT)
Message-Id: <20000718222750.0210537B6CF@hub.freebsd.org>
Date: Tue, 18 Jul 2000 15:27:50 -0700 (PDT)
From: lawlopez@cisco.com
Sender: nobody@FreeBSD.ORG
To: freebsd-gnats-submit@FreeBSD.org
Subject: pthreads: Cannot set scheduling timer/Cannot set virtual timer
X-Send-Pr-Version: www-1.0

>Number:         20016
>Category:       kern
>Synopsis:       pthreads: Cannot set scheduling timer/Cannot set virtual timer
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    freebsd-threads
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Jul 18 15:30:01 PDT 2000
>Closed-Date:    Fri Nov 16 17:09:50 UTC 2007
>Last-Modified:  Fri Nov 16 17:09:50 UTC 2007
>Originator:     Lawrence D. Lopez
>Release:        3.4
>Organization:
Cisco Systems
>Environment:
FreeBSD nptestbed2.dtdtools.cisco.com 3.4-RELEASE FreeBSD 3.4-RELEASE #0: Fri Jul 14 17:24:47 EDT 2000     lawlopez@nptestbed2.dtdtools.cisco.com:/usr/src/sys/compile/NPTESTBED2X  i386

>Description:
The threads library is terminating because setitimer is being
called with a negative number of microseconds.  This is occuring
because timeout function is taking so long that all of the
timecounter elements are used.

This is occuring because the system call gettimeofday is returning
a negative number of microseconds to _thread_kern_sched() in
/usr/src/lib/libc_r_g/uthread/uthread_kern.c

This is occuring because the microtime() function called
by the system call gettimeofday in
/usr/src/sys/kern/kern_clock.c is returning a negative number
of microseconds.

This is occurring because the inline function tco_delta()
is returning the a negative time.

This is occurring because the timecounter structure used
by microtime and given to tco_delta is being modified
while tco_delta is using it.

Specifically tcl_delta() is calling tc->tc_get_timecount(tc)
and is storing the value in a register.

A timer interrupt occurs at this point which then calls
a timeout function which does not mask off
timer interrupts but which processes for a period of time
long enough so that the original timecounter element used
by microtime is reused.

At this point sync_other_counter() resets tc->tc_offset_count
and tc_delta returns a very large number.

static __inline unsigned 
tco_delta(struct timecounter *tc)
{

        return ((tc->tc_get_timecount(tc) - tc->tc_offset_count) &
            tc->tc_counter_mask);
}


>How-To-Repeat:
It is difficult to reproduce the problem.

I think if you start up a timeout function which
spins for 100 ms and then call gettimeofday()
you may get lucky.


>Fix:
Add to the kernel configuration file:

options         "NTIMECOUNTER=100"

This would allow for timeout functions of up to 1000 milli seconds.

The comments in LINT while they may be correct should be more
specific.

>Release-Note:
>Audit-Trail:

From: Poul-Henning Kamp <phk@critter.freebsd.dk>
To: lawlopez@cisco.com
Cc: freebsd-gnats-submit@FreeBSD.ORG
Subject: Re: kern/20016: pthreads: Cannot set scheduling timer/Cannot set virtual timer 
Date: Wed, 19 Jul 2000 09:17:03 +0200

 In message <20000718222750.0210537B6CF@hub.freebsd.org>, lawlopez@cisco.com wri
 tes:
 
 >A timer interrupt occurs at this point which then calls
 >a timeout function which does not mask off
 >timer interrupts but which processes for a period of time
 >long enough so that the original timecounter element used
 >by microtime is reused.
 
 This is the culprit, do you know which timeout function this is ?
 
 --
 Poul-Henning Kamp       | UNIX since Zilog Zeus 3.20
 phk@FreeBSD.ORG         | TCP/IP since RFC 956
 FreeBSD coreteam member | BSD since 4.3-tahoe    
 Never attribute to malice what can adequately be explained by incompetence.
 
Responsible-Changed-From-To: freebsd-bugs->jasone 
Responsible-Changed-By: jasone 
Responsible-Changed-When: Mon Oct 23 14:35:35 PDT 2000 
Responsible-Changed-Why:  
Over to maintainer. 
Responsible-Changed-From-To: jasone->freebsd-bugs 
Responsible-Changed-By: jasone 
Responsible-Changed-When: Sat May 11 15:22:01 PDT 2002 
Responsible-Changed-Why:  


http://www.freebsd.org/cgi/query-pr.cgi?pr=20016 
Responsible-Changed-From-To: freebsd-bugs->freebsd-threads 
Responsible-Changed-By: kris 
Responsible-Changed-When: Sat Jul 12 18:36:05 PDT 2003 
Responsible-Changed-Why:  
Assign to threads mailing list 

http://www.freebsd.org/cgi/query-pr.cgi?pr=20016 

From: David Xu <davidxu@freebsd.org>
To: bug-followup@freebsd.org, lawlopez@cisco.com
Cc:  
Subject: Re: kern/20016: pthreads: Cannot set scheduling timer/Cannot set
 virtual timer
Date: Fri, 29 Apr 2005 19:43:54 +0800

 Why does the bug belong to thread library ? it is a kernel timer race bug.
 
 David Xu
 
State-Changed-From-To: open->closed 
State-Changed-By: kmacy 
State-Changed-When: Fri Nov 16 17:09:03 UTC 2007 
State-Changed-Why:  

libc_r is no longer supported and the timeout code has changed substantially 

http://www.freebsd.org/cgi/query-pr.cgi?pr=20016 
>Unformatted:
