NetBSD Problem Report #29698

From ignatios@sinus.cs.uni-bonn.de  Tue Mar 15 14:34:02 2005
Return-Path: <ignatios@sinus.cs.uni-bonn.de>
Received: from sinus.cs.uni-bonn.de (sinus.cs.uni-bonn.de [131.220.4.180])
	by narn.netbsd.org (Postfix) with ESMTP id 9DE5863B119
	for <gnats-bugs@gnats.NetBSD.org>; Tue, 15 Mar 2005 14:34:01 +0000 (UTC)
Message-Id: <200503151433.j2FEXwmX025396@sinus.cs.uni-bonn.de>
Date: Tue, 15 Mar 2005 14:33:58 GMT
From: Ignatios Souvatzis <ignatios@sinus.cs.uni-bonn.de>
Reply-To: ignatios@sinus.cs.uni-bonn.de,martin@netbsd.org
To: gnats-bugs@netbsd.org
Subject: floating point context not saved in pthread/mips
X-Send-Pr-Version: 3.95

>Number:         29698
>Category:       port-mips
>Synopsis:       floating context not saved in pthread/mips
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    port-mips-maintainer
>State:          closed
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Mar 15 14:35:00 +0000 2005
>Closed-Date:    Sun Feb 15 03:03:27 +0000 2009
>Last-Modified:  Sun Feb 15 03:03:27 +0000 2009
>Originator:     ignatios@sinus.cs.uni-bonn.de
>Release:        NetBSD 2.99.14
>Organization:
computer science department, university of Bonn, Germany
>Environment:
NetBSD tequila-sunrise.duskware.de 2.99.14 NetBSD 2.99.14 (TEQUILA) #0:
Fri Jan 14 09:54:37 CET 2005
martin@night-porter.duskware.de:/usr/src/sys/arch/sgimips/compile/TEQUILA
sgimips
>Description:

Running part of the regression test for context switches from the SR
library, roughly translated to using pthread only, shows wrong FP results.

> ===== checking floating point =====
> mul3...stirring...returning
> mul3...stirring...returning
> mul3...stirring...returning
> mul3...stirring...returning
> oops -- product returned was 0.97504285672921098271

The expected result is 223092870.


(cstest.c shows wrong FP results, too, but I wanted a non-SR program to
 demonstrate them to avoid being accused of abusing libpthread internals.)

>How-To-Repeat:
Compile this code (cc -o threadtest threadtest.c -lpthread -lm),
then run it.

#include <stdio.h>
#include <math.h>
#include <pthread.h>

double mul3(double, double, double, pthread_cond_t *);
void *stir(void *);
void *bar(void *);

pthread_cond_t s2, s5;
pthread_mutex_t m2, m5;
pthread_t thread2, thread5;
pthread_barrier_t startup;

int
main() {
	int rc;

	rc = pthread_mutex_init(&m2, 0);
	if (rc) 
		err("pthread_mutex_init");

	rc = pthread_mutex_init(&m5, 0);
	if (rc) 
		err("pthread_mutex_init");

	rc = pthread_cond_init(&s2, 0);
	if (rc) 
		err("pthread_cond_init");

	rc = pthread_cond_init(&s5, 0);
	if (rc) 
		err("pthread_cond_init");

	rc = pthread_create(&thread5, 0, stir, 0);
	if (rc) 
		err("pthread_create");

	rc = pthread_barrier_init(&startup, 0, 2);
	if (rc) 
		err("pthread_barrier_init");

	pthread_barrier_wait(&startup);
	pthread_join(thread2, 0);
	exit(0);
}


double
mul3(double x, double y, double z, pthread_cond_t *ctx) {
	printf("mul3...");

	pthread_cond_signal(&s5);
	pthread_cond_wait(ctx,&m2);

	printf("returning\n");
	return x*y*z;
}

void *
stir(void *arg) {
	double x = 1.7, y = 3.2, z = 2.4;
	int rc;

	pthread_mutex_lock(&m5);

	rc = pthread_create(&thread2, 0, bar, 0);
	if (rc) 
		err("pthread_create");

	for (;;) {
		pthread_cond_wait(&s5,&m5);
		printf("stirring...");
		x = sin ((y = cos (x + y + .4)) - (z = cos (x + z + .6)));
		pthread_cond_signal(&s2);
	}
}

void *
bar(void *arg) {
	double d;


	pthread_barrier_wait(&startup);
	pthread_mutex_lock(&m2);

	puts ("===== checking floating point =====");
	d = mul3 (mul3 (2., 3., 5., &s2), mul3 (7., 11., 13., &s2),
        	mul3 (17., 19., 23., &s2), &s2);
	if (d != 223092870.)
       		printf ("oops -- product returned was %.20g\n", d); 

	return(0);
}

>Fix:
Idea: save/restore fp regs in lib/libpthread/arch/mips/

>Release-Note:

>Audit-Trail:
From: Jason Thorpe <thorpej@shagadelic.org>
To: gnats-bugs@netbsd.org
Cc: port-mips-maintainer@netbsd.org, gnats-admin@netbsd.org,
	netbsd-bugs@netbsd.org
Subject: Re: port-mips/29698: floating point context not saved in pthread/mips
Date: Tue, 15 Mar 2005 08:38:12 -0800

 On Mar 15, 2005, at 6:35 AM, Ignatios Souvatzis wrote:

 > Idea: save/restore fp regs in lib/libpthread/arch/mips/

 Unconditional save/restore of FP context on every thread switch is 
 potentially quite expensive.  We really need some way to tell userspace 
 that a particular LWP has used the FPU.

 -- thorpej

From: Martin Husemann <martin@duskware.de>
To: Jason Thorpe <thorpej@shagadelic.org>
Cc: gnats-bugs@netbsd.org
Subject: Re: port-mips/29698: floating point context not saved in pthread/mips
Date: Tue, 15 Mar 2005 20:23:41 +0100

 On Tue, Mar 15, 2005 at 08:38:12AM -0800, Jason Thorpe wrote:
 > Unconditional save/restore of FP context on every thread switch is 
 > potentially quite expensive.  We really need some way to tell userspace 
 > that a particular LWP has used the FPU.

 Would userspace be able to look at the "FPU enabled" bit on all archs?

 We could tweak the _*context_u() functions to use the real *context()
 functions whenever this bit is enabled and the last context was a
 "light" one, and do the right thing (tm) if the last context allready
 was a full one. (This means passing a pre-filled context to this functions,
 of course)

 This sounds too easy, I'm probably missing something.

 Martin
 P.S.: should we move this to tech-kern?

State-Changed-From-To: open->suspended
State-Changed-By: ad@netbsd.org
State-Changed-When: Wed, 21 Mar 2007 17:37:47 +0000
State-Changed-Why:
This should be fixed in -current. Realistically, there is no chance
of it being fixed in a earlier release.


State-Changed-From-To: suspended->closed
State-Changed-By: dholland@NetBSD.org
State-Changed-When: Sun, 15 Feb 2009 03:03:27 +0000
State-Changed-Why:
Fixed in -5 and current, won't fix in -4 or -3  =>  closed, not suspended.


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.39 2013/11/01 18:47:49 spz Exp $
$NetBSD: gnats_config.sh,v 1.8 2006/05/07 09:23:38 tsutsui Exp $
Copyright © 1994-2007 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.